blob: 2c8c2696415e6cd1b2765179eb2fb490e4422a10 [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>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040096static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 struct rpc_cred *cred,
103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400110
111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
112static inline struct nfs4_label *
113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115{
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
David Quigleyaa9c2662013-05-22 12:50:44 -0400124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130}
131static inline void
132nfs4_label_release_security(struct nfs4_label *label)
133{
134 if (label)
135 security_release_secctx(label->label, label->len);
136}
137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143}
144#else
145static inline struct nfs4_label *
146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148{ return NULL; }
149static inline void
150nfs4_label_release_security(struct nfs4_label *label)
151{ return; }
152static inline u32 *
153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154{ return server->attr_bitmask; }
155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500158static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400168 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700181 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400184 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This is our standard bitmap for GETATTR requests.
189 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400190const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Trond Myklebust1549210f2012-06-05 09:16:47 -0400211static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400230};
231
Andy Adamsone23008e2012-10-02 21:07:32 -0400232static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400234 | FATTR4_WORD0_FILEID,
235};
236
David Quigleya09df2c2013-05-22 12:50:41 -0400237const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250};
251
Fred Isamandae100c2011-07-30 20:52:37 -0400252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700256 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800259 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
David Quigleya09df2c2013-05-22 12:50:41 -0400262const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500263 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500268 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400276};
277
Trond Myklebust30846df2018-04-07 13:44:28 -0400278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280{
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297}
298
299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301{
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303}
304
Al Virobc4785c2006-10-19 23:28:51 -0700305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct nfs4_readdir_arg *readdir)
307{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700309 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000312 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322 /*
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
328 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800359 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Trond Myklebust26d36302016-09-22 13:39:05 -0400362static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 struct rpc_cred *cred)
365{
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369}
370
371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 struct rpc_cred *cred)
374{
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377}
378
379static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 struct rpc_cred *cred)
382{
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387}
388
NeilBrown8478eaa2014-09-18 16:09:27 +1000389static long nfs4_update_delay(long *timeout)
390{
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401}
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
404{
405 int res = 0;
406
407 might_sleep();
408
NeilBrown8478eaa2014-09-18 16:09:27 +1000409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 if (fatal_signal_pending(current))
412 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 return res;
414}
415
416/* This is the error handling routine for processes that are allowed
417 * to sleep.
418 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400419static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500421{
422 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500423 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400424 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500426 int ret = errorcode;
427
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400428 exception->delay = 0;
429 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500430 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400431
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
434
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 switch(errorcode) {
436 case 0:
437 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400438 case -NFS4ERR_BADHANDLE:
439 case -ESTALE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
442 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400445 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400446 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
449 stateid);
450 goto wait_on_recovery;
451 }
452 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400453 if (inode) {
454 int err;
455
456 err = nfs_async_inode_return_delegation(inode,
457 stateid);
458 if (err == 0)
459 goto wait_on_recovery;
460 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
461 exception->retry = 1;
462 break;
463 }
464 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500465 if (state == NULL)
466 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400467 ret = nfs4_schedule_stateid_recovery(server, state);
468 if (ret < 0)
469 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500470 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500472 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500473 nfs4_schedule_lease_recovery(clp);
474 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400475 case -NFS4ERR_MOVED:
476 ret = nfs4_schedule_migration_recovery(server);
477 if (ret < 0)
478 break;
479 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400480 case -NFS4ERR_LEASE_MOVED:
481 nfs4_schedule_lease_moved_recovery(clp);
482 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500483#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400484 case -NFS4ERR_BADSESSION:
485 case -NFS4ERR_BADSLOT:
486 case -NFS4ERR_BAD_HIGH_SLOT:
487 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
488 case -NFS4ERR_DEADSESSION:
489 case -NFS4ERR_SEQ_FALSE_RETRY:
490 case -NFS4ERR_SEQ_MISORDERED:
491 dprintk("%s ERROR: %d Reset session\n", __func__,
492 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400493 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400494 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500495#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500496 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500497 if (exception->timeout > HZ) {
498 /* We have retried a decent amount, time to
499 * fail
500 */
501 ret = -EBUSY;
502 break;
503 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400505 nfs_inc_server_stats(server, NFSIOS_DELAY);
506 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400507 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400508 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400509 exception->delay = 1;
510 return 0;
511
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400512 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500513 case -NFS4ERR_OLD_STATEID:
514 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800515 break;
516 case -NFS4ERR_BADOWNER:
517 /* The following works around a Linux server bug! */
518 case -NFS4ERR_BADNAME:
519 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
520 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
521 exception->retry = 1;
522 printk(KERN_WARNING "NFS: v4 server %s "
523 "does not accept raw "
524 "uid/gids. "
525 "Reenabling the idmapper.\n",
526 server->nfs_client->cl_hostname);
527 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500528 }
529 /* We failed to handle the error */
530 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500531wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400532 exception->recovering = 1;
533 return 0;
534}
535
536/* This is the error handling routine for processes that are allowed
537 * to sleep.
538 */
539int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
540{
541 struct nfs_client *clp = server->nfs_client;
542 int ret;
543
544 ret = nfs4_do_handle_exception(server, errorcode, exception);
545 if (exception->delay) {
546 ret = nfs4_delay(server->client, &exception->timeout);
547 goto out_retry;
548 }
549 if (exception->recovering) {
550 ret = nfs4_wait_clnt_recover(clp);
551 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
552 return -EIO;
553 goto out_retry;
554 }
555 return ret;
556out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500557 if (ret == 0)
558 exception->retry = 1;
559 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500560}
561
Trond Myklebust037fc982015-09-20 15:51:00 -0400562static int
563nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
564 int errorcode, struct nfs4_exception *exception)
565{
566 struct nfs_client *clp = server->nfs_client;
567 int ret;
568
569 ret = nfs4_do_handle_exception(server, errorcode, exception);
570 if (exception->delay) {
571 rpc_delay(task, nfs4_update_delay(&exception->timeout));
572 goto out_retry;
573 }
574 if (exception->recovering) {
575 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
576 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
577 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
578 goto out_retry;
579 }
580 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
581 ret = -EIO;
582 return ret;
583out_retry:
584 if (ret == 0)
585 exception->retry = 1;
586 return ret;
587}
588
589static int
590nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
591 struct nfs4_state *state, long *timeout)
592{
593 struct nfs4_exception exception = {
594 .state = state,
595 };
596
597 if (task->tk_status >= 0)
598 return 0;
599 if (timeout)
600 exception.timeout = *timeout;
601 task->tk_status = nfs4_async_handle_exception(task, server,
602 task->tk_status,
603 &exception);
604 if (exception.delay && timeout)
605 *timeout = exception.timeout;
606 if (exception.retry)
607 return -EAGAIN;
608 return 0;
609}
610
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400611/*
612 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
613 * or 'false' otherwise.
614 */
615static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
616{
617 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500618 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400619}
Trond Myklebust65de8722008-12-23 15:21:44 -0500620
Trond Myklebust452e9352010-07-31 14:29:06 -0400621static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 spin_lock(&clp->cl_lock);
624 if (time_before(clp->cl_last_renewal,timestamp))
625 clp->cl_last_renewal = timestamp;
626 spin_unlock(&clp->cl_lock);
627}
628
Trond Myklebust452e9352010-07-31 14:29:06 -0400629static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
630{
Trond Myklebustbe824162015-07-05 14:50:46 -0400631 struct nfs_client *clp = server->nfs_client;
632
633 if (!nfs4_has_session(clp))
634 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400635}
636
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400637struct nfs4_call_sync_data {
638 const struct nfs_server *seq_server;
639 struct nfs4_sequence_args *seq_args;
640 struct nfs4_sequence_res *seq_res;
641};
642
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800643void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400644 struct nfs4_sequence_res *res, int cache_reply,
645 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400646{
647 args->sa_slot = NULL;
648 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400649 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400650
651 res->sr_slot = NULL;
652}
653
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400654static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400655{
656 struct nfs4_slot *slot = res->sr_slot;
657 struct nfs4_slot_table *tbl;
658
Chuck Lever3bd23842013-08-09 12:49:19 -0400659 tbl = slot->table;
660 spin_lock(&tbl->slot_tbl_lock);
661 if (!nfs41_wake_and_assign_slot(tbl, slot))
662 nfs4_free_slot(tbl, slot);
663 spin_unlock(&tbl->slot_tbl_lock);
664
665 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400666}
667
668static int nfs40_sequence_done(struct rpc_task *task,
669 struct nfs4_sequence_res *res)
670{
671 if (res->sr_slot != NULL)
672 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400673 return 1;
674}
675
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400676#if defined(CONFIG_NFS_V4_1)
677
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400678static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400679{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500680 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400681 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500682 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400683
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400684 if (!slot)
685 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500686 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500687 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500688
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400689 /* Bump the slot sequence number */
690 if (slot->seq_done)
691 slot->seq_nr++;
692 slot->seq_done = 0;
693
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500694 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500695 /* Be nice to the server: try to ensure that the last transmitted
696 * value for highest_user_slotid <= target_highest_slotid
697 */
698 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
699 send_new_highest_used_slotid = true;
700
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500701 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500702 send_new_highest_used_slotid = false;
703 goto out_unlock;
704 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500706
707 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
708 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500709out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500710 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500711 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400712 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400713 if (waitqueue_active(&tbl->slot_waitq))
714 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400715}
716
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400717static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
718{
719 nfs41_release_slot(res->sr_slot);
720 res->sr_slot = NULL;
721}
722
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400723static int nfs41_sequence_process(struct rpc_task *task,
724 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400725{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500726 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500727 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500730 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400731
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500732 if (slot == NULL)
733 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400734 /* don't increment the sequence number if the task wasn't sent */
735 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400736 goto out;
737
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500739
Trond Myklebustac20d162012-12-15 15:36:07 -0500740 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400741 if (res->sr_status != -NFS4ERR_DELAY)
742 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500743 interrupted = true;
744 }
745
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400746 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500747 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400748 switch (res->sr_status) {
749 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400750 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400751 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500752 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500753 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500754 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400755 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
756 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500757 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500759 case 1:
760 /*
761 * sr_status remains 1 if an RPC level error occurred.
762 * The server may or may not have processed the sequence
763 * operation..
764 * Mark the slot as having hosted an interrupted RPC call.
765 */
766 slot->interrupted = 1;
767 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 case -NFS4ERR_DELAY:
769 /* The server detected a resend of the RPC call and
770 * returned NFS4ERR_DELAY as per Section 2.10.6.2
771 * of RFC5661.
772 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500773 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400774 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500775 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500776 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400778 case -NFS4ERR_RETRY_UNCACHED_REP:
779 case -NFS4ERR_SEQ_FALSE_RETRY:
780 /*
781 * The server thinks we tried to replay a request.
782 * Retry the call after bumping the sequence ID.
783 */
784 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500785 case -NFS4ERR_BADSLOT:
786 /*
787 * The slot id we used was probably retired. Try again
788 * using a different slot id.
789 */
Trond Myklebust99589102018-06-09 12:50:50 -0400790 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400791 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500792 goto retry_nowait;
793 case -NFS4ERR_SEQ_MISORDERED:
794 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500795 * Was the last operation on this sequence interrupted?
796 * If so, retry after bumping the sequence number.
797 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400798 if (interrupted)
799 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500800 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500801 * Could this slot have been previously retired?
802 * If so, then the server may be expecting seq_nr = 1!
803 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500804 if (slot->seq_nr != 1) {
805 slot->seq_nr = 1;
806 goto retry_nowait;
807 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400808 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400809 default:
810 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400811 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400812 }
813out:
814 /* The session may be reset by one of the error handlers. */
815 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500816out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500817 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400818session_recover:
819 nfs4_schedule_session_recovery(session, res->sr_status);
820 goto retry_nowait;
821retry_new_seq:
822 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500823retry_nowait:
824 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400825 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500826 task->tk_status = 0;
827 ret = 0;
828 }
829 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400830out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400831 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400832 goto out;
833 rpc_delay(task, NFS4_POLL_RETRY_MAX);
834 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400835}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400836
837int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
838{
839 if (!nfs41_sequence_process(task, res))
840 return 0;
841 if (res->sr_slot != NULL)
842 nfs41_sequence_free_slot(res);
843 return 1;
844
845}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500846EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400847
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400848static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
849{
850 if (res->sr_slot == NULL)
851 return 1;
852 if (res->sr_slot->table->session != NULL)
853 return nfs41_sequence_process(task, res);
854 return nfs40_sequence_done(task, res);
855}
856
857static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot != NULL) {
860 if (res->sr_slot->table->session != NULL)
861 nfs41_sequence_free_slot(res);
862 else
863 nfs40_sequence_free_slot(res);
864 }
865}
866
Peng Tao2c4b1312014-06-11 05:24:16 +0800867int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400868{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500869 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400870 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400871 if (!res->sr_slot->table->session)
872 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400873 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400874}
Peng Tao2c4b1312014-06-11 05:24:16 +0800875EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400876
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
878{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400879 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400880
Trond Myklebust035168ab2010-06-16 09:52:26 -0400881 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
882
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500883 nfs4_setup_sequence(data->seq_server->nfs_client,
884 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400885}
886
Andy Adamson69ab40c2009-04-01 09:22:19 -0400887static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
888{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400889 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400890
Trond Myklebust14516c32010-07-31 14:29:06 -0400891 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400892}
893
Trond Myklebust17280172012-03-11 13:11:00 -0400894static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400896 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897};
898
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400899static void
900nfs4_sequence_process_interrupted(struct nfs_client *client,
901 struct nfs4_slot *slot, struct rpc_cred *cred)
902{
903 struct rpc_task *task;
904
905 task = _nfs41_proc_sequence(client, cred, slot, true);
906 if (!IS_ERR(task))
907 rpc_put_task_async(task);
908}
909
Chuck Lever3bd23842013-08-09 12:49:19 -0400910#else /* !CONFIG_NFS_V4_1 */
911
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400912static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
913{
914 return nfs40_sequence_done(task, res);
915}
916
917static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
918{
919 if (res->sr_slot != NULL)
920 nfs40_sequence_free_slot(res);
921}
922
Peng Tao2c4b1312014-06-11 05:24:16 +0800923int nfs4_sequence_done(struct rpc_task *task,
924 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400925{
Chuck Lever3bd23842013-08-09 12:49:19 -0400926 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400927}
Peng Tao2c4b1312014-06-11 05:24:16 +0800928EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400929
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400930static void
931nfs4_sequence_process_interrupted(struct nfs_client *client,
932 struct nfs4_slot *slot, struct rpc_cred *cred)
933{
934 WARN_ON_ONCE(1);
935 slot->interrupted = 0;
936}
937
Chuck Lever3bd23842013-08-09 12:49:19 -0400938#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400939
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400940static
941void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
942 struct nfs4_sequence_res *res,
943 struct nfs4_slot *slot)
944{
945 if (!slot)
946 return;
947 slot->privileged = args->sa_privileged ? 1 : 0;
948 args->sa_slot = slot;
949
950 res->sr_slot = slot;
951 res->sr_timestamp = jiffies;
952 res->sr_status_flags = 0;
953 res->sr_status = 1;
954
955}
956
957int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500958 struct nfs4_sequence_args *args,
959 struct nfs4_sequence_res *res,
960 struct rpc_task *task)
961{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500962 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500963 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500964 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500965
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500966 /* slot already allocated? */
967 if (res->sr_slot != NULL)
968 goto out_start;
969
Anna Schumaker76ee0352017-01-10 16:49:31 -0500970 if (session) {
971 tbl = &session->fc_slot_table;
972 task->tk_timeout = 0;
973 }
974
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400975 for (;;) {
976 spin_lock(&tbl->slot_tbl_lock);
977 /* The state manager will wait until the slot table is empty */
978 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
979 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500980
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400981 slot = nfs4_alloc_slot(tbl);
982 if (IS_ERR(slot)) {
983 /* Try again in 1/4 second */
984 if (slot == ERR_PTR(-ENOMEM))
985 task->tk_timeout = HZ >> 2;
986 goto out_sleep;
987 }
988 spin_unlock(&tbl->slot_tbl_lock);
989
990 if (likely(!slot->interrupted))
991 break;
992 nfs4_sequence_process_interrupted(client,
993 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500994 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500995
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400996 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500997
Anna Schumakerad05cc02017-01-11 13:37:06 -0500998 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500999out_start:
1000 rpc_call_start(task);
1001 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001002
1003out_sleep:
1004 if (args->sa_privileged)
1005 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1006 NULL, RPC_PRIORITY_PRIVILEGED);
1007 else
1008 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1009 spin_unlock(&tbl->slot_tbl_lock);
1010 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001011}
1012EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1013
Chuck Lever9915ea72013-08-09 12:48:27 -04001014static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1015{
1016 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001017 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001018 data->seq_args, data->seq_res, task);
1019}
1020
1021static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1022{
1023 struct nfs4_call_sync_data *data = calldata;
1024 nfs4_sequence_done(task, data->seq_res);
1025}
1026
1027static const struct rpc_call_ops nfs40_call_sync_ops = {
1028 .rpc_call_prepare = nfs40_call_sync_prepare,
1029 .rpc_call_done = nfs40_call_sync_done,
1030};
1031
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001032static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001033 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 struct nfs4_sequence_args *args,
1036 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001037{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001038 int ret;
1039 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001040 struct nfs_client *clp = server->nfs_client;
1041 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001042 .seq_server = server,
1043 .seq_args = args,
1044 .seq_res = res,
1045 };
1046 struct rpc_task_setup task_setup = {
1047 .rpc_client = clnt,
1048 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001049 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 .callback_data = &data
1051 };
1052
1053 task = rpc_run_task(&task_setup);
1054 if (IS_ERR(task))
1055 ret = PTR_ERR(task);
1056 else {
1057 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001058 rpc_put_task(task);
1059 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060 return ret;
1061}
1062
Bryan Schumaker7c513052011-03-24 17:12:24 +00001063int nfs4_call_sync(struct rpc_clnt *clnt,
1064 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001065 struct rpc_message *msg,
1066 struct nfs4_sequence_args *args,
1067 struct nfs4_sequence_res *res,
1068 int cache_reply)
1069{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001070 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001071 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001074static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1075 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct nfs_inode *nfsi = NFS_I(dir);
1078
1079 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001080 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1081 | NFS_INO_INVALID_MTIME
1082 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001083 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001084 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1085 nfsi->attrtimeo_timestamp = jiffies;
1086 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001088 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001089 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1090 NFS_INO_INVALID_ACL;
1091 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001092 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001093 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001094 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001095 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 spin_unlock(&dir->i_lock);
1097}
1098
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001099struct nfs4_open_createattrs {
1100 struct nfs4_label *label;
1101 struct iattr *sattr;
1102 const __u32 verf[2];
1103};
1104
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001105static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1106 int err, struct nfs4_exception *exception)
1107{
1108 if (err != -EINVAL)
1109 return false;
1110 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1111 return false;
1112 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1113 exception->retry = 1;
1114 return true;
1115}
1116
Trond Myklebust6ae37332015-01-30 14:21:14 -05001117static u32
1118nfs4_map_atomic_open_share(struct nfs_server *server,
1119 fmode_t fmode, int openflags)
1120{
1121 u32 res = 0;
1122
1123 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1124 case FMODE_READ:
1125 res = NFS4_SHARE_ACCESS_READ;
1126 break;
1127 case FMODE_WRITE:
1128 res = NFS4_SHARE_ACCESS_WRITE;
1129 break;
1130 case FMODE_READ|FMODE_WRITE:
1131 res = NFS4_SHARE_ACCESS_BOTH;
1132 }
1133 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1134 goto out;
1135 /* Want no delegation if we're using O_DIRECT */
1136 if (openflags & O_DIRECT)
1137 res |= NFS4_SHARE_WANT_NO_DELEG;
1138out:
1139 return res;
1140}
1141
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001142static enum open_claim_type4
1143nfs4_map_atomic_open_claim(struct nfs_server *server,
1144 enum open_claim_type4 claim)
1145{
1146 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1147 return claim;
1148 switch (claim) {
1149 default:
1150 return claim;
1151 case NFS4_OPEN_CLAIM_FH:
1152 return NFS4_OPEN_CLAIM_NULL;
1153 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1154 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1155 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1156 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1157 }
1158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161{
1162 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001163 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001164 p->o_res.seqid = p->o_arg.seqid;
1165 p->c_res.seqid = p->c_arg.seqid;
1166 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001167 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001168 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001169 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170}
1171
Al Viro82a2c1b2011-06-22 18:30:55 -04001172static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001174 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001175 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001176 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177{
Al Viro82a2c1b2011-06-22 18:30:55 -04001178 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001179 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001181 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001182 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 struct nfs4_opendata *p;
1184
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001185 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186 if (p == NULL)
1187 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001188
1189 p->f_label = nfs4_label_alloc(server, gfp_mask);
1190 if (IS_ERR(p->f_label))
1191 goto err_free_p;
1192
Kinglong Meea49c2692015-07-27 15:31:38 +08001193 p->a_label = nfs4_label_alloc(server, gfp_mask);
1194 if (IS_ERR(p->a_label))
1195 goto err_free_f;
1196
Trond Myklebust63f5f792015-01-23 19:19:25 -05001197 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1198 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001199 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001200 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001201 nfs_sb_active(dentry->d_sb);
1202 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001203 p->dir = parent;
1204 p->owner = sp;
1205 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001206 p->o_arg.open_flags = flags;
1207 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001208 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001209 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001210 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1211 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001212 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1213 * will return permission denied for all bits until close */
1214 if (!(flags & O_EXCL)) {
1215 /* ask server to check for all possible rights as results
1216 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001217 switch (p->o_arg.claim) {
1218 default:
1219 break;
1220 case NFS4_OPEN_CLAIM_NULL:
1221 case NFS4_OPEN_CLAIM_FH:
1222 p->o_arg.access = NFS4_ACCESS_READ |
1223 NFS4_ACCESS_MODIFY |
1224 NFS4_ACCESS_EXTEND |
1225 NFS4_ACCESS_EXECUTE;
1226 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001227 }
David Howells7539bba2006-08-22 20:06:09 -04001228 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001229 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1230 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001231 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001232 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001233 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001234 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001235 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001236 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 case NFS4_OPEN_CLAIM_NULL:
1238 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1239 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1240 p->o_arg.fh = NFS_FH(dir);
1241 break;
1242 case NFS4_OPEN_CLAIM_PREVIOUS:
1243 case NFS4_OPEN_CLAIM_FH:
1244 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001246 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001248 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001250 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001251
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001252 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001253 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001254 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001255 p->c_arg.fh = &p->o_res.fh;
1256 p->c_arg.stateid = &p->o_res.stateid;
1257 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001258 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001259 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001260 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001261
1262err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001263 nfs4_label_free(p->a_label);
1264err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001265 nfs4_label_free(p->f_label);
1266err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 kfree(p);
1268err:
1269 dput(parent);
1270 return NULL;
1271}
1272
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 struct nfs4_opendata *p = container_of(kref,
1276 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001277 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278
Fred Isaman30ae2412016-10-18 13:39:51 -04001279 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001281 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001282 if (p->state != NULL)
1283 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001285
Kinglong Meea49c2692015-07-27 15:31:38 +08001286 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001287 nfs4_label_free(p->f_label);
1288
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001290 dput(p->dentry);
1291 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001292 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001293 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294 kfree(p);
1295}
1296
1297static void nfs4_opendata_put(struct nfs4_opendata *p)
1298{
1299 if (p != NULL)
1300 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301}
1302
Trond Myklebust24311f82015-09-20 10:50:17 -04001303static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1304 fmode_t fmode)
1305{
1306 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1307 case FMODE_READ|FMODE_WRITE:
1308 return state->n_rdwr != 0;
1309 case FMODE_WRITE:
1310 return state->n_wronly != 0;
1311 case FMODE_READ:
1312 return state->n_rdonly != 0;
1313 }
1314 WARN_ON_ONCE(1);
1315 return false;
1316}
1317
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319{
1320 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321
Trond Myklebust536e43d2012-01-17 22:04:26 -05001322 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 goto out;
1324 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001326 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1327 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001328 break;
1329 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001330 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1331 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001332 break;
1333 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001334 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1335 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001336 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001338 return ret;
1339}
1340
Trond Myklebust2a606182015-08-19 22:30:00 -05001341static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1342 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001343{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001344 if (delegation == NULL)
1345 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001348 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1349 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001350 switch (claim) {
1351 case NFS4_OPEN_CLAIM_NULL:
1352 case NFS4_OPEN_CLAIM_FH:
1353 break;
1354 case NFS4_OPEN_CLAIM_PREVIOUS:
1355 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1356 break;
1357 default:
1358 return 0;
1359 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001360 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361 return 1;
1362}
1363
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001365{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001366 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001367 case FMODE_WRITE:
1368 state->n_wronly++;
1369 break;
1370 case FMODE_READ:
1371 state->n_rdonly++;
1372 break;
1373 case FMODE_READ|FMODE_WRITE:
1374 state->n_rdwr++;
1375 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001376 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001377}
1378
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001379#ifdef CONFIG_NFS_V4_1
1380static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1381{
1382 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1383 return true;
1384 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1385 return true;
1386 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1387 return true;
1388 return false;
1389}
1390#endif /* CONFIG_NFS_V4_1 */
1391
Trond Myklebustc9399f22017-11-06 15:28:01 -05001392static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1393{
1394 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1395 wake_up_all(&state->waitq);
1396}
1397
1398static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1399 const nfs4_stateid *stateid)
1400{
1401 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1402 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1403
1404 if (stateid_seqid == state_seqid + 1U ||
1405 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1406 nfs_state_log_update_open_stateid(state);
1407 else
1408 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1409}
1410
Trond Myklebust4f14c192014-02-12 19:15:06 -05001411static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001412{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001413 struct nfs_client *clp = state->owner->so_server->nfs_client;
1414 bool need_recover = false;
1415
1416 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1417 need_recover = true;
1418 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1419 need_recover = true;
1420 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1421 need_recover = true;
1422 if (need_recover)
1423 nfs4_state_mark_reclaim_nograce(clp, state);
1424}
1425
Trond Myklebustc9399f22017-11-06 15:28:01 -05001426/*
1427 * Check for whether or not the caller may update the open stateid
1428 * to the value passed in by stateid.
1429 *
1430 * Note: This function relies heavily on the server implementing
1431 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1432 * correctly.
1433 * i.e. The stateid seqids have to be initialised to 1, and
1434 * are then incremented on every state transition.
1435 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001436static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001437 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001438{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001439 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1440 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1441 if (stateid->seqid == cpu_to_be32(1))
1442 nfs_state_log_update_open_stateid(state);
1443 else
1444 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001445 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001446 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001447
1448 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1449 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001450 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001451 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001452 return false;
1453}
1454
Trond Myklebustf95549c2015-01-23 18:06:09 -05001455static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1456{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001457 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1458 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001459 if (state->n_wronly)
1460 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1461 if (state->n_rdonly)
1462 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 if (state->n_rdwr)
1464 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001465 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001466}
1467
Trond Myklebust226056c2014-02-11 10:41:07 -05001468static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1469 nfs4_stateid *stateid, fmode_t fmode)
1470{
1471 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1472 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1473 case FMODE_WRITE:
1474 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1475 break;
1476 case FMODE_READ:
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 break;
1479 case 0:
1480 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1481 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1482 clear_bit(NFS_OPEN_STATE, &state->flags);
1483 }
1484 if (stateid == NULL)
1485 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001486 /* Handle OPEN+OPEN_DOWNGRADE races */
1487 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1488 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001489 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001490 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001491 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001492 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001493 nfs4_stateid_copy(&state->stateid, stateid);
1494 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001495 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001496out:
1497 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001498}
1499
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001500static void nfs_clear_open_stateid(struct nfs4_state *state,
1501 nfs4_stateid *arg_stateid,
1502 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001503{
1504 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001505 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1506 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1507 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001508 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1510 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001511}
1512
Trond Myklebust1393d962016-09-22 13:39:13 -04001513static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001514 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001515{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001516 DEFINE_WAIT(wait);
1517 int status = 0;
1518 for (;;) {
1519
1520 if (!nfs_need_update_open_stateid(state, stateid))
1521 return;
1522 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001523 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001524 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001525 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001526 /* Rely on seqids for serialisation with NFSv4.0 */
1527 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1528 break;
1529
1530 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1531 /*
1532 * Ensure we process the state changes in the same order
1533 * in which the server processed them by delaying the
1534 * update of the stateid until we are in sequence.
1535 */
1536 write_sequnlock(&state->seqlock);
1537 spin_unlock(&state->owner->so_lock);
1538 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001539 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001540 if (!signal_pending(current)) {
1541 if (schedule_timeout(5*HZ) == 0)
1542 status = -EAGAIN;
1543 else
1544 status = 0;
1545 } else
1546 status = -EINTR;
1547 finish_wait(&state->waitq, &wait);
1548 rcu_read_lock();
1549 spin_lock(&state->owner->so_lock);
1550 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001551 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001552
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001553 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1554 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555 nfs4_stateid_copy(freeme, &state->open_stateid);
1556 nfs_test_and_clear_all_open_stateid(state);
1557 }
1558
Trond Myklebuste999e802014-02-10 18:20:47 -05001559 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1560 nfs4_stateid_copy(&state->stateid, stateid);
1561 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001562 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001564}
1565
Trond Myklebustc9399f22017-11-06 15:28:01 -05001566static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001567 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001568 fmode_t fmode,
1569 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001571 /*
1572 * Protect the call to nfs4_state_set_mode_locked and
1573 * serialise the stateid update
1574 */
1575 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001576 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1577 switch (fmode) {
1578 case FMODE_READ:
1579 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1580 break;
1581 case FMODE_WRITE:
1582 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1583 break;
1584 case FMODE_READ|FMODE_WRITE:
1585 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001586 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001587 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001588 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001589}
1590
1591static void nfs_state_set_delegation(struct nfs4_state *state,
1592 const nfs4_stateid *deleg_stateid,
1593 fmode_t fmode)
1594{
1595 /*
1596 * Protect the call to nfs4_state_set_mode_locked and
1597 * serialise the stateid update
1598 */
1599 write_seqlock(&state->seqlock);
1600 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1601 set_bit(NFS_DELEGATED_STATE, &state->flags);
1602 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Trond Myklebust1393d962016-09-22 13:39:13 -04001605static int update_open_stateid(struct nfs4_state *state,
1606 const nfs4_stateid *open_stateid,
1607 const nfs4_stateid *delegation,
1608 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001609{
Trond Myklebust1393d962016-09-22 13:39:13 -04001610 struct nfs_server *server = NFS_SERVER(state->inode);
1611 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001612 struct nfs_inode *nfsi = NFS_I(state->inode);
1613 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001614 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001615 int ret = 0;
1616
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001617 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001618
1619 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001620 spin_lock(&state->owner->so_lock);
1621 if (open_stateid != NULL) {
1622 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1623 ret = 1;
1624 }
1625
Trond Myklebust34310432008-12-23 15:21:38 -05001626 deleg_cur = rcu_dereference(nfsi->delegation);
1627 if (deleg_cur == NULL)
1628 goto no_delegation;
1629
1630 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001631 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001632 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001633 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001634 goto no_delegation_unlock;
1635
1636 if (delegation == NULL)
1637 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001638 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001639 goto no_delegation_unlock;
1640
Trond Myklebustb7391f42008-12-23 15:21:52 -05001641 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001642 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001643 ret = 1;
1644no_delegation_unlock:
1645 spin_unlock(&deleg_cur->lock);
1646no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001647 if (ret)
1648 update_open_stateflags(state, fmode);
1649 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001650 rcu_read_unlock();
1651
Trond Myklebust4f14c192014-02-12 19:15:06 -05001652 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001653 nfs4_schedule_state_manager(clp);
1654 if (freeme.type != 0)
1655 nfs4_test_and_free_stateid(server, &freeme,
1656 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001657
1658 return ret;
1659}
1660
Trond Myklebust39071e62015-01-24 15:07:56 -05001661static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1662 const nfs4_stateid *stateid)
1663{
1664 struct nfs4_state *state = lsp->ls_state;
1665 bool ret = false;
1666
1667 spin_lock(&state->state_lock);
1668 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1669 goto out_noupdate;
1670 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1671 goto out_noupdate;
1672 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1673 ret = true;
1674out_noupdate:
1675 spin_unlock(&state->state_lock);
1676 return ret;
1677}
Trond Myklebust34310432008-12-23 15:21:38 -05001678
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001679static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001680{
1681 struct nfs_delegation *delegation;
1682
Trond Myklebustf5086242018-03-20 16:43:13 -04001683 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684 rcu_read_lock();
1685 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001686 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001687 rcu_read_unlock();
1688 return;
1689 }
1690 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001691 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692}
1693
Trond Myklebust6ee41262007-07-08 14:11:36 -04001694static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001695{
1696 struct nfs4_state *state = opendata->state;
1697 struct nfs_inode *nfsi = NFS_I(state->inode);
1698 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001699 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001700 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001701 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001702 nfs4_stateid stateid;
1703 int ret = -EAGAIN;
1704
Trond Myklebustaac00a82007-07-05 19:02:21 -04001705 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001706 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001707 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001708 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001709 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001710 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001711 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001712 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001713 rcu_read_lock();
1714 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001715 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001716 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001717 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001718 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001720 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001722 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001723 if (!opendata->is_recover) {
1724 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1725 if (ret != 0)
1726 goto out;
1727 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001728 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001729
1730 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001731 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001732 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001733 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001734out:
1735 return ERR_PTR(ret);
1736out_return_state:
1737 atomic_inc(&state->count);
1738 return state;
1739}
1740
Andy Adamsone23008e2012-10-02 21:07:32 -04001741static void
1742nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1743{
1744 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1745 struct nfs_delegation *delegation;
1746 int delegation_flags = 0;
1747
1748 rcu_read_lock();
1749 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1750 if (delegation)
1751 delegation_flags = delegation->flags;
1752 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001753 switch (data->o_arg.claim) {
1754 default:
1755 break;
1756 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1757 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001758 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1759 "returning a delegation for "
1760 "OPEN(CLAIM_DELEGATE_CUR)\n",
1761 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001762 return;
1763 }
1764 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001765 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001766 data->owner->so_cred,
1767 data->o_res.delegation_type,
1768 &data->o_res.delegation,
1769 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001770 else
1771 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001772 data->owner->so_cred,
1773 data->o_res.delegation_type,
1774 &data->o_res.delegation,
1775 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001776}
1777
1778/*
1779 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1780 * and update the nfs4_state.
1781 */
1782static struct nfs4_state *
1783_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1784{
1785 struct inode *inode = data->state->inode;
1786 struct nfs4_state *state = data->state;
1787 int ret;
1788
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001789 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001790 if (data->rpc_status)
1791 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001792 /* cached opens have already been processed */
1793 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001794 }
1795
Andy Adamsone23008e2012-10-02 21:07:32 -04001796 ret = nfs_refresh_inode(inode, &data->f_attr);
1797 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001798 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001799
1800 if (data->o_res.delegation_type != 0)
1801 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001802update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001803 update_open_stateid(state, &data->o_res.stateid, NULL,
1804 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001805 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001806
1807 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001808}
1809
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001810static struct inode *
1811nfs4_opendata_get_inode(struct nfs4_opendata *data)
1812{
1813 struct inode *inode;
1814
1815 switch (data->o_arg.claim) {
1816 case NFS4_OPEN_CLAIM_NULL:
1817 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1818 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1819 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1820 return ERR_PTR(-EAGAIN);
1821 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1822 &data->f_attr, data->f_label);
1823 break;
1824 default:
1825 inode = d_inode(data->dentry);
1826 ihold(inode);
1827 nfs_refresh_inode(inode, &data->f_attr);
1828 }
1829 return inode;
1830}
1831
Andy Adamsone23008e2012-10-02 21:07:32 -04001832static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001833nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1834{
1835 struct nfs4_state *state;
1836 struct inode *inode;
1837
1838 inode = nfs4_opendata_get_inode(data);
1839 if (IS_ERR(inode))
1840 return ERR_CAST(inode);
1841 if (data->state != NULL && data->state->inode == inode) {
1842 state = data->state;
1843 atomic_inc(&state->count);
1844 } else
1845 state = nfs4_get_open_state(inode, data->owner);
1846 iput(inode);
1847 if (state == NULL)
1848 state = ERR_PTR(-ENOMEM);
1849 return state;
1850}
1851
Andy Adamsone23008e2012-10-02 21:07:32 -04001852static struct nfs4_state *
1853_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001854{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001855 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001858 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001859 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860 goto out;
1861 }
1862
Trond Myklebust75e8c482017-08-08 10:38:07 -04001863 state = nfs4_opendata_find_nfs4_state(data);
1864 if (IS_ERR(state))
1865 goto out;
1866
Andy Adamsone23008e2012-10-02 21:07:32 -04001867 if (data->o_res.delegation_type != 0)
1868 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001869 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001870 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001871out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001872 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001873 return state;
1874}
1875
Andy Adamsone23008e2012-10-02 21:07:32 -04001876static struct nfs4_state *
1877nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1878{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001879 struct nfs4_state *ret;
1880
Andy Adamsone23008e2012-10-02 21:07:32 -04001881 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001882 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1883 else
1884 ret = _nfs4_opendata_to_nfs4_state(data);
1885 nfs4_sequence_free_slot(&data->o_res.seq_res);
1886 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001887}
1888
Trond Myklebust864472e2006-01-03 09:55:15 +01001889static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1890{
1891 struct nfs_inode *nfsi = NFS_I(state->inode);
1892 struct nfs_open_context *ctx;
1893
1894 spin_lock(&state->inode->i_lock);
1895 list_for_each_entry(ctx, &nfsi->open_files, list) {
1896 if (ctx->state != state)
1897 continue;
1898 get_nfs_open_context(ctx);
1899 spin_unlock(&state->inode->i_lock);
1900 return ctx;
1901 }
1902 spin_unlock(&state->inode->i_lock);
1903 return ERR_PTR(-ENOENT);
1904}
1905
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001906static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1907 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001908{
1909 struct nfs4_opendata *opendata;
1910
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001911 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001912 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001913 if (opendata == NULL)
1914 return ERR_PTR(-ENOMEM);
1915 opendata->state = state;
1916 atomic_inc(&state->count);
1917 return opendata;
1918}
1919
Trond Myklebust24311f82015-09-20 10:50:17 -04001920static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1921 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001922{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001923 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 int ret;
1925
Trond Myklebust24311f82015-09-20 10:50:17 -04001926 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001927 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001928 opendata->o_arg.open_flags = 0;
1929 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001930 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1931 NFS_SB(opendata->dentry->d_sb),
1932 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001933 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1934 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1935 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001936 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001937 if (ret != 0)
1938 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001939 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001940 if (IS_ERR(newstate))
1941 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001942 if (newstate != opendata->state)
1943 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001944 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001945 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001946}
1947
1948static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1949{
Trond Myklebust864472e2006-01-03 09:55:15 +01001950 int ret;
1951
Trond Myklebust4f14c192014-02-12 19:15:06 -05001952 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1953 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1954 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1955 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001956 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001957 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001958 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001959 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001960 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1961 if (ret != 0)
1962 return ret;
1963 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1964 if (ret != 0)
1965 return ret;
1966 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1967 if (ret != 0)
1968 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001969 /*
1970 * We may have performed cached opens for all three recoveries.
1971 * Check if we need to update the current stateid.
1972 */
1973 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001974 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001975 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001976 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001977 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001978 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001979 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001980 return 0;
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/*
1984 * OPEN_RECLAIM:
1985 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001987static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001989 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001990 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001991 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int status;
1993
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001994 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1995 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001996 if (IS_ERR(opendata))
1997 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001998 rcu_read_lock();
1999 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002000 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002001 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002002 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002003 opendata->o_arg.u.delegation_type = delegation_type;
2004 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002005 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return status;
2007}
2008
Trond Myklebust539cd032007-06-05 11:46:42 -04002009static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct nfs_server *server = NFS_SERVER(state->inode);
2012 struct nfs4_exception exception = { };
2013 int err;
2014 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002015 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002016 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002017 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2018 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002019 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002020 break;
2021 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } while (exception.retry);
2023 return err;
2024}
2025
Trond Myklebust864472e2006-01-03 09:55:15 +01002026static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2027{
2028 struct nfs_open_context *ctx;
2029 int ret;
2030
2031 ctx = nfs4_state_find_open_context(state);
2032 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002033 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002034 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002035 put_nfs_open_context(ctx);
2036 return ret;
2037}
2038
NeilBrowndce26302017-12-13 09:57:09 +11002039static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002041 switch (err) {
2042 default:
2043 printk(KERN_ERR "NFS: %s: unhandled error "
2044 "%d.\n", __func__, err);
2045 case 0:
2046 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002047 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002048 case -ESTALE:
2049 break;
2050 case -NFS4ERR_BADSESSION:
2051 case -NFS4ERR_BADSLOT:
2052 case -NFS4ERR_BAD_HIGH_SLOT:
2053 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2054 case -NFS4ERR_DEADSESSION:
2055 set_bit(NFS_DELEGATED_STATE, &state->flags);
2056 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2057 return -EAGAIN;
2058 case -NFS4ERR_STALE_CLIENTID:
2059 case -NFS4ERR_STALE_STATEID:
2060 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002061 /* Don't recall a delegation if it was lost */
2062 nfs4_schedule_lease_recovery(server->nfs_client);
2063 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002064 case -NFS4ERR_MOVED:
2065 nfs4_schedule_migration_recovery(server);
2066 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002067 case -NFS4ERR_LEASE_MOVED:
2068 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2069 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002070 case -NFS4ERR_DELEG_REVOKED:
2071 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002072 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002073 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002074 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002075 nfs_inode_find_state_and_recover(state->inode,
2076 stateid);
2077 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002078 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002079 case -NFS4ERR_DELAY:
2080 case -NFS4ERR_GRACE:
2081 set_bit(NFS_DELEGATED_STATE, &state->flags);
2082 ssleep(1);
2083 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002084 case -ENOMEM:
2085 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002086 if (fl) {
2087 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2088 if (lsp)
2089 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2090 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002091 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return err;
2094}
2095
Trond Myklebust24311f82015-09-20 10:50:17 -04002096int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2097 struct nfs4_state *state, const nfs4_stateid *stateid,
2098 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002099{
2100 struct nfs_server *server = NFS_SERVER(state->inode);
2101 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002102 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002103
2104 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2105 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2106 if (IS_ERR(opendata))
2107 return PTR_ERR(opendata);
2108 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002109 write_seqlock(&state->seqlock);
2110 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2111 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002112 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2113 switch (type & (FMODE_READ|FMODE_WRITE)) {
2114 case FMODE_READ|FMODE_WRITE:
2115 case FMODE_WRITE:
2116 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2117 if (err)
2118 break;
2119 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2120 if (err)
2121 break;
2122 case FMODE_READ:
2123 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2124 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002125 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002126 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002127}
2128
Chuck Leverbe05c862013-08-09 12:49:47 -04002129static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2130{
2131 struct nfs4_opendata *data = calldata;
2132
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002133 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2134 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002135}
2136
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002137static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2138{
2139 struct nfs4_opendata *data = calldata;
2140
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002141 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002142
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002143 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002144 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002145 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002146 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002147 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002148 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002149 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002150}
2151
2152static void nfs4_open_confirm_release(void *calldata)
2153{
2154 struct nfs4_opendata *data = calldata;
2155 struct nfs4_state *state = NULL;
2156
2157 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002158 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002159 goto out_free;
2160 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002161 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002162 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002163 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002164 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002165 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002166out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002167 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002168}
2169
2170static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002171 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002172 .rpc_call_done = nfs4_open_confirm_done,
2173 .rpc_release = nfs4_open_confirm_release,
2174};
2175
2176/*
2177 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2178 */
2179static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2180{
David Howells2b0143b2015-03-17 22:25:59 +00002181 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002182 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002183 struct rpc_message msg = {
2184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2185 .rpc_argp = &data->c_arg,
2186 .rpc_resp = &data->c_res,
2187 .rpc_cred = data->owner->so_cred,
2188 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 struct rpc_task_setup task_setup_data = {
2190 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002191 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002192 .callback_ops = &nfs4_open_confirm_ops,
2193 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002194 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002195 .flags = RPC_TASK_ASYNC,
2196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 int status;
2198
Anna Schumakerfba83f32018-05-04 16:22:50 -04002199 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2200 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002201 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002202 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002203 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002204 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002205 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002206 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002207 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002208 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002209 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002210 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002211 smp_wmb();
2212 } else
2213 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002214 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return status;
2216}
2217
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220 struct nfs4_opendata *data = calldata;
2221 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002222 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002223 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002224
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002225 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002226 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002227 /*
2228 * Check if we still need to send an OPEN call, or if we can use
2229 * a delegation instead.
2230 */
2231 if (data->state != NULL) {
2232 struct nfs_delegation *delegation;
2233
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002234 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002235 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002236 rcu_read_lock();
2237 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002238 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002239 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002240 rcu_read_unlock();
2241 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002242 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002243 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002244 switch (claim) {
2245 default:
2246 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002247 case NFS4_OPEN_CLAIM_PREVIOUS:
2248 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2249 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002250 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002251 case NFS4_OPEN_CLAIM_FH:
2252 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002253 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002254 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002255 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002256 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002257 &data->o_res.seq_res,
2258 task) != 0)
2259 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002260
2261 /* Set the create mode (note dependency on the session type) */
2262 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2263 if (data->o_arg.open_flags & O_EXCL) {
2264 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2265 if (nfs4_has_persistent_session(clp))
2266 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2267 else if (clp->cl_mvops->minor_version > 0)
2268 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2269 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002270 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002271unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002272 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002273 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002274out_no_action:
2275 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002276out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002277 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002278}
2279
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002280static void nfs4_open_done(struct rpc_task *task, void *calldata)
2281{
2282 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002285
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002286 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002287 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002288
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002289 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002290 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2291 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002292 case S_IFREG:
2293 break;
2294 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002296 break;
2297 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002299 break;
2300 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002302 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002303 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002304 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002305 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2306 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002307 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002308 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002309}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002310
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311static void nfs4_open_release(void *calldata)
2312{
2313 struct nfs4_opendata *data = calldata;
2314 struct nfs4_state *state = NULL;
2315
2316 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002317 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318 goto out_free;
2319 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002320 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321 goto out_free;
2322 /* In case we need an open_confirm, no cleanup! */
2323 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2324 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002325 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002326 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002327 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002328out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002329 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002330}
2331
2332static const struct rpc_call_ops nfs4_open_ops = {
2333 .rpc_call_prepare = nfs4_open_prepare,
2334 .rpc_call_done = nfs4_open_done,
2335 .rpc_release = nfs4_open_release,
2336};
2337
Fred Isaman3b65a302016-09-19 10:06:49 -04002338static int nfs4_run_open_task(struct nfs4_opendata *data,
2339 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002340{
David Howells2b0143b2015-03-17 22:25:59 +00002341 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002342 struct nfs_server *server = NFS_SERVER(dir);
2343 struct nfs_openargs *o_arg = &data->o_arg;
2344 struct nfs_openres *o_res = &data->o_res;
2345 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002346 struct rpc_message msg = {
2347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2348 .rpc_argp = o_arg,
2349 .rpc_resp = o_res,
2350 .rpc_cred = data->owner->so_cred,
2351 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002352 struct rpc_task_setup task_setup_data = {
2353 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002354 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002355 .callback_ops = &nfs4_open_ops,
2356 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002357 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002358 .flags = RPC_TASK_ASYNC,
2359 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002360 int status;
2361
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002362 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002363 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002364 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002365 data->cancelled = false;
2366 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002367 if (!ctx) {
2368 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002369 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002370 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002371 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002372 pnfs_lgopen_prepare(data, ctx);
2373 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002374 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002375 if (IS_ERR(task))
2376 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002377 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002378 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002379 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002380 smp_wmb();
2381 } else
2382 status = data->rpc_status;
2383 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002384
2385 return status;
2386}
2387
2388static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2389{
David Howells2b0143b2015-03-17 22:25:59 +00002390 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002391 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002392 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002393
Fred Isaman3b65a302016-09-19 10:06:49 -04002394 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002395 if (status != 0 || !data->rpc_done)
2396 return status;
2397
Trond Myklebust6926afd2012-01-07 13:22:46 -05002398 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2399
Anna Schumakerd7e98252017-01-11 16:13:29 -05002400 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002401 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002402
2403 return status;
2404}
2405
Trond Myklebustf3792d62014-07-10 08:54:32 -04002406/*
2407 * Additional permission checks in order to distinguish between an
2408 * open for read, and an open for execute. This works around the
2409 * fact that NFSv4 OPEN treats read and execute permissions as being
2410 * the same.
2411 * Note that in the non-execute case, we want to turn off permission
2412 * checking if we just created a new file (POSIX open() semantics).
2413 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002414static int nfs4_opendata_access(struct rpc_cred *cred,
2415 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002416 struct nfs4_state *state, fmode_t fmode,
2417 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002418{
2419 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002420 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002421
2422 /* access call failed or for some reason the server doesn't
2423 * support any access modes -- defer access call until later */
2424 if (opendata->o_res.access_supported == 0)
2425 return 0;
2426
2427 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002428 /*
2429 * Use openflags to check for exec, because fmode won't
2430 * always have FMODE_EXEC set when file open for exec.
2431 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002432 if (openflags & __FMODE_EXEC) {
2433 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002434 if (S_ISDIR(state->inode->i_mode))
2435 mask = NFS4_ACCESS_LOOKUP;
2436 else
2437 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002438 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002439 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002440
2441 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002442 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2443 nfs_access_add_cache(state->inode, &cache);
2444
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002445 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2446 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002447 return 0;
2448
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002449 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002450}
2451
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002452/*
2453 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2454 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002455static int _nfs4_proc_open(struct nfs4_opendata *data,
2456 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002457{
David Howells2b0143b2015-03-17 22:25:59 +00002458 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002459 struct nfs_server *server = NFS_SERVER(dir);
2460 struct nfs_openargs *o_arg = &data->o_arg;
2461 struct nfs_openres *o_res = &data->o_res;
2462 int status;
2463
Fred Isaman3b65a302016-09-19 10:06:49 -04002464 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002465 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002466 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002467 if (status != 0) {
2468 if (status == -NFS4ERR_BADNAME &&
2469 !(o_arg->open_flags & O_CREAT))
2470 return -ENOENT;
2471 return status;
2472 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002473
Trond Myklebust6926afd2012-01-07 13:22:46 -05002474 nfs_fattr_map_and_free_names(server, &data->f_attr);
2475
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002476 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002477 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002478 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002479 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002480 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002481 if (data->file_created ||
2482 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002483 update_changeattr(dir, &o_res->cinfo,
2484 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002485 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002486 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2487 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002489 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002493 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2494 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002495 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2496 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002497 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501/*
2502 * OPEN_EXPIRED:
2503 * reclaim state on the server after a network partition.
2504 * Assumes caller holds the appropriate lock
2505 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002506static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002508 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002509 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002511 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002512 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002513 if (IS_ERR(opendata))
2514 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002515 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002516 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002517 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002518 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002522static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002523{
Trond Myklebust539cd032007-06-05 11:46:42 -04002524 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002525 struct nfs4_exception exception = { };
2526 int err;
2527
2528 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002529 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002530 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002531 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2532 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002533 switch (err) {
2534 default:
2535 goto out;
2536 case -NFS4ERR_GRACE:
2537 case -NFS4ERR_DELAY:
2538 nfs4_handle_exception(server, err, &exception);
2539 err = 0;
2540 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002541 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002542out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002543 return err;
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2547{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002549 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Trond Myklebust864472e2006-01-03 09:55:15 +01002551 ctx = nfs4_state_find_open_context(state);
2552 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002553 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002554 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002555 put_nfs_open_context(ctx);
2556 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Trond Myklebust41020b62016-09-22 13:38:58 -04002559static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2560 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002561{
Trond Myklebust41020b62016-09-22 13:38:58 -04002562 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002563 write_seqlock(&state->seqlock);
2564 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2565 write_sequnlock(&state->seqlock);
2566 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2567}
2568
2569static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2570{
2571 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002572 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002573}
2574
2575static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2576{
2577 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2578 nfs40_clear_delegation_stateid(state);
2579 return nfs4_open_expired(sp, state);
2580}
2581
Trond Myklebust45870d62016-09-22 13:38:59 -04002582static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2583 nfs4_stateid *stateid,
2584 struct rpc_cred *cred)
2585{
2586 return -NFS4ERR_BAD_STATEID;
2587}
2588
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002589#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002590static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2591 nfs4_stateid *stateid,
2592 struct rpc_cred *cred)
2593{
2594 int status;
2595
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002596 switch (stateid->type) {
2597 default:
2598 break;
2599 case NFS4_INVALID_STATEID_TYPE:
2600 case NFS4_SPECIAL_STATEID_TYPE:
2601 return -NFS4ERR_BAD_STATEID;
2602 case NFS4_REVOKED_STATEID_TYPE:
2603 goto out_free;
2604 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002605
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002606 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002607 switch (status) {
2608 case -NFS4ERR_EXPIRED:
2609 case -NFS4ERR_ADMIN_REVOKED:
2610 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002611 break;
2612 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002613 return status;
2614 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002615out_free:
2616 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002617 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002618 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002619}
2620
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002621static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002622{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002623 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002624 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002625 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002626 struct rpc_cred *cred;
2627 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002629 /* Get the delegation credential for use by test/free_stateid */
2630 rcu_read_lock();
2631 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002632 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002633 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002634 return;
2635 }
2636
2637 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002638 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2639 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2640 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002641 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002642 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002643 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002644 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002645
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002646 cred = get_rpccred(delegation->cred);
2647 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002648 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002649 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002650 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002651 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002652
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002653 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002654}
2655
2656/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002657 * nfs41_check_expired_locks - possibly free a lock stateid
2658 *
2659 * @state: NFSv4 state for an inode
2660 *
2661 * Returns NFS_OK if recovery for this stateid is now finished.
2662 * Otherwise a negative NFS4ERR value is returned.
2663 */
2664static int nfs41_check_expired_locks(struct nfs4_state *state)
2665{
2666 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002667 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002668 struct nfs_server *server = NFS_SERVER(state->inode);
2669
2670 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2671 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002672
2673 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2675 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2676 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2677
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002678 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002679 spin_unlock(&state->state_lock);
2680
2681 nfs4_put_lock_state(prev);
2682 prev = lsp;
2683
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002684 status = nfs41_test_and_free_expired_stateid(server,
2685 &lsp->ls_stateid,
2686 cred);
2687 trace_nfs4_test_lock_stateid(state, lsp, status);
2688 if (status == -NFS4ERR_EXPIRED ||
2689 status == -NFS4ERR_BAD_STATEID) {
2690 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002691 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002692 if (!recover_lost_locks)
2693 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2694 } else if (status != NFS_OK) {
2695 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002696 nfs4_put_lock_state(prev);
2697 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002698 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002699 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002700 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002701 }
2702 spin_unlock(&state->state_lock);
2703 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002704out:
2705 return ret;
2706}
2707
2708/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002709 * nfs41_check_open_stateid - possibly free an open stateid
2710 *
2711 * @state: NFSv4 state for an inode
2712 *
2713 * Returns NFS_OK if recovery for this stateid is now finished.
2714 * Otherwise a negative NFS4ERR value is returned.
2715 */
2716static int nfs41_check_open_stateid(struct nfs4_state *state)
2717{
2718 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002719 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002720 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002721 int status;
2722
Trond Myklebustb134fc42016-09-22 13:39:16 -04002723 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002724 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2725 if (nfs4_have_delegation(state->inode, state->state))
2726 return NFS_OK;
2727 return -NFS4ERR_OPENMODE;
2728 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002729 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002730 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002731 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002732 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002733 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002734 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2735 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2736 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002737 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002738 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002739 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002740 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002741 if (nfs_open_stateid_recover_openmode(state))
2742 return -NFS4ERR_OPENMODE;
2743 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002744}
2745
2746static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2747{
Chuck Levereb64cf92012-07-11 16:30:05 -04002748 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002749
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002750 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002751 status = nfs41_check_expired_locks(state);
2752 if (status != NFS_OK)
2753 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002754 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002755 if (status != NFS_OK)
2756 status = nfs4_open_expired(sp, state);
2757 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002758}
2759#endif
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002762 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2763 * fields corresponding to attributes that were used to store the verifier.
2764 * Make sure we clobber those fields in the later setattr call
2765 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002766static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002767 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002768{
Trond Myklebust609339c2018-03-28 16:18:17 -04002769 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2770 __u32 attrset[3];
2771 unsigned ret;
2772 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002773
Trond Myklebust609339c2018-03-28 16:18:17 -04002774 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2775 attrset[i] = opendata->o_res.attrset[i];
2776 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2777 attrset[i] &= ~bitmask[i];
2778 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002779
Trond Myklebust609339c2018-03-28 16:18:17 -04002780 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2781 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002782
Trond Myklebust609339c2018-03-28 16:18:17 -04002783 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2784 if (sattr->ia_valid & ATTR_ATIME_SET)
2785 ret |= ATTR_ATIME_SET;
2786 else
2787 ret |= ATTR_ATIME;
2788 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002789
Trond Myklebust609339c2018-03-28 16:18:17 -04002790 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2791 if (sattr->ia_valid & ATTR_MTIME_SET)
2792 ret |= ATTR_MTIME_SET;
2793 else
2794 ret |= ATTR_MTIME;
2795 }
2796
2797 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002798 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002799 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002800}
2801
Trond Myklebustc21443c2013-02-07 14:26:21 -05002802static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2803 fmode_t fmode,
2804 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002805 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002806{
2807 struct nfs4_state_owner *sp = opendata->owner;
2808 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002809 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002810 struct nfs4_state *state;
2811 unsigned int seq;
2812 int ret;
2813
2814 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2815
Fred Isaman3b65a302016-09-19 10:06:49 -04002816 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002817 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002818 goto out;
2819
Trond Myklebustae55e592018-05-22 11:17:16 -04002820 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002821 ret = PTR_ERR(state);
2822 if (IS_ERR(state))
2823 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002824 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002825 if (server->caps & NFS_CAP_POSIX_LOCK)
2826 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002827 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2828 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002829
Trond Myklebust275bb302013-05-29 13:11:28 -04002830 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002831 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002832 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002833 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002834 alias = d_exact_alias(dentry, state->inode);
2835 if (!alias)
2836 alias = d_splice_alias(igrab(state->inode), dentry);
2837 /* d_splice_alias() can't fail here - it's a non-directory */
2838 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002839 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002840 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002841 }
2842 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002843 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002844 }
2845
Trond Myklebustc21443c2013-02-07 14:26:21 -05002846 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2847 if (ret != 0)
2848 goto out;
2849
David Howells2b0143b2015-03-17 22:25:59 +00002850 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002851 nfs_inode_attach_open_context(ctx);
2852 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2853 nfs4_schedule_stateid_recovery(server, state);
Fred Isaman2409a972016-10-06 12:11:21 -04002854 else
2855 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002856 }
Fred Isaman2409a972016-10-06 12:11:21 -04002857
Trond Myklebustc21443c2013-02-07 14:26:21 -05002858out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002859 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002860 return ret;
2861}
2862
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002863/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002864 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 */
Andy Adamson82be4172012-05-23 05:02:35 -04002866static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002867 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002868 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002869 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002870 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 struct nfs4_state_owner *sp;
2873 struct nfs4_state *state = NULL;
2874 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002875 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002876 struct dentry *dentry = ctx->dentry;
2877 struct rpc_cred *cred = ctx->cred;
2878 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2879 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002880 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002881 struct iattr *sattr = c->sattr;
2882 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002883 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002884 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002888 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2889 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2891 goto out_err;
2892 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002893 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002894 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002895 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002896 if (d_really_is_positive(dentry))
2897 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002898 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002899 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002900 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002901 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2902 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002903 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002904 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
David Quigley14c43f72013-05-22 12:50:43 -04002906 if (label) {
2907 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2908 if (IS_ERR(olabel)) {
2909 status = PTR_ERR(olabel);
2910 goto err_opendata_put;
2911 }
2912 }
2913
Trond Myklebuste911b812014-03-26 13:24:37 -07002914 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2915 if (!opendata->f_attr.mdsthreshold) {
2916 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2917 if (!opendata->f_attr.mdsthreshold)
2918 goto err_free_label;
2919 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002920 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002921 }
David Howells2b0143b2015-03-17 22:25:59 +00002922 if (d_really_is_positive(dentry))
2923 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002924
Trond Myklebust3efb9722013-05-29 13:17:04 -04002925 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002926 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002927 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002928 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002929
NeilBrownefcbc042015-07-30 13:00:56 +10002930 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002931 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002932 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002933 /*
2934 * send create attributes which was not set by open
2935 * with an extra setattr.
2936 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002937 if (attrs || label) {
2938 unsigned ia_old = sattr->ia_valid;
2939
2940 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002941 nfs_fattr_init(opendata->o_res.f_attr);
2942 status = nfs4_do_setattr(state->inode, cred,
2943 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002944 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002945 if (status == 0) {
2946 nfs_setattr_update_inode(state->inode, sattr,
2947 opendata->o_res.f_attr);
2948 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2949 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002950 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002951 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002952 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002953 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002954 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002955
Trond Myklebuste911b812014-03-26 13:24:37 -07002956 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002957 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002958 opendata->f_attr.mdsthreshold = NULL;
2959 }
Andy Adamson82be4172012-05-23 05:02:35 -04002960
David Quigley14c43f72013-05-22 12:50:43 -04002961 nfs4_label_free(olabel);
2962
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002963 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002966err_free_label:
2967 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002968err_opendata_put:
2969 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002970err_put_state_owner:
2971 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return status;
2974}
2975
2976
Andy Adamson82be4172012-05-23 05:02:35 -04002977static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002978 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002979 int flags,
2980 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002981 struct nfs4_label *label,
2982 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002984 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 struct nfs4_exception exception = { };
2986 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002987 struct nfs4_open_createattrs c = {
2988 .label = label,
2989 .sattr = sattr,
2990 .verf = {
2991 [0] = (__u32)jiffies,
2992 [1] = (__u32)current->pid,
2993 },
2994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 int status;
2996
2997 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002998 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002999 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003000 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (status == 0)
3002 break;
3003 /* NOTE: BAD_SEQID means the server and client disagree about the
3004 * book-keeping w.r.t. state-changing operations
3005 * (OPEN/CLOSE/LOCK/LOCKU...)
3006 * It is actually a sign of a bug on the client or on the server.
3007 *
3008 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003009 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 * have unhashed the old state_owner for us, and that we can
3011 * therefore safely retry using a new one. We should still warn
3012 * the user though...
3013 */
3014 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003015 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003016 " returned a bad sequence-id error!\n",
3017 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 exception.retry = 1;
3019 continue;
3020 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003021 /*
3022 * BAD_STATEID on OPEN means that the server cancelled our
3023 * state before it received the OPEN_CONFIRM.
3024 * Recover by retrying the request as per the discussion
3025 * on Page 181 of RFC3530.
3026 */
3027 if (status == -NFS4ERR_BAD_STATEID) {
3028 exception.retry = 1;
3029 continue;
3030 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003031 if (status == -EAGAIN) {
3032 /* We must have found a delegation */
3033 exception.retry = 1;
3034 continue;
3035 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003036 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3037 continue;
3038 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 status, &exception));
3040 } while (exception.retry);
3041 return res;
3042}
3043
Trond Myklebust8487c472016-06-26 08:44:35 -04003044static int _nfs4_do_setattr(struct inode *inode,
3045 struct nfs_setattrargs *arg,
3046 struct nfs_setattrres *res,
3047 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003048 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003049{
3050 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003051 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3053 .rpc_argp = arg,
3054 .rpc_resp = res,
3055 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003056 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003057 struct rpc_cred *delegation_cred = NULL;
3058 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003059 bool truncate;
3060 int status;
3061
3062 nfs_fattr_init(res->fattr);
3063
3064 /* Servers should only apply open mode checks for file size changes */
3065 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003066 if (!truncate)
3067 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003068
Trond Myklebust991eedb2018-04-09 11:15:30 -04003069 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003070 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003071 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003072 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003073 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003074 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003075 l_ctx = nfs_get_lock_context(ctx);
3076 if (IS_ERR(l_ctx))
3077 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003078 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3079 &arg->stateid, &delegation_cred);
3080 nfs_put_lock_context(l_ctx);
3081 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003082 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003083 } else {
3084zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003085 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003086 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003087 if (delegation_cred)
3088 msg.rpc_cred = delegation_cred;
3089
3090 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3091
3092 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003093 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003094 renew_lease(server, timestamp);
3095 trace_nfs4_setattr(inode, &arg->stateid, status);
3096 return status;
3097}
3098
3099static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3100 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003101 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003102 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003104 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003105 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003106 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003107 struct nfs_setattrargs arg = {
3108 .fh = NFS_FH(inode),
3109 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003111 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003112 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003113 };
3114 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003116 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003118 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003119 struct nfs4_exception exception = {
3120 .state = state,
3121 .inode = inode,
3122 .stateid = &arg.stateid,
3123 };
3124 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003127 nfs4_bitmap_copy_adjust_setattr(bitmask,
3128 nfs4_bitmask(server, olabel),
3129 inode);
3130
NeilBrown29b59f92016-10-13 15:26:47 +11003131 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003132 switch (err) {
3133 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003134 if (!(sattr->ia_valid & ATTR_SIZE)) {
3135 pr_warn_once("NFSv4: server %s is incorrectly "
3136 "applying open mode checks to "
3137 "a SETATTR that is not "
3138 "changing file size.\n",
3139 server->nfs_client->cl_hostname);
3140 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003141 if (state && !(state->state & FMODE_WRITE)) {
3142 err = -EBADF;
3143 if (sattr->ia_valid & ATTR_OPEN)
3144 err = -EACCES;
3145 goto out;
3146 }
3147 }
3148 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003150out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return err;
3152}
3153
Peng Tao500d7012015-09-22 11:35:22 +08003154static bool
3155nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3156{
3157 if (inode == NULL || !nfs_have_layout(inode))
3158 return false;
3159
3160 return pnfs_wait_on_layoutreturn(inode, task);
3161}
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163struct nfs4_closedata {
3164 struct inode *inode;
3165 struct nfs4_state *state;
3166 struct nfs_closeargs arg;
3167 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003168 struct {
3169 struct nfs4_layoutreturn_args arg;
3170 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003171 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003172 u32 roc_barrier;
3173 bool roc;
3174 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003175 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003176 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177};
3178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003179static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003180{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003181 struct nfs4_closedata *calldata = data;
3182 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003183 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003184
Trond Myklebustcf805162016-11-15 14:56:07 -05003185 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003186 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3187 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003188 nfs4_put_open_state(calldata->state);
3189 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003190 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003191 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003192 kfree(calldata);
3193}
3194
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003195static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003197 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003200 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003201 struct nfs4_exception exception = {
3202 .state = state,
3203 .inode = calldata->inode,
3204 .stateid = &calldata->arg.stateid,
3205 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Chuck Levera3ca5652012-03-01 17:00:40 -05003207 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003208 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3209 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003210 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003211
3212 /* Handle Layoutreturn errors */
3213 if (calldata->arg.lr_args && task->tk_status != 0) {
3214 switch (calldata->res.lr_ret) {
3215 default:
3216 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3217 break;
3218 case 0:
3219 calldata->arg.lr_args = NULL;
3220 calldata->res.lr_res = NULL;
3221 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003222 case -NFS4ERR_OLD_STATEID:
3223 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003224 calldata->inode))
3225 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003226 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003227 case -NFS4ERR_ADMIN_REVOKED:
3228 case -NFS4ERR_DELEG_REVOKED:
3229 case -NFS4ERR_EXPIRED:
3230 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003231 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3232 case -NFS4ERR_WRONG_CRED:
3233 calldata->arg.lr_args = NULL;
3234 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003235 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003236 }
3237 }
3238
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003239 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 * the state_owner. we keep this around to process errors
3241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 switch (task->tk_status) {
3243 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003244 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003245 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003246 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003247 case -NFS4ERR_ACCESS:
3248 if (calldata->arg.bitmask != NULL) {
3249 calldata->arg.bitmask = NULL;
3250 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003251 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003252
3253 }
3254 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003255 case -NFS4ERR_OLD_STATEID:
3256 /* Did we race with OPEN? */
3257 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003258 state))
3259 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003260 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003261 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003263 case -NFS4ERR_EXPIRED:
3264 nfs4_free_revoked_stateid(server,
3265 &calldata->arg.stateid,
3266 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003267 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003268 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003271 task->tk_status = nfs4_async_handle_exception(task,
3272 server, task->tk_status, &exception);
3273 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003274 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003276 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3277 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003278out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003279 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003280 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003281 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003282 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003283 return;
3284lr_restart:
3285 calldata->res.lr_ret = 0;
3286out_restart:
3287 task->tk_status = 0;
3288 rpc_restart_call_prepare(task);
3289 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003292static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003294 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003295 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003296 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003297 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003298 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003299
Chuck Levera3ca5652012-03-01 17:00:40 -05003300 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003301 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003302 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003303
Trond Myklebust88069f72009-12-08 08:33:16 -05003304 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003305 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003306 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3307 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3308 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003309 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003310 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003311 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003312 if (state->n_rdonly == 0)
3313 call_close |= is_rdonly;
3314 else if (is_rdonly)
3315 calldata->arg.fmode |= FMODE_READ;
3316 if (state->n_wronly == 0)
3317 call_close |= is_wronly;
3318 else if (is_wronly)
3319 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003320 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3321 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003322 } else if (is_rdwr)
3323 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3324
Trond Myklebust5cc78612016-11-14 11:19:56 -05003325 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003326 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003327 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003328 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003329
3330 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003331 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003332 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003333 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003334
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003335 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003336 nfs_release_seqid(calldata->arg.seqid);
3337 goto out_wait;
3338 }
3339
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003340 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003341 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003342
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003343 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003344 /* Close-to-open cache consistency revalidation */
3345 if (!nfs4_have_delegation(inode, FMODE_READ))
3346 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3347 else
3348 calldata->arg.bitmask = NULL;
3349 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003350
Trond Myklebust6ae37332015-01-30 14:21:14 -05003351 calldata->arg.share_access =
3352 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3353 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003354
Trond Myklebustd8d84982016-12-19 12:14:44 -05003355 if (calldata->res.fattr == NULL)
3356 calldata->arg.bitmask = NULL;
3357 else if (calldata->arg.bitmask == NULL)
3358 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003359 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003360 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003361 &calldata->arg.seq_args,
3362 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003363 task) != 0)
3364 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003365 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003366 return;
3367out_no_action:
3368 task->tk_action = NULL;
3369out_wait:
3370 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003373static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003374 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003375 .rpc_call_done = nfs4_close_done,
3376 .rpc_release = nfs4_free_closedata,
3377};
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379/*
3380 * It is possible for data to be read/written from a mem-mapped file
3381 * after the sys_close call (which hits the vfs layer as a flush).
3382 * This means that we can't safely call nfsv4 close on a file until
3383 * the inode is cleared. This in turn means that we are not good
3384 * NFSv4 citizens - we do not indicate to the server to update the file's
3385 * share state even when we are done with one of the three share
3386 * stateid's in the inode.
3387 *
3388 * NOTE: Caller must be holding the sp->so_owner semaphore!
3389 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003390int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003392 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003393 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003395 struct nfs4_state_owner *sp = state->owner;
3396 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003397 struct rpc_message msg = {
3398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3399 .rpc_cred = state->owner->so_cred,
3400 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003401 struct rpc_task_setup task_setup_data = {
3402 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003403 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003404 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003405 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003406 .flags = RPC_TASK_ASYNC,
3407 };
Trond Myklebust95121352005-10-18 14:20:12 -07003408 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003410 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3411 &task_setup_data.rpc_client, &msg);
3412
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003413 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003415 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003416 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003417 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003419 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003420 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3421 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003423 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3424 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003425 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003426 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003427 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003428 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003429 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003430 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003431 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003432 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003433 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003434 calldata->lr.roc = pnfs_roc(state->inode,
3435 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3436 if (calldata->lr.roc) {
3437 calldata->arg.lr_args = &calldata->lr.arg;
3438 calldata->res.lr_res = &calldata->lr.res;
3439 }
Al Viro643168c2011-06-22 18:20:23 -04003440 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003441
Trond Myklebust1174dd12010-12-21 10:52:24 -05003442 msg.rpc_argp = &calldata->arg;
3443 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003444 task_setup_data.callback_data = calldata;
3445 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003446 if (IS_ERR(task))
3447 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003448 status = 0;
3449 if (wait)
3450 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003451 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003452 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003453out_free_calldata:
3454 kfree(calldata);
3455out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003456 nfs4_put_open_state(state);
3457 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003458 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Trond Myklebust2b484292010-09-17 10:56:51 -04003461static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003462nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3463 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003466 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3467
3468 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Trond Myklebust565277f2007-10-15 18:17:53 -04003470 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003471 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003472
3473 nfs4_label_release_security(label);
3474
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003475 if (IS_ERR(state))
3476 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003477 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Trond Myklebust1185a552009-12-03 15:54:02 -05003480static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003481{
3482 if (ctx->state == NULL)
3483 return;
3484 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003485 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003486 else
Al Viro643168c2011-06-22 18:20:23 -04003487 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Trond Myklebustb944dba2013-11-04 15:20:20 -05003490#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3491#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003492#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3495{
Kinglong Mee8c612822015-08-26 21:12:58 +08003496 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003497 struct nfs4_server_caps_arg args = {
3498 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003499 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 struct nfs4_server_caps_res res = {};
3502 struct rpc_message msg = {
3503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003504 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 .rpc_resp = &res,
3506 };
3507 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003508 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Kinglong Mee8c612822015-08-26 21:12:58 +08003510 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3511 FATTR4_WORD0_FH_EXPIRE_TYPE |
3512 FATTR4_WORD0_LINK_SUPPORT |
3513 FATTR4_WORD0_SYMLINK_SUPPORT |
3514 FATTR4_WORD0_ACLSUPPORT;
3515 if (minorversion)
3516 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3517
Bryan Schumaker7c513052011-03-24 17:12:24 +00003518 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003520 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003521 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003522 case 0:
3523 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3524 res.attr_bitmask[2] = 0;
3525 break;
3526 case 1:
3527 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3528 break;
3529 case 2:
3530 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003533 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3534 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3535 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3536 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003537 NFS_CAP_CTIME|NFS_CAP_MTIME|
3538 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003539 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3540 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 server->caps |= NFS_CAP_ACLS;
3542 if (res.has_links != 0)
3543 server->caps |= NFS_CAP_HARDLINKS;
3544 if (res.has_symlinks != 0)
3545 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003546 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3547 server->caps |= NFS_CAP_FILEID;
3548 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3549 server->caps |= NFS_CAP_MODE;
3550 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3551 server->caps |= NFS_CAP_NLINK;
3552 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3553 server->caps |= NFS_CAP_OWNER;
3554 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3555 server->caps |= NFS_CAP_OWNER_GROUP;
3556 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3557 server->caps |= NFS_CAP_ATIME;
3558 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3559 server->caps |= NFS_CAP_CTIME;
3560 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3561 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003562#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3563 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3564 server->caps |= NFS_CAP_SECURITY_LABEL;
3565#endif
3566 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3567 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003568 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003569
Trond Myklebusta65318b2009-03-11 14:10:28 -04003570 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3571 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3572 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003573 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003574
3575 /* Avoid a regression due to buggy server */
3576 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3577 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003578 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3579 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003582 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return status;
3586}
3587
Trond Myklebust55a97592006-06-09 09:34:19 -04003588int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 struct nfs4_exception exception = { };
3591 int err;
3592 do {
3593 err = nfs4_handle_exception(server,
3594 _nfs4_server_capabilities(server, fhandle),
3595 &exception);
3596 } while (exception.retry);
3597 return err;
3598}
3599
3600static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3601 struct nfs_fsinfo *info)
3602{
David Quigleyaa9c2662013-05-22 12:50:44 -04003603 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003605 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 };
3607 struct nfs4_lookup_res res = {
3608 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003609 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 .fh = fhandle,
3611 };
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3614 .rpc_argp = &args,
3615 .rpc_resp = &res,
3616 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003617
David Quigleyaa9c2662013-05-22 12:50:44 -04003618 bitmask[0] = nfs4_fattr_bitmap[0];
3619 bitmask[1] = nfs4_fattr_bitmap[1];
3620 /*
3621 * Process the label in the upcoming getfattr
3622 */
3623 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3624
Trond Myklebust0e574af2005-10-27 22:12:38 -04003625 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003626 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627}
3628
3629static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3630 struct nfs_fsinfo *info)
3631{
3632 struct nfs4_exception exception = { };
3633 int err;
3634 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003635 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003636 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003637 switch (err) {
3638 case 0:
3639 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003640 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003641 default:
3642 err = nfs4_handle_exception(server, err, &exception);
3643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003645out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return err;
3647}
3648
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003649static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3650 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3651{
Trond Myklebustc2190662013-08-26 19:23:04 -04003652 struct rpc_auth_create_args auth_args = {
3653 .pseudoflavor = flavor,
3654 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003655 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003656
Trond Myklebustc2190662013-08-26 19:23:04 -04003657 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003658 if (IS_ERR(auth))
3659 return -EACCES;
3660 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003661}
3662
Chuck Lever9a744ba2013-03-16 15:56:02 -04003663/*
3664 * Retry pseudoroot lookup with various security flavors. We do this when:
3665 *
3666 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3667 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3668 *
3669 * Returns zero on success, or a negative NFS4ERR value, or a
3670 * negative errno value.
3671 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003672static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003673 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003675 /* Per 3530bis 15.33.5 */
3676 static const rpc_authflavor_t flav_array[] = {
3677 RPC_AUTH_GSS_KRB5P,
3678 RPC_AUTH_GSS_KRB5I,
3679 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003680 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003681 RPC_AUTH_NULL,
3682 };
3683 int status = -EPERM;
3684 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003686 if (server->auth_info.flavor_len > 0) {
3687 /* try each flavor specified by user */
3688 for (i = 0; i < server->auth_info.flavor_len; i++) {
3689 status = nfs4_lookup_root_sec(server, fhandle, info,
3690 server->auth_info.flavors[i]);
3691 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3692 continue;
3693 break;
3694 }
3695 } else {
3696 /* no flavors specified by user, try default list */
3697 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3698 status = nfs4_lookup_root_sec(server, fhandle, info,
3699 flav_array[i]);
3700 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3701 continue;
3702 break;
3703 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003704 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003705
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003706 /*
3707 * -EACCESS could mean that the user doesn't have correct permissions
3708 * to access the mount. It could also mean that we tried to mount
3709 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3710 * existing mount programs don't handle -EACCES very well so it should
3711 * be mapped to -EPERM instead.
3712 */
3713 if (status == -EACCES)
3714 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003715 return status;
3716}
3717
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003718/**
3719 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3720 * @server: initialized nfs_server handle
3721 * @fhandle: we fill in the pseudo-fs root file handle
3722 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003723 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003724 *
3725 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003726 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003727int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003728 struct nfs_fsinfo *info,
3729 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003730{
Andre Przywarac7757072015-04-23 17:17:40 +01003731 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003732
Andre Przywarac7757072015-04-23 17:17:40 +01003733 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003734 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003735
3736 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003737 status = server->nfs_client->cl_mvops->find_root_sec(server,
3738 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 if (status == 0)
3741 status = nfs4_server_capabilities(server, fhandle);
3742 if (status == 0)
3743 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003744
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003745 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
Bryan Schumakerbae36242012-05-10 15:07:31 -04003748static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3749 struct nfs_fsinfo *info)
3750{
3751 int error;
3752 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003753 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003754
3755 error = nfs4_server_capabilities(server, mntfh);
3756 if (error < 0) {
3757 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3758 return error;
3759 }
3760
David Quigley14c43f72013-05-22 12:50:43 -04003761 label = nfs4_label_alloc(server, GFP_KERNEL);
3762 if (IS_ERR(label))
3763 return PTR_ERR(label);
3764
Trond Myklebusta841b542018-04-07 13:50:59 -04003765 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003766 if (error < 0) {
3767 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003768 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003769 }
3770
3771 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3772 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3773 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3774
David Quigley14c43f72013-05-22 12:50:43 -04003775err_free_label:
3776 nfs4_label_free(label);
3777
Bryan Schumakerbae36242012-05-10 15:07:31 -04003778 return error;
3779}
3780
Manoj Naik6b97fd32006-06-09 09:34:29 -04003781/*
3782 * Get locations and (maybe) other attributes of a referral.
3783 * Note that we'll actually follow the referral later when
3784 * we detect fsid mismatch in inode revalidation
3785 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003786static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3787 const struct qstr *name, struct nfs_fattr *fattr,
3788 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003789{
3790 int status = -ENOMEM;
3791 struct page *page = NULL;
3792 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003793
3794 page = alloc_page(GFP_KERNEL);
3795 if (page == NULL)
3796 goto out;
3797 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3798 if (locations == NULL)
3799 goto out;
3800
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003801 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003802 if (status != 0)
3803 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003804
3805 /*
3806 * If the fsid didn't change, this is a migration event, not a
3807 * referral. Cause us to drop into the exception handler, which
3808 * will kick off migration recovery.
3809 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003810 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003811 dprintk("%s: server did not return a different fsid for"
3812 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003813 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003814 goto out;
3815 }
Andy Adamson533eb462011-06-13 18:25:56 -04003816 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3817 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003818
Andy Adamson533eb462011-06-13 18:25:56 -04003819 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003820 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003821 memset(fhandle, 0, sizeof(struct nfs_fh));
3822out:
3823 if (page)
3824 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003825 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003826 return status;
3827}
3828
David Quigley1775fd32013-05-22 12:50:42 -04003829static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003830 struct nfs_fattr *fattr, struct nfs4_label *label,
3831 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Trond Myklebust771734f2018-04-07 13:54:23 -04003833 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct nfs4_getattr_arg args = {
3835 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003836 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 };
3838 struct nfs4_getattr_res res = {
3839 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003840 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 .server = server,
3842 };
3843 struct rpc_message msg = {
3844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3845 .rpc_argp = &args,
3846 .rpc_resp = &res,
3847 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003848
Trond Myklebust771734f2018-04-07 13:54:23 -04003849 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003850
Trond Myklebust0e574af2005-10-27 22:12:38 -04003851 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003852 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
David Quigley1775fd32013-05-22 12:50:42 -04003855static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003856 struct nfs_fattr *fattr, struct nfs4_label *label,
3857 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
3859 struct nfs4_exception exception = { };
3860 int err;
3861 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003862 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003863 trace_nfs4_getattr(server, fhandle, fattr, err);
3864 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 &exception);
3866 } while (exception.retry);
3867 return err;
3868}
3869
3870/*
3871 * The file is not closed if it is opened due to the a request to change
3872 * the size of the file. The open call will not be needed once the
3873 * VFS layer lookup-intents are implemented.
3874 *
3875 * Close is called when the inode is destroyed.
3876 * If we haven't opened the file for O_WRONLY, we
3877 * need to in the size_change case to obtain a stateid.
3878 *
3879 * Got race?
3880 * Because OPEN is always done by name in nfsv4, it is
3881 * possible that we opened a different file by the same
3882 * name. We can recognize this race condition, but we
3883 * can't do anything about it besides returning an error.
3884 *
3885 * This will be fixed with VFS changes (lookup-intent).
3886 */
3887static int
3888nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3889 struct iattr *sattr)
3890{
David Howells2b0143b2015-03-17 22:25:59 +00003891 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003892 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003893 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003894 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 int status;
3896
Peng Tao88ac8152014-09-12 11:04:10 +08003897 if (pnfs_ld_layoutret_on_setattr(inode) &&
3898 sattr->ia_valid & ATTR_SIZE &&
3899 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003900 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003901
Trond Myklebust0e574af2005-10-27 22:12:38 -04003902 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Andy Adamson26699402012-05-30 16:12:24 -04003904 /* Deal with open(O_TRUNC) */
3905 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003906 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003907
3908 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003909 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003910 return 0;
3911
Trond Myklebustd5308382005-11-04 15:33:38 -05003912 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003913 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003914
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003915 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003916 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003917 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003918 }
3919
David Quigley14c43f72013-05-22 12:50:43 -04003920 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3921 if (IS_ERR(label))
3922 return PTR_ERR(label);
3923
Trond Myklebust199366f2018-03-20 16:43:18 -04003924 /* Return any delegations if we're going to change ACLs */
3925 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003926 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003927
NeilBrown29b59f92016-10-13 15:26:47 +11003928 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003929 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003930 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003931 nfs_setsecurity(inode, fattr, label);
3932 }
David Quigley14c43f72013-05-22 12:50:43 -04003933 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 return status;
3935}
3936
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003937static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3938 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003939 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003940{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003941 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003942 int status;
3943 struct nfs4_lookup_arg args = {
3944 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003945 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003946 .name = name,
3947 };
3948 struct nfs4_lookup_res res = {
3949 .server = server,
3950 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003951 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003952 .fh = fhandle,
3953 };
3954 struct rpc_message msg = {
3955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3956 .rpc_argp = &args,
3957 .rpc_resp = &res,
3958 };
3959
David Quigleyaa9c2662013-05-22 12:50:44 -04003960 args.bitmask = nfs4_bitmask(server, label);
3961
David Howells2b3de442006-08-22 20:06:09 -04003962 nfs_fattr_init(fattr);
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003965 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 dprintk("NFS reply lookup: %d\n", status);
3967 return status;
3968}
3969
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003970static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003971{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003972 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003973 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003974 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3975 fattr->nlink = 2;
3976}
3977
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003978static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003979 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003980 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003981{
3982 struct nfs4_exception exception = { };
3983 struct rpc_clnt *client = *clnt;
3984 int err;
3985 do {
David Quigley1775fd32013-05-22 12:50:42 -04003986 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003987 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003988 switch (err) {
3989 case -NFS4ERR_BADNAME:
3990 err = -ENOENT;
3991 goto out;
3992 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003993 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003994 if (err == -NFS4ERR_MOVED)
3995 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003996 goto out;
3997 case -NFS4ERR_WRONGSEC:
3998 err = -EPERM;
3999 if (client != *clnt)
4000 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004001 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004002 if (IS_ERR(client))
4003 return PTR_ERR(client);
4004
4005 exception.retry = 1;
4006 break;
4007 default:
4008 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4009 }
4010 } while (exception.retry);
4011
4012out:
4013 if (err == 0)
4014 *clnt = client;
4015 else if (client != *clnt)
4016 rpc_shutdown_client(client);
4017
4018 return err;
4019}
4020
Al Virobeffb8f2016-07-20 16:34:42 -04004021static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004022 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4023 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004025 int status;
4026 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004027
David Quigley1775fd32013-05-22 12:50:42 -04004028 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004029 if (client != NFS_CLIENT(dir)) {
4030 rpc_shutdown_client(client);
4031 nfs_fixup_secinfo_attributes(fattr);
4032 }
4033 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004036struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004037nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004038 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4039{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004040 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004041 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004042
David Quigley1775fd32013-05-22 12:50:42 -04004043 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004044 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004045 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004046 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004047}
4048
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004049static int _nfs4_proc_lookupp(struct inode *inode,
4050 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4051 struct nfs4_label *label)
4052{
4053 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4054 struct nfs_server *server = NFS_SERVER(inode);
4055 int status;
4056 struct nfs4_lookupp_arg args = {
4057 .bitmask = server->attr_bitmask,
4058 .fh = NFS_FH(inode),
4059 };
4060 struct nfs4_lookupp_res res = {
4061 .server = server,
4062 .fattr = fattr,
4063 .label = label,
4064 .fh = fhandle,
4065 };
4066 struct rpc_message msg = {
4067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4068 .rpc_argp = &args,
4069 .rpc_resp = &res,
4070 };
4071
4072 args.bitmask = nfs4_bitmask(server, label);
4073
4074 nfs_fattr_init(fattr);
4075
4076 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4077 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4078 &res.seq_res, 0);
4079 dprintk("NFS reply lookupp: %d\n", status);
4080 return status;
4081}
4082
4083static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4084 struct nfs_fattr *fattr, struct nfs4_label *label)
4085{
4086 struct nfs4_exception exception = { };
4087 int err;
4088 do {
4089 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4090 trace_nfs4_lookupp(inode, err);
4091 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4092 &exception);
4093 } while (exception.retry);
4094 return err;
4095}
4096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4098{
Trond Myklebust76b32992007-08-10 17:45:11 -04004099 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 struct nfs4_accessargs args = {
4101 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004102 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004104 struct nfs4_accessres res = {
4105 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 struct rpc_message msg = {
4108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4109 .rpc_argp = &args,
4110 .rpc_resp = &res,
4111 .rpc_cred = entry->cred,
4112 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004113 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Trond Myklebust7c672652018-06-04 15:00:53 -04004115 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004116 res.fattr = nfs_alloc_fattr();
4117 if (res.fattr == NULL)
4118 return -ENOMEM;
4119 args.bitmask = server->cache_consistency_bitmask;
4120 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004121
Bryan Schumaker7c513052011-03-24 17:12:24 +00004122 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004124 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004125 if (res.fattr)
4126 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004128 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return status;
4130}
4131
4132static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4133{
4134 struct nfs4_exception exception = { };
4135 int err;
4136 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004137 err = _nfs4_proc_access(inode, entry);
4138 trace_nfs4_access(inode, err);
4139 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 &exception);
4141 } while (exception.retry);
4142 return err;
4143}
4144
4145/*
4146 * TODO: For the time being, we don't try to get any attributes
4147 * along with any of the zero-copy operations READ, READDIR,
4148 * READLINK, WRITE.
4149 *
4150 * In the case of the first three, we want to put the GETATTR
4151 * after the read-type operation -- this is because it is hard
4152 * to predict the length of a GETATTR response in v4, and thus
4153 * align the READ data correctly. This means that the GETATTR
4154 * may end up partially falling into the page cache, and we should
4155 * shift it into the 'tail' of the xdr_buf before processing.
4156 * To do this efficiently, we need to know the total length
4157 * of data received, which doesn't seem to be available outside
4158 * of the RPC layer.
4159 *
4160 * In the case of WRITE, we also want to put the GETATTR after
4161 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004162 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 *
4164 * Both of these changes to the XDR layer would in fact be quite
4165 * minor, but I decided to leave them for a subsequent patch.
4166 */
4167static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4168 unsigned int pgbase, unsigned int pglen)
4169{
4170 struct nfs4_readlink args = {
4171 .fh = NFS_FH(inode),
4172 .pgbase = pgbase,
4173 .pglen = pglen,
4174 .pages = &page,
4175 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004176 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 struct rpc_message msg = {
4178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4179 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004180 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 };
4182
Bryan Schumaker7c513052011-03-24 17:12:24 +00004183 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 -07004184}
4185
4186static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4187 unsigned int pgbase, unsigned int pglen)
4188{
4189 struct nfs4_exception exception = { };
4190 int err;
4191 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004192 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4193 trace_nfs4_readlink(inode, err);
4194 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 &exception);
4196 } while (exception.retry);
4197 return err;
4198}
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004201 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203static int
4204nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004205 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004207 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004208 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004209 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 int status = 0;
4212
NeilBrown532d4de2016-10-13 15:26:47 +11004213 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004214 if (IS_ERR(ctx))
4215 return PTR_ERR(ctx);
4216
David Quigleyaa9c2662013-05-22 12:50:44 -04004217 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4218
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004219 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4220 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004221 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (IS_ERR(state)) {
4223 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004227 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004228 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return status;
4230}
4231
Al Virobeffb8f2016-07-20 16:34:42 -04004232static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
Trond Myklebust16e42952005-10-27 22:12:44 -04004234 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004235 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004237 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004239 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004240 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004241 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4244 .rpc_argp = &args,
4245 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004247 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004248 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004249
Bryan Schumaker7c513052011-03-24 17:12:24 +00004250 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004251 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004252 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return status;
4254}
4255
Trond Myklebust912678d2018-03-20 16:43:15 -04004256static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4257{
4258 struct nfs4_exception exception = { };
4259 struct inode *inode = d_inode(dentry);
4260 int err;
4261
Trond Myklebustc01d3642018-03-20 16:43:20 -04004262 if (inode) {
4263 if (inode->i_nlink == 1)
4264 nfs4_inode_return_delegation(inode);
4265 else
4266 nfs4_inode_make_writeable(inode);
4267 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004268 do {
4269 err = _nfs4_proc_remove(dir, &dentry->d_name);
4270 trace_nfs4_remove(dir, &dentry->d_name, err);
4271 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4272 &exception);
4273 } while (exception.retry);
4274 return err;
4275}
4276
4277static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
4279 struct nfs4_exception exception = { };
4280 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004283 err = _nfs4_proc_remove(dir, name);
4284 trace_nfs4_remove(dir, name, err);
4285 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 &exception);
4287 } while (exception.retry);
4288 return err;
4289}
4290
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004291static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4292 struct dentry *dentry,
4293 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004295 struct nfs_removeargs *args = msg->rpc_argp;
4296 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Trond Myklebust977fcc22018-03-20 16:43:17 -04004298 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004300 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004301
4302 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004303
4304 if (inode)
4305 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004308static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4309{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004310 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004311 &data->args.seq_args,
4312 &data->res.seq_res,
4313 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004316static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004318 struct nfs_unlinkdata *data = task->tk_calldata;
4319 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004320
Trond Myklebust14516c32010-07-31 14:29:06 -04004321 if (!nfs4_sequence_done(task, &res->seq_res))
4322 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004323 if (nfs4_async_handle_error(task, res->server, NULL,
4324 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004325 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004326 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004327 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004328 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
4330
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004331static void nfs4_proc_rename_setup(struct rpc_message *msg,
4332 struct dentry *old_dentry,
4333 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004334{
Jeff Laytond3d41522010-09-17 17:31:57 -04004335 struct nfs_renameargs *arg = msg->rpc_argp;
4336 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004337 struct inode *old_inode = d_inode(old_dentry);
4338 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004339
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004340 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004341 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004342 if (new_inode)
4343 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004344 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004345 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004346 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004347}
4348
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004349static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4350{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004351 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004352 &data->args.seq_args,
4353 &data->res.seq_res,
4354 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004355}
4356
4357static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4358 struct inode *new_dir)
4359{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004360 struct nfs_renamedata *data = task->tk_calldata;
4361 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004362
4363 if (!nfs4_sequence_done(task, &res->seq_res))
4364 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004365 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004366 return 0;
4367
Trond Myklebustc733c492017-01-11 12:32:26 -05004368 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004369 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004370 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004371 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004372 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004373 return 1;
4374}
4375
Al Virobeffb8f2016-07-20 16:34:42 -04004376static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004378 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004379 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 struct nfs4_link_arg arg = {
4381 .fh = NFS_FH(inode),
4382 .dir_fh = NFS_FH(dir),
4383 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004384 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004386 struct nfs4_link_res res = {
4387 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004388 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004389 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 struct rpc_message msg = {
4391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4392 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004393 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004395 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Trond Myklebust136f2622010-04-16 16:22:49 -04004397 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004398 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004399 goto out;
4400
David Quigley14c43f72013-05-22 12:50:43 -04004401 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4402 if (IS_ERR(res.label)) {
4403 status = PTR_ERR(res.label);
4404 goto out;
4405 }
4406
Trond Myklebustc01d3642018-03-20 16:43:20 -04004407 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004408 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004409
Bryan Schumaker7c513052011-03-24 17:12:24 +00004410 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004411 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004412 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004413 status = nfs_post_op_update_inode(inode, res.fattr);
4414 if (!status)
4415 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004416 }
David Quigley14c43f72013-05-22 12:50:43 -04004417
4418
4419 nfs4_label_free(res.label);
4420
Trond Myklebust136f2622010-04-16 16:22:49 -04004421out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004422 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return status;
4424}
4425
Al Virobeffb8f2016-07-20 16:34:42 -04004426static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
4428 struct nfs4_exception exception = { };
4429 int err;
4430 do {
4431 err = nfs4_handle_exception(NFS_SERVER(inode),
4432 _nfs4_proc_link(inode, dir, name),
4433 &exception);
4434 } while (exception.retry);
4435 return err;
4436}
4437
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004438struct nfs4_createdata {
4439 struct rpc_message msg;
4440 struct nfs4_create_arg arg;
4441 struct nfs4_create_res res;
4442 struct nfs_fh fh;
4443 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004444 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445};
4446
4447static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004448 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449{
4450 struct nfs4_createdata *data;
4451
4452 data = kzalloc(sizeof(*data), GFP_KERNEL);
4453 if (data != NULL) {
4454 struct nfs_server *server = NFS_SERVER(dir);
4455
David Quigley14c43f72013-05-22 12:50:43 -04004456 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4457 if (IS_ERR(data->label))
4458 goto out_free;
4459
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004460 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4461 data->msg.rpc_argp = &data->arg;
4462 data->msg.rpc_resp = &data->res;
4463 data->arg.dir_fh = NFS_FH(dir);
4464 data->arg.server = server;
4465 data->arg.name = name;
4466 data->arg.attrs = sattr;
4467 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004468 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004469 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004470 data->res.server = server;
4471 data->res.fh = &data->fh;
4472 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004473 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004474 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004475 }
4476 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004477out_free:
4478 kfree(data);
4479 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004480}
4481
4482static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4483{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004484 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004485 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004486 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004487 update_changeattr(dir, &data->res.dir_cinfo,
4488 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004489 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004490 }
4491 return status;
4492}
4493
4494static void nfs4_free_createdata(struct nfs4_createdata *data)
4495{
David Quigley14c43f72013-05-22 12:50:43 -04004496 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004497 kfree(data);
4498}
4499
Chuck Lever4f390c12006-08-22 20:06:22 -04004500static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004501 struct page *page, unsigned int len, struct iattr *sattr,
4502 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004504 struct nfs4_createdata *data;
4505 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Chuck Lever94a6d752006-08-22 20:06:23 -04004507 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004508 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004509
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004510 status = -ENOMEM;
4511 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4512 if (data == NULL)
4513 goto out;
4514
4515 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4516 data->arg.u.symlink.pages = &page;
4517 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004518 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004520 status = nfs4_do_create(dir, dentry, data);
4521
4522 nfs4_free_createdata(data);
4523out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return status;
4525}
4526
Chuck Lever4f390c12006-08-22 20:06:22 -04004527static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004528 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004531 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004533
4534 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004537 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4538 trace_nfs4_symlink(dir, &dentry->d_name, err);
4539 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 &exception);
4541 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004542
4543 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return err;
4545}
4546
4547static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004548 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004550 struct nfs4_createdata *data;
4551 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004553 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4554 if (data == NULL)
4555 goto out;
4556
David Quigley1775fd32013-05-22 12:50:42 -04004557 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004558 status = nfs4_do_create(dir, dentry, data);
4559
4560 nfs4_free_createdata(data);
4561out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 return status;
4563}
4564
4565static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4566 struct iattr *sattr)
4567{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004568 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004570 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004572
David Quigleyaa9c2662013-05-22 12:50:44 -04004573 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4574
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004575 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4576 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004578 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4579 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4580 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 &exception);
4582 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004583 nfs4_label_release_security(label);
4584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 return err;
4586}
4587
4588static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004589 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
David Howells2b0143b2015-03-17 22:25:59 +00004591 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 struct nfs4_readdir_arg args = {
4593 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004594 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 .pgbase = 0,
4596 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004597 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004598 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 };
4600 struct nfs4_readdir_res res;
4601 struct rpc_message msg = {
4602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4603 .rpc_argp = &args,
4604 .rpc_resp = &res,
4605 .rpc_cred = cred,
4606 };
4607 int status;
4608
Al Viro6de14722013-09-16 10:53:17 -04004609 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4610 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004611 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004612 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004614 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 -05004615 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004616 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004617 status += args.pgbase;
4618 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004619
4620 nfs_invalidate_atime(dir);
4621
Harvey Harrison3110ff82008-05-02 13:42:44 -07004622 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 return status;
4624}
4625
4626static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004627 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
4629 struct nfs4_exception exception = { };
4630 int err;
4631 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004632 err = _nfs4_proc_readdir(dentry, cred, cookie,
4633 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004634 trace_nfs4_readdir(d_inode(dentry), err);
4635 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 &exception);
4637 } while (exception.retry);
4638 return err;
4639}
4640
4641static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004642 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004644 struct nfs4_createdata *data;
4645 int mode = sattr->ia_mode;
4646 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004648 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4649 if (data == NULL)
4650 goto out;
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004653 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004655 data->arg.ftype = NF4BLK;
4656 data->arg.u.device.specdata1 = MAJOR(rdev);
4657 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
4659 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004660 data->arg.ftype = NF4CHR;
4661 data->arg.u.device.specdata1 = MAJOR(rdev);
4662 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004663 } else if (!S_ISSOCK(mode)) {
4664 status = -EINVAL;
4665 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
David Quigley1775fd32013-05-22 12:50:42 -04004667
David Quigleyaa9c2662013-05-22 12:50:44 -04004668 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004669 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004670out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004671 nfs4_free_createdata(data);
4672out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return status;
4674}
4675
4676static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4677 struct iattr *sattr, dev_t rdev)
4678{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004679 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004681 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004683
David Quigleyaa9c2662013-05-22 12:50:44 -04004684 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4685
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004686 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4687 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004689 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4690 trace_nfs4_mknod(dir, &dentry->d_name, err);
4691 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 &exception);
4693 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004694
4695 nfs4_label_release_security(label);
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 return err;
4698}
4699
4700static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4701 struct nfs_fsstat *fsstat)
4702{
4703 struct nfs4_statfs_arg args = {
4704 .fh = fhandle,
4705 .bitmask = server->attr_bitmask,
4706 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004707 struct nfs4_statfs_res res = {
4708 .fsstat = fsstat,
4709 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 struct rpc_message msg = {
4711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4712 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004713 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 };
4715
Trond Myklebust0e574af2005-10-27 22:12:38 -04004716 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004717 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
4720static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4721{
4722 struct nfs4_exception exception = { };
4723 int err;
4724 do {
4725 err = nfs4_handle_exception(server,
4726 _nfs4_proc_statfs(server, fhandle, fsstat),
4727 &exception);
4728 } while (exception.retry);
4729 return err;
4730}
4731
4732static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4733 struct nfs_fsinfo *fsinfo)
4734{
4735 struct nfs4_fsinfo_arg args = {
4736 .fh = fhandle,
4737 .bitmask = server->attr_bitmask,
4738 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004739 struct nfs4_fsinfo_res res = {
4740 .fsinfo = fsinfo,
4741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 struct rpc_message msg = {
4743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4744 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004745 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 };
4747
Bryan Schumaker7c513052011-03-24 17:12:24 +00004748 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
4751static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4752{
4753 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004754 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 int err;
4756
4757 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004758 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004759 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004760 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004761 nfs4_set_lease_period(server->nfs_client,
4762 fsinfo->lease_time * HZ,
4763 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004764 break;
4765 }
4766 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 } while (exception.retry);
4768 return err;
4769}
4770
4771static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4772{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004773 int error;
4774
Trond Myklebust0e574af2005-10-27 22:12:38 -04004775 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004776 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004777 if (error == 0) {
4778 /* block layout checks this! */
4779 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004780 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004781 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004782
4783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
4786static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4787 struct nfs_pathconf *pathconf)
4788{
4789 struct nfs4_pathconf_arg args = {
4790 .fh = fhandle,
4791 .bitmask = server->attr_bitmask,
4792 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004793 struct nfs4_pathconf_res res = {
4794 .pathconf = pathconf,
4795 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 struct rpc_message msg = {
4797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4798 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004799 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 };
4801
4802 /* None of the pathconf attributes are mandatory to implement */
4803 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4804 memset(pathconf, 0, sizeof(*pathconf));
4805 return 0;
4806 }
4807
Trond Myklebust0e574af2005-10-27 22:12:38 -04004808 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004809 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
4812static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4813 struct nfs_pathconf *pathconf)
4814{
4815 struct nfs4_exception exception = { };
4816 int err;
4817
4818 do {
4819 err = nfs4_handle_exception(server,
4820 _nfs4_proc_pathconf(server, fhandle, pathconf),
4821 &exception);
4822 } while (exception.retry);
4823 return err;
4824}
4825
Trond Myklebust5521abf2013-03-16 20:54:34 -04004826int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004827 const struct nfs_open_context *ctx,
4828 const struct nfs_lock_context *l_ctx,
4829 fmode_t fmode)
4830{
NeilBrown17393472016-10-13 15:26:47 +11004831 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004832}
4833EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4834
Trond Myklebust5521abf2013-03-16 20:54:34 -04004835static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4836 const struct nfs_open_context *ctx,
4837 const struct nfs_lock_context *l_ctx,
4838 fmode_t fmode)
4839{
4840 nfs4_stateid current_stateid;
4841
Trond Myklebuste1253be2014-03-05 08:44:23 -05004842 /* If the current stateid represents a lost lock, then exit */
4843 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4844 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004845 return nfs4_stateid_match(stateid, &current_stateid);
4846}
4847
4848static bool nfs4_error_stateid_expired(int err)
4849{
4850 switch (err) {
4851 case -NFS4ERR_DELEG_REVOKED:
4852 case -NFS4ERR_ADMIN_REVOKED:
4853 case -NFS4ERR_BAD_STATEID:
4854 case -NFS4ERR_STALE_STATEID:
4855 case -NFS4ERR_OLD_STATEID:
4856 case -NFS4ERR_OPENMODE:
4857 case -NFS4ERR_EXPIRED:
4858 return true;
4859 }
4860 return false;
4861}
4862
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004863static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004865 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004867 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004868 if (task->tk_status < 0) {
4869 struct nfs4_exception exception = {
4870 .inode = hdr->inode,
4871 .state = hdr->args.context->state,
4872 .stateid = &hdr->args.stateid,
4873 };
4874 task->tk_status = nfs4_async_handle_exception(task,
4875 server, task->tk_status, &exception);
4876 if (exception.retry) {
4877 rpc_restart_call_prepare(task);
4878 return -EAGAIN;
4879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
Trond Myklebust5521abf2013-03-16 20:54:34 -04004887static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004888 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004889{
4890
4891 if (!nfs4_error_stateid_expired(task->tk_status) ||
4892 nfs4_stateid_is_current(&args->stateid,
4893 args->context,
4894 args->lock_context,
4895 FMODE_READ))
4896 return false;
4897 rpc_restart_call_prepare(task);
4898 return true;
4899}
4900
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004901static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004902{
4903
4904 dprintk("--> %s\n", __func__);
4905
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004907 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004909 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004910 if (task->tk_status > 0)
4911 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004912 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4913 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004914}
4915
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4917 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004919 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004920 if (!hdr->pgio_done_cb)
4921 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004922 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004923 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004926static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4927 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004928{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004929 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004930 &hdr->args.seq_args,
4931 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004932 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004933 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004934 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4935 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004936 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004937 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004938 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004939 return -EIO;
4940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004943static int nfs4_write_done_cb(struct rpc_task *task,
4944 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004946 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004947
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004948 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004949 if (task->tk_status < 0) {
4950 struct nfs4_exception exception = {
4951 .inode = hdr->inode,
4952 .state = hdr->args.context->state,
4953 .stateid = &hdr->args.stateid,
4954 };
4955 task->tk_status = nfs4_async_handle_exception(task,
4956 NFS_SERVER(inode), task->tk_status,
4957 &exception);
4958 if (exception.retry) {
4959 rpc_restart_call_prepare(task);
4960 return -EAGAIN;
4961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004963 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004964 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004965 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004966 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968}
4969
Trond Myklebust5521abf2013-03-16 20:54:34 -04004970static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004971 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004972{
4973
4974 if (!nfs4_error_stateid_expired(task->tk_status) ||
4975 nfs4_stateid_is_current(&args->stateid,
4976 args->context,
4977 args->lock_context,
4978 FMODE_WRITE))
4979 return false;
4980 rpc_restart_call_prepare(task);
4981 return true;
4982}
4983
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004984static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004985{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004986 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004987 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004988 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004989 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004990 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4991 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004992}
4993
Trond Myklebust5a37f852012-04-28 14:55:16 -04004994static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004995bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004996{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004997 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004998 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004999 return false;
5000 /* Otherwise, request attributes if and only if we don't hold
5001 * a delegation
5002 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005003 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005004}
Fred Isamana69aef12011-03-03 15:13:47 +00005005
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005006static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005007 struct rpc_message *msg,
5008 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005010 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005011
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005012 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5013 hdr->args.bitmask = NULL;
5014 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005015 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005016 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005017
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005018 if (!hdr->pgio_done_cb)
5019 hdr->pgio_done_cb = nfs4_write_done_cb;
5020 hdr->res.server = server;
5021 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005023 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005024 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005025 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026}
5027
Fred Isaman0b7c0152012-04-20 14:47:39 -04005028static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5029{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005030 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005031 &data->args.seq_args,
5032 &data->res.seq_res,
5033 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034}
5035
Fred Isaman0b7c0152012-04-20 14:47:39 -04005036static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005039
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005040 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005041 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5042 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005043 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005044 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047}
5048
Fred Isaman0b7c0152012-04-20 14:47:39 -04005049static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005050{
5051 if (!nfs4_sequence_done(task, &data->res.seq_res))
5052 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005053 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005054}
5055
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005056static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5057 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005059 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005060
Fred Isaman0b7c0152012-04-20 14:47:39 -04005061 if (data->commit_done_cb == NULL)
5062 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005063 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005065 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005066 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067}
5068
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005069struct nfs4_renewdata {
5070 struct nfs_client *client;
5071 unsigned long timestamp;
5072};
5073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074/*
5075 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5076 * standalone procedure for queueing an asynchronous RENEW.
5077 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005078static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005079{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005080 struct nfs4_renewdata *data = calldata;
5081 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005082
Elena Reshetova212bf412017-10-20 12:53:38 +03005083 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005084 nfs4_schedule_state_renewal(clp);
5085 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005086 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005087}
5088
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005089static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005091 struct nfs4_renewdata *data = calldata;
5092 struct nfs_client *clp = data->client;
5093 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005095 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005096 switch (task->tk_status) {
5097 case 0:
5098 break;
5099 case -NFS4ERR_LEASE_MOVED:
5100 nfs4_schedule_lease_moved_recovery(clp);
5101 break;
5102 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005103 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005104 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5105 return;
5106 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005107 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005108 return;
5109 }
5110 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005112 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113}
5114
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005115static const struct rpc_call_ops nfs4_renew_ops = {
5116 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005117 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005118};
5119
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005120static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121{
5122 struct rpc_message msg = {
5123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5124 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005125 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005127 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005129 if (renew_flags == 0)
5130 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005131 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005132 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005133 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005134 if (data == NULL) {
5135 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005136 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005137 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005138 data->client = clp;
5139 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005140 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005141 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142}
5143
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005144static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145{
5146 struct rpc_message msg = {
5147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5148 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005149 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 };
5151 unsigned long now = jiffies;
5152 int status;
5153
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005154 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 if (status < 0)
5156 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005157 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 return 0;
5159}
5160
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005161static inline int nfs4_server_supports_acls(struct nfs_server *server)
5162{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005163 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005164}
5165
Trond Myklebust21f498c2012-08-24 10:59:25 -04005166/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5167 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005168 * the stack.
5169 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005170#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005171
Neil Hormane9e3d722011-03-04 19:26:03 -05005172static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005173 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005174{
5175 struct page *newpage, **spages;
5176 int rc = 0;
5177 size_t len;
5178 spages = pages;
5179
5180 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005181 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005182 newpage = alloc_page(GFP_KERNEL);
5183
5184 if (newpage == NULL)
5185 goto unwind;
5186 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005187 buf += len;
5188 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005189 *pages++ = newpage;
5190 rc++;
5191 } while (buflen != 0);
5192
5193 return rc;
5194
5195unwind:
5196 for(; rc > 0; rc--)
5197 __free_page(spages[rc-1]);
5198 return -ENOMEM;
5199}
5200
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005201struct nfs4_cached_acl {
5202 int cached;
5203 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005204 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005205};
5206
5207static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005208{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005209 struct nfs_inode *nfsi = NFS_I(inode);
5210
5211 spin_lock(&inode->i_lock);
5212 kfree(nfsi->nfs4_acl);
5213 nfsi->nfs4_acl = acl;
5214 spin_unlock(&inode->i_lock);
5215}
5216
5217static void nfs4_zap_acl_attr(struct inode *inode)
5218{
5219 nfs4_set_cached_acl(inode, NULL);
5220}
5221
5222static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5223{
5224 struct nfs_inode *nfsi = NFS_I(inode);
5225 struct nfs4_cached_acl *acl;
5226 int ret = -ENOENT;
5227
5228 spin_lock(&inode->i_lock);
5229 acl = nfsi->nfs4_acl;
5230 if (acl == NULL)
5231 goto out;
5232 if (buf == NULL) /* user is just asking for length */
5233 goto out_len;
5234 if (acl->cached == 0)
5235 goto out;
5236 ret = -ERANGE; /* see getxattr(2) man page */
5237 if (acl->len > buflen)
5238 goto out;
5239 memcpy(buf, acl->data, acl->len);
5240out_len:
5241 ret = acl->len;
5242out:
5243 spin_unlock(&inode->i_lock);
5244 return ret;
5245}
5246
Sachin Prabhu5794d212012-04-17 14:36:40 +01005247static 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 +00005248{
5249 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005250 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005251
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005252 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005253 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005254 if (acl == NULL)
5255 goto out;
5256 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005257 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005258 } else {
5259 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5260 if (acl == NULL)
5261 goto out;
5262 acl->cached = 0;
5263 }
5264 acl->len = acl_len;
5265out:
5266 nfs4_set_cached_acl(inode, acl);
5267}
5268
Andy Adamsonbf118a32011-12-07 11:55:27 -05005269/*
5270 * The getxattr API returns the required buffer length when called with a
5271 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5272 * the required buf. On a NULL buf, we send a page of data to the server
5273 * guessing that the ACL request can be serviced by a page. If so, we cache
5274 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5275 * the cache. If not so, we throw away the page, and cache the required
5276 * length. The next getxattr call will then produce another round trip to
5277 * the server, this time with the input buf of the required size.
5278 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005279static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005280{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005281 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005282 struct nfs_getaclargs args = {
5283 .fh = NFS_FH(inode),
5284 .acl_pages = pages,
5285 .acl_len = buflen,
5286 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005287 struct nfs_getaclres res = {
5288 .acl_len = buflen,
5289 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005290 struct rpc_message msg = {
5291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5292 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005293 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005294 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005295 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005296 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005297
Trond Myklebust21f498c2012-08-24 10:59:25 -04005298 if (npages > ARRAY_SIZE(pages))
5299 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005300
Andy Adamsonbf118a32011-12-07 11:55:27 -05005301 for (i = 0; i < npages; i++) {
5302 pages[i] = alloc_page(GFP_KERNEL);
5303 if (!pages[i])
5304 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005305 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005306
5307 /* for decoding across pages */
5308 res.acl_scratch = alloc_page(GFP_KERNEL);
5309 if (!res.acl_scratch)
5310 goto out_free;
5311
Andy Adamsonbf118a32011-12-07 11:55:27 -05005312 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005313
Peng Taode040be2012-01-10 22:42:47 +08005314 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005315 __func__, buf, buflen, npages, args.acl_len);
5316 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5317 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005318 if (ret)
5319 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005320
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005321 /* Handle the case where the passed-in buffer is too short */
5322 if (res.acl_flags & NFS4_ACL_TRUNC) {
5323 /* Did the user only issue a request for the acl length? */
5324 if (buf == NULL)
5325 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005326 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005327 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005328 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005329 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005330 if (buf) {
5331 if (res.acl_len > buflen) {
5332 ret = -ERANGE;
5333 goto out_free;
5334 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005335 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005336 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005337out_ok:
5338 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005339out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005340 for (i = 0; i < npages; i++)
5341 if (pages[i])
5342 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005343 if (res.acl_scratch)
5344 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005345 return ret;
5346}
5347
Trond Myklebust16b42892006-08-24 12:27:15 -04005348static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5349{
5350 struct nfs4_exception exception = { };
5351 ssize_t ret;
5352 do {
5353 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005354 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005355 if (ret >= 0)
5356 break;
5357 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5358 } while (exception.retry);
5359 return ret;
5360}
5361
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005362static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5363{
5364 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005365 int ret;
5366
5367 if (!nfs4_server_supports_acls(server))
5368 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005369 ret = nfs_revalidate_inode(server, inode);
5370 if (ret < 0)
5371 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005372 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5373 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005374 ret = nfs4_read_cached_acl(inode, buf, buflen);
5375 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005376 /* -ENOENT is returned if there is no ACL or if there is an ACL
5377 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005378 return ret;
5379 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005380}
5381
Trond Myklebust16b42892006-08-24 12:27:15 -04005382static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005383{
5384 struct nfs_server *server = NFS_SERVER(inode);
5385 struct page *pages[NFS4ACL_MAXPAGES];
5386 struct nfs_setaclargs arg = {
5387 .fh = NFS_FH(inode),
5388 .acl_pages = pages,
5389 .acl_len = buflen,
5390 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005391 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005392 struct rpc_message msg = {
5393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5394 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005395 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005396 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005397 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005398 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005399
5400 if (!nfs4_server_supports_acls(server))
5401 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005402 if (npages > ARRAY_SIZE(pages))
5403 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005404 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005405 if (i < 0)
5406 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005407 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005408 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005409
5410 /*
5411 * Free each page after tx, so the only ref left is
5412 * held by the network stack
5413 */
5414 for (; i > 0; i--)
5415 put_page(pages[i-1]);
5416
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005417 /*
5418 * Acl update can result in inode attribute update.
5419 * so mark the attribute cache invalid.
5420 */
5421 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005422 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005423 | NFS_INO_INVALID_CTIME
5424 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005425 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005426 nfs_access_zap_cache(inode);
5427 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005428 return ret;
5429}
5430
Trond Myklebust16b42892006-08-24 12:27:15 -04005431static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5432{
5433 struct nfs4_exception exception = { };
5434 int err;
5435 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005436 err = __nfs4_proc_set_acl(inode, buf, buflen);
5437 trace_nfs4_set_acl(inode, err);
5438 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005439 &exception);
5440 } while (exception.retry);
5441 return err;
5442}
5443
David Quigleyaa9c2662013-05-22 12:50:44 -04005444#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5445static int _nfs4_get_security_label(struct inode *inode, void *buf,
5446 size_t buflen)
5447{
5448 struct nfs_server *server = NFS_SERVER(inode);
5449 struct nfs_fattr fattr;
5450 struct nfs4_label label = {0, 0, buflen, buf};
5451
5452 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005453 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005454 .fh = NFS_FH(inode),
5455 .bitmask = bitmask,
5456 };
5457 struct nfs4_getattr_res res = {
5458 .fattr = &fattr,
5459 .label = &label,
5460 .server = server,
5461 };
5462 struct rpc_message msg = {
5463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005464 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005465 .rpc_resp = &res,
5466 };
5467 int ret;
5468
5469 nfs_fattr_init(&fattr);
5470
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005471 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005472 if (ret)
5473 return ret;
5474 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5475 return -ENOENT;
5476 if (buflen < label.len)
5477 return -ERANGE;
5478 return 0;
5479}
5480
5481static int nfs4_get_security_label(struct inode *inode, void *buf,
5482 size_t buflen)
5483{
5484 struct nfs4_exception exception = { };
5485 int err;
5486
5487 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5488 return -EOPNOTSUPP;
5489
5490 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005491 err = _nfs4_get_security_label(inode, buf, buflen);
5492 trace_nfs4_get_security_label(inode, err);
5493 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005494 &exception);
5495 } while (exception.retry);
5496 return err;
5497}
5498
5499static int _nfs4_do_set_security_label(struct inode *inode,
5500 struct nfs4_label *ilabel,
5501 struct nfs_fattr *fattr,
5502 struct nfs4_label *olabel)
5503{
5504
5505 struct iattr sattr = {0};
5506 struct nfs_server *server = NFS_SERVER(inode);
5507 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005508 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005509 .fh = NFS_FH(inode),
5510 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005511 .server = server,
5512 .bitmask = bitmask,
5513 .label = ilabel,
5514 };
5515 struct nfs_setattrres res = {
5516 .fattr = fattr,
5517 .label = olabel,
5518 .server = server,
5519 };
5520 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5522 .rpc_argp = &arg,
5523 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005524 };
5525 int status;
5526
Jeff Layton12207f62013-11-01 10:49:32 -04005527 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005528
Jeff Layton12207f62013-11-01 10:49:32 -04005529 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005530 if (status)
5531 dprintk("%s failed: %d\n", __func__, status);
5532
5533 return status;
5534}
5535
5536static int nfs4_do_set_security_label(struct inode *inode,
5537 struct nfs4_label *ilabel,
5538 struct nfs_fattr *fattr,
5539 struct nfs4_label *olabel)
5540{
5541 struct nfs4_exception exception = { };
5542 int err;
5543
5544 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005545 err = _nfs4_do_set_security_label(inode, ilabel,
5546 fattr, olabel);
5547 trace_nfs4_set_security_label(inode, err);
5548 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005549 &exception);
5550 } while (exception.retry);
5551 return err;
5552}
5553
5554static int
Al Viro59301222016-05-27 10:19:30 -04005555nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005556{
5557 struct nfs4_label ilabel, *olabel = NULL;
5558 struct nfs_fattr fattr;
5559 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005560 int status;
5561
5562 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5563 return -EOPNOTSUPP;
5564
5565 nfs_fattr_init(&fattr);
5566
5567 ilabel.pi = 0;
5568 ilabel.lfs = 0;
5569 ilabel.label = (char *)buf;
5570 ilabel.len = buflen;
5571
5572 cred = rpc_lookup_cred();
5573 if (IS_ERR(cred))
5574 return PTR_ERR(cred);
5575
5576 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5577 if (IS_ERR(olabel)) {
5578 status = -PTR_ERR(olabel);
5579 goto out;
5580 }
5581
5582 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5583 if (status == 0)
5584 nfs_setsecurity(inode, &fattr, olabel);
5585
5586 nfs4_label_free(olabel);
5587out:
5588 put_rpccred(cred);
5589 return status;
5590}
5591#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5592
5593
Chuck Leverf0920752012-05-21 22:45:41 -04005594static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5595 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005596{
5597 __be32 verf[2];
5598
Chuck Lever2c820d92012-05-21 22:45:33 -04005599 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5600 /* An impossible timestamp guarantees this value
5601 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005602 verf[0] = cpu_to_be32(U32_MAX);
5603 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005604 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005605 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005606 u64 ns = ktime_to_ns(nn->boot_time);
5607
5608 verf[0] = cpu_to_be32(ns >> 32);
5609 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005610 }
Chuck Levercd937102012-03-02 17:14:31 -05005611 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5612}
5613
Jeff Laytona3192682015-06-09 19:43:59 -04005614static int
5615nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005616{
Jeff Laytona3192682015-06-09 19:43:59 -04005617 size_t len;
5618 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005619
Trond Myklebustceb3a162015-01-03 15:16:04 -05005620 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005621 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005622
Jeff Laytona3192682015-06-09 19:43:59 -04005623 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005624 len = 14 +
5625 strlen(clp->cl_rpcclient->cl_nodename) +
5626 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005627 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005628 1;
5629 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005630 if (nfs4_client_id_uniquifier[0] != '\0')
5631 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005632 if (len > NFS4_OPAQUE_LIMIT + 1)
5633 return -EINVAL;
5634
5635 /*
5636 * Since this string is allocated at mount time, and held until the
5637 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5638 * about a memory-reclaim deadlock.
5639 */
5640 str = kmalloc(len, GFP_KERNEL);
5641 if (!str)
5642 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005643
Chuck Levere984a552012-09-14 17:24:21 -04005644 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005645 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005646 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005647 clp->cl_rpcclient->cl_nodename,
5648 nfs4_client_id_uniquifier,
5649 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005650 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005651 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005652 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005653 clp->cl_rpcclient->cl_nodename,
5654 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005655 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005656 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005657
Jeff Laytona3192682015-06-09 19:43:59 -04005658 clp->cl_owner_id = str;
5659 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005660}
5661
Jeff Layton873e3852015-06-09 19:44:00 -04005662static int
5663nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005664{
Jeff Layton873e3852015-06-09 19:44:00 -04005665 size_t len;
5666 char *str;
5667
5668 len = 10 + 10 + 1 + 10 + 1 +
5669 strlen(nfs4_client_id_uniquifier) + 1 +
5670 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5671
5672 if (len > NFS4_OPAQUE_LIMIT + 1)
5673 return -EINVAL;
5674
5675 /*
5676 * Since this string is allocated at mount time, and held until the
5677 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5678 * about a memory-reclaim deadlock.
5679 */
5680 str = kmalloc(len, GFP_KERNEL);
5681 if (!str)
5682 return -ENOMEM;
5683
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005684 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005685 clp->rpc_ops->version, clp->cl_minorversion,
5686 nfs4_client_id_uniquifier,
5687 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005688 clp->cl_owner_id = str;
5689 return 0;
5690}
5691
5692static int
5693nfs4_init_uniform_client_string(struct nfs_client *clp)
5694{
Jeff Layton873e3852015-06-09 19:44:00 -04005695 size_t len;
5696 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005697
5698 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005699 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005700
5701 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005702 return nfs4_init_uniquifier_client_string(clp);
5703
5704 len = 10 + 10 + 1 + 10 + 1 +
5705 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5706
5707 if (len > NFS4_OPAQUE_LIMIT + 1)
5708 return -EINVAL;
5709
5710 /*
5711 * Since this string is allocated at mount time, and held until the
5712 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5713 * about a memory-reclaim deadlock.
5714 */
5715 str = kmalloc(len, GFP_KERNEL);
5716 if (!str)
5717 return -ENOMEM;
5718
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005719 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005720 clp->rpc_ops->version, clp->cl_minorversion,
5721 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005722 clp->cl_owner_id = str;
5723 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005724}
5725
Chuck Lever706cb8d2014-03-12 12:51:47 -04005726/*
5727 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5728 * services. Advertise one based on the address family of the
5729 * clientaddr.
5730 */
5731static unsigned int
5732nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5733{
5734 if (strchr(clp->cl_ipaddr, ':') != NULL)
5735 return scnprintf(buf, len, "tcp6");
5736 else
5737 return scnprintf(buf, len, "tcp");
5738}
5739
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005740static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5741{
5742 struct nfs4_setclientid *sc = calldata;
5743
5744 if (task->tk_status == 0)
5745 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5746}
5747
5748static const struct rpc_call_ops nfs4_setclientid_ops = {
5749 .rpc_call_done = nfs4_setclientid_done,
5750};
5751
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005752/**
5753 * nfs4_proc_setclientid - Negotiate client ID
5754 * @clp: state data structure
5755 * @program: RPC program for NFSv4 callback service
5756 * @port: IP port number for NFS4 callback service
5757 * @cred: RPC credential to use for this call
5758 * @res: where to place the result
5759 *
5760 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5761 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005762int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5763 unsigned short port, struct rpc_cred *cred,
5764 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
5766 nfs4_verifier sc_verifier;
5767 struct nfs4_setclientid setclientid = {
5768 .sc_verifier = &sc_verifier,
5769 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005770 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 };
5772 struct rpc_message msg = {
5773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5774 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005775 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005776 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005778 struct rpc_task *task;
5779 struct rpc_task_setup task_setup_data = {
5780 .rpc_client = clp->cl_rpcclient,
5781 .rpc_message = &msg,
5782 .callback_ops = &nfs4_setclientid_ops,
5783 .callback_data = &setclientid,
5784 .flags = RPC_TASK_TIMEOUT,
5785 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005786 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
Chuck Leverde734832012-07-11 16:30:50 -04005788 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005789 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005790
5791 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5792 status = nfs4_init_uniform_client_string(clp);
5793 else
Jeff Laytona3192682015-06-09 19:43:59 -04005794 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005795
5796 if (status)
5797 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005798
Chuck Leverde734832012-07-11 16:30:50 -04005799 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005800 setclientid.sc_netid_len =
5801 nfs4_init_callback_netid(clp,
5802 setclientid.sc_netid,
5803 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005804 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005805 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 clp->cl_ipaddr, port >> 8, port & 255);
5807
Jeff Layton3a6bb732015-06-09 19:43:57 -04005808 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005809 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005810 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005811 task = rpc_run_task(&task_setup_data);
5812 if (IS_ERR(task)) {
5813 status = PTR_ERR(task);
5814 goto out;
5815 }
5816 status = task->tk_status;
5817 if (setclientid.sc_cred) {
5818 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5819 put_rpccred(setclientid.sc_cred);
5820 }
5821 rpc_put_task(task);
5822out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005823 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005824 dprintk("NFS reply setclientid: %d\n", status);
5825 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005828/**
5829 * nfs4_proc_setclientid_confirm - Confirm client ID
5830 * @clp: state data structure
5831 * @res: result of a previous SETCLIENTID
5832 * @cred: RPC credential to use for this call
5833 *
5834 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5835 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005836int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005837 struct nfs4_setclientid_res *arg,
5838 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 struct rpc_message msg = {
5841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005842 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005843 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 int status;
5846
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005847 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5848 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5849 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005850 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005851 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005852 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 return status;
5854}
5855
Trond Myklebustfe650402006-01-03 09:55:18 +01005856struct nfs4_delegreturndata {
5857 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005858 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005859 struct nfs_fh fh;
5860 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005861 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005862 struct {
5863 struct nfs4_layoutreturn_args arg;
5864 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005865 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005866 u32 roc_barrier;
5867 bool roc;
5868 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005869 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005870 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005871 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005872};
5873
Trond Myklebustfe650402006-01-03 09:55:18 +01005874static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5875{
5876 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005877 struct nfs4_exception exception = {
5878 .inode = data->inode,
5879 .stateid = &data->stateid,
5880 };
Andy Adamson938e1012009-04-01 09:22:28 -04005881
Trond Myklebust14516c32010-07-31 14:29:06 -04005882 if (!nfs4_sequence_done(task, &data->res.seq_res))
5883 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005884
Trond Myklebustca8acf82013-08-13 10:36:56 -04005885 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005886
5887 /* Handle Layoutreturn errors */
5888 if (data->args.lr_args && task->tk_status != 0) {
5889 switch(data->res.lr_ret) {
5890 default:
5891 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5892 break;
5893 case 0:
5894 data->args.lr_args = NULL;
5895 data->res.lr_res = NULL;
5896 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005897 case -NFS4ERR_OLD_STATEID:
5898 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005899 data->inode))
5900 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005901 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005902 case -NFS4ERR_ADMIN_REVOKED:
5903 case -NFS4ERR_DELEG_REVOKED:
5904 case -NFS4ERR_EXPIRED:
5905 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005906 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5907 case -NFS4ERR_WRONG_CRED:
5908 data->args.lr_args = NULL;
5909 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005910 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005911 }
5912 }
5913
Ricardo Labiaga79708862009-12-07 09:23:21 -05005914 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005915 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005916 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005917 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005918 case -NFS4ERR_ADMIN_REVOKED:
5919 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005920 case -NFS4ERR_EXPIRED:
5921 nfs4_free_revoked_stateid(data->res.server,
5922 data->args.stateid,
5923 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005924 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005925 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005926 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005927 task->tk_status = 0;
5928 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005929 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005930 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5931 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005932 task->tk_status = 0;
5933 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005934 case -NFS4ERR_ACCESS:
5935 if (data->args.bitmask) {
5936 data->args.bitmask = NULL;
5937 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005938 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005939 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005940 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005941 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005942 task->tk_status = nfs4_async_handle_exception(task,
5943 data->res.server, task->tk_status,
5944 &exception);
5945 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005946 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005947 }
5948 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005949 return;
5950lr_restart:
5951 data->res.lr_ret = 0;
5952out_restart:
5953 task->tk_status = 0;
5954 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005955}
5956
5957static void nfs4_delegreturn_release(void *calldata)
5958{
Peng Tao039b7562014-07-03 13:05:02 +08005959 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005960 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005961
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005962 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005963 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005964 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5965 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005966 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005967 nfs_iput_and_deactive(inode);
5968 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005969 kfree(calldata);
5970}
5971
Andy Adamson938e1012009-04-01 09:22:28 -04005972static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5973{
5974 struct nfs4_delegreturndata *d_data;
5975
5976 d_data = (struct nfs4_delegreturndata *)data;
5977
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005978 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005979 return;
5980
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005981 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005982 &d_data->args.seq_args,
5983 &d_data->res.seq_res,
5984 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005985}
Andy Adamson938e1012009-04-01 09:22:28 -04005986
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005987static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005988 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005989 .rpc_call_done = nfs4_delegreturn_done,
5990 .rpc_release = nfs4_delegreturn_release,
5991};
5992
Trond Myklebuste6f81072008-01-24 18:14:34 -05005993static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005994{
5995 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005996 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005997 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005998 struct rpc_message msg = {
5999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6000 .rpc_cred = cred,
6001 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006002 struct rpc_task_setup task_setup_data = {
6003 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006004 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006005 .callback_ops = &nfs4_delegreturn_ops,
6006 .flags = RPC_TASK_ASYNC,
6007 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006008 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006009
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006010 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006011 if (data == NULL)
6012 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006013 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006014
6015 nfs4_state_protect(server->nfs_client,
6016 NFS_SP4_MACH_CRED_CLEANUP,
6017 &task_setup_data.rpc_client, &msg);
6018
Trond Myklebustfe650402006-01-03 09:55:18 +01006019 data->args.fhandle = &data->fh;
6020 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006021 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006022 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006023 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006024 data->res.fattr = &data->fattr;
6025 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006026 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006027 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006028 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006029 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006030 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006031 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006032 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006033 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006034 if (data->lr.roc) {
6035 data->args.lr_args = &data->lr.arg;
6036 data->res.lr_res = &data->lr.res;
6037 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006038 } else if (data->lr.roc) {
6039 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6040 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006041 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006042
Trond Myklebustc970aa82007-07-14 15:39:59 -04006043 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006044 msg.rpc_argp = &data->args;
6045 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006046 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006047 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006048 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006049 if (!issync)
6050 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006051 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006052 if (status != 0)
6053 goto out;
6054 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006055out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006056 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006057 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058}
6059
Trond Myklebuste6f81072008-01-24 18:14:34 -05006060int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061{
6062 struct nfs_server *server = NFS_SERVER(inode);
6063 struct nfs4_exception exception = { };
6064 int err;
6065 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006066 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006067 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 switch (err) {
6069 case -NFS4ERR_STALE_STATEID:
6070 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 case 0:
6072 return 0;
6073 }
6074 err = nfs4_handle_exception(server, err, &exception);
6075 } while (exception.retry);
6076 return err;
6077}
6078
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6080{
6081 struct inode *inode = state->inode;
6082 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006083 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006084 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006086 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006088 struct nfs_lockt_res res = {
6089 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 };
6091 struct rpc_message msg = {
6092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006093 .rpc_argp = &arg,
6094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 .rpc_cred = state->owner->so_cred,
6096 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 struct nfs4_lock_state *lsp;
6098 int status;
6099
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006101 status = nfs4_set_lock_state(state, request);
6102 if (status != 0)
6103 goto out;
6104 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006105 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006106 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006107 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006108 switch (status) {
6109 case 0:
6110 request->fl_type = F_UNLCK;
6111 break;
6112 case -NFS4ERR_DENIED:
6113 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006115 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006116 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 return status;
6119}
6120
6121static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6122{
6123 struct nfs4_exception exception = { };
6124 int err;
6125
6126 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006127 err = _nfs4_proc_getlk(state, cmd, request);
6128 trace_nfs4_get_lock(request, state, cmd, err);
6129 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 &exception);
6131 } while (exception.retry);
6132 return err;
6133}
6134
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006135struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006136 struct nfs_locku_args arg;
6137 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006138 struct nfs4_lock_state *lsp;
6139 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006140 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006141 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006142 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006143 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006144};
6145
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006146static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6147 struct nfs_open_context *ctx,
6148 struct nfs4_lock_state *lsp,
6149 struct nfs_seqid *seqid)
6150{
6151 struct nfs4_unlockdata *p;
6152 struct inode *inode = lsp->ls_state->inode;
6153
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006154 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006155 if (p == NULL)
6156 return NULL;
6157 p->arg.fh = NFS_FH(inode);
6158 p->arg.fl = &p->fl;
6159 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006160 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006161 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006162 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006163 /* Ensure we don't close file until we're done freeing locks! */
6164 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006165 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006166 memcpy(&p->fl, fl, sizeof(p->fl));
6167 p->server = NFS_SERVER(inode);
6168 return p;
6169}
6170
Trond Myklebust06f814a2006-01-03 09:55:07 +01006171static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006172{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006173 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006174 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006175 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006176 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006177 put_nfs_open_context(calldata->ctx);
6178 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006179}
6180
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006181static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006182{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006183 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006184 struct nfs4_exception exception = {
6185 .inode = calldata->lsp->ls_state->inode,
6186 .stateid = &calldata->arg.stateid,
6187 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006188
Trond Myklebust14516c32010-07-31 14:29:06 -04006189 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6190 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006191 switch (task->tk_status) {
6192 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006193 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006194 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006195 if (nfs4_update_lock_stateid(calldata->lsp,
6196 &calldata->res.stateid))
6197 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006198 case -NFS4ERR_ADMIN_REVOKED:
6199 case -NFS4ERR_EXPIRED:
6200 nfs4_free_revoked_stateid(calldata->server,
6201 &calldata->arg.stateid,
6202 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006203 case -NFS4ERR_BAD_STATEID:
6204 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006205 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006206 if (!nfs4_stateid_match(&calldata->arg.stateid,
6207 &calldata->lsp->ls_stateid))
6208 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006209 break;
6210 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006211 task->tk_status = nfs4_async_handle_exception(task,
6212 calldata->server, task->tk_status,
6213 &exception);
6214 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006215 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006216 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006217 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006218}
6219
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006220static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006221{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006222 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006224 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6225 nfs_async_iocounter_wait(task, calldata->l_ctx))
6226 return;
6227
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006228 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006229 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006230 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006231 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006232 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006233 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006234 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006235 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006236 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006237 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006238 &calldata->res.seq_res,
6239 task) != 0)
6240 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006241 return;
6242out_no_action:
6243 task->tk_action = NULL;
6244out_wait:
6245 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246}
6247
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006248static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006249 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006250 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006251 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006252};
6253
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6255 struct nfs_open_context *ctx,
6256 struct nfs4_lock_state *lsp,
6257 struct nfs_seqid *seqid)
6258{
6259 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006260 struct rpc_message msg = {
6261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6262 .rpc_cred = ctx->cred,
6263 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006264 struct rpc_task_setup task_setup_data = {
6265 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006266 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006267 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006268 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006269 .flags = RPC_TASK_ASYNC,
6270 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006271
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006272 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6273 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6274
Frank Filz137d6ac2007-07-09 15:32:29 -07006275 /* Ensure this is an unlock - when canceling a lock, the
6276 * canceled lock is passed in, and it won't be an unlock.
6277 */
6278 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006279 if (fl->fl_flags & FL_CLOSE)
6280 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006281
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6283 if (data == NULL) {
6284 nfs_free_seqid(seqid);
6285 return ERR_PTR(-ENOMEM);
6286 }
6287
Anna Schumakerfba83f32018-05-04 16:22:50 -04006288 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006289 msg.rpc_argp = &data->arg;
6290 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006291 task_setup_data.callback_data = data;
6292 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293}
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6296{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006297 struct inode *inode = state->inode;
6298 struct nfs4_state_owner *sp = state->owner;
6299 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006300 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006301 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006302 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006303 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006304 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006305 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
Trond Myklebust9b073572006-06-29 16:38:34 -04006307 status = nfs4_set_lock_state(state, request);
6308 /* Unlock _before_ we do the RPC call */
6309 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006310 /* Exclude nfs_delegation_claim_locks() */
6311 mutex_lock(&sp->so_delegreturn_mutex);
6312 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006313 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006314 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006315 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006316 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006317 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006318 }
6319 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006320 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006321 if (status != 0)
6322 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006323 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006324 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006325 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6326 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006327 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6328 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006329 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006330 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006331 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006332 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006333 status = PTR_ERR(task);
6334 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006335 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006336 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006337 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006338out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006339 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006340 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006341 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342}
6343
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006344struct nfs4_lockdata {
6345 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006346 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006347 struct nfs4_lock_state *lsp;
6348 struct nfs_open_context *ctx;
6349 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006350 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351 int rpc_status;
6352 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006353 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006354};
6355
6356static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006357 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6358 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006359{
6360 struct nfs4_lockdata *p;
6361 struct inode *inode = lsp->ls_state->inode;
6362 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006363 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006364
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006365 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006366 if (p == NULL)
6367 return NULL;
6368
6369 p->arg.fh = NFS_FH(inode);
6370 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006371 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006372 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006373 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006374 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6375 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006376 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006377 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006378 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006379 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006380 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006381 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006382 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006383 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006384 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006385 p->ctx = get_nfs_open_context(ctx);
6386 memcpy(&p->fl, fl, sizeof(p->fl));
6387 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006388out_free_seqid:
6389 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006390out_free:
6391 kfree(p);
6392 return NULL;
6393}
6394
6395static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6396{
6397 struct nfs4_lockdata *data = calldata;
6398 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Harvey Harrison3110ff82008-05-02 13:42:44 -07006400 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006401 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006402 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006403 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006404 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006405 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006406 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006407 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006408 nfs4_stateid_copy(&data->arg.open_stateid,
6409 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006410 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006411 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006412 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006413 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006414 nfs4_stateid_copy(&data->arg.lock_stateid,
6415 &data->lsp->ls_stateid);
6416 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006417 if (!nfs4_valid_open_stateid(state)) {
6418 data->rpc_status = -EBADF;
6419 task->tk_action = NULL;
6420 goto out_release_open_seqid;
6421 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006422 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006423 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006424 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006425 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006426 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006427 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006428out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006429 nfs_release_seqid(data->arg.open_seqid);
6430out_release_lock_seqid:
6431 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006432out_wait:
6433 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006434 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006435}
6436
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006437static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6438{
6439 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006440 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006441
Harvey Harrison3110ff82008-05-02 13:42:44 -07006442 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006443
Trond Myklebust14516c32010-07-31 14:29:06 -04006444 if (!nfs4_sequence_done(task, &data->res.seq_res))
6445 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006446
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006447 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006448 switch (task->tk_status) {
6449 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006450 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006451 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006452 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006453 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006454 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebustc69899a2015-01-24 16:03:52 -05006455 break;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006456 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006457
Trond Myklebust39071e62015-01-24 15:07:56 -05006458 if (data->arg.new_lock_owner != 0) {
6459 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6460 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6461 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006462 goto out_done;
6463 } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
6464 goto out_done;
6465
Trond Myklebust425c1d42015-01-24 14:57:53 -05006466 break;
6467 case -NFS4ERR_BAD_STATEID:
6468 case -NFS4ERR_OLD_STATEID:
6469 case -NFS4ERR_STALE_STATEID:
6470 case -NFS4ERR_EXPIRED:
6471 if (data->arg.new_lock_owner != 0) {
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006472 if (nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006473 &lsp->ls_state->open_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006474 goto out_done;
6475 } else if (nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006476 &lsp->ls_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006477 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006478 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006479 if (!data->cancelled)
6480 rpc_restart_call_prepare(task);
6481out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006482 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006483}
6484
6485static void nfs4_lock_release(void *calldata)
6486{
6487 struct nfs4_lockdata *data = calldata;
6488
Harvey Harrison3110ff82008-05-02 13:42:44 -07006489 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006490 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006491 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006492 struct rpc_task *task;
6493 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6494 data->arg.lock_seqid);
6495 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006496 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006497 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006498 } else
6499 nfs_free_seqid(data->arg.lock_seqid);
6500 nfs4_put_lock_state(data->lsp);
6501 put_nfs_open_context(data->ctx);
6502 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006503 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006504}
6505
6506static const struct rpc_call_ops nfs4_lock_ops = {
6507 .rpc_call_prepare = nfs4_lock_prepare,
6508 .rpc_call_done = nfs4_lock_done,
6509 .rpc_release = nfs4_lock_release,
6510};
6511
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006512static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6513{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006514 switch (error) {
6515 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006516 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006517 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006518 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006519 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006520 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006521 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006522 break;
6523 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006524 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006525 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006526 };
6527}
6528
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006529static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006530{
6531 struct nfs4_lockdata *data;
6532 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006533 struct rpc_message msg = {
6534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6535 .rpc_cred = state->owner->so_cred,
6536 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006537 struct rpc_task_setup task_setup_data = {
6538 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006539 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006540 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006541 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006542 .flags = RPC_TASK_ASYNC,
6543 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006544 int ret;
6545
Harvey Harrison3110ff82008-05-02 13:42:44 -07006546 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006547 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006548 fl->fl_u.nfs4_fl.owner,
6549 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006550 if (data == NULL)
6551 return -ENOMEM;
6552 if (IS_SETLKW(cmd))
6553 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006554 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6555 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006556 msg.rpc_argp = &data->arg;
6557 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006558 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006559 if (recovery_type > NFS_LOCK_NEW) {
6560 if (recovery_type == NFS_LOCK_RECLAIM)
6561 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006562 } else
6563 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006564 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006565 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006566 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006567 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006568 if (ret == 0) {
6569 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006570 if (ret)
6571 nfs4_handle_setlk_error(data->server, data->lsp,
6572 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006573 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006574 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006575 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006576 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006577 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006578 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579}
6580
6581static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6582{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006583 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006584 struct nfs4_exception exception = {
6585 .inode = state->inode,
6586 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006587 int err;
6588
6589 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006590 /* Cache the lock if possible... */
6591 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6592 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006593 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006594 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006595 break;
6596 nfs4_handle_exception(server, err, &exception);
6597 } while (exception.retry);
6598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599}
6600
6601static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6602{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006603 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006604 struct nfs4_exception exception = {
6605 .inode = state->inode,
6606 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006607 int err;
6608
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006609 err = nfs4_set_lock_state(state, request);
6610 if (err != 0)
6611 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006612 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006613 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6614 return 0;
6615 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006616 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006617 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6618 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006619 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006620 switch (err) {
6621 default:
6622 goto out;
6623 case -NFS4ERR_GRACE:
6624 case -NFS4ERR_DELAY:
6625 nfs4_handle_exception(server, err, &exception);
6626 err = 0;
6627 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006628 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006629out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631}
6632
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006633#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006634static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6635{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006636 struct nfs4_lock_state *lsp;
6637 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006638
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006639 status = nfs4_set_lock_state(state, request);
6640 if (status != 0)
6641 return status;
6642 lsp = request->fl_u.nfs4_fl.owner;
6643 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6644 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6645 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006646 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006647}
6648#endif
6649
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6651{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006652 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006653 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006654 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006655 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
Trond Myklebust01c3b862006-06-29 16:38:39 -04006657 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006658 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006659 if (status < 0)
6660 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006661 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006662 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006663 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006664 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006665 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006666 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006667 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006668 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006669 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006670 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006671 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006672 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006673 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006674 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006675out:
6676 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 return status;
6678}
6679
6680static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6681{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006682 struct nfs4_exception exception = {
6683 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006684 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006685 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 int err;
6687
6688 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006689 err = _nfs4_proc_setlk(state, cmd, request);
6690 if (err == -NFS4ERR_DENIED)
6691 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006693 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 } while (exception.retry);
6695 return err;
6696}
6697
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006698#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6699#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6700
6701static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006702nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6703 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006704{
6705 int status = -ERESTARTSYS;
6706 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6707
6708 while(!signalled()) {
6709 status = nfs4_proc_setlk(state, cmd, request);
6710 if ((status != -EAGAIN) || IS_SETLK(cmd))
6711 break;
6712 freezable_schedule_timeout_interruptible(timeout);
6713 timeout *= 2;
6714 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6715 status = -ERESTARTSYS;
6716 }
6717 return status;
6718}
6719
Jeff Laytona1d617d82016-09-17 18:17:39 -04006720#ifdef CONFIG_NFS_V4_1
6721struct nfs4_lock_waiter {
6722 struct task_struct *task;
6723 struct inode *inode;
6724 struct nfs_lowner *owner;
6725 bool notified;
6726};
6727
6728static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006729nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006730{
6731 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006732 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006733
Jeff Layton57174592018-03-18 08:37:03 -04006734 /* NULL key means to wake up everyone */
6735 if (key) {
6736 struct cb_notify_lock_args *cbnl = key;
6737 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6738 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006739
Jeff Layton57174592018-03-18 08:37:03 -04006740 /* Only wake if the callback was for the same owner. */
6741 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6742 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006743
Jeff Layton57174592018-03-18 08:37:03 -04006744 /* Make sure it's for the right inode */
6745 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6746 return 0;
6747
6748 waiter->notified = true;
6749 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006750
6751 /* override "private" so we can use default_wake_function */
6752 wait->private = waiter->task;
6753 ret = autoremove_wake_function(wait, mode, flags, key);
6754 wait->private = waiter;
6755 return ret;
6756}
6757
6758static int
6759nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6760{
6761 int status = -ERESTARTSYS;
6762 unsigned long flags;
6763 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6764 struct nfs_server *server = NFS_SERVER(state->inode);
6765 struct nfs_client *clp = server->nfs_client;
6766 wait_queue_head_t *q = &clp->cl_lock_waitq;
6767 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6768 .id = lsp->ls_seqid.owner_id,
6769 .s_dev = server->s_dev };
6770 struct nfs4_lock_waiter waiter = { .task = current,
6771 .inode = state->inode,
6772 .owner = &owner,
6773 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006774 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006775
6776 /* Don't bother with waitqueue if we don't expect a callback */
6777 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6778 return nfs4_retry_setlk_simple(state, cmd, request);
6779
6780 init_wait(&wait);
6781 wait.private = &waiter;
6782 wait.func = nfs4_wake_lock_waiter;
6783 add_wait_queue(q, &wait);
6784
6785 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006786 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006787 status = nfs4_proc_setlk(state, cmd, request);
6788 if ((status != -EAGAIN) || IS_SETLK(cmd))
6789 break;
6790
6791 status = -ERESTARTSYS;
6792 spin_lock_irqsave(&q->lock, flags);
6793 if (waiter.notified) {
6794 spin_unlock_irqrestore(&q->lock, flags);
6795 continue;
6796 }
6797 set_current_state(TASK_INTERRUPTIBLE);
6798 spin_unlock_irqrestore(&q->lock, flags);
6799
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006800 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006801 }
6802
6803 finish_wait(q, &wait);
6804 return status;
6805}
6806#else /* !CONFIG_NFS_V4_1 */
6807static inline int
6808nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6809{
6810 return nfs4_retry_setlk_simple(state, cmd, request);
6811}
6812#endif
6813
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814static int
6815nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6816{
6817 struct nfs_open_context *ctx;
6818 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 int status;
6820
6821 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006822 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 state = ctx->state;
6824
Trond Myklebustd9531262009-07-21 19:22:38 -04006825 if (IS_GETLK(cmd)) {
6826 if (state != NULL)
6827 return nfs4_proc_getlk(state, F_GETLK, request);
6828 return 0;
6829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
6831 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6832 return -EINVAL;
6833
Trond Myklebustd9531262009-07-21 19:22:38 -04006834 if (request->fl_type == F_UNLCK) {
6835 if (state != NULL)
6836 return nfs4_proc_unlck(state, cmd, request);
6837 return 0;
6838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
Trond Myklebustd9531262009-07-21 19:22:38 -04006840 if (state == NULL)
6841 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006842
6843 if ((request->fl_flags & FL_POSIX) &&
6844 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6845 return -ENOLCK;
6846
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006847 /*
6848 * Don't rely on the VFS having checked the file open mode,
6849 * since it won't do this for flock() locks.
6850 */
6851 switch (request->fl_type) {
6852 case F_RDLCK:
6853 if (!(filp->f_mode & FMODE_READ))
6854 return -EBADF;
6855 break;
6856 case F_WRLCK:
6857 if (!(filp->f_mode & FMODE_WRITE))
6858 return -EBADF;
6859 }
6860
Jeff Layton1ea67db2016-09-17 18:17:37 -04006861 status = nfs4_set_lock_state(state, request);
6862 if (status != 0)
6863 return status;
6864
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006865 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866}
6867
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006868int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006869{
6870 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006871 int err;
6872
6873 err = nfs4_set_lock_state(state, fl);
6874 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006875 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006876 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006877 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006878}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006879
Trond Myklebustcf470c32012-03-07 13:49:12 -05006880struct nfs_release_lockowner_data {
6881 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006882 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006883 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006884 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006885 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006886};
6887
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006888static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6889{
6890 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006891 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006892 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6893 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006894 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006895 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006896}
6897
6898static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6899{
6900 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006901 struct nfs_server *server = data->server;
6902
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006903 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006904
6905 switch (task->tk_status) {
6906 case 0:
6907 renew_lease(server, data->timestamp);
6908 break;
6909 case -NFS4ERR_STALE_CLIENTID:
6910 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006911 nfs4_schedule_lease_recovery(server->nfs_client);
6912 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006913 case -NFS4ERR_LEASE_MOVED:
6914 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006915 if (nfs4_async_handle_error(task, server,
6916 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006917 rpc_restart_call_prepare(task);
6918 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006919}
6920
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006921static void nfs4_release_lockowner_release(void *calldata)
6922{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006923 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006924 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006925 kfree(calldata);
6926}
6927
Trond Myklebust17280172012-03-11 13:11:00 -04006928static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006929 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6930 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006931 .rpc_release = nfs4_release_lockowner_release,
6932};
6933
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006934static void
6935nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006936{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006937 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6940 };
6941
6942 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006943 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006944
Trond Myklebustcf470c32012-03-07 13:49:12 -05006945 data = kmalloc(sizeof(*data), GFP_NOFS);
6946 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006947 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006948 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006949 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006950 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6951 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6952 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006953
Trond Myklebustcf470c32012-03-07 13:49:12 -05006954 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006955 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006956 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006957 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006958}
6959
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006960#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6961
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006962static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006963 struct dentry *unused, struct inode *inode,
6964 const char *key, const void *buf,
6965 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006966{
Al Viro59301222016-05-27 10:19:30 -04006967 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006968}
6969
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006970static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006971 struct dentry *unused, struct inode *inode,
6972 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006973{
Al Virob2968212016-04-10 20:48:24 -04006974 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006975}
6976
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006977static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006978{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006979 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006980}
6981
David Quigleyc9bccef2013-05-22 12:50:45 -04006982#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006983
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006984static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006985 struct dentry *unused, struct inode *inode,
6986 const char *key, const void *buf,
6987 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006988{
6989 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006990 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006991
6992 return -EOPNOTSUPP;
6993}
6994
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006995static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006996 struct dentry *unused, struct inode *inode,
6997 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006998{
6999 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007000 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007001 return -EOPNOTSUPP;
7002}
7003
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007004static ssize_t
7005nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007006{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007007 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007008
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007009 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7010 len = security_inode_listsecurity(inode, list, list_len);
7011 if (list_len && len > list_len)
7012 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007013 }
7014 return len;
7015}
7016
7017static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7018 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007019 .get = nfs4_xattr_get_nfs4_label,
7020 .set = nfs4_xattr_set_nfs4_label,
7021};
David Quigleyc9bccef2013-05-22 12:50:45 -04007022
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007023#else
7024
7025static ssize_t
7026nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7027{
7028 return 0;
7029}
7030
7031#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007032
Andy Adamson533eb462011-06-13 18:25:56 -04007033/*
7034 * nfs_fhget will use either the mounted_on_fileid or the fileid
7035 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007036static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7037{
Andy Adamson533eb462011-06-13 18:25:56 -04007038 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7039 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7040 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007041 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007042 return;
7043
7044 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007045 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007046 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7047 fattr->nlink = 2;
7048}
7049
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007050static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7051 const struct qstr *name,
7052 struct nfs4_fs_locations *fs_locations,
7053 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007054{
7055 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007056 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007057 struct nfs4_fs_locations_arg args = {
7058 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007059 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007060 .page = page,
7061 .bitmask = bitmask,
7062 };
Benny Halevy22958462009-04-01 09:22:02 -04007063 struct nfs4_fs_locations_res res = {
7064 .fs_locations = fs_locations,
7065 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7068 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007069 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007070 };
7071 int status;
7072
Harvey Harrison3110ff82008-05-02 13:42:44 -07007073 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007074
Chuck Leverc05cefc2017-11-05 15:45:22 -05007075 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7076 bitmask[1] = nfs4_fattr_bitmap[1];
7077
Andy Adamson533eb462011-06-13 18:25:56 -04007078 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7079 * is not supported */
7080 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007081 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007082 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007083 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007084
Trond Myklebustc228fd32007-01-13 02:28:11 -05007085 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007086 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007087 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007088 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007089 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007090 return status;
7091}
7092
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007093int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7094 const struct qstr *name,
7095 struct nfs4_fs_locations *fs_locations,
7096 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007097{
7098 struct nfs4_exception exception = { };
7099 int err;
7100 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007101 err = _nfs4_proc_fs_locations(client, dir, name,
7102 fs_locations, page);
7103 trace_nfs4_get_fs_locations(dir, name, err);
7104 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007105 &exception);
7106 } while (exception.retry);
7107 return err;
7108}
7109
Chuck Leverb03d7352013-10-17 14:12:50 -04007110/*
7111 * This operation also signals the server that this client is
7112 * performing migration recovery. The server can stop returning
7113 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7114 * appended to this compound to identify the client ID which is
7115 * performing recovery.
7116 */
7117static int _nfs40_proc_get_locations(struct inode *inode,
7118 struct nfs4_fs_locations *locations,
7119 struct page *page, struct rpc_cred *cred)
7120{
7121 struct nfs_server *server = NFS_SERVER(inode);
7122 struct rpc_clnt *clnt = server->client;
7123 u32 bitmask[2] = {
7124 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7125 };
7126 struct nfs4_fs_locations_arg args = {
7127 .clientid = server->nfs_client->cl_clientid,
7128 .fh = NFS_FH(inode),
7129 .page = page,
7130 .bitmask = bitmask,
7131 .migration = 1, /* skip LOOKUP */
7132 .renew = 1, /* append RENEW */
7133 };
7134 struct nfs4_fs_locations_res res = {
7135 .fs_locations = locations,
7136 .migration = 1,
7137 .renew = 1,
7138 };
7139 struct rpc_message msg = {
7140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7141 .rpc_argp = &args,
7142 .rpc_resp = &res,
7143 .rpc_cred = cred,
7144 };
7145 unsigned long now = jiffies;
7146 int status;
7147
7148 nfs_fattr_init(&locations->fattr);
7149 locations->server = server;
7150 locations->nlocations = 0;
7151
Anna Schumakerfba83f32018-05-04 16:22:50 -04007152 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007153 status = nfs4_call_sync_sequence(clnt, server, &msg,
7154 &args.seq_args, &res.seq_res);
7155 if (status)
7156 return status;
7157
7158 renew_lease(server, now);
7159 return 0;
7160}
7161
7162#ifdef CONFIG_NFS_V4_1
7163
7164/*
7165 * This operation also signals the server that this client is
7166 * performing migration recovery. The server can stop asserting
7167 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7168 * performing this operation is identified in the SEQUENCE
7169 * operation in this compound.
7170 *
7171 * When the client supports GETATTR(fs_locations_info), it can
7172 * be plumbed in here.
7173 */
7174static int _nfs41_proc_get_locations(struct inode *inode,
7175 struct nfs4_fs_locations *locations,
7176 struct page *page, struct rpc_cred *cred)
7177{
7178 struct nfs_server *server = NFS_SERVER(inode);
7179 struct rpc_clnt *clnt = server->client;
7180 u32 bitmask[2] = {
7181 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7182 };
7183 struct nfs4_fs_locations_arg args = {
7184 .fh = NFS_FH(inode),
7185 .page = page,
7186 .bitmask = bitmask,
7187 .migration = 1, /* skip LOOKUP */
7188 };
7189 struct nfs4_fs_locations_res res = {
7190 .fs_locations = locations,
7191 .migration = 1,
7192 };
7193 struct rpc_message msg = {
7194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7195 .rpc_argp = &args,
7196 .rpc_resp = &res,
7197 .rpc_cred = cred,
7198 };
7199 int status;
7200
7201 nfs_fattr_init(&locations->fattr);
7202 locations->server = server;
7203 locations->nlocations = 0;
7204
Anna Schumakerfba83f32018-05-04 16:22:50 -04007205 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007206 status = nfs4_call_sync_sequence(clnt, server, &msg,
7207 &args.seq_args, &res.seq_res);
7208 if (status == NFS4_OK &&
7209 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7210 status = -NFS4ERR_LEASE_MOVED;
7211 return status;
7212}
7213
7214#endif /* CONFIG_NFS_V4_1 */
7215
7216/**
7217 * nfs4_proc_get_locations - discover locations for a migrated FSID
7218 * @inode: inode on FSID that is migrating
7219 * @locations: result of query
7220 * @page: buffer
7221 * @cred: credential to use for this operation
7222 *
7223 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7224 * operation failed, or a negative errno if a local error occurred.
7225 *
7226 * On success, "locations" is filled in, but if the server has
7227 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7228 * asserted.
7229 *
7230 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7231 * from this client that require migration recovery.
7232 */
7233int nfs4_proc_get_locations(struct inode *inode,
7234 struct nfs4_fs_locations *locations,
7235 struct page *page, struct rpc_cred *cred)
7236{
7237 struct nfs_server *server = NFS_SERVER(inode);
7238 struct nfs_client *clp = server->nfs_client;
7239 const struct nfs4_mig_recovery_ops *ops =
7240 clp->cl_mvops->mig_recovery_ops;
7241 struct nfs4_exception exception = { };
7242 int status;
7243
7244 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7245 (unsigned long long)server->fsid.major,
7246 (unsigned long long)server->fsid.minor,
7247 clp->cl_hostname);
7248 nfs_display_fhandle(NFS_FH(inode), __func__);
7249
7250 do {
7251 status = ops->get_locations(inode, locations, page, cred);
7252 if (status != -NFS4ERR_DELAY)
7253 break;
7254 nfs4_handle_exception(server, status, &exception);
7255 } while (exception.retry);
7256 return status;
7257}
7258
Chuck Lever44c99932013-10-17 14:13:30 -04007259/*
7260 * This operation also signals the server that this client is
7261 * performing "lease moved" recovery. The server can stop
7262 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7263 * is appended to this compound to identify the client ID which is
7264 * performing recovery.
7265 */
7266static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7267{
7268 struct nfs_server *server = NFS_SERVER(inode);
7269 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7270 struct rpc_clnt *clnt = server->client;
7271 struct nfs4_fsid_present_arg args = {
7272 .fh = NFS_FH(inode),
7273 .clientid = clp->cl_clientid,
7274 .renew = 1, /* append RENEW */
7275 };
7276 struct nfs4_fsid_present_res res = {
7277 .renew = 1,
7278 };
7279 struct rpc_message msg = {
7280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7281 .rpc_argp = &args,
7282 .rpc_resp = &res,
7283 .rpc_cred = cred,
7284 };
7285 unsigned long now = jiffies;
7286 int status;
7287
7288 res.fh = nfs_alloc_fhandle();
7289 if (res.fh == NULL)
7290 return -ENOMEM;
7291
Anna Schumakerfba83f32018-05-04 16:22:50 -04007292 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007293 status = nfs4_call_sync_sequence(clnt, server, &msg,
7294 &args.seq_args, &res.seq_res);
7295 nfs_free_fhandle(res.fh);
7296 if (status)
7297 return status;
7298
7299 do_renew_lease(clp, now);
7300 return 0;
7301}
7302
7303#ifdef CONFIG_NFS_V4_1
7304
7305/*
7306 * This operation also signals the server that this client is
7307 * performing "lease moved" recovery. The server can stop asserting
7308 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7309 * this operation is identified in the SEQUENCE operation in this
7310 * compound.
7311 */
7312static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7313{
7314 struct nfs_server *server = NFS_SERVER(inode);
7315 struct rpc_clnt *clnt = server->client;
7316 struct nfs4_fsid_present_arg args = {
7317 .fh = NFS_FH(inode),
7318 };
7319 struct nfs4_fsid_present_res res = {
7320 };
7321 struct rpc_message msg = {
7322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7323 .rpc_argp = &args,
7324 .rpc_resp = &res,
7325 .rpc_cred = cred,
7326 };
7327 int status;
7328
7329 res.fh = nfs_alloc_fhandle();
7330 if (res.fh == NULL)
7331 return -ENOMEM;
7332
Anna Schumakerfba83f32018-05-04 16:22:50 -04007333 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007334 status = nfs4_call_sync_sequence(clnt, server, &msg,
7335 &args.seq_args, &res.seq_res);
7336 nfs_free_fhandle(res.fh);
7337 if (status == NFS4_OK &&
7338 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7339 status = -NFS4ERR_LEASE_MOVED;
7340 return status;
7341}
7342
7343#endif /* CONFIG_NFS_V4_1 */
7344
7345/**
7346 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7347 * @inode: inode on FSID to check
7348 * @cred: credential to use for this operation
7349 *
7350 * Server indicates whether the FSID is present, moved, or not
7351 * recognized. This operation is necessary to clear a LEASE_MOVED
7352 * condition for this client ID.
7353 *
7354 * Returns NFS4_OK if the FSID is present on this server,
7355 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7356 * NFS4ERR code if some error occurred on the server, or a
7357 * negative errno if a local failure occurred.
7358 */
7359int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7360{
7361 struct nfs_server *server = NFS_SERVER(inode);
7362 struct nfs_client *clp = server->nfs_client;
7363 const struct nfs4_mig_recovery_ops *ops =
7364 clp->cl_mvops->mig_recovery_ops;
7365 struct nfs4_exception exception = { };
7366 int status;
7367
7368 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7369 (unsigned long long)server->fsid.major,
7370 (unsigned long long)server->fsid.minor,
7371 clp->cl_hostname);
7372 nfs_display_fhandle(NFS_FH(inode), __func__);
7373
7374 do {
7375 status = ops->fsid_present(inode, cred);
7376 if (status != -NFS4ERR_DELAY)
7377 break;
7378 nfs4_handle_exception(server, status, &exception);
7379 } while (exception.retry);
7380 return status;
7381}
7382
Andy Adamson5ec16a82013-08-08 10:57:55 -04007383/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007384 * If 'use_integrity' is true and the state managment nfs_client
7385 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7386 * and the machine credential as per RFC3530bis and RFC5661 Security
7387 * Considerations sections. Otherwise, just use the user cred with the
7388 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007389 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007390static 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 +00007391{
7392 int status;
7393 struct nfs4_secinfo_arg args = {
7394 .dir_fh = NFS_FH(dir),
7395 .name = name,
7396 };
7397 struct nfs4_secinfo_res res = {
7398 .flavors = flavors,
7399 };
7400 struct rpc_message msg = {
7401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7402 .rpc_argp = &args,
7403 .rpc_resp = &res,
7404 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007405 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007406 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007407
7408 if (use_integrity) {
7409 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007410 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7411 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007412 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007413
7414 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007415
7416 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7417 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7418
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007419 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7420 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007421 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007422
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007423 if (cred)
7424 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007425
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007426 return status;
7427}
7428
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007429int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7430 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007431{
7432 struct nfs4_exception exception = { };
7433 int err;
7434 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007435 err = -NFS4ERR_WRONGSEC;
7436
7437 /* try to use integrity protection with machine cred */
7438 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7439 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7440
7441 /*
7442 * if unable to use integrity protection, or SECINFO with
7443 * integrity protection returns NFS4ERR_WRONGSEC (which is
7444 * disallowed by spec, but exists in deployed servers) use
7445 * the current filesystem's rpc_client and the user cred.
7446 */
7447 if (err == -NFS4ERR_WRONGSEC)
7448 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7449
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007450 trace_nfs4_secinfo(dir, name, err);
7451 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007452 &exception);
7453 } while (exception.retry);
7454 return err;
7455}
7456
Andy Adamson557134a2009-04-01 09:21:53 -04007457#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007458/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007459 * Check the exchange flags returned by the server for invalid flags, having
7460 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7461 * DS flags set.
7462 */
7463static int nfs4_check_cl_exchange_flags(u32 flags)
7464{
7465 if (flags & ~EXCHGID4_FLAG_MASK_R)
7466 goto out_inval;
7467 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7468 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7469 goto out_inval;
7470 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7471 goto out_inval;
7472 return NFS_OK;
7473out_inval:
7474 return -NFS4ERR_INVAL;
7475}
7476
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007477static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007478nfs41_same_server_scope(struct nfs41_server_scope *a,
7479 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007480{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007481 if (a->server_scope_sz != b->server_scope_sz)
7482 return false;
7483 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007484}
7485
Andy Adamson02a95de2016-02-05 16:08:37 -05007486static void
7487nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7488{
7489}
7490
7491static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7492 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7493};
7494
Andy Adamson357f54d2010-12-14 10:11:57 -05007495/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007496 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007497 *
7498 * The 4.1 client currently uses the same TCP connection for the
7499 * fore and backchannel.
7500 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007501static
7502int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7503 struct rpc_xprt *xprt,
7504 struct nfs_client *clp,
7505 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007506{
7507 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007508 struct nfs41_bind_conn_to_session_args args = {
7509 .client = clp,
7510 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7511 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007512 struct nfs41_bind_conn_to_session_res res;
7513 struct rpc_message msg = {
7514 .rpc_proc =
7515 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007516 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007517 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007518 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007519 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007520 struct rpc_task_setup task_setup_data = {
7521 .rpc_client = clnt,
7522 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007523 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007524 .rpc_message = &msg,
7525 .flags = RPC_TASK_TIMEOUT,
7526 };
7527 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007528
Trond Myklebust71a097c2015-02-18 09:27:18 -08007529 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7530 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7531 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007532
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007533 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7534 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7535 args.dir = NFS4_CDFC4_FORE;
7536
7537 task = rpc_run_task(&task_setup_data);
7538 if (!IS_ERR(task)) {
7539 status = task->tk_status;
7540 rpc_put_task(task);
7541 } else
7542 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007543 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007544 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007545 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007546 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7547 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007548 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007549 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007550 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007551 dprintk("NFS: %s: Unexpected direction from server\n",
7552 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007553 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007554 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007555 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007556 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7557 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007558 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007559 }
7560 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007561
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007562 return status;
7563}
7564
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007565struct rpc_bind_conn_calldata {
7566 struct nfs_client *clp;
7567 struct rpc_cred *cred;
7568};
7569
7570static int
7571nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7572 struct rpc_xprt *xprt,
7573 void *calldata)
7574{
7575 struct rpc_bind_conn_calldata *p = calldata;
7576
7577 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7578}
7579
7580int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7581{
7582 struct rpc_bind_conn_calldata data = {
7583 .clp = clp,
7584 .cred = cred,
7585 };
7586 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7587 nfs4_proc_bind_conn_to_session_callback, &data);
7588}
7589
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007590/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007591 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7592 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007593 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007594static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7595 .how = SP4_MACH_CRED,
7596 .enforce.u.words = {
7597 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7598 1 << (OP_EXCHANGE_ID - 32) |
7599 1 << (OP_CREATE_SESSION - 32) |
7600 1 << (OP_DESTROY_SESSION - 32) |
7601 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007602 },
7603 .allow.u.words = {
7604 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007605 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007606 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007607 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007608 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007609 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007610 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007611 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007612 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007613 1 << (OP_FREE_STATEID - 32) |
7614 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007615 }
7616};
7617
7618/*
7619 * Select the state protection mode for client `clp' given the server results
7620 * from exchange_id in `sp'.
7621 *
7622 * Returns 0 on success, negative errno otherwise.
7623 */
7624static int nfs4_sp4_select_mode(struct nfs_client *clp,
7625 struct nfs41_state_protection *sp)
7626{
7627 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7628 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7629 1 << (OP_EXCHANGE_ID - 32) |
7630 1 << (OP_CREATE_SESSION - 32) |
7631 1 << (OP_DESTROY_SESSION - 32) |
7632 1 << (OP_DESTROY_CLIENTID - 32)
7633 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007634 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007635 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007636 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007637
7638 if (sp->how == SP4_MACH_CRED) {
7639 /* Print state protect result */
7640 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7641 for (i = 0; i <= LAST_NFS4_OP; i++) {
7642 if (test_bit(i, sp->enforce.u.longs))
7643 dfprintk(MOUNT, " enforce op %d\n", i);
7644 if (test_bit(i, sp->allow.u.longs))
7645 dfprintk(MOUNT, " allow op %d\n", i);
7646 }
7647
7648 /* make sure nothing is on enforce list that isn't supported */
7649 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7650 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7651 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007652 ret = -EINVAL;
7653 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007654 }
7655 }
7656
7657 /*
7658 * Minimal mode - state operations are allowed to use machine
7659 * credential. Note this already happens by default, so the
7660 * client doesn't have to do anything more than the negotiation.
7661 *
7662 * NOTE: we don't care if EXCHANGE_ID is in the list -
7663 * we're already using the machine cred for exchange_id
7664 * and will never use a different cred.
7665 */
7666 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7667 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7668 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7669 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7670 dfprintk(MOUNT, "sp4_mach_cred:\n");
7671 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007672 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007673 } else {
7674 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007675 ret = -EINVAL;
7676 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007677 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007678
7679 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007680 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7681 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007682 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7683 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007684 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007685 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007686
Andrew Elble99ade3c2015-12-02 09:39:51 -05007687 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7688 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007689 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007690 }
7691
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007692 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7693 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7694 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007695 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007696 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007697
7698 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7699 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7700 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007701 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007702 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007703
7704 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7705 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007706 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007707 }
7708
7709 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7710 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007711 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007712 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007713 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007714out:
7715 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007716 return 0;
7717}
7718
Andy Adamson8d89bd72016-09-09 09:22:18 -04007719struct nfs41_exchange_id_data {
7720 struct nfs41_exchange_id_res res;
7721 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007722};
7723
Andy Adamson8d89bd72016-09-09 09:22:18 -04007724static void nfs4_exchange_id_release(void *data)
7725{
7726 struct nfs41_exchange_id_data *cdata =
7727 (struct nfs41_exchange_id_data *)data;
7728
Olga Kornievskaia63513232017-03-13 10:36:19 -04007729 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007730 kfree(cdata->res.impl_id);
7731 kfree(cdata->res.server_scope);
7732 kfree(cdata->res.server_owner);
7733 kfree(cdata);
7734}
7735
7736static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007737 .rpc_release = nfs4_exchange_id_release,
7738};
7739
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007740/*
7741 * _nfs4_proc_exchange_id()
7742 *
7743 * Wrapper for EXCHANGE_ID operation.
7744 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007745static struct rpc_task *
7746nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007747 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007748{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007749 struct rpc_message msg = {
7750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007751 .rpc_cred = cred,
7752 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007753 struct rpc_task_setup task_setup_data = {
7754 .rpc_client = clp->cl_rpcclient,
7755 .callback_ops = &nfs4_exchange_id_call_ops,
7756 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007757 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007758 };
7759 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007760 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007761
Elena Reshetova212bf412017-10-20 12:53:38 +03007762 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007763 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007764
Trond Myklebust9c760d12017-07-31 18:38:50 -04007765 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007766 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007767 if (!calldata)
7768 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007769
Trond Myklebustfd405592017-08-01 16:02:47 -04007770 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007771
7772 status = nfs4_init_uniform_client_string(clp);
7773 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007774 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007775
Andy Adamson8d89bd72016-09-09 09:22:18 -04007776 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7777 GFP_NOFS);
7778 status = -ENOMEM;
7779 if (unlikely(calldata->res.server_owner == NULL))
7780 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007781
Andy Adamson8d89bd72016-09-09 09:22:18 -04007782 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007783 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007784 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007785 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007786
Andy Adamson8d89bd72016-09-09 09:22:18 -04007787 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7788 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007789 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007790
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007791 switch (sp4_how) {
7792 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007793 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007794 break;
7795
7796 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007797 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007798 break;
7799
7800 default:
7801 /* unsupported! */
7802 WARN_ON_ONCE(1);
7803 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007804 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007805 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007806 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007807 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007808 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007809 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7810 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007811 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007812 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007813 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7814 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007815#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007816 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007817#endif
7818 msg.rpc_argp = &calldata->args;
7819 msg.rpc_resp = &calldata->res;
7820 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007821
Trond Myklebust9c760d12017-07-31 18:38:50 -04007822 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007823
7824out_impl_id:
7825 kfree(calldata->res.impl_id);
7826out_server_scope:
7827 kfree(calldata->res.server_scope);
7828out_server_owner:
7829 kfree(calldata->res.server_owner);
7830out_calldata:
7831 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007832out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007833 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007834 return ERR_PTR(status);
7835}
7836
7837/*
7838 * _nfs4_proc_exchange_id()
7839 *
7840 * Wrapper for EXCHANGE_ID operation.
7841 */
7842static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7843 u32 sp4_how)
7844{
7845 struct rpc_task *task;
7846 struct nfs41_exchange_id_args *argp;
7847 struct nfs41_exchange_id_res *resp;
7848 int status;
7849
7850 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7851 if (IS_ERR(task))
7852 return PTR_ERR(task);
7853
7854 argp = task->tk_msg.rpc_argp;
7855 resp = task->tk_msg.rpc_resp;
7856 status = task->tk_status;
7857 if (status != 0)
7858 goto out;
7859
7860 status = nfs4_check_cl_exchange_flags(resp->flags);
7861 if (status != 0)
7862 goto out;
7863
7864 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7865 if (status != 0)
7866 goto out;
7867
7868 clp->cl_clientid = resp->clientid;
7869 clp->cl_exchange_flags = resp->flags;
7870 clp->cl_seqid = resp->seqid;
7871 /* Client ID is not confirmed */
7872 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7873 clear_bit(NFS4_SESSION_ESTABLISHED,
7874 &clp->cl_session->session_state);
7875
7876 if (clp->cl_serverscope != NULL &&
7877 !nfs41_same_server_scope(clp->cl_serverscope,
7878 resp->server_scope)) {
7879 dprintk("%s: server_scope mismatch detected\n",
7880 __func__);
7881 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7882 }
7883
7884 swap(clp->cl_serverowner, resp->server_owner);
7885 swap(clp->cl_serverscope, resp->server_scope);
7886 swap(clp->cl_implid, resp->impl_id);
7887
7888 /* Save the EXCHANGE_ID verifier session trunk tests */
7889 memcpy(clp->cl_confirm.data, argp->verifier.data,
7890 sizeof(clp->cl_confirm.data));
7891out:
7892 trace_nfs4_exchange_id(clp, status);
7893 rpc_put_task(task);
7894 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007895}
7896
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007897/*
7898 * nfs4_proc_exchange_id()
7899 *
7900 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7901 *
7902 * Since the clientid has expired, all compounds using sessions
7903 * associated with the stale clientid will be returning
7904 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7905 * be in some phase of session reset.
7906 *
7907 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7908 */
7909int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7910{
7911 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7912 int status;
7913
7914 /* try SP4_MACH_CRED if krb5i/p */
7915 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7916 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007917 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007918 if (!status)
7919 return 0;
7920 }
7921
7922 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007923 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007924}
7925
Andy Adamson04fa2c62016-09-09 09:22:29 -04007926/**
7927 * nfs4_test_session_trunk
7928 *
7929 * This is an add_xprt_test() test function called from
7930 * rpc_clnt_setup_test_and_add_xprt.
7931 *
7932 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7933 * and is dereferrenced in nfs4_exchange_id_release
7934 *
7935 * Upon success, add the new transport to the rpc_clnt
7936 *
7937 * @clnt: struct rpc_clnt to get new transport
7938 * @xprt: the rpc_xprt to test
7939 * @data: call data for _nfs4_proc_exchange_id.
7940 */
7941int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7942 void *data)
7943{
7944 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007945 struct rpc_task *task;
7946 int status;
7947
Andy Adamson04fa2c62016-09-09 09:22:29 -04007948 u32 sp4_how;
7949
7950 dprintk("--> %s try %s\n", __func__,
7951 xprt->address_strings[RPC_DISPLAY_ADDR]);
7952
7953 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7954
7955 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007956 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7957 if (IS_ERR(task))
7958 return PTR_ERR(task);
7959
7960 status = task->tk_status;
7961 if (status == 0)
7962 status = nfs4_detect_session_trunking(adata->clp,
7963 task->tk_msg.rpc_resp, xprt);
7964
7965 rpc_put_task(task);
7966 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007967}
7968EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7969
Trond Myklebust66245532012-05-25 17:18:09 -04007970static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7971 struct rpc_cred *cred)
7972{
7973 struct rpc_message msg = {
7974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7975 .rpc_argp = clp,
7976 .rpc_cred = cred,
7977 };
7978 int status;
7979
7980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007981 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007982 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007983 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007984 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7985 return status;
7986}
7987
7988static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7989 struct rpc_cred *cred)
7990{
7991 unsigned int loop;
7992 int ret;
7993
7994 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7995 ret = _nfs4_proc_destroy_clientid(clp, cred);
7996 switch (ret) {
7997 case -NFS4ERR_DELAY:
7998 case -NFS4ERR_CLIENTID_BUSY:
7999 ssleep(1);
8000 break;
8001 default:
8002 return ret;
8003 }
8004 }
8005 return 0;
8006}
8007
8008int nfs4_destroy_clientid(struct nfs_client *clp)
8009{
8010 struct rpc_cred *cred;
8011 int ret = 0;
8012
8013 if (clp->cl_mvops->minor_version < 1)
8014 goto out;
8015 if (clp->cl_exchange_flags == 0)
8016 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008017 if (clp->cl_preserve_clid)
8018 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008019 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008020 ret = nfs4_proc_destroy_clientid(clp, cred);
8021 if (cred)
8022 put_rpccred(cred);
8023 switch (ret) {
8024 case 0:
8025 case -NFS4ERR_STALE_CLIENTID:
8026 clp->cl_exchange_flags = 0;
8027 }
8028out:
8029 return ret;
8030}
8031
Andy Adamson2050f0c2009-04-01 09:22:30 -04008032struct nfs4_get_lease_time_data {
8033 struct nfs4_get_lease_time_args *args;
8034 struct nfs4_get_lease_time_res *res;
8035 struct nfs_client *clp;
8036};
8037
8038static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8039 void *calldata)
8040{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008041 struct nfs4_get_lease_time_data *data =
8042 (struct nfs4_get_lease_time_data *)calldata;
8043
8044 dprintk("--> %s\n", __func__);
8045 /* just setup sequence, do not trigger session recovery
8046 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008047 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008048 &data->args->la_seq_args,
8049 &data->res->lr_seq_res,
8050 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008051 dprintk("<-- %s\n", __func__);
8052}
8053
8054/*
8055 * Called from nfs4_state_manager thread for session setup, so don't recover
8056 * from sequence operation or clientid errors.
8057 */
8058static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8059{
8060 struct nfs4_get_lease_time_data *data =
8061 (struct nfs4_get_lease_time_data *)calldata;
8062
8063 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008064 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8065 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008066 switch (task->tk_status) {
8067 case -NFS4ERR_DELAY:
8068 case -NFS4ERR_GRACE:
8069 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8070 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8071 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008072 /* fall through */
8073 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008074 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008075 return;
8076 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008077 dprintk("<-- %s\n", __func__);
8078}
8079
Trond Myklebust17280172012-03-11 13:11:00 -04008080static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008081 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8082 .rpc_call_done = nfs4_get_lease_time_done,
8083};
8084
8085int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8086{
8087 struct rpc_task *task;
8088 struct nfs4_get_lease_time_args args;
8089 struct nfs4_get_lease_time_res res = {
8090 .lr_fsinfo = fsinfo,
8091 };
8092 struct nfs4_get_lease_time_data data = {
8093 .args = &args,
8094 .res = &res,
8095 .clp = clp,
8096 };
8097 struct rpc_message msg = {
8098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8099 .rpc_argp = &args,
8100 .rpc_resp = &res,
8101 };
8102 struct rpc_task_setup task_setup = {
8103 .rpc_client = clp->cl_rpcclient,
8104 .rpc_message = &msg,
8105 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008106 .callback_data = &data,
8107 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008108 };
8109 int status;
8110
Anna Schumakerfba83f32018-05-04 16:22:50 -04008111 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008112 task = rpc_run_task(&task_setup);
8113
8114 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008115 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008116
Anna Schumakerf6148712017-04-07 14:15:23 -04008117 status = task->tk_status;
8118 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008119 return status;
8120}
8121
Andy Adamsonfc931582009-04-01 09:22:31 -04008122/*
8123 * Initialize the values to be used by the client in CREATE_SESSION
8124 * If nfs4_init_session set the fore channel request and response sizes,
8125 * use them.
8126 *
8127 * Set the back channel max_resp_sz_cached to zero to force the client to
8128 * always set csa_cachethis to FALSE because the current implementation
8129 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8130 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008131static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8132 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008133{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008134 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008135 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008136
Andy Adamson18aad3d2013-06-26 12:21:49 -04008137 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8138 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8139
Andy Adamsonfc931582009-04-01 09:22:31 -04008140 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008141 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8142 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008143 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008144 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008145
8146 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008147 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008148 __func__,
8149 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008150 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008151
8152 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008153 args->bc_attrs.max_rqst_sz = max_bc_payload;
8154 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008155 args->bc_attrs.max_resp_sz_cached = 0;
8156 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008157 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008158
8159 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8160 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8161 __func__,
8162 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8163 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8164 args->bc_attrs.max_reqs);
8165}
8166
Trond Myklebust79969dd2015-02-18 11:30:18 -08008167static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8168 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008169{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008170 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008171 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008172
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008173 if (rcvd->max_resp_sz > sent->max_resp_sz)
8174 return -EINVAL;
8175 /*
8176 * Our requested max_ops is the minimum we need; we're not
8177 * prepared to break up compounds into smaller pieces than that.
8178 * So, no point even trying to continue if the server won't
8179 * cooperate:
8180 */
8181 if (rcvd->max_ops < sent->max_ops)
8182 return -EINVAL;
8183 if (rcvd->max_reqs == 0)
8184 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008185 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8186 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008187 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008188}
8189
Trond Myklebust79969dd2015-02-18 11:30:18 -08008190static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8191 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008192{
8193 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008194 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008195
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008196 if (!(res->flags & SESSION4_BACK_CHAN))
8197 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008198 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8199 return -EINVAL;
8200 if (rcvd->max_resp_sz < sent->max_resp_sz)
8201 return -EINVAL;
8202 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8203 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008204 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008205 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008206 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008207 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008208out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008209 return 0;
8210}
Andy Adamson8d353012009-04-01 09:22:32 -04008211
Andy Adamson8d353012009-04-01 09:22:32 -04008212static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008213 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008214{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008215 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008216
Trond Myklebust79969dd2015-02-18 11:30:18 -08008217 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008218 if (ret)
8219 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008220 return nfs4_verify_back_channel_attrs(args, res);
8221}
8222
8223static void nfs4_update_session(struct nfs4_session *session,
8224 struct nfs41_create_session_res *res)
8225{
8226 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008227 /* Mark client id and session as being confirmed */
8228 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8229 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008230 session->flags = res->flags;
8231 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008232 if (res->flags & SESSION4_BACK_CHAN)
8233 memcpy(&session->bc_attrs, &res->bc_attrs,
8234 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008235}
8236
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008237static int _nfs4_proc_create_session(struct nfs_client *clp,
8238 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008239{
8240 struct nfs4_session *session = clp->cl_session;
8241 struct nfs41_create_session_args args = {
8242 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008243 .clientid = clp->cl_clientid,
8244 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008245 .cb_program = NFS4_CALLBACK,
8246 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008247 struct nfs41_create_session_res res;
8248
Andy Adamsonfc931582009-04-01 09:22:31 -04008249 struct rpc_message msg = {
8250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8251 .rpc_argp = &args,
8252 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008253 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008254 };
8255 int status;
8256
Chuck Lever6b26cc82016-05-02 14:40:40 -04008257 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008258 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008259
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008260 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008261 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008262
Trond Myklebustb519d402016-09-11 14:50:01 -04008263 switch (status) {
8264 case -NFS4ERR_STALE_CLIENTID:
8265 case -NFS4ERR_DELAY:
8266 case -ETIMEDOUT:
8267 case -EACCES:
8268 case -EAGAIN:
8269 goto out;
8270 };
8271
8272 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008273 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008274 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008275 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008276 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008277 if (status)
8278 goto out;
8279 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008280 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008281out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008282 return status;
8283}
8284
8285/*
8286 * Issues a CREATE_SESSION operation to the server.
8287 * It is the responsibility of the caller to verify the session is
8288 * expired before calling this routine.
8289 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008290int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008291{
8292 int status;
8293 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008294 struct nfs4_session *session = clp->cl_session;
8295
8296 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8297
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008298 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008299 if (status)
8300 goto out;
8301
Andy Adamsonaacd5532011-11-09 13:58:21 -05008302 /* Init or reset the session slot tables */
8303 status = nfs4_setup_session_slot_tables(session);
8304 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008305 if (status)
8306 goto out;
8307
8308 ptr = (unsigned *)&session->sess_id.data[0];
8309 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8310 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008311out:
8312 dprintk("<-- %s\n", __func__);
8313 return status;
8314}
8315
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008316/*
8317 * Issue the over-the-wire RPC DESTROY_SESSION.
8318 * The caller must serialize access to this routine.
8319 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008320int nfs4_proc_destroy_session(struct nfs4_session *session,
8321 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008322{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008323 struct rpc_message msg = {
8324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8325 .rpc_argp = session,
8326 .rpc_cred = cred,
8327 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008328 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008329
8330 dprintk("--> nfs4_proc_destroy_session\n");
8331
8332 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008333 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8334 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008335
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008336 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008337 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008338
8339 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008340 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008341 "Session has been destroyed regardless...\n", status);
8342
8343 dprintk("<-- nfs4_proc_destroy_session\n");
8344 return status;
8345}
8346
Trond Myklebust7b38c362012-05-23 13:23:31 -04008347/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008348 * Renew the cl_session lease.
8349 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008350struct nfs4_sequence_data {
8351 struct nfs_client *clp;
8352 struct nfs4_sequence_args args;
8353 struct nfs4_sequence_res res;
8354};
8355
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008356static void nfs41_sequence_release(void *data)
8357{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008358 struct nfs4_sequence_data *calldata = data;
8359 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008360
Elena Reshetova212bf412017-10-20 12:53:38 +03008361 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008362 nfs4_schedule_state_renewal(clp);
8363 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008364 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008365}
8366
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008367static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8368{
8369 switch(task->tk_status) {
8370 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008371 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8372 return -EAGAIN;
8373 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008374 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008375 }
8376 return 0;
8377}
8378
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008379static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008380{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008381 struct nfs4_sequence_data *calldata = data;
8382 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008383
Trond Myklebust14516c32010-07-31 14:29:06 -04008384 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8385 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008386
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008387 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008388 if (task->tk_status < 0) {
8389 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008390 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008391 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008392
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008393 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8394 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008395 return;
8396 }
8397 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008398 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008399out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008400 dprintk("<-- %s\n", __func__);
8401}
8402
8403static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8404{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008405 struct nfs4_sequence_data *calldata = data;
8406 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008407 struct nfs4_sequence_args *args;
8408 struct nfs4_sequence_res *res;
8409
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008410 args = task->tk_msg.rpc_argp;
8411 res = task->tk_msg.rpc_resp;
8412
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008413 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008414}
8415
8416static const struct rpc_call_ops nfs41_sequence_ops = {
8417 .rpc_call_done = nfs41_sequence_call_done,
8418 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008419 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008420};
8421
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008422static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8423 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008424 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008425 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008426{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008427 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008428 struct rpc_message msg = {
8429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8430 .rpc_cred = cred,
8431 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008432 struct rpc_task_setup task_setup_data = {
8433 .rpc_client = clp->cl_rpcclient,
8434 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008435 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008436 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008437 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008438 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008439
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008440 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008441 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008442 goto out_err;
8443
8444 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008445 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008446 if (calldata == NULL)
8447 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008448 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008449 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008450 msg.rpc_argp = &calldata->args;
8451 msg.rpc_resp = &calldata->res;
8452 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008453 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008454
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008455 ret = rpc_run_task(&task_setup_data);
8456 if (IS_ERR(ret))
8457 goto out_err;
8458 return ret;
8459out_put_clp:
8460 nfs_put_client(clp);
8461out_err:
8462 nfs41_release_slot(slot);
8463 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008464}
8465
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008466static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008467{
8468 struct rpc_task *task;
8469 int ret = 0;
8470
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008471 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008472 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008473 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008474 if (IS_ERR(task))
8475 ret = PTR_ERR(task);
8476 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008477 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008478 dprintk("<-- %s status=%d\n", __func__, ret);
8479 return ret;
8480}
8481
8482static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8483{
8484 struct rpc_task *task;
8485 int ret;
8486
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008487 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008488 if (IS_ERR(task)) {
8489 ret = PTR_ERR(task);
8490 goto out;
8491 }
8492 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008493 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008494 ret = task->tk_status;
8495 rpc_put_task(task);
8496out:
8497 dprintk("<-- %s status=%d\n", __func__, ret);
8498 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008499}
8500
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008501struct nfs4_reclaim_complete_data {
8502 struct nfs_client *clp;
8503 struct nfs41_reclaim_complete_args arg;
8504 struct nfs41_reclaim_complete_res res;
8505};
8506
8507static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8508{
8509 struct nfs4_reclaim_complete_data *calldata = data;
8510
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008511 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008512 &calldata->arg.seq_args,
8513 &calldata->res.seq_res,
8514 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008515}
8516
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008517static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8518{
8519 switch(task->tk_status) {
8520 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008521 wake_up_all(&clp->cl_lock_waitq);
8522 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008523 case -NFS4ERR_COMPLETE_ALREADY:
8524 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8525 break;
8526 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008527 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008528 /* fall through */
8529 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008530 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008531 case -NFS4ERR_BADSESSION:
8532 case -NFS4ERR_DEADSESSION:
8533 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8534 nfs4_schedule_session_recovery(clp->cl_session,
8535 task->tk_status);
8536 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008537 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008538 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008539 }
8540 return 0;
8541}
8542
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008543static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8544{
8545 struct nfs4_reclaim_complete_data *calldata = data;
8546 struct nfs_client *clp = calldata->clp;
8547 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8548
8549 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008550 if (!nfs41_sequence_done(task, res))
8551 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008552
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008553 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008554 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8555 rpc_restart_call_prepare(task);
8556 return;
8557 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008558 dprintk("<-- %s\n", __func__);
8559}
8560
8561static void nfs4_free_reclaim_complete_data(void *data)
8562{
8563 struct nfs4_reclaim_complete_data *calldata = data;
8564
8565 kfree(calldata);
8566}
8567
8568static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8569 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8570 .rpc_call_done = nfs4_reclaim_complete_done,
8571 .rpc_release = nfs4_free_reclaim_complete_data,
8572};
8573
8574/*
8575 * Issue a global reclaim complete.
8576 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008577static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8578 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008579{
8580 struct nfs4_reclaim_complete_data *calldata;
8581 struct rpc_task *task;
8582 struct rpc_message msg = {
8583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008584 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008585 };
8586 struct rpc_task_setup task_setup_data = {
8587 .rpc_client = clp->cl_rpcclient,
8588 .rpc_message = &msg,
8589 .callback_ops = &nfs4_reclaim_complete_call_ops,
8590 .flags = RPC_TASK_ASYNC,
8591 };
8592 int status = -ENOMEM;
8593
8594 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008595 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008596 if (calldata == NULL)
8597 goto out;
8598 calldata->clp = clp;
8599 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008600
Anna Schumakerfba83f32018-05-04 16:22:50 -04008601 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008602 msg.rpc_argp = &calldata->arg;
8603 msg.rpc_resp = &calldata->res;
8604 task_setup_data.callback_data = calldata;
8605 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008606 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008607 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008608 goto out;
8609 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008610 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008611 if (status == 0)
8612 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008613 rpc_put_task(task);
8614out:
8615 dprintk("<-- %s status=%d\n", __func__, status);
8616 return status;
8617}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008618
8619static void
8620nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8621{
8622 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008623 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008624
8625 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008626 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008627 &lgp->res.seq_res, task);
8628 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008629}
8630
8631static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8632{
8633 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008634
8635 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008636 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008637 dprintk("<-- %s\n", __func__);
8638}
8639
8640static int
8641nfs4_layoutget_handle_exception(struct rpc_task *task,
8642 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8643{
Trond Myklebustee314c22012-10-01 17:25:48 -07008644 struct inode *inode = lgp->args.inode;
8645 struct nfs_server *server = NFS_SERVER(inode);
8646 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008647 int nfs4err = task->tk_status;
8648 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008649 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008650
Boaz Harroshed7e5422014-01-22 20:34:54 +02008651 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008652
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008653 nfs4_sequence_free_slot(&lgp->res.seq_res);
8654
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008655 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008656 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008657 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008658
8659 /*
8660 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8661 * on the file. set tk_status to -ENODATA to tell upper layer to
8662 * retry go inband.
8663 */
8664 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008665 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008666 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008667 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008668 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8669 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8670 */
8671 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008672 status = -EOVERFLOW;
8673 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008674 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008675 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008676 * (or clients) writing to the same RAID stripe except when
8677 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008678 *
8679 * Treat it like we would RECALLCONFLICT -- we retry for a little
8680 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008681 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008682 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008683 if (lgp->args.minlength == 0) {
8684 status = -EOVERFLOW;
8685 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008686 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008687 status = -EBUSY;
8688 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008689 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008690 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008691 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008692 case -NFS4ERR_DELEG_REVOKED:
8693 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008694 case -NFS4ERR_EXPIRED:
8695 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008696 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008697 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008698 lo = NFS_I(inode)->layout;
8699 /* If the open stateid was bad, then recover it. */
8700 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008701 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008702 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008703 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008704 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008705 break;
8706 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008707
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008708 /*
8709 * Mark the bad layout state as invalid, then retry
8710 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008711 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008712 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008713 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008714 pnfs_free_lseg_list(&head);
8715 status = -EAGAIN;
8716 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008717 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008718
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008719 err = nfs4_handle_exception(server, nfs4err, exception);
8720 if (!status) {
8721 if (exception->retry)
8722 status = -EAGAIN;
8723 else
8724 status = err;
8725 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008726out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008727 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008728 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008729}
8730
Fred Isamandacb4522016-09-19 17:47:09 -04008731size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008732{
8733 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8734 return nfs_page_array_len(0, max_resp_sz);
8735}
8736
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008737static void nfs4_layoutget_release(void *calldata)
8738{
8739 struct nfs4_layoutget *lgp = calldata;
8740
8741 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008742 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008743 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008744 dprintk("<-- %s\n", __func__);
8745}
8746
8747static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8748 .rpc_call_prepare = nfs4_layoutget_prepare,
8749 .rpc_call_done = nfs4_layoutget_done,
8750 .rpc_release = nfs4_layoutget_release,
8751};
8752
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008753struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008754nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008755{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008756 struct inode *inode = lgp->args.inode;
8757 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008758 struct rpc_task *task;
8759 struct rpc_message msg = {
8760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8761 .rpc_argp = &lgp->args,
8762 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008763 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008764 };
8765 struct rpc_task_setup task_setup_data = {
8766 .rpc_client = server->client,
8767 .rpc_message = &msg,
8768 .callback_ops = &nfs4_layoutget_call_ops,
8769 .callback_data = lgp,
8770 .flags = RPC_TASK_ASYNC,
8771 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008772 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008773 struct nfs4_exception exception = {
8774 .inode = inode,
8775 .timeout = *timeout,
8776 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008777 int status = 0;
8778
8779 dprintk("--> %s\n", __func__);
8780
Peng Tao4bd5a982014-11-17 11:05:17 +08008781 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8782 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8783
Anna Schumakerfba83f32018-05-04 16:22:50 -04008784 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008785
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008786 task = rpc_run_task(&task_setup_data);
8787 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008788 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008789 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008790 if (status != 0)
8791 goto out;
8792
8793 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8794 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008795 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8796 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008797 } else
8798 lseg = pnfs_layout_process(lgp);
8799out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008800 trace_nfs4_layoutget(lgp->args.ctx,
8801 &lgp->args.range,
8802 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008803 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008804 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008805
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008806 rpc_put_task(task);
8807 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008808 if (status)
8809 return ERR_PTR(status);
8810 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008811}
8812
Benny Halevycbe82602011-05-22 19:52:37 +03008813static void
8814nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8815{
8816 struct nfs4_layoutreturn *lrp = calldata;
8817
8818 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008819 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008820 &lrp->args.seq_args,
8821 &lrp->res.seq_res,
8822 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008823}
8824
8825static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8826{
8827 struct nfs4_layoutreturn *lrp = calldata;
8828 struct nfs_server *server;
8829
8830 dprintk("--> %s\n", __func__);
8831
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008832 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008833 return;
8834
8835 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008836 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008837 case -NFS4ERR_OLD_STATEID:
8838 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8839 lrp->args.inode))
8840 goto out_restart;
8841 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008842 default:
8843 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008844 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008845 case 0:
8846 break;
8847 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008848 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008849 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008850 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008851 }
Benny Halevycbe82602011-05-22 19:52:37 +03008852 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008853 return;
8854out_restart:
8855 task->tk_status = 0;
8856 nfs4_sequence_free_slot(&lrp->res.seq_res);
8857 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008858}
8859
8860static void nfs4_layoutreturn_release(void *calldata)
8861{
8862 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008863 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008864
8865 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008866 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008867 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008868 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008869 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8870 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008871 pnfs_put_layout_hdr(lrp->args.layout);
8872 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008873 kfree(calldata);
8874 dprintk("<-- %s\n", __func__);
8875}
8876
8877static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8878 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8879 .rpc_call_done = nfs4_layoutreturn_done,
8880 .rpc_release = nfs4_layoutreturn_release,
8881};
8882
Peng Tao6c166052014-11-17 09:30:40 +08008883int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008884{
8885 struct rpc_task *task;
8886 struct rpc_message msg = {
8887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8888 .rpc_argp = &lrp->args,
8889 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008890 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008891 };
8892 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008893 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008894 .rpc_message = &msg,
8895 .callback_ops = &nfs4_layoutreturn_call_ops,
8896 .callback_data = lrp,
8897 };
Peng Tao6c166052014-11-17 09:30:40 +08008898 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008899
Andrew Elble99ade3c2015-12-02 09:39:51 -05008900 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8901 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8902 &task_setup_data.rpc_client, &msg);
8903
Benny Halevycbe82602011-05-22 19:52:37 +03008904 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008905 if (!sync) {
8906 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8907 if (!lrp->inode) {
8908 nfs4_layoutreturn_release(lrp);
8909 return -EAGAIN;
8910 }
8911 task_setup_data.flags |= RPC_TASK_ASYNC;
8912 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008913 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008914 task = rpc_run_task(&task_setup_data);
8915 if (IS_ERR(task))
8916 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008917 if (sync)
8918 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008919 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008920 dprintk("<-- %s status=%d\n", __func__, status);
8921 rpc_put_task(task);
8922 return status;
8923}
8924
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008925static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008926_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8927 struct pnfs_device *pdev,
8928 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008929{
8930 struct nfs4_getdeviceinfo_args args = {
8931 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008932 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8933 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008934 };
8935 struct nfs4_getdeviceinfo_res res = {
8936 .pdev = pdev,
8937 };
8938 struct rpc_message msg = {
8939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8940 .rpc_argp = &args,
8941 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008942 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008943 };
8944 int status;
8945
8946 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008947 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008948 if (res.notification & ~args.notify_types)
8949 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008950 if (res.notification != args.notify_types)
8951 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008952
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008953 dprintk("<-- %s status=%d\n", __func__, status);
8954
8955 return status;
8956}
8957
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008958int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8959 struct pnfs_device *pdev,
8960 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008961{
8962 struct nfs4_exception exception = { };
8963 int err;
8964
8965 do {
8966 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008967 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008968 &exception);
8969 } while (exception.retry);
8970 return err;
8971}
8972EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8973
Andy Adamson863a3c62011-03-23 13:27:54 +00008974static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8975{
8976 struct nfs4_layoutcommit_data *data = calldata;
8977 struct nfs_server *server = NFS_SERVER(data->args.inode);
8978
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008979 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008980 &data->args.seq_args,
8981 &data->res.seq_res,
8982 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008983}
8984
8985static void
8986nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8987{
8988 struct nfs4_layoutcommit_data *data = calldata;
8989 struct nfs_server *server = NFS_SERVER(data->args.inode);
8990
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008991 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008992 return;
8993
8994 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008995 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8996 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8997 case -NFS4ERR_BADLAYOUT: /* no layout */
8998 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008999 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009000 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009001 break;
9002 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009003 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009004 rpc_restart_call_prepare(task);
9005 return;
9006 }
9007 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009008}
9009
9010static void nfs4_layoutcommit_release(void *calldata)
9011{
9012 struct nfs4_layoutcommit_data *data = calldata;
9013
Andy Adamsondb29c082011-07-30 20:52:38 -04009014 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009015 nfs_post_op_update_inode_force_wcc(data->args.inode,
9016 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009017 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009018 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009019 kfree(data);
9020}
9021
9022static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9023 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9024 .rpc_call_done = nfs4_layoutcommit_done,
9025 .rpc_release = nfs4_layoutcommit_release,
9026};
9027
9028int
Andy Adamsonef311532011-03-12 02:58:10 -05009029nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009030{
9031 struct rpc_message msg = {
9032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9033 .rpc_argp = &data->args,
9034 .rpc_resp = &data->res,
9035 .rpc_cred = data->cred,
9036 };
9037 struct rpc_task_setup task_setup_data = {
9038 .task = &data->task,
9039 .rpc_client = NFS_CLIENT(data->args.inode),
9040 .rpc_message = &msg,
9041 .callback_ops = &nfs4_layoutcommit_ops,
9042 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009043 };
9044 struct rpc_task *task;
9045 int status = 0;
9046
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009047 dprintk("NFS: initiating layoutcommit call. sync %d "
9048 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009049 data->args.lastbytewritten,
9050 data->args.inode->i_ino);
9051
Trond Myklebust472e2592015-02-05 16:50:30 -05009052 if (!sync) {
9053 data->inode = nfs_igrab_and_active(data->args.inode);
9054 if (data->inode == NULL) {
9055 nfs4_layoutcommit_release(data);
9056 return -EAGAIN;
9057 }
9058 task_setup_data.flags = RPC_TASK_ASYNC;
9059 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009060 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009061 task = rpc_run_task(&task_setup_data);
9062 if (IS_ERR(task))
9063 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009064 if (sync)
9065 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009066 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009067 dprintk("%s: status %d\n", __func__, status);
9068 rpc_put_task(task);
9069 return status;
9070}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009071
Andy Adamson97431202013-08-08 10:57:56 -04009072/**
9073 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9074 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9075 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009076static int
9077_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009078 struct nfs_fsinfo *info,
9079 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009080{
9081 struct nfs41_secinfo_no_name_args args = {
9082 .style = SECINFO_STYLE_CURRENT_FH,
9083 };
9084 struct nfs4_secinfo_res res = {
9085 .flavors = flavors,
9086 };
9087 struct rpc_message msg = {
9088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9089 .rpc_argp = &args,
9090 .rpc_resp = &res,
9091 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009092 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009093 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009094 int status;
9095
9096 if (use_integrity) {
9097 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009098 cred = nfs4_get_clid_cred(server->nfs_client);
9099 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009100 }
9101
9102 dprintk("--> %s\n", __func__);
9103 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9104 &res.seq_res, 0);
9105 dprintk("<-- %s status=%d\n", __func__, status);
9106
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009107 if (cred)
9108 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009109
9110 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009111}
9112
9113static int
9114nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9115 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9116{
9117 struct nfs4_exception exception = { };
9118 int err;
9119 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009120 /* first try using integrity protection */
9121 err = -NFS4ERR_WRONGSEC;
9122
9123 /* try to use integrity protection with machine cred */
9124 if (_nfs4_is_integrity_protected(server->nfs_client))
9125 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9126 flavors, true);
9127
9128 /*
9129 * if unable to use integrity protection, or SECINFO with
9130 * integrity protection returns NFS4ERR_WRONGSEC (which is
9131 * disallowed by spec, but exists in deployed servers) use
9132 * the current filesystem's rpc_client and the user cred.
9133 */
9134 if (err == -NFS4ERR_WRONGSEC)
9135 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9136 flavors, false);
9137
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009138 switch (err) {
9139 case 0:
9140 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009141 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009142 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009143 default:
9144 err = nfs4_handle_exception(server, err, &exception);
9145 }
9146 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009147out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009148 return err;
9149}
9150
9151static int
9152nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9153 struct nfs_fsinfo *info)
9154{
9155 int err;
9156 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009157 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009158 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009159 struct nfs4_secinfo4 *secinfo;
9160 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009161
9162 page = alloc_page(GFP_KERNEL);
9163 if (!page) {
9164 err = -ENOMEM;
9165 goto out;
9166 }
9167
9168 flavors = page_address(page);
9169 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9170
9171 /*
9172 * Fall back on "guess and check" method if
9173 * the server doesn't support SECINFO_NO_NAME
9174 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009175 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009176 err = nfs4_find_root_sec(server, fhandle, info);
9177 goto out_freepage;
9178 }
9179 if (err)
9180 goto out_freepage;
9181
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009182 for (i = 0; i < flavors->num_flavors; i++) {
9183 secinfo = &flavors->flavors[i];
9184
9185 switch (secinfo->flavor) {
9186 case RPC_AUTH_NULL:
9187 case RPC_AUTH_UNIX:
9188 case RPC_AUTH_GSS:
9189 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9190 &secinfo->flavor_info);
9191 break;
9192 default:
9193 flavor = RPC_AUTH_MAXFLAVOR;
9194 break;
9195 }
9196
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009197 if (!nfs_auth_info_match(&server->auth_info, flavor))
9198 flavor = RPC_AUTH_MAXFLAVOR;
9199
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009200 if (flavor != RPC_AUTH_MAXFLAVOR) {
9201 err = nfs4_lookup_root_sec(server, fhandle,
9202 info, flavor);
9203 if (!err)
9204 break;
9205 }
9206 }
9207
9208 if (flavor == RPC_AUTH_MAXFLAVOR)
9209 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009210
9211out_freepage:
9212 put_page(page);
9213 if (err == -EACCES)
9214 return -EPERM;
9215out:
9216 return err;
9217}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009218
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009219static int _nfs41_test_stateid(struct nfs_server *server,
9220 nfs4_stateid *stateid,
9221 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009222{
9223 int status;
9224 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009225 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009226 };
9227 struct nfs41_test_stateid_res res;
9228 struct rpc_message msg = {
9229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9230 .rpc_argp = &args,
9231 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009232 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009233 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009234 struct rpc_clnt *rpc_client = server->client;
9235
9236 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9237 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009238
Chuck Lever38527b12012-07-11 16:30:23 -04009239 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009240 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009241 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009242 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009243 if (status != NFS_OK) {
9244 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009245 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009246 }
9247 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009248 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009249}
9250
Trond Myklebust43912bb2016-09-22 13:38:56 -04009251static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9252 int err, struct nfs4_exception *exception)
9253{
9254 exception->retry = 0;
9255 switch(err) {
9256 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009257 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009258 nfs4_handle_exception(server, err, exception);
9259 break;
9260 case -NFS4ERR_BADSESSION:
9261 case -NFS4ERR_BADSLOT:
9262 case -NFS4ERR_BAD_HIGH_SLOT:
9263 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9264 case -NFS4ERR_DEADSESSION:
9265 nfs4_do_handle_exception(server, err, exception);
9266 }
9267}
9268
Chuck Lever38527b12012-07-11 16:30:23 -04009269/**
9270 * nfs41_test_stateid - perform a TEST_STATEID operation
9271 *
9272 * @server: server / transport on which to perform the operation
9273 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009274 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009275 *
9276 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9277 * Otherwise a negative NFS4ERR value is returned if the operation
9278 * failed or the state ID is not currently valid.
9279 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009280static int nfs41_test_stateid(struct nfs_server *server,
9281 nfs4_stateid *stateid,
9282 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009283{
9284 struct nfs4_exception exception = { };
9285 int err;
9286 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009287 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009288 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009289 } while (exception.retry);
9290 return err;
9291}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009292
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009293struct nfs_free_stateid_data {
9294 struct nfs_server *server;
9295 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009296 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009297};
9298
9299static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9300{
9301 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009302 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009303 &data->args.seq_args,
9304 &data->res.seq_res,
9305 task);
9306}
9307
9308static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9309{
9310 struct nfs_free_stateid_data *data = calldata;
9311
9312 nfs41_sequence_done(task, &data->res.seq_res);
9313
9314 switch (task->tk_status) {
9315 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009316 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009317 rpc_restart_call_prepare(task);
9318 }
9319}
9320
9321static void nfs41_free_stateid_release(void *calldata)
9322{
9323 kfree(calldata);
9324}
9325
Trond Myklebust17f26b12013-08-21 15:48:42 -04009326static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009327 .rpc_call_prepare = nfs41_free_stateid_prepare,
9328 .rpc_call_done = nfs41_free_stateid_done,
9329 .rpc_release = nfs41_free_stateid_release,
9330};
9331
Anna Schumaker2f261022018-05-15 13:03:39 -04009332/**
9333 * nfs41_free_stateid - perform a FREE_STATEID operation
9334 *
9335 * @server: server / transport on which to perform the operation
9336 * @stateid: state ID to release
9337 * @cred: credential
9338 * @is_recovery: set to true if this call needs to be privileged
9339 *
9340 * Note: this function is always asynchronous.
9341 */
9342static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009343 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009344 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009345 bool privileged)
9346{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009347 struct rpc_message msg = {
9348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009349 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009350 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009351 struct rpc_task_setup task_setup = {
9352 .rpc_client = server->client,
9353 .rpc_message = &msg,
9354 .callback_ops = &nfs41_free_stateid_ops,
9355 .flags = RPC_TASK_ASYNC,
9356 };
9357 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009358 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009359
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009360 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9361 &task_setup.rpc_client, &msg);
9362
Chuck Lever38527b12012-07-11 16:30:23 -04009363 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009364 data = kmalloc(sizeof(*data), GFP_NOFS);
9365 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009366 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009367 data->server = server;
9368 nfs4_stateid_copy(&data->args.stateid, stateid);
9369
9370 task_setup.callback_data = data;
9371
9372 msg.rpc_argp = &data->args;
9373 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009374 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009375 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009376 if (IS_ERR(task))
9377 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009378 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009379 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009380}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009381
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009382static void
9383nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009384{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009385 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009386
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009387 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009388 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009389}
9390
Trond Myklebust36281ca2012-03-04 18:13:56 -05009391static bool nfs41_match_stateid(const nfs4_stateid *s1,
9392 const nfs4_stateid *s2)
9393{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009394 if (s1->type != s2->type)
9395 return false;
9396
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009397 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009398 return false;
9399
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009400 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009401 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009402
Anna Schumaker045c5512017-01-11 16:59:48 -05009403 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009404}
9405
Andy Adamson557134a2009-04-01 09:21:53 -04009406#endif /* CONFIG_NFS_V4_1 */
9407
Trond Myklebust36281ca2012-03-04 18:13:56 -05009408static bool nfs4_match_stateid(const nfs4_stateid *s1,
9409 const nfs4_stateid *s2)
9410{
Trond Myklebustf597c532012-03-04 18:13:56 -05009411 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009412}
9413
9414
Trond Myklebust17280172012-03-11 13:11:00 -04009415static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009416 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009417 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418 .recover_open = nfs4_open_reclaim,
9419 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009420 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009421 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422};
9423
Andy Adamson591d71c2009-04-01 09:22:47 -04009424#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009425static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009426 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9427 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9428 .recover_open = nfs4_open_reclaim,
9429 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009430 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009431 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009432 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009433};
9434#endif /* CONFIG_NFS_V4_1 */
9435
Trond Myklebust17280172012-03-11 13:11:00 -04009436static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009437 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009438 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009439 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009441 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442};
9443
Andy Adamson591d71c2009-04-01 09:22:47 -04009444#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009445static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009446 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9447 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009448 .recover_open = nfs41_open_expired,
9449 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009450 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009451};
9452#endif /* CONFIG_NFS_V4_1 */
9453
Trond Myklebust17280172012-03-11 13:11:00 -04009454static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009455 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009456 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009457 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009458};
9459
9460#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009461static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009462 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009463 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009464 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009465};
9466#endif
9467
Chuck Leverec011fe2013-10-17 14:12:39 -04009468static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009469 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009470 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009471};
9472
9473#if defined(CONFIG_NFS_V4_1)
9474static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009475 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009476 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009477};
9478#endif /* CONFIG_NFS_V4_1 */
9479
Trond Myklebust97dc1352010-06-16 09:52:26 -04009480static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9481 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009482 .init_caps = NFS_CAP_READDIRPLUS
9483 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009484 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009485 .init_client = nfs40_init_client,
9486 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009487 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009488 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009489 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009490 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009491 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009492 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009493 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9494 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9495 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009496 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009497};
9498
9499#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009500static struct nfs_seqid *
9501nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9502{
9503 return NULL;
9504}
9505
Trond Myklebust97dc1352010-06-16 09:52:26 -04009506static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9507 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009508 .init_caps = NFS_CAP_READDIRPLUS
9509 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009510 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009511 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009512 | NFS_CAP_ATOMIC_OPEN_V1
9513 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009514 .init_client = nfs41_init_client,
9515 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009516 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009517 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009518 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009519 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009520 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009521 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009522 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009523 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9524 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9525 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009526 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009527};
9528#endif
9529
Steve Dickson42c2c422013-05-22 12:50:38 -04009530#if defined(CONFIG_NFS_V4_2)
9531static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9532 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009533 .init_caps = NFS_CAP_READDIRPLUS
9534 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009535 | NFS_CAP_POSIX_LOCK
9536 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009537 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009538 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009539 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009540 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009541 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009542 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009543 | NFS_CAP_LAYOUTSTATS
9544 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009545 .init_client = nfs41_init_client,
9546 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009547 .match_stateid = nfs41_match_stateid,
9548 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009549 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009550 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009551 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009552 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009553 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009554 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9555 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9556 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009557 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009558};
9559#endif
9560
Trond Myklebust97dc1352010-06-16 09:52:26 -04009561const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9562 [0] = &nfs_v4_0_minor_ops,
9563#if defined(CONFIG_NFS_V4_1)
9564 [1] = &nfs_v4_1_minor_ops,
9565#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009566#if defined(CONFIG_NFS_V4_2)
9567 [2] = &nfs_v4_2_minor_ops,
9568#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009569};
9570
Trond Myklebust13997822016-07-24 17:10:52 -04009571static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009572{
9573 ssize_t error, error2;
9574
9575 error = generic_listxattr(dentry, list, size);
9576 if (error < 0)
9577 return error;
9578 if (list) {
9579 list += error;
9580 size -= error;
9581 }
9582
9583 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9584 if (error2 < 0)
9585 return error2;
9586 return error + error2;
9587}
9588
Trond Myklebust17f26b12013-08-21 15:48:42 -04009589static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009590 .create = nfs_create,
9591 .lookup = nfs_lookup,
9592 .atomic_open = nfs_atomic_open,
9593 .link = nfs_link,
9594 .unlink = nfs_unlink,
9595 .symlink = nfs_symlink,
9596 .mkdir = nfs_mkdir,
9597 .rmdir = nfs_rmdir,
9598 .mknod = nfs_mknod,
9599 .rename = nfs_rename,
9600 .permission = nfs_permission,
9601 .getattr = nfs_getattr,
9602 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009603 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009604};
9605
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009606static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009607 .permission = nfs_permission,
9608 .getattr = nfs_getattr,
9609 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009610 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009611};
9612
David Howells509de812006-08-22 20:06:11 -04009613const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614 .version = 4, /* protocol version */
9615 .dentry_ops = &nfs4_dentry_operations,
9616 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009617 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009618 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009620 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009621 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622 .getattr = nfs4_proc_getattr,
9623 .setattr = nfs4_proc_setattr,
9624 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009625 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626 .access = nfs4_proc_access,
9627 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628 .create = nfs4_proc_create,
9629 .remove = nfs4_proc_remove,
9630 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009631 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009633 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009634 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009635 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 .link = nfs4_proc_link,
9637 .symlink = nfs4_proc_symlink,
9638 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009639 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 .readdir = nfs4_proc_readdir,
9641 .mknod = nfs4_proc_mknod,
9642 .statfs = nfs4_proc_statfs,
9643 .fsinfo = nfs4_proc_fsinfo,
9644 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009645 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009647 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009649 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009650 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009651 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009653 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009654 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009656 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009657 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009658 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009659 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009660 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009661 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009662 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009663 .create_server = nfs4_create_server,
9664 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665};
9666
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009667static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009668 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009669 .list = nfs4_xattr_list_nfs4_acl,
9670 .get = nfs4_xattr_get_nfs4_acl,
9671 .set = nfs4_xattr_set_nfs4_acl,
9672};
9673
9674const struct xattr_handler *nfs4_xattr_handlers[] = {
9675 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009676#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9677 &nfs4_xattr_nfs4_label_handler,
9678#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009679 NULL
9680};
9681
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682/*
9683 * Local variables:
9684 * c-basic-offset: 8
9685 * End:
9686 */