blob: 6dd146885da99304c8183f5fae21741f4aa3625f [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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebustc8bf7072018-06-15 16:31:02 -04003297 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003298 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003299 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003300
Chuck Levera3ca5652012-03-01 17:00:40 -05003301 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003302 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003303 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003304
Trond Myklebust88069f72009-12-08 08:33:16 -05003305 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003306 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003307 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3308 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3309 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003310 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003311 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003312 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003313 if (state->n_rdonly == 0)
3314 call_close |= is_rdonly;
3315 else if (is_rdonly)
3316 calldata->arg.fmode |= FMODE_READ;
3317 if (state->n_wronly == 0)
3318 call_close |= is_wronly;
3319 else if (is_wronly)
3320 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003321 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3322 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003323 } else if (is_rdwr)
3324 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3325
Trond Myklebust5cc78612016-11-14 11:19:56 -05003326 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003327 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003328 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003329 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003330
3331 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003332 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003333 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003334 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003335
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003336 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003337 nfs_release_seqid(calldata->arg.seqid);
3338 goto out_wait;
3339 }
3340
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003341 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3342 if (lo && !pnfs_layout_is_valid(lo)) {
3343 calldata->arg.lr_args = NULL;
3344 calldata->res.lr_res = NULL;
3345 }
3346
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003347 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003348 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003349
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003350 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003351 /* Close-to-open cache consistency revalidation */
3352 if (!nfs4_have_delegation(inode, FMODE_READ))
3353 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3354 else
3355 calldata->arg.bitmask = NULL;
3356 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003357
Trond Myklebust6ae37332015-01-30 14:21:14 -05003358 calldata->arg.share_access =
3359 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3360 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003361
Trond Myklebustd8d84982016-12-19 12:14:44 -05003362 if (calldata->res.fattr == NULL)
3363 calldata->arg.bitmask = NULL;
3364 else if (calldata->arg.bitmask == NULL)
3365 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003366 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003367 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003368 &calldata->arg.seq_args,
3369 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003370 task) != 0)
3371 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003372 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003373 return;
3374out_no_action:
3375 task->tk_action = NULL;
3376out_wait:
3377 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003380static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003381 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003382 .rpc_call_done = nfs4_close_done,
3383 .rpc_release = nfs4_free_closedata,
3384};
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386/*
3387 * It is possible for data to be read/written from a mem-mapped file
3388 * after the sys_close call (which hits the vfs layer as a flush).
3389 * This means that we can't safely call nfsv4 close on a file until
3390 * the inode is cleared. This in turn means that we are not good
3391 * NFSv4 citizens - we do not indicate to the server to update the file's
3392 * share state even when we are done with one of the three share
3393 * stateid's in the inode.
3394 *
3395 * NOTE: Caller must be holding the sp->so_owner semaphore!
3396 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003397int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003399 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003400 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003402 struct nfs4_state_owner *sp = state->owner;
3403 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003404 struct rpc_message msg = {
3405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3406 .rpc_cred = state->owner->so_cred,
3407 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003408 struct rpc_task_setup task_setup_data = {
3409 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003410 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003411 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003412 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003413 .flags = RPC_TASK_ASYNC,
3414 };
Trond Myklebust95121352005-10-18 14:20:12 -07003415 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003417 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3418 &task_setup_data.rpc_client, &msg);
3419
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003420 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003422 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003423 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003424 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003426 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003427 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3428 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003430 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3431 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003432 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003433 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003434 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003435 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003436 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003437 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003438 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003439 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003440 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003441 calldata->lr.roc = pnfs_roc(state->inode,
3442 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3443 if (calldata->lr.roc) {
3444 calldata->arg.lr_args = &calldata->lr.arg;
3445 calldata->res.lr_res = &calldata->lr.res;
3446 }
Al Viro643168c2011-06-22 18:20:23 -04003447 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003448
Trond Myklebust1174dd12010-12-21 10:52:24 -05003449 msg.rpc_argp = &calldata->arg;
3450 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003451 task_setup_data.callback_data = calldata;
3452 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003453 if (IS_ERR(task))
3454 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003455 status = 0;
3456 if (wait)
3457 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003458 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003459 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003460out_free_calldata:
3461 kfree(calldata);
3462out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003463 nfs4_put_open_state(state);
3464 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003465 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466}
3467
Trond Myklebust2b484292010-09-17 10:56:51 -04003468static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003469nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3470 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003473 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3474
3475 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Trond Myklebust565277f2007-10-15 18:17:53 -04003477 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003478 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003479
3480 nfs4_label_release_security(label);
3481
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003482 if (IS_ERR(state))
3483 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003484 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Trond Myklebust1185a552009-12-03 15:54:02 -05003487static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003488{
3489 if (ctx->state == NULL)
3490 return;
3491 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003492 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003493 else
Al Viro643168c2011-06-22 18:20:23 -04003494 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Trond Myklebustb944dba2013-11-04 15:20:20 -05003497#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3498#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003499#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3502{
Kinglong Mee8c612822015-08-26 21:12:58 +08003503 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003504 struct nfs4_server_caps_arg args = {
3505 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003506 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003507 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 struct nfs4_server_caps_res res = {};
3509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003511 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 .rpc_resp = &res,
3513 };
3514 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003515 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Kinglong Mee8c612822015-08-26 21:12:58 +08003517 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3518 FATTR4_WORD0_FH_EXPIRE_TYPE |
3519 FATTR4_WORD0_LINK_SUPPORT |
3520 FATTR4_WORD0_SYMLINK_SUPPORT |
3521 FATTR4_WORD0_ACLSUPPORT;
3522 if (minorversion)
3523 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3524
Bryan Schumaker7c513052011-03-24 17:12:24 +00003525 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003527 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003528 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003529 case 0:
3530 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3531 res.attr_bitmask[2] = 0;
3532 break;
3533 case 1:
3534 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3535 break;
3536 case 2:
3537 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003540 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3541 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3542 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3543 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003544 NFS_CAP_CTIME|NFS_CAP_MTIME|
3545 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003546 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3547 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 server->caps |= NFS_CAP_ACLS;
3549 if (res.has_links != 0)
3550 server->caps |= NFS_CAP_HARDLINKS;
3551 if (res.has_symlinks != 0)
3552 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003553 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3554 server->caps |= NFS_CAP_FILEID;
3555 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3556 server->caps |= NFS_CAP_MODE;
3557 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3558 server->caps |= NFS_CAP_NLINK;
3559 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3560 server->caps |= NFS_CAP_OWNER;
3561 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3562 server->caps |= NFS_CAP_OWNER_GROUP;
3563 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3564 server->caps |= NFS_CAP_ATIME;
3565 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3566 server->caps |= NFS_CAP_CTIME;
3567 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3568 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003569#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3570 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3571 server->caps |= NFS_CAP_SECURITY_LABEL;
3572#endif
3573 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3574 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003575 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003576
Trond Myklebusta65318b2009-03-11 14:10:28 -04003577 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3578 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3579 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003580 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003581
3582 /* Avoid a regression due to buggy server */
3583 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3584 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003585 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3586 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003589 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return status;
3593}
3594
Trond Myklebust55a97592006-06-09 09:34:19 -04003595int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
3597 struct nfs4_exception exception = { };
3598 int err;
3599 do {
3600 err = nfs4_handle_exception(server,
3601 _nfs4_server_capabilities(server, fhandle),
3602 &exception);
3603 } while (exception.retry);
3604 return err;
3605}
3606
3607static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3608 struct nfs_fsinfo *info)
3609{
David Quigleyaa9c2662013-05-22 12:50:44 -04003610 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003612 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 };
3614 struct nfs4_lookup_res res = {
3615 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003616 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 .fh = fhandle,
3618 };
3619 struct rpc_message msg = {
3620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3621 .rpc_argp = &args,
3622 .rpc_resp = &res,
3623 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003624
David Quigleyaa9c2662013-05-22 12:50:44 -04003625 bitmask[0] = nfs4_fattr_bitmap[0];
3626 bitmask[1] = nfs4_fattr_bitmap[1];
3627 /*
3628 * Process the label in the upcoming getfattr
3629 */
3630 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3631
Trond Myklebust0e574af2005-10-27 22:12:38 -04003632 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003633 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
3636static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3637 struct nfs_fsinfo *info)
3638{
3639 struct nfs4_exception exception = { };
3640 int err;
3641 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003642 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003643 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003644 switch (err) {
3645 case 0:
3646 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003647 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003648 default:
3649 err = nfs4_handle_exception(server, err, &exception);
3650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003652out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 return err;
3654}
3655
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003656static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3657 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3658{
Trond Myklebustc2190662013-08-26 19:23:04 -04003659 struct rpc_auth_create_args auth_args = {
3660 .pseudoflavor = flavor,
3661 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003662 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003663
Trond Myklebustc2190662013-08-26 19:23:04 -04003664 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003665 if (IS_ERR(auth))
3666 return -EACCES;
3667 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003668}
3669
Chuck Lever9a744ba2013-03-16 15:56:02 -04003670/*
3671 * Retry pseudoroot lookup with various security flavors. We do this when:
3672 *
3673 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3674 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3675 *
3676 * Returns zero on success, or a negative NFS4ERR value, or a
3677 * negative errno value.
3678 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003679static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003680 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003682 /* Per 3530bis 15.33.5 */
3683 static const rpc_authflavor_t flav_array[] = {
3684 RPC_AUTH_GSS_KRB5P,
3685 RPC_AUTH_GSS_KRB5I,
3686 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003687 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003688 RPC_AUTH_NULL,
3689 };
3690 int status = -EPERM;
3691 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003693 if (server->auth_info.flavor_len > 0) {
3694 /* try each flavor specified by user */
3695 for (i = 0; i < server->auth_info.flavor_len; i++) {
3696 status = nfs4_lookup_root_sec(server, fhandle, info,
3697 server->auth_info.flavors[i]);
3698 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3699 continue;
3700 break;
3701 }
3702 } else {
3703 /* no flavors specified by user, try default list */
3704 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3705 status = nfs4_lookup_root_sec(server, fhandle, info,
3706 flav_array[i]);
3707 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3708 continue;
3709 break;
3710 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003711 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003712
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003713 /*
3714 * -EACCESS could mean that the user doesn't have correct permissions
3715 * to access the mount. It could also mean that we tried to mount
3716 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3717 * existing mount programs don't handle -EACCES very well so it should
3718 * be mapped to -EPERM instead.
3719 */
3720 if (status == -EACCES)
3721 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003722 return status;
3723}
3724
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003725/**
3726 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3727 * @server: initialized nfs_server handle
3728 * @fhandle: we fill in the pseudo-fs root file handle
3729 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003730 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003731 *
3732 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003733 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003734int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003735 struct nfs_fsinfo *info,
3736 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003737{
Andre Przywarac7757072015-04-23 17:17:40 +01003738 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003739
Andre Przywarac7757072015-04-23 17:17:40 +01003740 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003741 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003742
3743 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003744 status = server->nfs_client->cl_mvops->find_root_sec(server,
3745 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 if (status == 0)
3748 status = nfs4_server_capabilities(server, fhandle);
3749 if (status == 0)
3750 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003751
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003752 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
Bryan Schumakerbae36242012-05-10 15:07:31 -04003755static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3756 struct nfs_fsinfo *info)
3757{
3758 int error;
3759 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003760 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003761
3762 error = nfs4_server_capabilities(server, mntfh);
3763 if (error < 0) {
3764 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3765 return error;
3766 }
3767
David Quigley14c43f72013-05-22 12:50:43 -04003768 label = nfs4_label_alloc(server, GFP_KERNEL);
3769 if (IS_ERR(label))
3770 return PTR_ERR(label);
3771
Trond Myklebusta841b542018-04-07 13:50:59 -04003772 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003773 if (error < 0) {
3774 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003775 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003776 }
3777
3778 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3779 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3780 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3781
David Quigley14c43f72013-05-22 12:50:43 -04003782err_free_label:
3783 nfs4_label_free(label);
3784
Bryan Schumakerbae36242012-05-10 15:07:31 -04003785 return error;
3786}
3787
Manoj Naik6b97fd32006-06-09 09:34:29 -04003788/*
3789 * Get locations and (maybe) other attributes of a referral.
3790 * Note that we'll actually follow the referral later when
3791 * we detect fsid mismatch in inode revalidation
3792 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003793static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3794 const struct qstr *name, struct nfs_fattr *fattr,
3795 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003796{
3797 int status = -ENOMEM;
3798 struct page *page = NULL;
3799 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003800
3801 page = alloc_page(GFP_KERNEL);
3802 if (page == NULL)
3803 goto out;
3804 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3805 if (locations == NULL)
3806 goto out;
3807
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003808 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003809 if (status != 0)
3810 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003811
3812 /*
3813 * If the fsid didn't change, this is a migration event, not a
3814 * referral. Cause us to drop into the exception handler, which
3815 * will kick off migration recovery.
3816 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003817 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003818 dprintk("%s: server did not return a different fsid for"
3819 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003820 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003821 goto out;
3822 }
Andy Adamson533eb462011-06-13 18:25:56 -04003823 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3824 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003825
Andy Adamson533eb462011-06-13 18:25:56 -04003826 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003827 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003828 memset(fhandle, 0, sizeof(struct nfs_fh));
3829out:
3830 if (page)
3831 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003832 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003833 return status;
3834}
3835
David Quigley1775fd32013-05-22 12:50:42 -04003836static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003837 struct nfs_fattr *fattr, struct nfs4_label *label,
3838 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Trond Myklebust771734f2018-04-07 13:54:23 -04003840 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 struct nfs4_getattr_arg args = {
3842 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003843 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 };
3845 struct nfs4_getattr_res res = {
3846 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003847 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 .server = server,
3849 };
3850 struct rpc_message msg = {
3851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3852 .rpc_argp = &args,
3853 .rpc_resp = &res,
3854 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003855
Trond Myklebust771734f2018-04-07 13:54:23 -04003856 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003857
Trond Myklebust0e574af2005-10-27 22:12:38 -04003858 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003859 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
David Quigley1775fd32013-05-22 12:50:42 -04003862static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003863 struct nfs_fattr *fattr, struct nfs4_label *label,
3864 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
3866 struct nfs4_exception exception = { };
3867 int err;
3868 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003869 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003870 trace_nfs4_getattr(server, fhandle, fattr, err);
3871 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 &exception);
3873 } while (exception.retry);
3874 return err;
3875}
3876
3877/*
3878 * The file is not closed if it is opened due to the a request to change
3879 * the size of the file. The open call will not be needed once the
3880 * VFS layer lookup-intents are implemented.
3881 *
3882 * Close is called when the inode is destroyed.
3883 * If we haven't opened the file for O_WRONLY, we
3884 * need to in the size_change case to obtain a stateid.
3885 *
3886 * Got race?
3887 * Because OPEN is always done by name in nfsv4, it is
3888 * possible that we opened a different file by the same
3889 * name. We can recognize this race condition, but we
3890 * can't do anything about it besides returning an error.
3891 *
3892 * This will be fixed with VFS changes (lookup-intent).
3893 */
3894static int
3895nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3896 struct iattr *sattr)
3897{
David Howells2b0143b2015-03-17 22:25:59 +00003898 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003899 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003900 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003901 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 int status;
3903
Peng Tao88ac8152014-09-12 11:04:10 +08003904 if (pnfs_ld_layoutret_on_setattr(inode) &&
3905 sattr->ia_valid & ATTR_SIZE &&
3906 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003907 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003908
Trond Myklebust0e574af2005-10-27 22:12:38 -04003909 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Andy Adamson26699402012-05-30 16:12:24 -04003911 /* Deal with open(O_TRUNC) */
3912 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003913 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003914
3915 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003916 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003917 return 0;
3918
Trond Myklebustd5308382005-11-04 15:33:38 -05003919 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003920 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003921
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003922 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003923 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003924 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003925 }
3926
David Quigley14c43f72013-05-22 12:50:43 -04003927 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3928 if (IS_ERR(label))
3929 return PTR_ERR(label);
3930
Trond Myklebust199366f2018-03-20 16:43:18 -04003931 /* Return any delegations if we're going to change ACLs */
3932 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003933 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003934
NeilBrown29b59f92016-10-13 15:26:47 +11003935 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003936 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003937 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003938 nfs_setsecurity(inode, fattr, label);
3939 }
David Quigley14c43f72013-05-22 12:50:43 -04003940 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return status;
3942}
3943
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003944static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3945 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003946 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003947{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003948 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003949 int status;
3950 struct nfs4_lookup_arg args = {
3951 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003952 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003953 .name = name,
3954 };
3955 struct nfs4_lookup_res res = {
3956 .server = server,
3957 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003958 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003959 .fh = fhandle,
3960 };
3961 struct rpc_message msg = {
3962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3963 .rpc_argp = &args,
3964 .rpc_resp = &res,
3965 };
3966
David Quigleyaa9c2662013-05-22 12:50:44 -04003967 args.bitmask = nfs4_bitmask(server, label);
3968
David Howells2b3de442006-08-22 20:06:09 -04003969 nfs_fattr_init(fattr);
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003972 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 dprintk("NFS reply lookup: %d\n", status);
3974 return status;
3975}
3976
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003977static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003978{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003979 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003980 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003981 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3982 fattr->nlink = 2;
3983}
3984
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003985static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003986 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003987 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003988{
3989 struct nfs4_exception exception = { };
3990 struct rpc_clnt *client = *clnt;
3991 int err;
3992 do {
David Quigley1775fd32013-05-22 12:50:42 -04003993 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003994 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003995 switch (err) {
3996 case -NFS4ERR_BADNAME:
3997 err = -ENOENT;
3998 goto out;
3999 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004000 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004001 if (err == -NFS4ERR_MOVED)
4002 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004003 goto out;
4004 case -NFS4ERR_WRONGSEC:
4005 err = -EPERM;
4006 if (client != *clnt)
4007 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004008 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004009 if (IS_ERR(client))
4010 return PTR_ERR(client);
4011
4012 exception.retry = 1;
4013 break;
4014 default:
4015 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4016 }
4017 } while (exception.retry);
4018
4019out:
4020 if (err == 0)
4021 *clnt = client;
4022 else if (client != *clnt)
4023 rpc_shutdown_client(client);
4024
4025 return err;
4026}
4027
Al Virobeffb8f2016-07-20 16:34:42 -04004028static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004029 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4030 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004032 int status;
4033 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004034
David Quigley1775fd32013-05-22 12:50:42 -04004035 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004036 if (client != NFS_CLIENT(dir)) {
4037 rpc_shutdown_client(client);
4038 nfs_fixup_secinfo_attributes(fattr);
4039 }
4040 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004043struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004044nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004045 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4046{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004047 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004048 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004049
David Quigley1775fd32013-05-22 12:50:42 -04004050 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004051 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004052 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004053 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004054}
4055
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004056static int _nfs4_proc_lookupp(struct inode *inode,
4057 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4058 struct nfs4_label *label)
4059{
4060 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4061 struct nfs_server *server = NFS_SERVER(inode);
4062 int status;
4063 struct nfs4_lookupp_arg args = {
4064 .bitmask = server->attr_bitmask,
4065 .fh = NFS_FH(inode),
4066 };
4067 struct nfs4_lookupp_res res = {
4068 .server = server,
4069 .fattr = fattr,
4070 .label = label,
4071 .fh = fhandle,
4072 };
4073 struct rpc_message msg = {
4074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4075 .rpc_argp = &args,
4076 .rpc_resp = &res,
4077 };
4078
4079 args.bitmask = nfs4_bitmask(server, label);
4080
4081 nfs_fattr_init(fattr);
4082
4083 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4084 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4085 &res.seq_res, 0);
4086 dprintk("NFS reply lookupp: %d\n", status);
4087 return status;
4088}
4089
4090static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4091 struct nfs_fattr *fattr, struct nfs4_label *label)
4092{
4093 struct nfs4_exception exception = { };
4094 int err;
4095 do {
4096 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4097 trace_nfs4_lookupp(inode, err);
4098 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4099 &exception);
4100 } while (exception.retry);
4101 return err;
4102}
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4105{
Trond Myklebust76b32992007-08-10 17:45:11 -04004106 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 struct nfs4_accessargs args = {
4108 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004109 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004111 struct nfs4_accessres res = {
4112 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 struct rpc_message msg = {
4115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4116 .rpc_argp = &args,
4117 .rpc_resp = &res,
4118 .rpc_cred = entry->cred,
4119 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004120 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Trond Myklebust7c672652018-06-04 15:00:53 -04004122 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004123 res.fattr = nfs_alloc_fattr();
4124 if (res.fattr == NULL)
4125 return -ENOMEM;
4126 args.bitmask = server->cache_consistency_bitmask;
4127 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004128
Bryan Schumaker7c513052011-03-24 17:12:24 +00004129 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004131 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004132 if (res.fattr)
4133 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004135 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return status;
4137}
4138
4139static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4140{
4141 struct nfs4_exception exception = { };
4142 int err;
4143 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004144 err = _nfs4_proc_access(inode, entry);
4145 trace_nfs4_access(inode, err);
4146 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 &exception);
4148 } while (exception.retry);
4149 return err;
4150}
4151
4152/*
4153 * TODO: For the time being, we don't try to get any attributes
4154 * along with any of the zero-copy operations READ, READDIR,
4155 * READLINK, WRITE.
4156 *
4157 * In the case of the first three, we want to put the GETATTR
4158 * after the read-type operation -- this is because it is hard
4159 * to predict the length of a GETATTR response in v4, and thus
4160 * align the READ data correctly. This means that the GETATTR
4161 * may end up partially falling into the page cache, and we should
4162 * shift it into the 'tail' of the xdr_buf before processing.
4163 * To do this efficiently, we need to know the total length
4164 * of data received, which doesn't seem to be available outside
4165 * of the RPC layer.
4166 *
4167 * In the case of WRITE, we also want to put the GETATTR after
4168 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004169 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 *
4171 * Both of these changes to the XDR layer would in fact be quite
4172 * minor, but I decided to leave them for a subsequent patch.
4173 */
4174static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4175 unsigned int pgbase, unsigned int pglen)
4176{
4177 struct nfs4_readlink args = {
4178 .fh = NFS_FH(inode),
4179 .pgbase = pgbase,
4180 .pglen = pglen,
4181 .pages = &page,
4182 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004183 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4186 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004187 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 };
4189
Bryan Schumaker7c513052011-03-24 17:12:24 +00004190 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 -07004191}
4192
4193static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4194 unsigned int pgbase, unsigned int pglen)
4195{
4196 struct nfs4_exception exception = { };
4197 int err;
4198 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004199 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4200 trace_nfs4_readlink(inode, err);
4201 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 &exception);
4203 } while (exception.retry);
4204 return err;
4205}
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004208 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210static int
4211nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004212 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004214 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004215 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004216 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 int status = 0;
4219
NeilBrown532d4de2016-10-13 15:26:47 +11004220 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004221 if (IS_ERR(ctx))
4222 return PTR_ERR(ctx);
4223
David Quigleyaa9c2662013-05-22 12:50:44 -04004224 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4225
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004226 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4227 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004228 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 if (IS_ERR(state)) {
4230 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004234 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004235 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return status;
4237}
4238
Al Virobeffb8f2016-07-20 16:34:42 -04004239static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
Trond Myklebust16e42952005-10-27 22:12:44 -04004241 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004242 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004244 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004246 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004247 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004248 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4251 .rpc_argp = &args,
4252 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004254 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004255 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004256
Bryan Schumaker7c513052011-03-24 17:12:24 +00004257 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004258 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004259 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return status;
4261}
4262
Trond Myklebust912678d2018-03-20 16:43:15 -04004263static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4264{
4265 struct nfs4_exception exception = { };
4266 struct inode *inode = d_inode(dentry);
4267 int err;
4268
Trond Myklebustc01d3642018-03-20 16:43:20 -04004269 if (inode) {
4270 if (inode->i_nlink == 1)
4271 nfs4_inode_return_delegation(inode);
4272 else
4273 nfs4_inode_make_writeable(inode);
4274 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004275 do {
4276 err = _nfs4_proc_remove(dir, &dentry->d_name);
4277 trace_nfs4_remove(dir, &dentry->d_name, err);
4278 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4279 &exception);
4280 } while (exception.retry);
4281 return err;
4282}
4283
4284static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
4286 struct nfs4_exception exception = { };
4287 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004290 err = _nfs4_proc_remove(dir, name);
4291 trace_nfs4_remove(dir, name, err);
4292 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 &exception);
4294 } while (exception.retry);
4295 return err;
4296}
4297
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004298static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4299 struct dentry *dentry,
4300 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004302 struct nfs_removeargs *args = msg->rpc_argp;
4303 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Trond Myklebust977fcc22018-03-20 16:43:17 -04004305 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004307 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004308
4309 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004310
4311 if (inode)
4312 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313}
4314
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004315static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4316{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004317 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004318 &data->args.seq_args,
4319 &data->res.seq_res,
4320 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}
4322
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004323static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004325 struct nfs_unlinkdata *data = task->tk_calldata;
4326 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004327
Trond Myklebust14516c32010-07-31 14:29:06 -04004328 if (!nfs4_sequence_done(task, &res->seq_res))
4329 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004330 if (nfs4_async_handle_error(task, res->server, NULL,
4331 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004332 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004333 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004334 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004335 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336}
4337
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004338static void nfs4_proc_rename_setup(struct rpc_message *msg,
4339 struct dentry *old_dentry,
4340 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004341{
Jeff Laytond3d41522010-09-17 17:31:57 -04004342 struct nfs_renameargs *arg = msg->rpc_argp;
4343 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004344 struct inode *old_inode = d_inode(old_dentry);
4345 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004346
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004347 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004348 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004349 if (new_inode)
4350 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004352 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004353 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004354}
4355
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004356static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4357{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004358 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004359 &data->args.seq_args,
4360 &data->res.seq_res,
4361 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004362}
4363
4364static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4365 struct inode *new_dir)
4366{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004367 struct nfs_renamedata *data = task->tk_calldata;
4368 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004369
4370 if (!nfs4_sequence_done(task, &res->seq_res))
4371 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004372 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004373 return 0;
4374
Trond Myklebustc733c492017-01-11 12:32:26 -05004375 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004376 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004377 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004378 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004379 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004380 return 1;
4381}
4382
Al Virobeffb8f2016-07-20 16:34:42 -04004383static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004385 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004386 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 struct nfs4_link_arg arg = {
4388 .fh = NFS_FH(inode),
4389 .dir_fh = NFS_FH(dir),
4390 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004391 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004393 struct nfs4_link_res res = {
4394 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004395 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004396 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4399 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004400 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004402 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Trond Myklebust136f2622010-04-16 16:22:49 -04004404 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004405 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004406 goto out;
4407
David Quigley14c43f72013-05-22 12:50:43 -04004408 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4409 if (IS_ERR(res.label)) {
4410 status = PTR_ERR(res.label);
4411 goto out;
4412 }
4413
Trond Myklebustc01d3642018-03-20 16:43:20 -04004414 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004415 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004416
Bryan Schumaker7c513052011-03-24 17:12:24 +00004417 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004418 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004419 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004420 status = nfs_post_op_update_inode(inode, res.fattr);
4421 if (!status)
4422 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004423 }
David Quigley14c43f72013-05-22 12:50:43 -04004424
4425
4426 nfs4_label_free(res.label);
4427
Trond Myklebust136f2622010-04-16 16:22:49 -04004428out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004429 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 return status;
4431}
4432
Al Virobeffb8f2016-07-20 16:34:42 -04004433static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
4435 struct nfs4_exception exception = { };
4436 int err;
4437 do {
4438 err = nfs4_handle_exception(NFS_SERVER(inode),
4439 _nfs4_proc_link(inode, dir, name),
4440 &exception);
4441 } while (exception.retry);
4442 return err;
4443}
4444
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445struct nfs4_createdata {
4446 struct rpc_message msg;
4447 struct nfs4_create_arg arg;
4448 struct nfs4_create_res res;
4449 struct nfs_fh fh;
4450 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004451 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004452};
4453
4454static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004455 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004456{
4457 struct nfs4_createdata *data;
4458
4459 data = kzalloc(sizeof(*data), GFP_KERNEL);
4460 if (data != NULL) {
4461 struct nfs_server *server = NFS_SERVER(dir);
4462
David Quigley14c43f72013-05-22 12:50:43 -04004463 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4464 if (IS_ERR(data->label))
4465 goto out_free;
4466
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004467 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4468 data->msg.rpc_argp = &data->arg;
4469 data->msg.rpc_resp = &data->res;
4470 data->arg.dir_fh = NFS_FH(dir);
4471 data->arg.server = server;
4472 data->arg.name = name;
4473 data->arg.attrs = sattr;
4474 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004475 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004476 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004477 data->res.server = server;
4478 data->res.fh = &data->fh;
4479 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004480 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004481 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004482 }
4483 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004484out_free:
4485 kfree(data);
4486 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004487}
4488
4489static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4490{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004491 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004492 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004493 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004494 update_changeattr(dir, &data->res.dir_cinfo,
4495 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004496 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004497 }
4498 return status;
4499}
4500
4501static void nfs4_free_createdata(struct nfs4_createdata *data)
4502{
David Quigley14c43f72013-05-22 12:50:43 -04004503 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004504 kfree(data);
4505}
4506
Chuck Lever4f390c12006-08-22 20:06:22 -04004507static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004508 struct page *page, unsigned int len, struct iattr *sattr,
4509 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004511 struct nfs4_createdata *data;
4512 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Chuck Lever94a6d752006-08-22 20:06:23 -04004514 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004515 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004516
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004517 status = -ENOMEM;
4518 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4519 if (data == NULL)
4520 goto out;
4521
4522 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4523 data->arg.u.symlink.pages = &page;
4524 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004525 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004527 status = nfs4_do_create(dir, dentry, data);
4528
4529 nfs4_free_createdata(data);
4530out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return status;
4532}
4533
Chuck Lever4f390c12006-08-22 20:06:22 -04004534static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004535 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
4537 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004538 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004540
4541 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004544 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4545 trace_nfs4_symlink(dir, &dentry->d_name, err);
4546 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 &exception);
4548 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004549
4550 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return err;
4552}
4553
4554static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004555 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004557 struct nfs4_createdata *data;
4558 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004560 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4561 if (data == NULL)
4562 goto out;
4563
David Quigley1775fd32013-05-22 12:50:42 -04004564 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004565 status = nfs4_do_create(dir, dentry, data);
4566
4567 nfs4_free_createdata(data);
4568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 return status;
4570}
4571
4572static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4573 struct iattr *sattr)
4574{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004575 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004577 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004579
David Quigleyaa9c2662013-05-22 12:50:44 -04004580 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4581
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004582 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4583 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004585 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4586 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4587 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 &exception);
4589 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004590 nfs4_label_release_security(label);
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 return err;
4593}
4594
4595static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004596 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
David Howells2b0143b2015-03-17 22:25:59 +00004598 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 struct nfs4_readdir_arg args = {
4600 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004601 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 .pgbase = 0,
4603 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004604 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004605 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 };
4607 struct nfs4_readdir_res res;
4608 struct rpc_message msg = {
4609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4610 .rpc_argp = &args,
4611 .rpc_resp = &res,
4612 .rpc_cred = cred,
4613 };
4614 int status;
4615
Al Viro6de14722013-09-16 10:53:17 -04004616 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4617 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004618 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004619 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004621 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 -05004622 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004623 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004624 status += args.pgbase;
4625 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004626
4627 nfs_invalidate_atime(dir);
4628
Harvey Harrison3110ff82008-05-02 13:42:44 -07004629 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return status;
4631}
4632
4633static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004634 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
4636 struct nfs4_exception exception = { };
4637 int err;
4638 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004639 err = _nfs4_proc_readdir(dentry, cred, cookie,
4640 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004641 trace_nfs4_readdir(d_inode(dentry), err);
4642 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 &exception);
4644 } while (exception.retry);
4645 return err;
4646}
4647
4648static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004649 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004651 struct nfs4_createdata *data;
4652 int mode = sattr->ia_mode;
4653 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004655 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4656 if (data == NULL)
4657 goto out;
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004660 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004662 data->arg.ftype = NF4BLK;
4663 data->arg.u.device.specdata1 = MAJOR(rdev);
4664 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004667 data->arg.ftype = NF4CHR;
4668 data->arg.u.device.specdata1 = MAJOR(rdev);
4669 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004670 } else if (!S_ISSOCK(mode)) {
4671 status = -EINVAL;
4672 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
David Quigley1775fd32013-05-22 12:50:42 -04004674
David Quigleyaa9c2662013-05-22 12:50:44 -04004675 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004676 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004677out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004678 nfs4_free_createdata(data);
4679out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return status;
4681}
4682
4683static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4684 struct iattr *sattr, dev_t rdev)
4685{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004686 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004688 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004690
David Quigleyaa9c2662013-05-22 12:50:44 -04004691 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4692
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004693 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4694 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004696 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4697 trace_nfs4_mknod(dir, &dentry->d_name, err);
4698 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 &exception);
4700 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004701
4702 nfs4_label_release_security(label);
4703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 return err;
4705}
4706
4707static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4708 struct nfs_fsstat *fsstat)
4709{
4710 struct nfs4_statfs_arg args = {
4711 .fh = fhandle,
4712 .bitmask = server->attr_bitmask,
4713 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004714 struct nfs4_statfs_res res = {
4715 .fsstat = fsstat,
4716 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 struct rpc_message msg = {
4718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4719 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004720 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 };
4722
Trond Myklebust0e574af2005-10-27 22:12:38 -04004723 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
4727static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4728{
4729 struct nfs4_exception exception = { };
4730 int err;
4731 do {
4732 err = nfs4_handle_exception(server,
4733 _nfs4_proc_statfs(server, fhandle, fsstat),
4734 &exception);
4735 } while (exception.retry);
4736 return err;
4737}
4738
4739static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4740 struct nfs_fsinfo *fsinfo)
4741{
4742 struct nfs4_fsinfo_arg args = {
4743 .fh = fhandle,
4744 .bitmask = server->attr_bitmask,
4745 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004746 struct nfs4_fsinfo_res res = {
4747 .fsinfo = fsinfo,
4748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 struct rpc_message msg = {
4750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4751 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004752 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 };
4754
Bryan Schumaker7c513052011-03-24 17:12:24 +00004755 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
4757
4758static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4759{
4760 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004761 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 int err;
4763
4764 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004765 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004766 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004767 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004768 nfs4_set_lease_period(server->nfs_client,
4769 fsinfo->lease_time * HZ,
4770 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004771 break;
4772 }
4773 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 } while (exception.retry);
4775 return err;
4776}
4777
4778static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4779{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004780 int error;
4781
Trond Myklebust0e574af2005-10-27 22:12:38 -04004782 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004783 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004784 if (error == 0) {
4785 /* block layout checks this! */
4786 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004787 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004788 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004789
4790 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
4793static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4794 struct nfs_pathconf *pathconf)
4795{
4796 struct nfs4_pathconf_arg args = {
4797 .fh = fhandle,
4798 .bitmask = server->attr_bitmask,
4799 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004800 struct nfs4_pathconf_res res = {
4801 .pathconf = pathconf,
4802 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 struct rpc_message msg = {
4804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4805 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004806 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 };
4808
4809 /* None of the pathconf attributes are mandatory to implement */
4810 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4811 memset(pathconf, 0, sizeof(*pathconf));
4812 return 0;
4813 }
4814
Trond Myklebust0e574af2005-10-27 22:12:38 -04004815 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004816 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818
4819static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4820 struct nfs_pathconf *pathconf)
4821{
4822 struct nfs4_exception exception = { };
4823 int err;
4824
4825 do {
4826 err = nfs4_handle_exception(server,
4827 _nfs4_proc_pathconf(server, fhandle, pathconf),
4828 &exception);
4829 } while (exception.retry);
4830 return err;
4831}
4832
Trond Myklebust5521abf2013-03-16 20:54:34 -04004833int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004834 const struct nfs_open_context *ctx,
4835 const struct nfs_lock_context *l_ctx,
4836 fmode_t fmode)
4837{
NeilBrown17393472016-10-13 15:26:47 +11004838 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004839}
4840EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4841
Trond Myklebust5521abf2013-03-16 20:54:34 -04004842static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4843 const struct nfs_open_context *ctx,
4844 const struct nfs_lock_context *l_ctx,
4845 fmode_t fmode)
4846{
4847 nfs4_stateid current_stateid;
4848
Trond Myklebuste1253be2014-03-05 08:44:23 -05004849 /* If the current stateid represents a lost lock, then exit */
4850 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4851 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004852 return nfs4_stateid_match(stateid, &current_stateid);
4853}
4854
4855static bool nfs4_error_stateid_expired(int err)
4856{
4857 switch (err) {
4858 case -NFS4ERR_DELEG_REVOKED:
4859 case -NFS4ERR_ADMIN_REVOKED:
4860 case -NFS4ERR_BAD_STATEID:
4861 case -NFS4ERR_STALE_STATEID:
4862 case -NFS4ERR_OLD_STATEID:
4863 case -NFS4ERR_OPENMODE:
4864 case -NFS4ERR_EXPIRED:
4865 return true;
4866 }
4867 return false;
4868}
4869
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004870static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004872 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004874 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004875 if (task->tk_status < 0) {
4876 struct nfs4_exception exception = {
4877 .inode = hdr->inode,
4878 .state = hdr->args.context->state,
4879 .stateid = &hdr->args.stateid,
4880 };
4881 task->tk_status = nfs4_async_handle_exception(task,
4882 server, task->tk_status, &exception);
4883 if (exception.retry) {
4884 rpc_restart_call_prepare(task);
4885 return -EAGAIN;
4886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004890 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Trond Myklebust5521abf2013-03-16 20:54:34 -04004894static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004895 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004896{
4897
4898 if (!nfs4_error_stateid_expired(task->tk_status) ||
4899 nfs4_stateid_is_current(&args->stateid,
4900 args->context,
4901 args->lock_context,
4902 FMODE_READ))
4903 return false;
4904 rpc_restart_call_prepare(task);
4905 return true;
4906}
4907
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004909{
4910
4911 dprintk("--> %s\n", __func__);
4912
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004913 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004914 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004915 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004916 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004917 if (task->tk_status > 0)
4918 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004919 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4920 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004921}
4922
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004923static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4924 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004926 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004927 if (!hdr->pgio_done_cb)
4928 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004930 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004933static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4934 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004935{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004936 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004937 &hdr->args.seq_args,
4938 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004939 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004940 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004941 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4942 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004943 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004944 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004945 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004946 return -EIO;
4947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948}
4949
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004950static int nfs4_write_done_cb(struct rpc_task *task,
4951 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004953 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004954
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004955 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004956 if (task->tk_status < 0) {
4957 struct nfs4_exception exception = {
4958 .inode = hdr->inode,
4959 .state = hdr->args.context->state,
4960 .stateid = &hdr->args.stateid,
4961 };
4962 task->tk_status = nfs4_async_handle_exception(task,
4963 NFS_SERVER(inode), task->tk_status,
4964 &exception);
4965 if (exception.retry) {
4966 rpc_restart_call_prepare(task);
4967 return -EAGAIN;
4968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004970 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004971 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004972 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004973 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
4976
Trond Myklebust5521abf2013-03-16 20:54:34 -04004977static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004978 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004979{
4980
4981 if (!nfs4_error_stateid_expired(task->tk_status) ||
4982 nfs4_stateid_is_current(&args->stateid,
4983 args->context,
4984 args->lock_context,
4985 FMODE_WRITE))
4986 return false;
4987 rpc_restart_call_prepare(task);
4988 return true;
4989}
4990
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004991static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004992{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004993 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004994 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004995 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004996 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004997 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4998 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004999}
5000
Trond Myklebust5a37f852012-04-28 14:55:16 -04005001static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005002bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005003{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005004 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005005 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005006 return false;
5007 /* Otherwise, request attributes if and only if we don't hold
5008 * a delegation
5009 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005010 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005011}
Fred Isamana69aef12011-03-03 15:13:47 +00005012
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005013static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005014 struct rpc_message *msg,
5015 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005017 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005018
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005019 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5020 hdr->args.bitmask = NULL;
5021 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005022 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005023 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005024
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005025 if (!hdr->pgio_done_cb)
5026 hdr->pgio_done_cb = nfs4_write_done_cb;
5027 hdr->res.server = server;
5028 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005030 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005031 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005032 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
Fred Isaman0b7c0152012-04-20 14:47:39 -04005035static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5036{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005037 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005038 &data->args.seq_args,
5039 &data->res.seq_res,
5040 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041}
5042
Fred Isaman0b7c0152012-04-20 14:47:39 -04005043static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005046
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005047 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005048 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5049 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005050 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005051 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
Fred Isaman0b7c0152012-04-20 14:47:39 -04005056static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005057{
5058 if (!nfs4_sequence_done(task, &data->res.seq_res))
5059 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005060 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005061}
5062
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005063static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5064 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005066 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005067
Fred Isaman0b7c0152012-04-20 14:47:39 -04005068 if (data->commit_done_cb == NULL)
5069 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005070 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005071 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005072 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005073 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074}
5075
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005076struct nfs4_renewdata {
5077 struct nfs_client *client;
5078 unsigned long timestamp;
5079};
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081/*
5082 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5083 * standalone procedure for queueing an asynchronous RENEW.
5084 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005085static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005086{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005087 struct nfs4_renewdata *data = calldata;
5088 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005089
Elena Reshetova212bf412017-10-20 12:53:38 +03005090 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005091 nfs4_schedule_state_renewal(clp);
5092 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005093 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005094}
5095
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005096static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005098 struct nfs4_renewdata *data = calldata;
5099 struct nfs_client *clp = data->client;
5100 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005102 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005103 switch (task->tk_status) {
5104 case 0:
5105 break;
5106 case -NFS4ERR_LEASE_MOVED:
5107 nfs4_schedule_lease_moved_recovery(clp);
5108 break;
5109 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005110 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005111 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5112 return;
5113 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005114 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005115 return;
5116 }
5117 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005119 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120}
5121
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005122static const struct rpc_call_ops nfs4_renew_ops = {
5123 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005124 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005125};
5126
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005127static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128{
5129 struct rpc_message msg = {
5130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5131 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005132 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005134 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005136 if (renew_flags == 0)
5137 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005138 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005139 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005140 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005141 if (data == NULL) {
5142 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005143 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005144 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005145 data->client = clp;
5146 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005147 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005148 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005151static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152{
5153 struct rpc_message msg = {
5154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5155 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005156 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 };
5158 unsigned long now = jiffies;
5159 int status;
5160
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005161 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (status < 0)
5163 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005164 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 return 0;
5166}
5167
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005168static inline int nfs4_server_supports_acls(struct nfs_server *server)
5169{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005170 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005171}
5172
Trond Myklebust21f498c2012-08-24 10:59:25 -04005173/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5174 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005175 * the stack.
5176 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005177#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005178
Neil Hormane9e3d722011-03-04 19:26:03 -05005179static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005180 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005181{
5182 struct page *newpage, **spages;
5183 int rc = 0;
5184 size_t len;
5185 spages = pages;
5186
5187 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005188 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005189 newpage = alloc_page(GFP_KERNEL);
5190
5191 if (newpage == NULL)
5192 goto unwind;
5193 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005194 buf += len;
5195 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005196 *pages++ = newpage;
5197 rc++;
5198 } while (buflen != 0);
5199
5200 return rc;
5201
5202unwind:
5203 for(; rc > 0; rc--)
5204 __free_page(spages[rc-1]);
5205 return -ENOMEM;
5206}
5207
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005208struct nfs4_cached_acl {
5209 int cached;
5210 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005211 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005212};
5213
5214static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005215{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005216 struct nfs_inode *nfsi = NFS_I(inode);
5217
5218 spin_lock(&inode->i_lock);
5219 kfree(nfsi->nfs4_acl);
5220 nfsi->nfs4_acl = acl;
5221 spin_unlock(&inode->i_lock);
5222}
5223
5224static void nfs4_zap_acl_attr(struct inode *inode)
5225{
5226 nfs4_set_cached_acl(inode, NULL);
5227}
5228
5229static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5230{
5231 struct nfs_inode *nfsi = NFS_I(inode);
5232 struct nfs4_cached_acl *acl;
5233 int ret = -ENOENT;
5234
5235 spin_lock(&inode->i_lock);
5236 acl = nfsi->nfs4_acl;
5237 if (acl == NULL)
5238 goto out;
5239 if (buf == NULL) /* user is just asking for length */
5240 goto out_len;
5241 if (acl->cached == 0)
5242 goto out;
5243 ret = -ERANGE; /* see getxattr(2) man page */
5244 if (acl->len > buflen)
5245 goto out;
5246 memcpy(buf, acl->data, acl->len);
5247out_len:
5248 ret = acl->len;
5249out:
5250 spin_unlock(&inode->i_lock);
5251 return ret;
5252}
5253
Sachin Prabhu5794d212012-04-17 14:36:40 +01005254static 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 +00005255{
5256 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005257 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005258
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005259 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005260 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005261 if (acl == NULL)
5262 goto out;
5263 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005264 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005265 } else {
5266 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5267 if (acl == NULL)
5268 goto out;
5269 acl->cached = 0;
5270 }
5271 acl->len = acl_len;
5272out:
5273 nfs4_set_cached_acl(inode, acl);
5274}
5275
Andy Adamsonbf118a32011-12-07 11:55:27 -05005276/*
5277 * The getxattr API returns the required buffer length when called with a
5278 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5279 * the required buf. On a NULL buf, we send a page of data to the server
5280 * guessing that the ACL request can be serviced by a page. If so, we cache
5281 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5282 * the cache. If not so, we throw away the page, and cache the required
5283 * length. The next getxattr call will then produce another round trip to
5284 * the server, this time with the input buf of the required size.
5285 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005286static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005287{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005288 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005289 struct nfs_getaclargs args = {
5290 .fh = NFS_FH(inode),
5291 .acl_pages = pages,
5292 .acl_len = buflen,
5293 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005294 struct nfs_getaclres res = {
5295 .acl_len = buflen,
5296 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005297 struct rpc_message msg = {
5298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5299 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005300 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005301 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005302 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005303 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005304
Trond Myklebust21f498c2012-08-24 10:59:25 -04005305 if (npages > ARRAY_SIZE(pages))
5306 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005307
Andy Adamsonbf118a32011-12-07 11:55:27 -05005308 for (i = 0; i < npages; i++) {
5309 pages[i] = alloc_page(GFP_KERNEL);
5310 if (!pages[i])
5311 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005312 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005313
5314 /* for decoding across pages */
5315 res.acl_scratch = alloc_page(GFP_KERNEL);
5316 if (!res.acl_scratch)
5317 goto out_free;
5318
Andy Adamsonbf118a32011-12-07 11:55:27 -05005319 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005320
Peng Taode040be2012-01-10 22:42:47 +08005321 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005322 __func__, buf, buflen, npages, args.acl_len);
5323 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5324 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005325 if (ret)
5326 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005327
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005328 /* Handle the case where the passed-in buffer is too short */
5329 if (res.acl_flags & NFS4_ACL_TRUNC) {
5330 /* Did the user only issue a request for the acl length? */
5331 if (buf == NULL)
5332 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005333 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005334 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005335 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005336 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005337 if (buf) {
5338 if (res.acl_len > buflen) {
5339 ret = -ERANGE;
5340 goto out_free;
5341 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005342 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005343 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005344out_ok:
5345 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005346out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005347 for (i = 0; i < npages; i++)
5348 if (pages[i])
5349 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005350 if (res.acl_scratch)
5351 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005352 return ret;
5353}
5354
Trond Myklebust16b42892006-08-24 12:27:15 -04005355static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5356{
5357 struct nfs4_exception exception = { };
5358 ssize_t ret;
5359 do {
5360 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005361 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005362 if (ret >= 0)
5363 break;
5364 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5365 } while (exception.retry);
5366 return ret;
5367}
5368
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005369static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5370{
5371 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005372 int ret;
5373
5374 if (!nfs4_server_supports_acls(server))
5375 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005376 ret = nfs_revalidate_inode(server, inode);
5377 if (ret < 0)
5378 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005379 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5380 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005381 ret = nfs4_read_cached_acl(inode, buf, buflen);
5382 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005383 /* -ENOENT is returned if there is no ACL or if there is an ACL
5384 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005385 return ret;
5386 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005387}
5388
Trond Myklebust16b42892006-08-24 12:27:15 -04005389static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005390{
5391 struct nfs_server *server = NFS_SERVER(inode);
5392 struct page *pages[NFS4ACL_MAXPAGES];
5393 struct nfs_setaclargs arg = {
5394 .fh = NFS_FH(inode),
5395 .acl_pages = pages,
5396 .acl_len = buflen,
5397 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005398 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005399 struct rpc_message msg = {
5400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5401 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005402 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005403 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005404 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005405 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005406
5407 if (!nfs4_server_supports_acls(server))
5408 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005409 if (npages > ARRAY_SIZE(pages))
5410 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005411 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005412 if (i < 0)
5413 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005414 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005415 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005416
5417 /*
5418 * Free each page after tx, so the only ref left is
5419 * held by the network stack
5420 */
5421 for (; i > 0; i--)
5422 put_page(pages[i-1]);
5423
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005424 /*
5425 * Acl update can result in inode attribute update.
5426 * so mark the attribute cache invalid.
5427 */
5428 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005429 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005430 | NFS_INO_INVALID_CTIME
5431 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005432 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005433 nfs_access_zap_cache(inode);
5434 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005435 return ret;
5436}
5437
Trond Myklebust16b42892006-08-24 12:27:15 -04005438static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5439{
5440 struct nfs4_exception exception = { };
5441 int err;
5442 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005443 err = __nfs4_proc_set_acl(inode, buf, buflen);
5444 trace_nfs4_set_acl(inode, err);
5445 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005446 &exception);
5447 } while (exception.retry);
5448 return err;
5449}
5450
David Quigleyaa9c2662013-05-22 12:50:44 -04005451#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5452static int _nfs4_get_security_label(struct inode *inode, void *buf,
5453 size_t buflen)
5454{
5455 struct nfs_server *server = NFS_SERVER(inode);
5456 struct nfs_fattr fattr;
5457 struct nfs4_label label = {0, 0, buflen, buf};
5458
5459 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005460 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005461 .fh = NFS_FH(inode),
5462 .bitmask = bitmask,
5463 };
5464 struct nfs4_getattr_res res = {
5465 .fattr = &fattr,
5466 .label = &label,
5467 .server = server,
5468 };
5469 struct rpc_message msg = {
5470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005471 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005472 .rpc_resp = &res,
5473 };
5474 int ret;
5475
5476 nfs_fattr_init(&fattr);
5477
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005478 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005479 if (ret)
5480 return ret;
5481 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5482 return -ENOENT;
5483 if (buflen < label.len)
5484 return -ERANGE;
5485 return 0;
5486}
5487
5488static int nfs4_get_security_label(struct inode *inode, void *buf,
5489 size_t buflen)
5490{
5491 struct nfs4_exception exception = { };
5492 int err;
5493
5494 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5495 return -EOPNOTSUPP;
5496
5497 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005498 err = _nfs4_get_security_label(inode, buf, buflen);
5499 trace_nfs4_get_security_label(inode, err);
5500 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005501 &exception);
5502 } while (exception.retry);
5503 return err;
5504}
5505
5506static int _nfs4_do_set_security_label(struct inode *inode,
5507 struct nfs4_label *ilabel,
5508 struct nfs_fattr *fattr,
5509 struct nfs4_label *olabel)
5510{
5511
5512 struct iattr sattr = {0};
5513 struct nfs_server *server = NFS_SERVER(inode);
5514 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005515 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005516 .fh = NFS_FH(inode),
5517 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005518 .server = server,
5519 .bitmask = bitmask,
5520 .label = ilabel,
5521 };
5522 struct nfs_setattrres res = {
5523 .fattr = fattr,
5524 .label = olabel,
5525 .server = server,
5526 };
5527 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5529 .rpc_argp = &arg,
5530 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005531 };
5532 int status;
5533
Jeff Layton12207f62013-11-01 10:49:32 -04005534 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005535
Jeff Layton12207f62013-11-01 10:49:32 -04005536 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005537 if (status)
5538 dprintk("%s failed: %d\n", __func__, status);
5539
5540 return status;
5541}
5542
5543static int nfs4_do_set_security_label(struct inode *inode,
5544 struct nfs4_label *ilabel,
5545 struct nfs_fattr *fattr,
5546 struct nfs4_label *olabel)
5547{
5548 struct nfs4_exception exception = { };
5549 int err;
5550
5551 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005552 err = _nfs4_do_set_security_label(inode, ilabel,
5553 fattr, olabel);
5554 trace_nfs4_set_security_label(inode, err);
5555 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005556 &exception);
5557 } while (exception.retry);
5558 return err;
5559}
5560
5561static int
Al Viro59301222016-05-27 10:19:30 -04005562nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005563{
5564 struct nfs4_label ilabel, *olabel = NULL;
5565 struct nfs_fattr fattr;
5566 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005567 int status;
5568
5569 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5570 return -EOPNOTSUPP;
5571
5572 nfs_fattr_init(&fattr);
5573
5574 ilabel.pi = 0;
5575 ilabel.lfs = 0;
5576 ilabel.label = (char *)buf;
5577 ilabel.len = buflen;
5578
5579 cred = rpc_lookup_cred();
5580 if (IS_ERR(cred))
5581 return PTR_ERR(cred);
5582
5583 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5584 if (IS_ERR(olabel)) {
5585 status = -PTR_ERR(olabel);
5586 goto out;
5587 }
5588
5589 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5590 if (status == 0)
5591 nfs_setsecurity(inode, &fattr, olabel);
5592
5593 nfs4_label_free(olabel);
5594out:
5595 put_rpccred(cred);
5596 return status;
5597}
5598#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5599
5600
Chuck Leverf0920752012-05-21 22:45:41 -04005601static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5602 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005603{
5604 __be32 verf[2];
5605
Chuck Lever2c820d92012-05-21 22:45:33 -04005606 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5607 /* An impossible timestamp guarantees this value
5608 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005609 verf[0] = cpu_to_be32(U32_MAX);
5610 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005611 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005612 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005613 u64 ns = ktime_to_ns(nn->boot_time);
5614
5615 verf[0] = cpu_to_be32(ns >> 32);
5616 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005617 }
Chuck Levercd937102012-03-02 17:14:31 -05005618 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5619}
5620
Jeff Laytona3192682015-06-09 19:43:59 -04005621static int
5622nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005623{
Jeff Laytona3192682015-06-09 19:43:59 -04005624 size_t len;
5625 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005626
Trond Myklebustceb3a162015-01-03 15:16:04 -05005627 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005628 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005629
Jeff Laytona3192682015-06-09 19:43:59 -04005630 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005631 len = 14 +
5632 strlen(clp->cl_rpcclient->cl_nodename) +
5633 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005634 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005635 1;
5636 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005637 if (nfs4_client_id_uniquifier[0] != '\0')
5638 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005639 if (len > NFS4_OPAQUE_LIMIT + 1)
5640 return -EINVAL;
5641
5642 /*
5643 * Since this string is allocated at mount time, and held until the
5644 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5645 * about a memory-reclaim deadlock.
5646 */
5647 str = kmalloc(len, GFP_KERNEL);
5648 if (!str)
5649 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005650
Chuck Levere984a552012-09-14 17:24:21 -04005651 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005652 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005653 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005654 clp->cl_rpcclient->cl_nodename,
5655 nfs4_client_id_uniquifier,
5656 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005657 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005658 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005659 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005660 clp->cl_rpcclient->cl_nodename,
5661 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005662 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005663 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005664
Jeff Laytona3192682015-06-09 19:43:59 -04005665 clp->cl_owner_id = str;
5666 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005667}
5668
Jeff Layton873e3852015-06-09 19:44:00 -04005669static int
5670nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005671{
Jeff Layton873e3852015-06-09 19:44:00 -04005672 size_t len;
5673 char *str;
5674
5675 len = 10 + 10 + 1 + 10 + 1 +
5676 strlen(nfs4_client_id_uniquifier) + 1 +
5677 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5678
5679 if (len > NFS4_OPAQUE_LIMIT + 1)
5680 return -EINVAL;
5681
5682 /*
5683 * Since this string is allocated at mount time, and held until the
5684 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5685 * about a memory-reclaim deadlock.
5686 */
5687 str = kmalloc(len, GFP_KERNEL);
5688 if (!str)
5689 return -ENOMEM;
5690
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005691 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005692 clp->rpc_ops->version, clp->cl_minorversion,
5693 nfs4_client_id_uniquifier,
5694 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005695 clp->cl_owner_id = str;
5696 return 0;
5697}
5698
5699static int
5700nfs4_init_uniform_client_string(struct nfs_client *clp)
5701{
Jeff Layton873e3852015-06-09 19:44:00 -04005702 size_t len;
5703 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005704
5705 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005706 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005707
5708 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005709 return nfs4_init_uniquifier_client_string(clp);
5710
5711 len = 10 + 10 + 1 + 10 + 1 +
5712 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5713
5714 if (len > NFS4_OPAQUE_LIMIT + 1)
5715 return -EINVAL;
5716
5717 /*
5718 * Since this string is allocated at mount time, and held until the
5719 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5720 * about a memory-reclaim deadlock.
5721 */
5722 str = kmalloc(len, GFP_KERNEL);
5723 if (!str)
5724 return -ENOMEM;
5725
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005726 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005727 clp->rpc_ops->version, clp->cl_minorversion,
5728 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005729 clp->cl_owner_id = str;
5730 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005731}
5732
Chuck Lever706cb8d2014-03-12 12:51:47 -04005733/*
5734 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5735 * services. Advertise one based on the address family of the
5736 * clientaddr.
5737 */
5738static unsigned int
5739nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5740{
5741 if (strchr(clp->cl_ipaddr, ':') != NULL)
5742 return scnprintf(buf, len, "tcp6");
5743 else
5744 return scnprintf(buf, len, "tcp");
5745}
5746
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005747static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5748{
5749 struct nfs4_setclientid *sc = calldata;
5750
5751 if (task->tk_status == 0)
5752 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5753}
5754
5755static const struct rpc_call_ops nfs4_setclientid_ops = {
5756 .rpc_call_done = nfs4_setclientid_done,
5757};
5758
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005759/**
5760 * nfs4_proc_setclientid - Negotiate client ID
5761 * @clp: state data structure
5762 * @program: RPC program for NFSv4 callback service
5763 * @port: IP port number for NFS4 callback service
5764 * @cred: RPC credential to use for this call
5765 * @res: where to place the result
5766 *
5767 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5768 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005769int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5770 unsigned short port, struct rpc_cred *cred,
5771 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772{
5773 nfs4_verifier sc_verifier;
5774 struct nfs4_setclientid setclientid = {
5775 .sc_verifier = &sc_verifier,
5776 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005777 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 };
5779 struct rpc_message msg = {
5780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5781 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005782 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005783 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005785 struct rpc_task *task;
5786 struct rpc_task_setup task_setup_data = {
5787 .rpc_client = clp->cl_rpcclient,
5788 .rpc_message = &msg,
5789 .callback_ops = &nfs4_setclientid_ops,
5790 .callback_data = &setclientid,
5791 .flags = RPC_TASK_TIMEOUT,
5792 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005793 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
Chuck Leverde734832012-07-11 16:30:50 -04005795 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005796 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005797
5798 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5799 status = nfs4_init_uniform_client_string(clp);
5800 else
Jeff Laytona3192682015-06-09 19:43:59 -04005801 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005802
5803 if (status)
5804 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005805
Chuck Leverde734832012-07-11 16:30:50 -04005806 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005807 setclientid.sc_netid_len =
5808 nfs4_init_callback_netid(clp,
5809 setclientid.sc_netid,
5810 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005811 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005812 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 clp->cl_ipaddr, port >> 8, port & 255);
5814
Jeff Layton3a6bb732015-06-09 19:43:57 -04005815 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005816 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005817 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005818 task = rpc_run_task(&task_setup_data);
5819 if (IS_ERR(task)) {
5820 status = PTR_ERR(task);
5821 goto out;
5822 }
5823 status = task->tk_status;
5824 if (setclientid.sc_cred) {
5825 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5826 put_rpccred(setclientid.sc_cred);
5827 }
5828 rpc_put_task(task);
5829out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005830 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005831 dprintk("NFS reply setclientid: %d\n", status);
5832 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833}
5834
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005835/**
5836 * nfs4_proc_setclientid_confirm - Confirm client ID
5837 * @clp: state data structure
5838 * @res: result of a previous SETCLIENTID
5839 * @cred: RPC credential to use for this call
5840 *
5841 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5842 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005843int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005844 struct nfs4_setclientid_res *arg,
5845 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 struct rpc_message msg = {
5848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005849 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005850 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 int status;
5853
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005854 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5855 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5856 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005857 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005858 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005859 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 return status;
5861}
5862
Trond Myklebustfe650402006-01-03 09:55:18 +01005863struct nfs4_delegreturndata {
5864 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005865 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005866 struct nfs_fh fh;
5867 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005868 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005869 struct {
5870 struct nfs4_layoutreturn_args arg;
5871 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005872 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005873 u32 roc_barrier;
5874 bool roc;
5875 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005876 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005877 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005878 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005879};
5880
Trond Myklebustfe650402006-01-03 09:55:18 +01005881static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5882{
5883 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005884 struct nfs4_exception exception = {
5885 .inode = data->inode,
5886 .stateid = &data->stateid,
5887 };
Andy Adamson938e1012009-04-01 09:22:28 -04005888
Trond Myklebust14516c32010-07-31 14:29:06 -04005889 if (!nfs4_sequence_done(task, &data->res.seq_res))
5890 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005891
Trond Myklebustca8acf82013-08-13 10:36:56 -04005892 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005893
5894 /* Handle Layoutreturn errors */
5895 if (data->args.lr_args && task->tk_status != 0) {
5896 switch(data->res.lr_ret) {
5897 default:
5898 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5899 break;
5900 case 0:
5901 data->args.lr_args = NULL;
5902 data->res.lr_res = NULL;
5903 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005904 case -NFS4ERR_OLD_STATEID:
5905 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005906 data->inode))
5907 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005908 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005909 case -NFS4ERR_ADMIN_REVOKED:
5910 case -NFS4ERR_DELEG_REVOKED:
5911 case -NFS4ERR_EXPIRED:
5912 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005913 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5914 case -NFS4ERR_WRONG_CRED:
5915 data->args.lr_args = NULL;
5916 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005917 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005918 }
5919 }
5920
Ricardo Labiaga79708862009-12-07 09:23:21 -05005921 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005922 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005923 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005924 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005925 case -NFS4ERR_ADMIN_REVOKED:
5926 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005927 case -NFS4ERR_EXPIRED:
5928 nfs4_free_revoked_stateid(data->res.server,
5929 data->args.stateid,
5930 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005931 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005932 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005933 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005934 task->tk_status = 0;
5935 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005936 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005937 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5938 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005939 task->tk_status = 0;
5940 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005941 case -NFS4ERR_ACCESS:
5942 if (data->args.bitmask) {
5943 data->args.bitmask = NULL;
5944 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005945 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005946 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005947 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005948 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005949 task->tk_status = nfs4_async_handle_exception(task,
5950 data->res.server, task->tk_status,
5951 &exception);
5952 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005953 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005954 }
5955 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005956 return;
5957lr_restart:
5958 data->res.lr_ret = 0;
5959out_restart:
5960 task->tk_status = 0;
5961 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005962}
5963
5964static void nfs4_delegreturn_release(void *calldata)
5965{
Peng Tao039b7562014-07-03 13:05:02 +08005966 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005967 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005968
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005969 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005970 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005971 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5972 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005973 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005974 nfs_iput_and_deactive(inode);
5975 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005976 kfree(calldata);
5977}
5978
Andy Adamson938e1012009-04-01 09:22:28 -04005979static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5980{
5981 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04005982 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04005983
5984 d_data = (struct nfs4_delegreturndata *)data;
5985
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005986 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005987 return;
5988
Trond Myklebustc8bf7072018-06-15 16:31:02 -04005989 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
5990 if (lo && !pnfs_layout_is_valid(lo)) {
5991 d_data->args.lr_args = NULL;
5992 d_data->res.lr_res = NULL;
5993 }
5994
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005995 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005996 &d_data->args.seq_args,
5997 &d_data->res.seq_res,
5998 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005999}
Andy Adamson938e1012009-04-01 09:22:28 -04006000
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006001static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006002 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006003 .rpc_call_done = nfs4_delegreturn_done,
6004 .rpc_release = nfs4_delegreturn_release,
6005};
6006
Trond Myklebuste6f81072008-01-24 18:14:34 -05006007static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006008{
6009 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006010 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006011 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006012 struct rpc_message msg = {
6013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6014 .rpc_cred = cred,
6015 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006016 struct rpc_task_setup task_setup_data = {
6017 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006018 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006019 .callback_ops = &nfs4_delegreturn_ops,
6020 .flags = RPC_TASK_ASYNC,
6021 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006022 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006023
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006024 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006025 if (data == NULL)
6026 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006027 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006028
6029 nfs4_state_protect(server->nfs_client,
6030 NFS_SP4_MACH_CRED_CLEANUP,
6031 &task_setup_data.rpc_client, &msg);
6032
Trond Myklebustfe650402006-01-03 09:55:18 +01006033 data->args.fhandle = &data->fh;
6034 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006035 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006036 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006037 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006038 data->res.fattr = &data->fattr;
6039 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006040 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006041 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006042 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006043 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006044 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006045 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006046 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006047 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006048 if (data->lr.roc) {
6049 data->args.lr_args = &data->lr.arg;
6050 data->res.lr_res = &data->lr.res;
6051 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006052 } else if (data->lr.roc) {
6053 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6054 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006055 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006056
Trond Myklebustc970aa82007-07-14 15:39:59 -04006057 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006058 msg.rpc_argp = &data->args;
6059 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006060 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006061 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006062 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006063 if (!issync)
6064 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006065 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006066 if (status != 0)
6067 goto out;
6068 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006069out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006070 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006071 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072}
6073
Trond Myklebuste6f81072008-01-24 18:14:34 -05006074int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075{
6076 struct nfs_server *server = NFS_SERVER(inode);
6077 struct nfs4_exception exception = { };
6078 int err;
6079 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006080 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006081 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 switch (err) {
6083 case -NFS4ERR_STALE_STATEID:
6084 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 case 0:
6086 return 0;
6087 }
6088 err = nfs4_handle_exception(server, err, &exception);
6089 } while (exception.retry);
6090 return err;
6091}
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6094{
6095 struct inode *inode = state->inode;
6096 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006097 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006098 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006102 struct nfs_lockt_res res = {
6103 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 };
6105 struct rpc_message msg = {
6106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006107 .rpc_argp = &arg,
6108 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 .rpc_cred = state->owner->so_cred,
6110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 struct nfs4_lock_state *lsp;
6112 int status;
6113
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006114 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006115 status = nfs4_set_lock_state(state, request);
6116 if (status != 0)
6117 goto out;
6118 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006119 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006120 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006121 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006122 switch (status) {
6123 case 0:
6124 request->fl_type = F_UNLCK;
6125 break;
6126 case -NFS4ERR_DENIED:
6127 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006129 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006130 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006131out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 return status;
6133}
6134
6135static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6136{
6137 struct nfs4_exception exception = { };
6138 int err;
6139
6140 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006141 err = _nfs4_proc_getlk(state, cmd, request);
6142 trace_nfs4_get_lock(request, state, cmd, err);
6143 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 &exception);
6145 } while (exception.retry);
6146 return err;
6147}
6148
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006149struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006150 struct nfs_locku_args arg;
6151 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006152 struct nfs4_lock_state *lsp;
6153 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006154 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006155 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006156 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006157 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006158};
6159
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006160static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6161 struct nfs_open_context *ctx,
6162 struct nfs4_lock_state *lsp,
6163 struct nfs_seqid *seqid)
6164{
6165 struct nfs4_unlockdata *p;
6166 struct inode *inode = lsp->ls_state->inode;
6167
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006168 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006169 if (p == NULL)
6170 return NULL;
6171 p->arg.fh = NFS_FH(inode);
6172 p->arg.fl = &p->fl;
6173 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006174 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006175 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006176 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006177 /* Ensure we don't close file until we're done freeing locks! */
6178 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006179 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006180 memcpy(&p->fl, fl, sizeof(p->fl));
6181 p->server = NFS_SERVER(inode);
6182 return p;
6183}
6184
Trond Myklebust06f814a2006-01-03 09:55:07 +01006185static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006186{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006187 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006188 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006189 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006190 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006191 put_nfs_open_context(calldata->ctx);
6192 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006193}
6194
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006195static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006196{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006197 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006198 struct nfs4_exception exception = {
6199 .inode = calldata->lsp->ls_state->inode,
6200 .stateid = &calldata->arg.stateid,
6201 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006202
Trond Myklebust14516c32010-07-31 14:29:06 -04006203 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6204 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006205 switch (task->tk_status) {
6206 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006207 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006208 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006209 if (nfs4_update_lock_stateid(calldata->lsp,
6210 &calldata->res.stateid))
6211 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006212 case -NFS4ERR_ADMIN_REVOKED:
6213 case -NFS4ERR_EXPIRED:
6214 nfs4_free_revoked_stateid(calldata->server,
6215 &calldata->arg.stateid,
6216 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006217 case -NFS4ERR_BAD_STATEID:
6218 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006219 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006220 if (!nfs4_stateid_match(&calldata->arg.stateid,
6221 &calldata->lsp->ls_stateid))
6222 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006223 break;
6224 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006225 task->tk_status = nfs4_async_handle_exception(task,
6226 calldata->server, task->tk_status,
6227 &exception);
6228 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006229 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006230 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006231 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006232}
6233
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006234static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006235{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006236 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006238 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6239 nfs_async_iocounter_wait(task, calldata->l_ctx))
6240 return;
6241
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006242 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006243 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006244 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006245 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006246 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006247 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006248 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006249 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006250 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006251 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006252 &calldata->res.seq_res,
6253 task) != 0)
6254 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006255 return;
6256out_no_action:
6257 task->tk_action = NULL;
6258out_wait:
6259 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260}
6261
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006262static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006263 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006264 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006265 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006266};
6267
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006268static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6269 struct nfs_open_context *ctx,
6270 struct nfs4_lock_state *lsp,
6271 struct nfs_seqid *seqid)
6272{
6273 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006274 struct rpc_message msg = {
6275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6276 .rpc_cred = ctx->cred,
6277 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006278 struct rpc_task_setup task_setup_data = {
6279 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006280 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006281 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006282 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006283 .flags = RPC_TASK_ASYNC,
6284 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006286 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6287 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6288
Frank Filz137d6ac2007-07-09 15:32:29 -07006289 /* Ensure this is an unlock - when canceling a lock, the
6290 * canceled lock is passed in, and it won't be an unlock.
6291 */
6292 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006293 if (fl->fl_flags & FL_CLOSE)
6294 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006295
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006296 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6297 if (data == NULL) {
6298 nfs_free_seqid(seqid);
6299 return ERR_PTR(-ENOMEM);
6300 }
6301
Anna Schumakerfba83f32018-05-04 16:22:50 -04006302 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006303 msg.rpc_argp = &data->arg;
6304 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006305 task_setup_data.callback_data = data;
6306 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006307}
6308
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6310{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006311 struct inode *inode = state->inode;
6312 struct nfs4_state_owner *sp = state->owner;
6313 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006314 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006315 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006316 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006317 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006318 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006319 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320
Trond Myklebust9b073572006-06-29 16:38:34 -04006321 status = nfs4_set_lock_state(state, request);
6322 /* Unlock _before_ we do the RPC call */
6323 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006324 /* Exclude nfs_delegation_claim_locks() */
6325 mutex_lock(&sp->so_delegreturn_mutex);
6326 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006327 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006328 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006329 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006330 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006331 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006332 }
6333 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006334 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006335 if (status != 0)
6336 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006337 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006338 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006339 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6340 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006341 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6342 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006343 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006344 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006345 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006346 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006347 status = PTR_ERR(task);
6348 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006349 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006350 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006351 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006352out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006353 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006354 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006355 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356}
6357
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006358struct nfs4_lockdata {
6359 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006360 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006361 struct nfs4_lock_state *lsp;
6362 struct nfs_open_context *ctx;
6363 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006364 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006365 int rpc_status;
6366 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006367 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006368};
6369
6370static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006371 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6372 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006373{
6374 struct nfs4_lockdata *p;
6375 struct inode *inode = lsp->ls_state->inode;
6376 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006377 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006378
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006379 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006380 if (p == NULL)
6381 return NULL;
6382
6383 p->arg.fh = NFS_FH(inode);
6384 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006385 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006386 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006387 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006388 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6389 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006390 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006391 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006392 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006393 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006394 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006395 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006396 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006397 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006398 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006399 p->ctx = get_nfs_open_context(ctx);
6400 memcpy(&p->fl, fl, sizeof(p->fl));
6401 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006402out_free_seqid:
6403 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006404out_free:
6405 kfree(p);
6406 return NULL;
6407}
6408
6409static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6410{
6411 struct nfs4_lockdata *data = calldata;
6412 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
Harvey Harrison3110ff82008-05-02 13:42:44 -07006414 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006415 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006416 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006417 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006418 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006419 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006420 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006421 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006422 nfs4_stateid_copy(&data->arg.open_stateid,
6423 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006424 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006425 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006426 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006427 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006428 nfs4_stateid_copy(&data->arg.lock_stateid,
6429 &data->lsp->ls_stateid);
6430 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006431 if (!nfs4_valid_open_stateid(state)) {
6432 data->rpc_status = -EBADF;
6433 task->tk_action = NULL;
6434 goto out_release_open_seqid;
6435 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006436 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006437 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006438 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006439 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006440 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006441 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006442out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006443 nfs_release_seqid(data->arg.open_seqid);
6444out_release_lock_seqid:
6445 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006446out_wait:
6447 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006448 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006449}
6450
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006451static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6452{
6453 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006454 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006455
Harvey Harrison3110ff82008-05-02 13:42:44 -07006456 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006457
Trond Myklebust14516c32010-07-31 14:29:06 -04006458 if (!nfs4_sequence_done(task, &data->res.seq_res))
6459 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006460
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006461 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006462 switch (task->tk_status) {
6463 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006464 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006465 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006466 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006467 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006468 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebustc69899a2015-01-24 16:03:52 -05006469 break;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006470 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006471
Trond Myklebust39071e62015-01-24 15:07:56 -05006472 if (data->arg.new_lock_owner != 0) {
6473 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6474 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6475 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006476 goto out_done;
6477 } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
6478 goto out_done;
6479
Trond Myklebust425c1d42015-01-24 14:57:53 -05006480 break;
6481 case -NFS4ERR_BAD_STATEID:
6482 case -NFS4ERR_OLD_STATEID:
6483 case -NFS4ERR_STALE_STATEID:
6484 case -NFS4ERR_EXPIRED:
6485 if (data->arg.new_lock_owner != 0) {
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006486 if (nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006487 &lsp->ls_state->open_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006488 goto out_done;
6489 } else if (nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006490 &lsp->ls_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006491 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006492 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006493 if (!data->cancelled)
6494 rpc_restart_call_prepare(task);
6495out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006496 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006497}
6498
6499static void nfs4_lock_release(void *calldata)
6500{
6501 struct nfs4_lockdata *data = calldata;
6502
Harvey Harrison3110ff82008-05-02 13:42:44 -07006503 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006504 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006505 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006506 struct rpc_task *task;
6507 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6508 data->arg.lock_seqid);
6509 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006510 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006511 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006512 } else
6513 nfs_free_seqid(data->arg.lock_seqid);
6514 nfs4_put_lock_state(data->lsp);
6515 put_nfs_open_context(data->ctx);
6516 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006517 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006518}
6519
6520static const struct rpc_call_ops nfs4_lock_ops = {
6521 .rpc_call_prepare = nfs4_lock_prepare,
6522 .rpc_call_done = nfs4_lock_done,
6523 .rpc_release = nfs4_lock_release,
6524};
6525
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006526static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6527{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006528 switch (error) {
6529 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006530 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006531 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006532 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006533 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006534 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006535 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006536 break;
6537 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006538 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006539 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006540 };
6541}
6542
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006543static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006544{
6545 struct nfs4_lockdata *data;
6546 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006547 struct rpc_message msg = {
6548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6549 .rpc_cred = state->owner->so_cred,
6550 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006551 struct rpc_task_setup task_setup_data = {
6552 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006553 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006554 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006555 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006556 .flags = RPC_TASK_ASYNC,
6557 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006558 int ret;
6559
Harvey Harrison3110ff82008-05-02 13:42:44 -07006560 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006561 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006562 fl->fl_u.nfs4_fl.owner,
6563 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006564 if (data == NULL)
6565 return -ENOMEM;
6566 if (IS_SETLKW(cmd))
6567 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006568 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6569 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006570 msg.rpc_argp = &data->arg;
6571 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006572 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006573 if (recovery_type > NFS_LOCK_NEW) {
6574 if (recovery_type == NFS_LOCK_RECLAIM)
6575 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006576 } else
6577 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006578 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006579 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006580 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006581 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006582 if (ret == 0) {
6583 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006584 if (ret)
6585 nfs4_handle_setlk_error(data->server, data->lsp,
6586 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006587 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006588 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006589 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006590 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006591 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593}
6594
6595static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6596{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006597 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006598 struct nfs4_exception exception = {
6599 .inode = state->inode,
6600 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006601 int err;
6602
6603 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006604 /* Cache the lock if possible... */
6605 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6606 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006607 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006608 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006609 break;
6610 nfs4_handle_exception(server, err, &exception);
6611 } while (exception.retry);
6612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613}
6614
6615static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6616{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006617 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006618 struct nfs4_exception exception = {
6619 .inode = state->inode,
6620 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006621 int err;
6622
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006623 err = nfs4_set_lock_state(state, request);
6624 if (err != 0)
6625 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006626 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006627 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6628 return 0;
6629 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006630 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006631 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6632 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006633 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006634 switch (err) {
6635 default:
6636 goto out;
6637 case -NFS4ERR_GRACE:
6638 case -NFS4ERR_DELAY:
6639 nfs4_handle_exception(server, err, &exception);
6640 err = 0;
6641 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006642 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006643out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645}
6646
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006647#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006648static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6649{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006650 struct nfs4_lock_state *lsp;
6651 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006652
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006653 status = nfs4_set_lock_state(state, request);
6654 if (status != 0)
6655 return status;
6656 lsp = request->fl_u.nfs4_fl.owner;
6657 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6658 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6659 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006660 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006661}
6662#endif
6663
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6665{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006666 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006667 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006668 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006669 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
Trond Myklebust01c3b862006-06-29 16:38:39 -04006671 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006672 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006673 if (status < 0)
6674 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006675 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006676 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006677 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006678 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006679 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006680 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006681 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006682 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006683 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006684 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006685 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006686 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006687 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006688 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006689out:
6690 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 return status;
6692}
6693
6694static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6695{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006696 struct nfs4_exception exception = {
6697 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006698 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006699 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 int err;
6701
6702 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006703 err = _nfs4_proc_setlk(state, cmd, request);
6704 if (err == -NFS4ERR_DENIED)
6705 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006707 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 } while (exception.retry);
6709 return err;
6710}
6711
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006712#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6713#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6714
6715static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006716nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6717 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006718{
6719 int status = -ERESTARTSYS;
6720 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6721
6722 while(!signalled()) {
6723 status = nfs4_proc_setlk(state, cmd, request);
6724 if ((status != -EAGAIN) || IS_SETLK(cmd))
6725 break;
6726 freezable_schedule_timeout_interruptible(timeout);
6727 timeout *= 2;
6728 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6729 status = -ERESTARTSYS;
6730 }
6731 return status;
6732}
6733
Jeff Laytona1d617d82016-09-17 18:17:39 -04006734#ifdef CONFIG_NFS_V4_1
6735struct nfs4_lock_waiter {
6736 struct task_struct *task;
6737 struct inode *inode;
6738 struct nfs_lowner *owner;
6739 bool notified;
6740};
6741
6742static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006743nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006744{
6745 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006746 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006747
Jeff Layton57174592018-03-18 08:37:03 -04006748 /* NULL key means to wake up everyone */
6749 if (key) {
6750 struct cb_notify_lock_args *cbnl = key;
6751 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6752 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006753
Jeff Layton57174592018-03-18 08:37:03 -04006754 /* Only wake if the callback was for the same owner. */
6755 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6756 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006757
Jeff Layton57174592018-03-18 08:37:03 -04006758 /* Make sure it's for the right inode */
6759 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6760 return 0;
6761
6762 waiter->notified = true;
6763 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006764
6765 /* override "private" so we can use default_wake_function */
6766 wait->private = waiter->task;
6767 ret = autoremove_wake_function(wait, mode, flags, key);
6768 wait->private = waiter;
6769 return ret;
6770}
6771
6772static int
6773nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6774{
6775 int status = -ERESTARTSYS;
6776 unsigned long flags;
6777 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6778 struct nfs_server *server = NFS_SERVER(state->inode);
6779 struct nfs_client *clp = server->nfs_client;
6780 wait_queue_head_t *q = &clp->cl_lock_waitq;
6781 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6782 .id = lsp->ls_seqid.owner_id,
6783 .s_dev = server->s_dev };
6784 struct nfs4_lock_waiter waiter = { .task = current,
6785 .inode = state->inode,
6786 .owner = &owner,
6787 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006788 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006789
6790 /* Don't bother with waitqueue if we don't expect a callback */
6791 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6792 return nfs4_retry_setlk_simple(state, cmd, request);
6793
6794 init_wait(&wait);
6795 wait.private = &waiter;
6796 wait.func = nfs4_wake_lock_waiter;
6797 add_wait_queue(q, &wait);
6798
6799 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006800 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006801 status = nfs4_proc_setlk(state, cmd, request);
6802 if ((status != -EAGAIN) || IS_SETLK(cmd))
6803 break;
6804
6805 status = -ERESTARTSYS;
6806 spin_lock_irqsave(&q->lock, flags);
6807 if (waiter.notified) {
6808 spin_unlock_irqrestore(&q->lock, flags);
6809 continue;
6810 }
6811 set_current_state(TASK_INTERRUPTIBLE);
6812 spin_unlock_irqrestore(&q->lock, flags);
6813
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006814 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006815 }
6816
6817 finish_wait(q, &wait);
6818 return status;
6819}
6820#else /* !CONFIG_NFS_V4_1 */
6821static inline int
6822nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6823{
6824 return nfs4_retry_setlk_simple(state, cmd, request);
6825}
6826#endif
6827
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828static int
6829nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6830{
6831 struct nfs_open_context *ctx;
6832 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 int status;
6834
6835 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006836 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 state = ctx->state;
6838
Trond Myklebustd9531262009-07-21 19:22:38 -04006839 if (IS_GETLK(cmd)) {
6840 if (state != NULL)
6841 return nfs4_proc_getlk(state, F_GETLK, request);
6842 return 0;
6843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
6845 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6846 return -EINVAL;
6847
Trond Myklebustd9531262009-07-21 19:22:38 -04006848 if (request->fl_type == F_UNLCK) {
6849 if (state != NULL)
6850 return nfs4_proc_unlck(state, cmd, request);
6851 return 0;
6852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
Trond Myklebustd9531262009-07-21 19:22:38 -04006854 if (state == NULL)
6855 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006856
6857 if ((request->fl_flags & FL_POSIX) &&
6858 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6859 return -ENOLCK;
6860
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006861 /*
6862 * Don't rely on the VFS having checked the file open mode,
6863 * since it won't do this for flock() locks.
6864 */
6865 switch (request->fl_type) {
6866 case F_RDLCK:
6867 if (!(filp->f_mode & FMODE_READ))
6868 return -EBADF;
6869 break;
6870 case F_WRLCK:
6871 if (!(filp->f_mode & FMODE_WRITE))
6872 return -EBADF;
6873 }
6874
Jeff Layton1ea67db2016-09-17 18:17:37 -04006875 status = nfs4_set_lock_state(state, request);
6876 if (status != 0)
6877 return status;
6878
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006879 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880}
6881
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006882int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006883{
6884 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006885 int err;
6886
6887 err = nfs4_set_lock_state(state, fl);
6888 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006889 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006890 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006891 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006892}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006893
Trond Myklebustcf470c32012-03-07 13:49:12 -05006894struct nfs_release_lockowner_data {
6895 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006896 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006897 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006898 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006899 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006900};
6901
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006902static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6903{
6904 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006905 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006906 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6907 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006908 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006909 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006910}
6911
6912static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6913{
6914 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006915 struct nfs_server *server = data->server;
6916
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006917 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006918
6919 switch (task->tk_status) {
6920 case 0:
6921 renew_lease(server, data->timestamp);
6922 break;
6923 case -NFS4ERR_STALE_CLIENTID:
6924 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006925 nfs4_schedule_lease_recovery(server->nfs_client);
6926 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006927 case -NFS4ERR_LEASE_MOVED:
6928 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006929 if (nfs4_async_handle_error(task, server,
6930 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006931 rpc_restart_call_prepare(task);
6932 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006933}
6934
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006935static void nfs4_release_lockowner_release(void *calldata)
6936{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006937 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006938 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006939 kfree(calldata);
6940}
6941
Trond Myklebust17280172012-03-11 13:11:00 -04006942static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006943 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6944 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006945 .rpc_release = nfs4_release_lockowner_release,
6946};
6947
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006948static void
6949nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006950{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006951 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006952 struct rpc_message msg = {
6953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6954 };
6955
6956 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006957 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006958
Trond Myklebustcf470c32012-03-07 13:49:12 -05006959 data = kmalloc(sizeof(*data), GFP_NOFS);
6960 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006961 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006962 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006963 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006964 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6965 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6966 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006967
Trond Myklebustcf470c32012-03-07 13:49:12 -05006968 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006969 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006970 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006971 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006972}
6973
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006974#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6975
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006976static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006977 struct dentry *unused, struct inode *inode,
6978 const char *key, const void *buf,
6979 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006980{
Al Viro59301222016-05-27 10:19:30 -04006981 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006982}
6983
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006984static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006985 struct dentry *unused, struct inode *inode,
6986 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006987{
Al Virob2968212016-04-10 20:48:24 -04006988 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006989}
6990
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006991static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006992{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006993 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006994}
6995
David Quigleyc9bccef2013-05-22 12:50:45 -04006996#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006997
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006998static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006999 struct dentry *unused, struct inode *inode,
7000 const char *key, const void *buf,
7001 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007002{
7003 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007004 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007005
7006 return -EOPNOTSUPP;
7007}
7008
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007009static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007010 struct dentry *unused, struct inode *inode,
7011 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007012{
7013 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007014 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007015 return -EOPNOTSUPP;
7016}
7017
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007018static ssize_t
7019nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007020{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007021 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007022
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007023 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7024 len = security_inode_listsecurity(inode, list, list_len);
7025 if (list_len && len > list_len)
7026 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007027 }
7028 return len;
7029}
7030
7031static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7032 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007033 .get = nfs4_xattr_get_nfs4_label,
7034 .set = nfs4_xattr_set_nfs4_label,
7035};
David Quigleyc9bccef2013-05-22 12:50:45 -04007036
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007037#else
7038
7039static ssize_t
7040nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7041{
7042 return 0;
7043}
7044
7045#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007046
Andy Adamson533eb462011-06-13 18:25:56 -04007047/*
7048 * nfs_fhget will use either the mounted_on_fileid or the fileid
7049 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007050static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7051{
Andy Adamson533eb462011-06-13 18:25:56 -04007052 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7053 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7054 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007055 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007056 return;
7057
7058 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007059 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007060 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7061 fattr->nlink = 2;
7062}
7063
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007064static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7065 const struct qstr *name,
7066 struct nfs4_fs_locations *fs_locations,
7067 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007068{
7069 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007070 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007071 struct nfs4_fs_locations_arg args = {
7072 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007073 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007074 .page = page,
7075 .bitmask = bitmask,
7076 };
Benny Halevy22958462009-04-01 09:22:02 -04007077 struct nfs4_fs_locations_res res = {
7078 .fs_locations = fs_locations,
7079 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007080 struct rpc_message msg = {
7081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7082 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007083 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007084 };
7085 int status;
7086
Harvey Harrison3110ff82008-05-02 13:42:44 -07007087 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007088
Chuck Leverc05cefc2017-11-05 15:45:22 -05007089 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7090 bitmask[1] = nfs4_fattr_bitmap[1];
7091
Andy Adamson533eb462011-06-13 18:25:56 -04007092 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7093 * is not supported */
7094 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007095 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007096 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007097 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007098
Trond Myklebustc228fd32007-01-13 02:28:11 -05007099 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007100 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007101 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007102 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007103 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007104 return status;
7105}
7106
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007107int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7108 const struct qstr *name,
7109 struct nfs4_fs_locations *fs_locations,
7110 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007111{
7112 struct nfs4_exception exception = { };
7113 int err;
7114 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007115 err = _nfs4_proc_fs_locations(client, dir, name,
7116 fs_locations, page);
7117 trace_nfs4_get_fs_locations(dir, name, err);
7118 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007119 &exception);
7120 } while (exception.retry);
7121 return err;
7122}
7123
Chuck Leverb03d7352013-10-17 14:12:50 -04007124/*
7125 * This operation also signals the server that this client is
7126 * performing migration recovery. The server can stop returning
7127 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7128 * appended to this compound to identify the client ID which is
7129 * performing recovery.
7130 */
7131static int _nfs40_proc_get_locations(struct inode *inode,
7132 struct nfs4_fs_locations *locations,
7133 struct page *page, struct rpc_cred *cred)
7134{
7135 struct nfs_server *server = NFS_SERVER(inode);
7136 struct rpc_clnt *clnt = server->client;
7137 u32 bitmask[2] = {
7138 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7139 };
7140 struct nfs4_fs_locations_arg args = {
7141 .clientid = server->nfs_client->cl_clientid,
7142 .fh = NFS_FH(inode),
7143 .page = page,
7144 .bitmask = bitmask,
7145 .migration = 1, /* skip LOOKUP */
7146 .renew = 1, /* append RENEW */
7147 };
7148 struct nfs4_fs_locations_res res = {
7149 .fs_locations = locations,
7150 .migration = 1,
7151 .renew = 1,
7152 };
7153 struct rpc_message msg = {
7154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7155 .rpc_argp = &args,
7156 .rpc_resp = &res,
7157 .rpc_cred = cred,
7158 };
7159 unsigned long now = jiffies;
7160 int status;
7161
7162 nfs_fattr_init(&locations->fattr);
7163 locations->server = server;
7164 locations->nlocations = 0;
7165
Anna Schumakerfba83f32018-05-04 16:22:50 -04007166 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007167 status = nfs4_call_sync_sequence(clnt, server, &msg,
7168 &args.seq_args, &res.seq_res);
7169 if (status)
7170 return status;
7171
7172 renew_lease(server, now);
7173 return 0;
7174}
7175
7176#ifdef CONFIG_NFS_V4_1
7177
7178/*
7179 * This operation also signals the server that this client is
7180 * performing migration recovery. The server can stop asserting
7181 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7182 * performing this operation is identified in the SEQUENCE
7183 * operation in this compound.
7184 *
7185 * When the client supports GETATTR(fs_locations_info), it can
7186 * be plumbed in here.
7187 */
7188static int _nfs41_proc_get_locations(struct inode *inode,
7189 struct nfs4_fs_locations *locations,
7190 struct page *page, struct rpc_cred *cred)
7191{
7192 struct nfs_server *server = NFS_SERVER(inode);
7193 struct rpc_clnt *clnt = server->client;
7194 u32 bitmask[2] = {
7195 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7196 };
7197 struct nfs4_fs_locations_arg args = {
7198 .fh = NFS_FH(inode),
7199 .page = page,
7200 .bitmask = bitmask,
7201 .migration = 1, /* skip LOOKUP */
7202 };
7203 struct nfs4_fs_locations_res res = {
7204 .fs_locations = locations,
7205 .migration = 1,
7206 };
7207 struct rpc_message msg = {
7208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7209 .rpc_argp = &args,
7210 .rpc_resp = &res,
7211 .rpc_cred = cred,
7212 };
7213 int status;
7214
7215 nfs_fattr_init(&locations->fattr);
7216 locations->server = server;
7217 locations->nlocations = 0;
7218
Anna Schumakerfba83f32018-05-04 16:22:50 -04007219 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007220 status = nfs4_call_sync_sequence(clnt, server, &msg,
7221 &args.seq_args, &res.seq_res);
7222 if (status == NFS4_OK &&
7223 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7224 status = -NFS4ERR_LEASE_MOVED;
7225 return status;
7226}
7227
7228#endif /* CONFIG_NFS_V4_1 */
7229
7230/**
7231 * nfs4_proc_get_locations - discover locations for a migrated FSID
7232 * @inode: inode on FSID that is migrating
7233 * @locations: result of query
7234 * @page: buffer
7235 * @cred: credential to use for this operation
7236 *
7237 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7238 * operation failed, or a negative errno if a local error occurred.
7239 *
7240 * On success, "locations" is filled in, but if the server has
7241 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7242 * asserted.
7243 *
7244 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7245 * from this client that require migration recovery.
7246 */
7247int nfs4_proc_get_locations(struct inode *inode,
7248 struct nfs4_fs_locations *locations,
7249 struct page *page, struct rpc_cred *cred)
7250{
7251 struct nfs_server *server = NFS_SERVER(inode);
7252 struct nfs_client *clp = server->nfs_client;
7253 const struct nfs4_mig_recovery_ops *ops =
7254 clp->cl_mvops->mig_recovery_ops;
7255 struct nfs4_exception exception = { };
7256 int status;
7257
7258 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7259 (unsigned long long)server->fsid.major,
7260 (unsigned long long)server->fsid.minor,
7261 clp->cl_hostname);
7262 nfs_display_fhandle(NFS_FH(inode), __func__);
7263
7264 do {
7265 status = ops->get_locations(inode, locations, page, cred);
7266 if (status != -NFS4ERR_DELAY)
7267 break;
7268 nfs4_handle_exception(server, status, &exception);
7269 } while (exception.retry);
7270 return status;
7271}
7272
Chuck Lever44c99932013-10-17 14:13:30 -04007273/*
7274 * This operation also signals the server that this client is
7275 * performing "lease moved" recovery. The server can stop
7276 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7277 * is appended to this compound to identify the client ID which is
7278 * performing recovery.
7279 */
7280static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7281{
7282 struct nfs_server *server = NFS_SERVER(inode);
7283 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7284 struct rpc_clnt *clnt = server->client;
7285 struct nfs4_fsid_present_arg args = {
7286 .fh = NFS_FH(inode),
7287 .clientid = clp->cl_clientid,
7288 .renew = 1, /* append RENEW */
7289 };
7290 struct nfs4_fsid_present_res res = {
7291 .renew = 1,
7292 };
7293 struct rpc_message msg = {
7294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7295 .rpc_argp = &args,
7296 .rpc_resp = &res,
7297 .rpc_cred = cred,
7298 };
7299 unsigned long now = jiffies;
7300 int status;
7301
7302 res.fh = nfs_alloc_fhandle();
7303 if (res.fh == NULL)
7304 return -ENOMEM;
7305
Anna Schumakerfba83f32018-05-04 16:22:50 -04007306 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007307 status = nfs4_call_sync_sequence(clnt, server, &msg,
7308 &args.seq_args, &res.seq_res);
7309 nfs_free_fhandle(res.fh);
7310 if (status)
7311 return status;
7312
7313 do_renew_lease(clp, now);
7314 return 0;
7315}
7316
7317#ifdef CONFIG_NFS_V4_1
7318
7319/*
7320 * This operation also signals the server that this client is
7321 * performing "lease moved" recovery. The server can stop asserting
7322 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7323 * this operation is identified in the SEQUENCE operation in this
7324 * compound.
7325 */
7326static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7327{
7328 struct nfs_server *server = NFS_SERVER(inode);
7329 struct rpc_clnt *clnt = server->client;
7330 struct nfs4_fsid_present_arg args = {
7331 .fh = NFS_FH(inode),
7332 };
7333 struct nfs4_fsid_present_res res = {
7334 };
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7337 .rpc_argp = &args,
7338 .rpc_resp = &res,
7339 .rpc_cred = cred,
7340 };
7341 int status;
7342
7343 res.fh = nfs_alloc_fhandle();
7344 if (res.fh == NULL)
7345 return -ENOMEM;
7346
Anna Schumakerfba83f32018-05-04 16:22:50 -04007347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007348 status = nfs4_call_sync_sequence(clnt, server, &msg,
7349 &args.seq_args, &res.seq_res);
7350 nfs_free_fhandle(res.fh);
7351 if (status == NFS4_OK &&
7352 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7353 status = -NFS4ERR_LEASE_MOVED;
7354 return status;
7355}
7356
7357#endif /* CONFIG_NFS_V4_1 */
7358
7359/**
7360 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7361 * @inode: inode on FSID to check
7362 * @cred: credential to use for this operation
7363 *
7364 * Server indicates whether the FSID is present, moved, or not
7365 * recognized. This operation is necessary to clear a LEASE_MOVED
7366 * condition for this client ID.
7367 *
7368 * Returns NFS4_OK if the FSID is present on this server,
7369 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7370 * NFS4ERR code if some error occurred on the server, or a
7371 * negative errno if a local failure occurred.
7372 */
7373int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7374{
7375 struct nfs_server *server = NFS_SERVER(inode);
7376 struct nfs_client *clp = server->nfs_client;
7377 const struct nfs4_mig_recovery_ops *ops =
7378 clp->cl_mvops->mig_recovery_ops;
7379 struct nfs4_exception exception = { };
7380 int status;
7381
7382 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7383 (unsigned long long)server->fsid.major,
7384 (unsigned long long)server->fsid.minor,
7385 clp->cl_hostname);
7386 nfs_display_fhandle(NFS_FH(inode), __func__);
7387
7388 do {
7389 status = ops->fsid_present(inode, cred);
7390 if (status != -NFS4ERR_DELAY)
7391 break;
7392 nfs4_handle_exception(server, status, &exception);
7393 } while (exception.retry);
7394 return status;
7395}
7396
Andy Adamson5ec16a82013-08-08 10:57:55 -04007397/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007398 * If 'use_integrity' is true and the state managment nfs_client
7399 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7400 * and the machine credential as per RFC3530bis and RFC5661 Security
7401 * Considerations sections. Otherwise, just use the user cred with the
7402 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007403 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007404static 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 +00007405{
7406 int status;
7407 struct nfs4_secinfo_arg args = {
7408 .dir_fh = NFS_FH(dir),
7409 .name = name,
7410 };
7411 struct nfs4_secinfo_res res = {
7412 .flavors = flavors,
7413 };
7414 struct rpc_message msg = {
7415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7416 .rpc_argp = &args,
7417 .rpc_resp = &res,
7418 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007419 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007420 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007421
7422 if (use_integrity) {
7423 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007424 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7425 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007426 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007427
7428 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007429
7430 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7431 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7432
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007433 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7434 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007435 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007436
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007437 if (cred)
7438 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007439
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007440 return status;
7441}
7442
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007443int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7444 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007445{
7446 struct nfs4_exception exception = { };
7447 int err;
7448 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007449 err = -NFS4ERR_WRONGSEC;
7450
7451 /* try to use integrity protection with machine cred */
7452 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7453 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7454
7455 /*
7456 * if unable to use integrity protection, or SECINFO with
7457 * integrity protection returns NFS4ERR_WRONGSEC (which is
7458 * disallowed by spec, but exists in deployed servers) use
7459 * the current filesystem's rpc_client and the user cred.
7460 */
7461 if (err == -NFS4ERR_WRONGSEC)
7462 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7463
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007464 trace_nfs4_secinfo(dir, name, err);
7465 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007466 &exception);
7467 } while (exception.retry);
7468 return err;
7469}
7470
Andy Adamson557134a2009-04-01 09:21:53 -04007471#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007472/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007473 * Check the exchange flags returned by the server for invalid flags, having
7474 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7475 * DS flags set.
7476 */
7477static int nfs4_check_cl_exchange_flags(u32 flags)
7478{
7479 if (flags & ~EXCHGID4_FLAG_MASK_R)
7480 goto out_inval;
7481 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7482 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7483 goto out_inval;
7484 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7485 goto out_inval;
7486 return NFS_OK;
7487out_inval:
7488 return -NFS4ERR_INVAL;
7489}
7490
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007491static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007492nfs41_same_server_scope(struct nfs41_server_scope *a,
7493 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007494{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007495 if (a->server_scope_sz != b->server_scope_sz)
7496 return false;
7497 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007498}
7499
Andy Adamson02a95de2016-02-05 16:08:37 -05007500static void
7501nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7502{
7503}
7504
7505static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7506 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7507};
7508
Andy Adamson357f54d2010-12-14 10:11:57 -05007509/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007510 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007511 *
7512 * The 4.1 client currently uses the same TCP connection for the
7513 * fore and backchannel.
7514 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007515static
7516int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7517 struct rpc_xprt *xprt,
7518 struct nfs_client *clp,
7519 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007520{
7521 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007522 struct nfs41_bind_conn_to_session_args args = {
7523 .client = clp,
7524 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7525 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007526 struct nfs41_bind_conn_to_session_res res;
7527 struct rpc_message msg = {
7528 .rpc_proc =
7529 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007530 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007531 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007532 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007533 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007534 struct rpc_task_setup task_setup_data = {
7535 .rpc_client = clnt,
7536 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007537 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007538 .rpc_message = &msg,
7539 .flags = RPC_TASK_TIMEOUT,
7540 };
7541 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007542
Trond Myklebust71a097c2015-02-18 09:27:18 -08007543 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7544 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7545 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007546
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007547 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7548 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7549 args.dir = NFS4_CDFC4_FORE;
7550
7551 task = rpc_run_task(&task_setup_data);
7552 if (!IS_ERR(task)) {
7553 status = task->tk_status;
7554 rpc_put_task(task);
7555 } else
7556 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007557 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007558 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007559 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007560 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7561 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007562 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007563 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007564 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007565 dprintk("NFS: %s: Unexpected direction from server\n",
7566 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007567 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007568 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007569 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007570 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7571 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007572 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007573 }
7574 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007575
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007576 return status;
7577}
7578
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007579struct rpc_bind_conn_calldata {
7580 struct nfs_client *clp;
7581 struct rpc_cred *cred;
7582};
7583
7584static int
7585nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7586 struct rpc_xprt *xprt,
7587 void *calldata)
7588{
7589 struct rpc_bind_conn_calldata *p = calldata;
7590
7591 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7592}
7593
7594int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7595{
7596 struct rpc_bind_conn_calldata data = {
7597 .clp = clp,
7598 .cred = cred,
7599 };
7600 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7601 nfs4_proc_bind_conn_to_session_callback, &data);
7602}
7603
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007604/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007605 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7606 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007607 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007608static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7609 .how = SP4_MACH_CRED,
7610 .enforce.u.words = {
7611 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7612 1 << (OP_EXCHANGE_ID - 32) |
7613 1 << (OP_CREATE_SESSION - 32) |
7614 1 << (OP_DESTROY_SESSION - 32) |
7615 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007616 },
7617 .allow.u.words = {
7618 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007619 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007620 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007621 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007622 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007623 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007624 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007625 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007626 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007627 1 << (OP_FREE_STATEID - 32) |
7628 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007629 }
7630};
7631
7632/*
7633 * Select the state protection mode for client `clp' given the server results
7634 * from exchange_id in `sp'.
7635 *
7636 * Returns 0 on success, negative errno otherwise.
7637 */
7638static int nfs4_sp4_select_mode(struct nfs_client *clp,
7639 struct nfs41_state_protection *sp)
7640{
7641 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7642 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7643 1 << (OP_EXCHANGE_ID - 32) |
7644 1 << (OP_CREATE_SESSION - 32) |
7645 1 << (OP_DESTROY_SESSION - 32) |
7646 1 << (OP_DESTROY_CLIENTID - 32)
7647 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007648 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007649 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007650 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007651
7652 if (sp->how == SP4_MACH_CRED) {
7653 /* Print state protect result */
7654 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7655 for (i = 0; i <= LAST_NFS4_OP; i++) {
7656 if (test_bit(i, sp->enforce.u.longs))
7657 dfprintk(MOUNT, " enforce op %d\n", i);
7658 if (test_bit(i, sp->allow.u.longs))
7659 dfprintk(MOUNT, " allow op %d\n", i);
7660 }
7661
7662 /* make sure nothing is on enforce list that isn't supported */
7663 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7664 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7665 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007666 ret = -EINVAL;
7667 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007668 }
7669 }
7670
7671 /*
7672 * Minimal mode - state operations are allowed to use machine
7673 * credential. Note this already happens by default, so the
7674 * client doesn't have to do anything more than the negotiation.
7675 *
7676 * NOTE: we don't care if EXCHANGE_ID is in the list -
7677 * we're already using the machine cred for exchange_id
7678 * and will never use a different cred.
7679 */
7680 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7681 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7682 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7683 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7684 dfprintk(MOUNT, "sp4_mach_cred:\n");
7685 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007686 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007687 } else {
7688 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007689 ret = -EINVAL;
7690 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007691 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007692
7693 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007694 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7695 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007696 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7697 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007698 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007699 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007700
Andrew Elble99ade3c2015-12-02 09:39:51 -05007701 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7702 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007703 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007704 }
7705
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007706 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7707 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7708 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007709 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007710 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007711
7712 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7713 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7714 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007715 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007716 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007717
7718 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7719 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007720 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007721 }
7722
7723 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7724 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007725 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007726 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007727 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007728out:
7729 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007730 return 0;
7731}
7732
Andy Adamson8d89bd72016-09-09 09:22:18 -04007733struct nfs41_exchange_id_data {
7734 struct nfs41_exchange_id_res res;
7735 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007736};
7737
Andy Adamson8d89bd72016-09-09 09:22:18 -04007738static void nfs4_exchange_id_release(void *data)
7739{
7740 struct nfs41_exchange_id_data *cdata =
7741 (struct nfs41_exchange_id_data *)data;
7742
Olga Kornievskaia63513232017-03-13 10:36:19 -04007743 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007744 kfree(cdata->res.impl_id);
7745 kfree(cdata->res.server_scope);
7746 kfree(cdata->res.server_owner);
7747 kfree(cdata);
7748}
7749
7750static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007751 .rpc_release = nfs4_exchange_id_release,
7752};
7753
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007754/*
7755 * _nfs4_proc_exchange_id()
7756 *
7757 * Wrapper for EXCHANGE_ID operation.
7758 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007759static struct rpc_task *
7760nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007761 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007762{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007763 struct rpc_message msg = {
7764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007765 .rpc_cred = cred,
7766 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007767 struct rpc_task_setup task_setup_data = {
7768 .rpc_client = clp->cl_rpcclient,
7769 .callback_ops = &nfs4_exchange_id_call_ops,
7770 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007771 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007772 };
7773 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007774 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007775
Elena Reshetova212bf412017-10-20 12:53:38 +03007776 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007777 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007778
Trond Myklebust9c760d12017-07-31 18:38:50 -04007779 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007780 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007781 if (!calldata)
7782 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007783
Trond Myklebustfd405592017-08-01 16:02:47 -04007784 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007785
7786 status = nfs4_init_uniform_client_string(clp);
7787 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007788 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007789
Andy Adamson8d89bd72016-09-09 09:22:18 -04007790 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7791 GFP_NOFS);
7792 status = -ENOMEM;
7793 if (unlikely(calldata->res.server_owner == NULL))
7794 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007795
Andy Adamson8d89bd72016-09-09 09:22:18 -04007796 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007797 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007798 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007799 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007800
Andy Adamson8d89bd72016-09-09 09:22:18 -04007801 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7802 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007803 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007804
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007805 switch (sp4_how) {
7806 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007807 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007808 break;
7809
7810 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007811 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007812 break;
7813
7814 default:
7815 /* unsupported! */
7816 WARN_ON_ONCE(1);
7817 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007818 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007819 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007820 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007821 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007822 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007823 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7824 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007825 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007826 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007827 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7828 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007829#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007830 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007831#endif
7832 msg.rpc_argp = &calldata->args;
7833 msg.rpc_resp = &calldata->res;
7834 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007835
Trond Myklebust9c760d12017-07-31 18:38:50 -04007836 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007837
7838out_impl_id:
7839 kfree(calldata->res.impl_id);
7840out_server_scope:
7841 kfree(calldata->res.server_scope);
7842out_server_owner:
7843 kfree(calldata->res.server_owner);
7844out_calldata:
7845 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007846out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007847 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007848 return ERR_PTR(status);
7849}
7850
7851/*
7852 * _nfs4_proc_exchange_id()
7853 *
7854 * Wrapper for EXCHANGE_ID operation.
7855 */
7856static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7857 u32 sp4_how)
7858{
7859 struct rpc_task *task;
7860 struct nfs41_exchange_id_args *argp;
7861 struct nfs41_exchange_id_res *resp;
7862 int status;
7863
7864 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7865 if (IS_ERR(task))
7866 return PTR_ERR(task);
7867
7868 argp = task->tk_msg.rpc_argp;
7869 resp = task->tk_msg.rpc_resp;
7870 status = task->tk_status;
7871 if (status != 0)
7872 goto out;
7873
7874 status = nfs4_check_cl_exchange_flags(resp->flags);
7875 if (status != 0)
7876 goto out;
7877
7878 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7879 if (status != 0)
7880 goto out;
7881
7882 clp->cl_clientid = resp->clientid;
7883 clp->cl_exchange_flags = resp->flags;
7884 clp->cl_seqid = resp->seqid;
7885 /* Client ID is not confirmed */
7886 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7887 clear_bit(NFS4_SESSION_ESTABLISHED,
7888 &clp->cl_session->session_state);
7889
7890 if (clp->cl_serverscope != NULL &&
7891 !nfs41_same_server_scope(clp->cl_serverscope,
7892 resp->server_scope)) {
7893 dprintk("%s: server_scope mismatch detected\n",
7894 __func__);
7895 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7896 }
7897
7898 swap(clp->cl_serverowner, resp->server_owner);
7899 swap(clp->cl_serverscope, resp->server_scope);
7900 swap(clp->cl_implid, resp->impl_id);
7901
7902 /* Save the EXCHANGE_ID verifier session trunk tests */
7903 memcpy(clp->cl_confirm.data, argp->verifier.data,
7904 sizeof(clp->cl_confirm.data));
7905out:
7906 trace_nfs4_exchange_id(clp, status);
7907 rpc_put_task(task);
7908 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007909}
7910
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007911/*
7912 * nfs4_proc_exchange_id()
7913 *
7914 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7915 *
7916 * Since the clientid has expired, all compounds using sessions
7917 * associated with the stale clientid will be returning
7918 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7919 * be in some phase of session reset.
7920 *
7921 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7922 */
7923int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7924{
7925 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7926 int status;
7927
7928 /* try SP4_MACH_CRED if krb5i/p */
7929 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7930 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007931 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007932 if (!status)
7933 return 0;
7934 }
7935
7936 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007937 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007938}
7939
Andy Adamson04fa2c62016-09-09 09:22:29 -04007940/**
7941 * nfs4_test_session_trunk
7942 *
7943 * This is an add_xprt_test() test function called from
7944 * rpc_clnt_setup_test_and_add_xprt.
7945 *
7946 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7947 * and is dereferrenced in nfs4_exchange_id_release
7948 *
7949 * Upon success, add the new transport to the rpc_clnt
7950 *
7951 * @clnt: struct rpc_clnt to get new transport
7952 * @xprt: the rpc_xprt to test
7953 * @data: call data for _nfs4_proc_exchange_id.
7954 */
7955int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7956 void *data)
7957{
7958 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007959 struct rpc_task *task;
7960 int status;
7961
Andy Adamson04fa2c62016-09-09 09:22:29 -04007962 u32 sp4_how;
7963
7964 dprintk("--> %s try %s\n", __func__,
7965 xprt->address_strings[RPC_DISPLAY_ADDR]);
7966
7967 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7968
7969 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007970 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7971 if (IS_ERR(task))
7972 return PTR_ERR(task);
7973
7974 status = task->tk_status;
7975 if (status == 0)
7976 status = nfs4_detect_session_trunking(adata->clp,
7977 task->tk_msg.rpc_resp, xprt);
7978
7979 rpc_put_task(task);
7980 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007981}
7982EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7983
Trond Myklebust66245532012-05-25 17:18:09 -04007984static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7985 struct rpc_cred *cred)
7986{
7987 struct rpc_message msg = {
7988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7989 .rpc_argp = clp,
7990 .rpc_cred = cred,
7991 };
7992 int status;
7993
7994 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007995 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007996 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007997 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007998 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7999 return status;
8000}
8001
8002static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8003 struct rpc_cred *cred)
8004{
8005 unsigned int loop;
8006 int ret;
8007
8008 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8009 ret = _nfs4_proc_destroy_clientid(clp, cred);
8010 switch (ret) {
8011 case -NFS4ERR_DELAY:
8012 case -NFS4ERR_CLIENTID_BUSY:
8013 ssleep(1);
8014 break;
8015 default:
8016 return ret;
8017 }
8018 }
8019 return 0;
8020}
8021
8022int nfs4_destroy_clientid(struct nfs_client *clp)
8023{
8024 struct rpc_cred *cred;
8025 int ret = 0;
8026
8027 if (clp->cl_mvops->minor_version < 1)
8028 goto out;
8029 if (clp->cl_exchange_flags == 0)
8030 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008031 if (clp->cl_preserve_clid)
8032 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008033 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008034 ret = nfs4_proc_destroy_clientid(clp, cred);
8035 if (cred)
8036 put_rpccred(cred);
8037 switch (ret) {
8038 case 0:
8039 case -NFS4ERR_STALE_CLIENTID:
8040 clp->cl_exchange_flags = 0;
8041 }
8042out:
8043 return ret;
8044}
8045
Andy Adamson2050f0c2009-04-01 09:22:30 -04008046struct nfs4_get_lease_time_data {
8047 struct nfs4_get_lease_time_args *args;
8048 struct nfs4_get_lease_time_res *res;
8049 struct nfs_client *clp;
8050};
8051
8052static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8053 void *calldata)
8054{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008055 struct nfs4_get_lease_time_data *data =
8056 (struct nfs4_get_lease_time_data *)calldata;
8057
8058 dprintk("--> %s\n", __func__);
8059 /* just setup sequence, do not trigger session recovery
8060 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008061 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008062 &data->args->la_seq_args,
8063 &data->res->lr_seq_res,
8064 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008065 dprintk("<-- %s\n", __func__);
8066}
8067
8068/*
8069 * Called from nfs4_state_manager thread for session setup, so don't recover
8070 * from sequence operation or clientid errors.
8071 */
8072static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8073{
8074 struct nfs4_get_lease_time_data *data =
8075 (struct nfs4_get_lease_time_data *)calldata;
8076
8077 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008078 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8079 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008080 switch (task->tk_status) {
8081 case -NFS4ERR_DELAY:
8082 case -NFS4ERR_GRACE:
8083 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8084 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8085 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008086 /* fall through */
8087 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008088 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008089 return;
8090 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008091 dprintk("<-- %s\n", __func__);
8092}
8093
Trond Myklebust17280172012-03-11 13:11:00 -04008094static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008095 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8096 .rpc_call_done = nfs4_get_lease_time_done,
8097};
8098
8099int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8100{
8101 struct rpc_task *task;
8102 struct nfs4_get_lease_time_args args;
8103 struct nfs4_get_lease_time_res res = {
8104 .lr_fsinfo = fsinfo,
8105 };
8106 struct nfs4_get_lease_time_data data = {
8107 .args = &args,
8108 .res = &res,
8109 .clp = clp,
8110 };
8111 struct rpc_message msg = {
8112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8113 .rpc_argp = &args,
8114 .rpc_resp = &res,
8115 };
8116 struct rpc_task_setup task_setup = {
8117 .rpc_client = clp->cl_rpcclient,
8118 .rpc_message = &msg,
8119 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008120 .callback_data = &data,
8121 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008122 };
8123 int status;
8124
Anna Schumakerfba83f32018-05-04 16:22:50 -04008125 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008126 task = rpc_run_task(&task_setup);
8127
8128 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008129 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008130
Anna Schumakerf6148712017-04-07 14:15:23 -04008131 status = task->tk_status;
8132 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008133 return status;
8134}
8135
Andy Adamsonfc931582009-04-01 09:22:31 -04008136/*
8137 * Initialize the values to be used by the client in CREATE_SESSION
8138 * If nfs4_init_session set the fore channel request and response sizes,
8139 * use them.
8140 *
8141 * Set the back channel max_resp_sz_cached to zero to force the client to
8142 * always set csa_cachethis to FALSE because the current implementation
8143 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8144 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008145static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8146 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008147{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008148 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008149 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008150
Andy Adamson18aad3d2013-06-26 12:21:49 -04008151 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8152 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8153
Andy Adamsonfc931582009-04-01 09:22:31 -04008154 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008155 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8156 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008157 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008158 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008159
8160 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008161 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008162 __func__,
8163 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008164 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008165
8166 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008167 args->bc_attrs.max_rqst_sz = max_bc_payload;
8168 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008169 args->bc_attrs.max_resp_sz_cached = 0;
8170 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008171 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008172
8173 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8174 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8175 __func__,
8176 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8177 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8178 args->bc_attrs.max_reqs);
8179}
8180
Trond Myklebust79969dd2015-02-18 11:30:18 -08008181static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8182 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008183{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008184 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008185 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008186
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008187 if (rcvd->max_resp_sz > sent->max_resp_sz)
8188 return -EINVAL;
8189 /*
8190 * Our requested max_ops is the minimum we need; we're not
8191 * prepared to break up compounds into smaller pieces than that.
8192 * So, no point even trying to continue if the server won't
8193 * cooperate:
8194 */
8195 if (rcvd->max_ops < sent->max_ops)
8196 return -EINVAL;
8197 if (rcvd->max_reqs == 0)
8198 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008199 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8200 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008201 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008202}
8203
Trond Myklebust79969dd2015-02-18 11:30:18 -08008204static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8205 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008206{
8207 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008208 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008209
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008210 if (!(res->flags & SESSION4_BACK_CHAN))
8211 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008212 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8213 return -EINVAL;
8214 if (rcvd->max_resp_sz < sent->max_resp_sz)
8215 return -EINVAL;
8216 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8217 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008218 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008219 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008220 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008221 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008222out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008223 return 0;
8224}
Andy Adamson8d353012009-04-01 09:22:32 -04008225
Andy Adamson8d353012009-04-01 09:22:32 -04008226static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008227 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008228{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008229 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008230
Trond Myklebust79969dd2015-02-18 11:30:18 -08008231 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008232 if (ret)
8233 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008234 return nfs4_verify_back_channel_attrs(args, res);
8235}
8236
8237static void nfs4_update_session(struct nfs4_session *session,
8238 struct nfs41_create_session_res *res)
8239{
8240 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008241 /* Mark client id and session as being confirmed */
8242 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8243 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008244 session->flags = res->flags;
8245 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008246 if (res->flags & SESSION4_BACK_CHAN)
8247 memcpy(&session->bc_attrs, &res->bc_attrs,
8248 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008249}
8250
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008251static int _nfs4_proc_create_session(struct nfs_client *clp,
8252 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008253{
8254 struct nfs4_session *session = clp->cl_session;
8255 struct nfs41_create_session_args args = {
8256 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008257 .clientid = clp->cl_clientid,
8258 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008259 .cb_program = NFS4_CALLBACK,
8260 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008261 struct nfs41_create_session_res res;
8262
Andy Adamsonfc931582009-04-01 09:22:31 -04008263 struct rpc_message msg = {
8264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8265 .rpc_argp = &args,
8266 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008267 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008268 };
8269 int status;
8270
Chuck Lever6b26cc82016-05-02 14:40:40 -04008271 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008272 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008273
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008274 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008275 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008276
Trond Myklebustb519d402016-09-11 14:50:01 -04008277 switch (status) {
8278 case -NFS4ERR_STALE_CLIENTID:
8279 case -NFS4ERR_DELAY:
8280 case -ETIMEDOUT:
8281 case -EACCES:
8282 case -EAGAIN:
8283 goto out;
8284 };
8285
8286 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008287 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008288 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008289 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008290 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008291 if (status)
8292 goto out;
8293 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008294 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008295out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008296 return status;
8297}
8298
8299/*
8300 * Issues a CREATE_SESSION operation to the server.
8301 * It is the responsibility of the caller to verify the session is
8302 * expired before calling this routine.
8303 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008304int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008305{
8306 int status;
8307 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008308 struct nfs4_session *session = clp->cl_session;
8309
8310 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8311
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008312 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008313 if (status)
8314 goto out;
8315
Andy Adamsonaacd5532011-11-09 13:58:21 -05008316 /* Init or reset the session slot tables */
8317 status = nfs4_setup_session_slot_tables(session);
8318 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008319 if (status)
8320 goto out;
8321
8322 ptr = (unsigned *)&session->sess_id.data[0];
8323 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8324 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008325out:
8326 dprintk("<-- %s\n", __func__);
8327 return status;
8328}
8329
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008330/*
8331 * Issue the over-the-wire RPC DESTROY_SESSION.
8332 * The caller must serialize access to this routine.
8333 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008334int nfs4_proc_destroy_session(struct nfs4_session *session,
8335 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008336{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008337 struct rpc_message msg = {
8338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8339 .rpc_argp = session,
8340 .rpc_cred = cred,
8341 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008342 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008343
8344 dprintk("--> nfs4_proc_destroy_session\n");
8345
8346 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008347 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8348 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008349
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008350 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008351 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008352
8353 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008354 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008355 "Session has been destroyed regardless...\n", status);
8356
8357 dprintk("<-- nfs4_proc_destroy_session\n");
8358 return status;
8359}
8360
Trond Myklebust7b38c362012-05-23 13:23:31 -04008361/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008362 * Renew the cl_session lease.
8363 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008364struct nfs4_sequence_data {
8365 struct nfs_client *clp;
8366 struct nfs4_sequence_args args;
8367 struct nfs4_sequence_res res;
8368};
8369
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008370static void nfs41_sequence_release(void *data)
8371{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008372 struct nfs4_sequence_data *calldata = data;
8373 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008374
Elena Reshetova212bf412017-10-20 12:53:38 +03008375 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008376 nfs4_schedule_state_renewal(clp);
8377 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008378 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008379}
8380
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008381static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8382{
8383 switch(task->tk_status) {
8384 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008385 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8386 return -EAGAIN;
8387 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008388 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008389 }
8390 return 0;
8391}
8392
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008393static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008394{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008395 struct nfs4_sequence_data *calldata = data;
8396 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008397
Trond Myklebust14516c32010-07-31 14:29:06 -04008398 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8399 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008400
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008401 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008402 if (task->tk_status < 0) {
8403 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008404 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008405 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008406
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008407 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8408 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008409 return;
8410 }
8411 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008412 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008413out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008414 dprintk("<-- %s\n", __func__);
8415}
8416
8417static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8418{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008419 struct nfs4_sequence_data *calldata = data;
8420 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008421 struct nfs4_sequence_args *args;
8422 struct nfs4_sequence_res *res;
8423
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008424 args = task->tk_msg.rpc_argp;
8425 res = task->tk_msg.rpc_resp;
8426
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008427 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008428}
8429
8430static const struct rpc_call_ops nfs41_sequence_ops = {
8431 .rpc_call_done = nfs41_sequence_call_done,
8432 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008433 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008434};
8435
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008436static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8437 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008438 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008439 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008440{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008441 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008442 struct rpc_message msg = {
8443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8444 .rpc_cred = cred,
8445 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008446 struct rpc_task_setup task_setup_data = {
8447 .rpc_client = clp->cl_rpcclient,
8448 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008449 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008450 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008451 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008452 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008453
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008454 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008455 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008456 goto out_err;
8457
8458 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008459 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008460 if (calldata == NULL)
8461 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008462 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008463 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008464 msg.rpc_argp = &calldata->args;
8465 msg.rpc_resp = &calldata->res;
8466 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008467 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008468
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008469 ret = rpc_run_task(&task_setup_data);
8470 if (IS_ERR(ret))
8471 goto out_err;
8472 return ret;
8473out_put_clp:
8474 nfs_put_client(clp);
8475out_err:
8476 nfs41_release_slot(slot);
8477 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008478}
8479
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008480static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008481{
8482 struct rpc_task *task;
8483 int ret = 0;
8484
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008485 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008486 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008487 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008488 if (IS_ERR(task))
8489 ret = PTR_ERR(task);
8490 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008491 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008492 dprintk("<-- %s status=%d\n", __func__, ret);
8493 return ret;
8494}
8495
8496static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8497{
8498 struct rpc_task *task;
8499 int ret;
8500
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008501 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008502 if (IS_ERR(task)) {
8503 ret = PTR_ERR(task);
8504 goto out;
8505 }
8506 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008507 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008508 ret = task->tk_status;
8509 rpc_put_task(task);
8510out:
8511 dprintk("<-- %s status=%d\n", __func__, ret);
8512 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008513}
8514
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008515struct nfs4_reclaim_complete_data {
8516 struct nfs_client *clp;
8517 struct nfs41_reclaim_complete_args arg;
8518 struct nfs41_reclaim_complete_res res;
8519};
8520
8521static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8522{
8523 struct nfs4_reclaim_complete_data *calldata = data;
8524
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008525 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008526 &calldata->arg.seq_args,
8527 &calldata->res.seq_res,
8528 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008529}
8530
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008531static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8532{
8533 switch(task->tk_status) {
8534 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008535 wake_up_all(&clp->cl_lock_waitq);
8536 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008537 case -NFS4ERR_COMPLETE_ALREADY:
8538 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8539 break;
8540 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008541 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008542 /* fall through */
8543 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008544 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008545 case -NFS4ERR_BADSESSION:
8546 case -NFS4ERR_DEADSESSION:
8547 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8548 nfs4_schedule_session_recovery(clp->cl_session,
8549 task->tk_status);
8550 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008551 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008552 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008553 }
8554 return 0;
8555}
8556
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008557static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8558{
8559 struct nfs4_reclaim_complete_data *calldata = data;
8560 struct nfs_client *clp = calldata->clp;
8561 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8562
8563 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008564 if (!nfs41_sequence_done(task, res))
8565 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008566
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008567 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008568 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8569 rpc_restart_call_prepare(task);
8570 return;
8571 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008572 dprintk("<-- %s\n", __func__);
8573}
8574
8575static void nfs4_free_reclaim_complete_data(void *data)
8576{
8577 struct nfs4_reclaim_complete_data *calldata = data;
8578
8579 kfree(calldata);
8580}
8581
8582static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8583 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8584 .rpc_call_done = nfs4_reclaim_complete_done,
8585 .rpc_release = nfs4_free_reclaim_complete_data,
8586};
8587
8588/*
8589 * Issue a global reclaim complete.
8590 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008591static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8592 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008593{
8594 struct nfs4_reclaim_complete_data *calldata;
8595 struct rpc_task *task;
8596 struct rpc_message msg = {
8597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008598 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008599 };
8600 struct rpc_task_setup task_setup_data = {
8601 .rpc_client = clp->cl_rpcclient,
8602 .rpc_message = &msg,
8603 .callback_ops = &nfs4_reclaim_complete_call_ops,
8604 .flags = RPC_TASK_ASYNC,
8605 };
8606 int status = -ENOMEM;
8607
8608 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008609 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008610 if (calldata == NULL)
8611 goto out;
8612 calldata->clp = clp;
8613 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008614
Anna Schumakerfba83f32018-05-04 16:22:50 -04008615 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008616 msg.rpc_argp = &calldata->arg;
8617 msg.rpc_resp = &calldata->res;
8618 task_setup_data.callback_data = calldata;
8619 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008620 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008621 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008622 goto out;
8623 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008624 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008625 if (status == 0)
8626 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008627 rpc_put_task(task);
8628out:
8629 dprintk("<-- %s status=%d\n", __func__, status);
8630 return status;
8631}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008632
8633static void
8634nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8635{
8636 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008637 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008638
8639 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008640 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008641 &lgp->res.seq_res, task);
8642 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008643}
8644
8645static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8646{
8647 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008648
8649 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008650 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008651 dprintk("<-- %s\n", __func__);
8652}
8653
8654static int
8655nfs4_layoutget_handle_exception(struct rpc_task *task,
8656 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8657{
Trond Myklebustee314c22012-10-01 17:25:48 -07008658 struct inode *inode = lgp->args.inode;
8659 struct nfs_server *server = NFS_SERVER(inode);
8660 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008661 int nfs4err = task->tk_status;
8662 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008663 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008664
Boaz Harroshed7e5422014-01-22 20:34:54 +02008665 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008666
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008667 nfs4_sequence_free_slot(&lgp->res.seq_res);
8668
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008669 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008671 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008672
8673 /*
8674 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8675 * on the file. set tk_status to -ENODATA to tell upper layer to
8676 * retry go inband.
8677 */
8678 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008679 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008680 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008681 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008682 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8683 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8684 */
8685 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008686 status = -EOVERFLOW;
8687 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008688 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008689 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008690 * (or clients) writing to the same RAID stripe except when
8691 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008692 *
8693 * Treat it like we would RECALLCONFLICT -- we retry for a little
8694 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008695 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008696 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008697 if (lgp->args.minlength == 0) {
8698 status = -EOVERFLOW;
8699 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008700 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008701 status = -EBUSY;
8702 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008703 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008704 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008705 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008706 case -NFS4ERR_DELEG_REVOKED:
8707 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008708 case -NFS4ERR_EXPIRED:
8709 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008710 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008711 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008712 lo = NFS_I(inode)->layout;
8713 /* If the open stateid was bad, then recover it. */
8714 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008715 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008716 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008717 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008718 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008719 break;
8720 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008721
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008722 /*
8723 * Mark the bad layout state as invalid, then retry
8724 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008725 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008726 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008727 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008728 pnfs_free_lseg_list(&head);
8729 status = -EAGAIN;
8730 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008731 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008732
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008733 err = nfs4_handle_exception(server, nfs4err, exception);
8734 if (!status) {
8735 if (exception->retry)
8736 status = -EAGAIN;
8737 else
8738 status = err;
8739 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008740out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008741 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008742 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008743}
8744
Fred Isamandacb4522016-09-19 17:47:09 -04008745size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008746{
8747 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8748 return nfs_page_array_len(0, max_resp_sz);
8749}
8750
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008751static void nfs4_layoutget_release(void *calldata)
8752{
8753 struct nfs4_layoutget *lgp = calldata;
8754
8755 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008756 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008757 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008758 dprintk("<-- %s\n", __func__);
8759}
8760
8761static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8762 .rpc_call_prepare = nfs4_layoutget_prepare,
8763 .rpc_call_done = nfs4_layoutget_done,
8764 .rpc_release = nfs4_layoutget_release,
8765};
8766
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008767struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008768nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008769{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008770 struct inode *inode = lgp->args.inode;
8771 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008772 struct rpc_task *task;
8773 struct rpc_message msg = {
8774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8775 .rpc_argp = &lgp->args,
8776 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008777 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008778 };
8779 struct rpc_task_setup task_setup_data = {
8780 .rpc_client = server->client,
8781 .rpc_message = &msg,
8782 .callback_ops = &nfs4_layoutget_call_ops,
8783 .callback_data = lgp,
8784 .flags = RPC_TASK_ASYNC,
8785 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008786 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008787 struct nfs4_exception exception = {
8788 .inode = inode,
8789 .timeout = *timeout,
8790 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008791 int status = 0;
8792
8793 dprintk("--> %s\n", __func__);
8794
Peng Tao4bd5a982014-11-17 11:05:17 +08008795 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8796 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8797
Anna Schumakerfba83f32018-05-04 16:22:50 -04008798 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008799
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008800 task = rpc_run_task(&task_setup_data);
8801 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008802 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008803 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008804 if (status != 0)
8805 goto out;
8806
8807 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8808 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008809 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8810 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008811 } else
8812 lseg = pnfs_layout_process(lgp);
8813out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008814 trace_nfs4_layoutget(lgp->args.ctx,
8815 &lgp->args.range,
8816 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008817 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008818 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008819
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008820 rpc_put_task(task);
8821 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008822 if (status)
8823 return ERR_PTR(status);
8824 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008825}
8826
Benny Halevycbe82602011-05-22 19:52:37 +03008827static void
8828nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8829{
8830 struct nfs4_layoutreturn *lrp = calldata;
8831
8832 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008833 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008834 &lrp->args.seq_args,
8835 &lrp->res.seq_res,
8836 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008837 if (!pnfs_layout_is_valid(lrp->args.layout))
8838 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008839}
8840
8841static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8842{
8843 struct nfs4_layoutreturn *lrp = calldata;
8844 struct nfs_server *server;
8845
8846 dprintk("--> %s\n", __func__);
8847
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008848 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008849 return;
8850
8851 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008852 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008853 case -NFS4ERR_OLD_STATEID:
8854 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8855 lrp->args.inode))
8856 goto out_restart;
8857 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008858 default:
8859 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008860 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008861 case 0:
8862 break;
8863 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008864 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008865 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008866 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008867 }
Benny Halevycbe82602011-05-22 19:52:37 +03008868 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008869 return;
8870out_restart:
8871 task->tk_status = 0;
8872 nfs4_sequence_free_slot(&lrp->res.seq_res);
8873 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008874}
8875
8876static void nfs4_layoutreturn_release(void *calldata)
8877{
8878 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008879 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008880
8881 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008882 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008883 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008884 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008885 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8886 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008887 pnfs_put_layout_hdr(lrp->args.layout);
8888 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008889 kfree(calldata);
8890 dprintk("<-- %s\n", __func__);
8891}
8892
8893static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8894 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8895 .rpc_call_done = nfs4_layoutreturn_done,
8896 .rpc_release = nfs4_layoutreturn_release,
8897};
8898
Peng Tao6c166052014-11-17 09:30:40 +08008899int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008900{
8901 struct rpc_task *task;
8902 struct rpc_message msg = {
8903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8904 .rpc_argp = &lrp->args,
8905 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008906 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008907 };
8908 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008909 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008910 .rpc_message = &msg,
8911 .callback_ops = &nfs4_layoutreturn_call_ops,
8912 .callback_data = lrp,
8913 };
Peng Tao6c166052014-11-17 09:30:40 +08008914 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008915
Andrew Elble99ade3c2015-12-02 09:39:51 -05008916 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8917 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8918 &task_setup_data.rpc_client, &msg);
8919
Benny Halevycbe82602011-05-22 19:52:37 +03008920 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008921 if (!sync) {
8922 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8923 if (!lrp->inode) {
8924 nfs4_layoutreturn_release(lrp);
8925 return -EAGAIN;
8926 }
8927 task_setup_data.flags |= RPC_TASK_ASYNC;
8928 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008929 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008930 task = rpc_run_task(&task_setup_data);
8931 if (IS_ERR(task))
8932 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008933 if (sync)
8934 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008935 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008936 dprintk("<-- %s status=%d\n", __func__, status);
8937 rpc_put_task(task);
8938 return status;
8939}
8940
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008941static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008942_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8943 struct pnfs_device *pdev,
8944 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008945{
8946 struct nfs4_getdeviceinfo_args args = {
8947 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008948 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8949 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008950 };
8951 struct nfs4_getdeviceinfo_res res = {
8952 .pdev = pdev,
8953 };
8954 struct rpc_message msg = {
8955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8956 .rpc_argp = &args,
8957 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008958 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008959 };
8960 int status;
8961
8962 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008963 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008964 if (res.notification & ~args.notify_types)
8965 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008966 if (res.notification != args.notify_types)
8967 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008968
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008969 dprintk("<-- %s status=%d\n", __func__, status);
8970
8971 return status;
8972}
8973
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008974int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8975 struct pnfs_device *pdev,
8976 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008977{
8978 struct nfs4_exception exception = { };
8979 int err;
8980
8981 do {
8982 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008983 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008984 &exception);
8985 } while (exception.retry);
8986 return err;
8987}
8988EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8989
Andy Adamson863a3c62011-03-23 13:27:54 +00008990static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8991{
8992 struct nfs4_layoutcommit_data *data = calldata;
8993 struct nfs_server *server = NFS_SERVER(data->args.inode);
8994
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008995 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008996 &data->args.seq_args,
8997 &data->res.seq_res,
8998 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008999}
9000
9001static void
9002nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9003{
9004 struct nfs4_layoutcommit_data *data = calldata;
9005 struct nfs_server *server = NFS_SERVER(data->args.inode);
9006
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009007 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009008 return;
9009
9010 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009011 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9012 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9013 case -NFS4ERR_BADLAYOUT: /* no layout */
9014 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009015 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009016 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009017 break;
9018 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009019 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009020 rpc_restart_call_prepare(task);
9021 return;
9022 }
9023 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009024}
9025
9026static void nfs4_layoutcommit_release(void *calldata)
9027{
9028 struct nfs4_layoutcommit_data *data = calldata;
9029
Andy Adamsondb29c082011-07-30 20:52:38 -04009030 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009031 nfs_post_op_update_inode_force_wcc(data->args.inode,
9032 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009033 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009034 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009035 kfree(data);
9036}
9037
9038static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9039 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9040 .rpc_call_done = nfs4_layoutcommit_done,
9041 .rpc_release = nfs4_layoutcommit_release,
9042};
9043
9044int
Andy Adamsonef311532011-03-12 02:58:10 -05009045nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009046{
9047 struct rpc_message msg = {
9048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9049 .rpc_argp = &data->args,
9050 .rpc_resp = &data->res,
9051 .rpc_cred = data->cred,
9052 };
9053 struct rpc_task_setup task_setup_data = {
9054 .task = &data->task,
9055 .rpc_client = NFS_CLIENT(data->args.inode),
9056 .rpc_message = &msg,
9057 .callback_ops = &nfs4_layoutcommit_ops,
9058 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009059 };
9060 struct rpc_task *task;
9061 int status = 0;
9062
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009063 dprintk("NFS: initiating layoutcommit call. sync %d "
9064 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009065 data->args.lastbytewritten,
9066 data->args.inode->i_ino);
9067
Trond Myklebust472e2592015-02-05 16:50:30 -05009068 if (!sync) {
9069 data->inode = nfs_igrab_and_active(data->args.inode);
9070 if (data->inode == NULL) {
9071 nfs4_layoutcommit_release(data);
9072 return -EAGAIN;
9073 }
9074 task_setup_data.flags = RPC_TASK_ASYNC;
9075 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009076 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009077 task = rpc_run_task(&task_setup_data);
9078 if (IS_ERR(task))
9079 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009080 if (sync)
9081 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009082 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009083 dprintk("%s: status %d\n", __func__, status);
9084 rpc_put_task(task);
9085 return status;
9086}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009087
Andy Adamson97431202013-08-08 10:57:56 -04009088/**
9089 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9090 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9091 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009092static int
9093_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009094 struct nfs_fsinfo *info,
9095 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009096{
9097 struct nfs41_secinfo_no_name_args args = {
9098 .style = SECINFO_STYLE_CURRENT_FH,
9099 };
9100 struct nfs4_secinfo_res res = {
9101 .flavors = flavors,
9102 };
9103 struct rpc_message msg = {
9104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9105 .rpc_argp = &args,
9106 .rpc_resp = &res,
9107 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009108 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009109 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009110 int status;
9111
9112 if (use_integrity) {
9113 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009114 cred = nfs4_get_clid_cred(server->nfs_client);
9115 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009116 }
9117
9118 dprintk("--> %s\n", __func__);
9119 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9120 &res.seq_res, 0);
9121 dprintk("<-- %s status=%d\n", __func__, status);
9122
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009123 if (cred)
9124 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009125
9126 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009127}
9128
9129static int
9130nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9131 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9132{
9133 struct nfs4_exception exception = { };
9134 int err;
9135 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009136 /* first try using integrity protection */
9137 err = -NFS4ERR_WRONGSEC;
9138
9139 /* try to use integrity protection with machine cred */
9140 if (_nfs4_is_integrity_protected(server->nfs_client))
9141 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9142 flavors, true);
9143
9144 /*
9145 * if unable to use integrity protection, or SECINFO with
9146 * integrity protection returns NFS4ERR_WRONGSEC (which is
9147 * disallowed by spec, but exists in deployed servers) use
9148 * the current filesystem's rpc_client and the user cred.
9149 */
9150 if (err == -NFS4ERR_WRONGSEC)
9151 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9152 flavors, false);
9153
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009154 switch (err) {
9155 case 0:
9156 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009157 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009158 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009159 default:
9160 err = nfs4_handle_exception(server, err, &exception);
9161 }
9162 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009163out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009164 return err;
9165}
9166
9167static int
9168nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9169 struct nfs_fsinfo *info)
9170{
9171 int err;
9172 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009173 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009174 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009175 struct nfs4_secinfo4 *secinfo;
9176 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009177
9178 page = alloc_page(GFP_KERNEL);
9179 if (!page) {
9180 err = -ENOMEM;
9181 goto out;
9182 }
9183
9184 flavors = page_address(page);
9185 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9186
9187 /*
9188 * Fall back on "guess and check" method if
9189 * the server doesn't support SECINFO_NO_NAME
9190 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009191 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009192 err = nfs4_find_root_sec(server, fhandle, info);
9193 goto out_freepage;
9194 }
9195 if (err)
9196 goto out_freepage;
9197
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009198 for (i = 0; i < flavors->num_flavors; i++) {
9199 secinfo = &flavors->flavors[i];
9200
9201 switch (secinfo->flavor) {
9202 case RPC_AUTH_NULL:
9203 case RPC_AUTH_UNIX:
9204 case RPC_AUTH_GSS:
9205 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9206 &secinfo->flavor_info);
9207 break;
9208 default:
9209 flavor = RPC_AUTH_MAXFLAVOR;
9210 break;
9211 }
9212
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009213 if (!nfs_auth_info_match(&server->auth_info, flavor))
9214 flavor = RPC_AUTH_MAXFLAVOR;
9215
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009216 if (flavor != RPC_AUTH_MAXFLAVOR) {
9217 err = nfs4_lookup_root_sec(server, fhandle,
9218 info, flavor);
9219 if (!err)
9220 break;
9221 }
9222 }
9223
9224 if (flavor == RPC_AUTH_MAXFLAVOR)
9225 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009226
9227out_freepage:
9228 put_page(page);
9229 if (err == -EACCES)
9230 return -EPERM;
9231out:
9232 return err;
9233}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009234
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009235static int _nfs41_test_stateid(struct nfs_server *server,
9236 nfs4_stateid *stateid,
9237 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009238{
9239 int status;
9240 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009241 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009242 };
9243 struct nfs41_test_stateid_res res;
9244 struct rpc_message msg = {
9245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9246 .rpc_argp = &args,
9247 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009248 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009249 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009250 struct rpc_clnt *rpc_client = server->client;
9251
9252 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9253 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009254
Chuck Lever38527b12012-07-11 16:30:23 -04009255 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009256 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009257 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009258 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009259 if (status != NFS_OK) {
9260 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009261 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009262 }
9263 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009264 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009265}
9266
Trond Myklebust43912bb2016-09-22 13:38:56 -04009267static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9268 int err, struct nfs4_exception *exception)
9269{
9270 exception->retry = 0;
9271 switch(err) {
9272 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009273 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009274 nfs4_handle_exception(server, err, exception);
9275 break;
9276 case -NFS4ERR_BADSESSION:
9277 case -NFS4ERR_BADSLOT:
9278 case -NFS4ERR_BAD_HIGH_SLOT:
9279 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9280 case -NFS4ERR_DEADSESSION:
9281 nfs4_do_handle_exception(server, err, exception);
9282 }
9283}
9284
Chuck Lever38527b12012-07-11 16:30:23 -04009285/**
9286 * nfs41_test_stateid - perform a TEST_STATEID operation
9287 *
9288 * @server: server / transport on which to perform the operation
9289 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009290 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009291 *
9292 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9293 * Otherwise a negative NFS4ERR value is returned if the operation
9294 * failed or the state ID is not currently valid.
9295 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009296static int nfs41_test_stateid(struct nfs_server *server,
9297 nfs4_stateid *stateid,
9298 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009299{
9300 struct nfs4_exception exception = { };
9301 int err;
9302 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009303 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009304 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009305 } while (exception.retry);
9306 return err;
9307}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009308
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009309struct nfs_free_stateid_data {
9310 struct nfs_server *server;
9311 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009312 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009313};
9314
9315static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9316{
9317 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009318 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009319 &data->args.seq_args,
9320 &data->res.seq_res,
9321 task);
9322}
9323
9324static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9325{
9326 struct nfs_free_stateid_data *data = calldata;
9327
9328 nfs41_sequence_done(task, &data->res.seq_res);
9329
9330 switch (task->tk_status) {
9331 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009332 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009333 rpc_restart_call_prepare(task);
9334 }
9335}
9336
9337static void nfs41_free_stateid_release(void *calldata)
9338{
9339 kfree(calldata);
9340}
9341
Trond Myklebust17f26b12013-08-21 15:48:42 -04009342static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009343 .rpc_call_prepare = nfs41_free_stateid_prepare,
9344 .rpc_call_done = nfs41_free_stateid_done,
9345 .rpc_release = nfs41_free_stateid_release,
9346};
9347
Anna Schumaker2f261022018-05-15 13:03:39 -04009348/**
9349 * nfs41_free_stateid - perform a FREE_STATEID operation
9350 *
9351 * @server: server / transport on which to perform the operation
9352 * @stateid: state ID to release
9353 * @cred: credential
9354 * @is_recovery: set to true if this call needs to be privileged
9355 *
9356 * Note: this function is always asynchronous.
9357 */
9358static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009359 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009360 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009361 bool privileged)
9362{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009363 struct rpc_message msg = {
9364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009365 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009366 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009367 struct rpc_task_setup task_setup = {
9368 .rpc_client = server->client,
9369 .rpc_message = &msg,
9370 .callback_ops = &nfs41_free_stateid_ops,
9371 .flags = RPC_TASK_ASYNC,
9372 };
9373 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009374 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009375
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009376 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9377 &task_setup.rpc_client, &msg);
9378
Chuck Lever38527b12012-07-11 16:30:23 -04009379 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009380 data = kmalloc(sizeof(*data), GFP_NOFS);
9381 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009382 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009383 data->server = server;
9384 nfs4_stateid_copy(&data->args.stateid, stateid);
9385
9386 task_setup.callback_data = data;
9387
9388 msg.rpc_argp = &data->args;
9389 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009390 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009391 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009392 if (IS_ERR(task))
9393 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009394 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009395 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009396}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009397
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009398static void
9399nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009400{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009401 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009402
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009403 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009404 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009405}
9406
Trond Myklebust36281ca2012-03-04 18:13:56 -05009407static bool nfs41_match_stateid(const nfs4_stateid *s1,
9408 const nfs4_stateid *s2)
9409{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009410 if (s1->type != s2->type)
9411 return false;
9412
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009413 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009414 return false;
9415
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009416 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009417 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009418
Anna Schumaker045c5512017-01-11 16:59:48 -05009419 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009420}
9421
Andy Adamson557134a2009-04-01 09:21:53 -04009422#endif /* CONFIG_NFS_V4_1 */
9423
Trond Myklebust36281ca2012-03-04 18:13:56 -05009424static bool nfs4_match_stateid(const nfs4_stateid *s1,
9425 const nfs4_stateid *s2)
9426{
Trond Myklebustf597c532012-03-04 18:13:56 -05009427 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009428}
9429
9430
Trond Myklebust17280172012-03-11 13:11:00 -04009431static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009432 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009433 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 .recover_open = nfs4_open_reclaim,
9435 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009436 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009437 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438};
9439
Andy Adamson591d71c2009-04-01 09:22:47 -04009440#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009441static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009442 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9443 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9444 .recover_open = nfs4_open_reclaim,
9445 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009446 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009447 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009448 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009449};
9450#endif /* CONFIG_NFS_V4_1 */
9451
Trond Myklebust17280172012-03-11 13:11:00 -04009452static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009453 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009454 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009455 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009457 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458};
9459
Andy Adamson591d71c2009-04-01 09:22:47 -04009460#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009461static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009462 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9463 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009464 .recover_open = nfs41_open_expired,
9465 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009466 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009467};
9468#endif /* CONFIG_NFS_V4_1 */
9469
Trond Myklebust17280172012-03-11 13:11:00 -04009470static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009471 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009472 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009473 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009474};
9475
9476#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009477static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009478 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009479 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009480 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009481};
9482#endif
9483
Chuck Leverec011fe2013-10-17 14:12:39 -04009484static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009485 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009486 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009487};
9488
9489#if defined(CONFIG_NFS_V4_1)
9490static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009491 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009492 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009493};
9494#endif /* CONFIG_NFS_V4_1 */
9495
Trond Myklebust97dc1352010-06-16 09:52:26 -04009496static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9497 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009498 .init_caps = NFS_CAP_READDIRPLUS
9499 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009500 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009501 .init_client = nfs40_init_client,
9502 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009503 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009504 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009505 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009506 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009507 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009508 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009509 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9510 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9511 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009512 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009513};
9514
9515#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009516static struct nfs_seqid *
9517nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9518{
9519 return NULL;
9520}
9521
Trond Myklebust97dc1352010-06-16 09:52:26 -04009522static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9523 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009524 .init_caps = NFS_CAP_READDIRPLUS
9525 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009526 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009527 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009528 | NFS_CAP_ATOMIC_OPEN_V1
9529 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009530 .init_client = nfs41_init_client,
9531 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009532 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009533 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009534 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009535 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009536 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009537 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009538 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009539 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9540 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9541 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009542 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009543};
9544#endif
9545
Steve Dickson42c2c422013-05-22 12:50:38 -04009546#if defined(CONFIG_NFS_V4_2)
9547static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9548 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009549 .init_caps = NFS_CAP_READDIRPLUS
9550 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009551 | NFS_CAP_POSIX_LOCK
9552 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009553 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009554 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009555 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009556 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009557 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009558 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009559 | NFS_CAP_LAYOUTSTATS
9560 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009561 .init_client = nfs41_init_client,
9562 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009563 .match_stateid = nfs41_match_stateid,
9564 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009565 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009566 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009567 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009568 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009569 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009570 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9571 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9572 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009573 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009574};
9575#endif
9576
Trond Myklebust97dc1352010-06-16 09:52:26 -04009577const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9578 [0] = &nfs_v4_0_minor_ops,
9579#if defined(CONFIG_NFS_V4_1)
9580 [1] = &nfs_v4_1_minor_ops,
9581#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009582#if defined(CONFIG_NFS_V4_2)
9583 [2] = &nfs_v4_2_minor_ops,
9584#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009585};
9586
Trond Myklebust13997822016-07-24 17:10:52 -04009587static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009588{
9589 ssize_t error, error2;
9590
9591 error = generic_listxattr(dentry, list, size);
9592 if (error < 0)
9593 return error;
9594 if (list) {
9595 list += error;
9596 size -= error;
9597 }
9598
9599 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9600 if (error2 < 0)
9601 return error2;
9602 return error + error2;
9603}
9604
Trond Myklebust17f26b12013-08-21 15:48:42 -04009605static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009606 .create = nfs_create,
9607 .lookup = nfs_lookup,
9608 .atomic_open = nfs_atomic_open,
9609 .link = nfs_link,
9610 .unlink = nfs_unlink,
9611 .symlink = nfs_symlink,
9612 .mkdir = nfs_mkdir,
9613 .rmdir = nfs_rmdir,
9614 .mknod = nfs_mknod,
9615 .rename = nfs_rename,
9616 .permission = nfs_permission,
9617 .getattr = nfs_getattr,
9618 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009619 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009620};
9621
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009622static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009623 .permission = nfs_permission,
9624 .getattr = nfs_getattr,
9625 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009626 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009627};
9628
David Howells509de812006-08-22 20:06:11 -04009629const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630 .version = 4, /* protocol version */
9631 .dentry_ops = &nfs4_dentry_operations,
9632 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009633 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009634 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009636 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009637 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638 .getattr = nfs4_proc_getattr,
9639 .setattr = nfs4_proc_setattr,
9640 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009641 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 .access = nfs4_proc_access,
9643 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 .create = nfs4_proc_create,
9645 .remove = nfs4_proc_remove,
9646 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009647 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009649 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009650 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009651 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .link = nfs4_proc_link,
9653 .symlink = nfs4_proc_symlink,
9654 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009655 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 .readdir = nfs4_proc_readdir,
9657 .mknod = nfs4_proc_mknod,
9658 .statfs = nfs4_proc_statfs,
9659 .fsinfo = nfs4_proc_fsinfo,
9660 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009661 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009662 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009663 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009665 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009667 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009669 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009670 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009672 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009673 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009674 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009675 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009676 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009677 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009678 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009679 .create_server = nfs4_create_server,
9680 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009681};
9682
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009683static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009684 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009685 .list = nfs4_xattr_list_nfs4_acl,
9686 .get = nfs4_xattr_get_nfs4_acl,
9687 .set = nfs4_xattr_set_nfs4_acl,
9688};
9689
9690const struct xattr_handler *nfs4_xattr_handlers[] = {
9691 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009692#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9693 &nfs4_xattr_nfs4_label_handler,
9694#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009695 NULL
9696};
9697
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698/*
9699 * Local variables:
9700 * c-basic-offset: 8
9701 * End:
9702 */