blob: f9a70aaf13da1a673ffa9482e75f2310647cd093 [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 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500452 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400453 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400454 if (inode) {
455 int err;
456
457 err = nfs_async_inode_return_delegation(inode,
458 stateid);
459 if (err == 0)
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
463 break;
464 }
465 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500466 if (state == NULL)
467 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400468 ret = nfs4_schedule_stateid_recovery(server, state);
469 if (ret < 0)
470 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500471 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500472 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500473 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400476 case -NFS4ERR_MOVED:
477 ret = nfs4_schedule_migration_recovery(server);
478 if (ret < 0)
479 break;
480 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500484#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
493 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400495 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500496#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500497 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
500 * fail
501 */
502 ret = -EBUSY;
503 break;
504 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500505 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500506 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400507 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500508 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400509 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400510 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400511 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400512 exception->delay = 1;
513 return 0;
514
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400515 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800518 break;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
527 "uid/gids. "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
530 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500531 }
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500534wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400535 exception->recovering = 1;
536 return 0;
537}
538
539/* This is the error handling routine for processes that are allowed
540 * to sleep.
541 */
542int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
543{
544 struct nfs_client *clp = server->nfs_client;
545 int ret;
546
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
550 goto out_retry;
551 }
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
555 return -EIO;
556 goto out_retry;
557 }
558 return ret;
559out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500560 if (ret == 0)
561 exception->retry = 1;
562 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500563}
564
Trond Myklebust037fc982015-09-20 15:51:00 -0400565static int
566nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
568{
569 struct nfs_client *clp = server->nfs_client;
570 int ret;
571
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
575 goto out_retry;
576 }
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
581 goto out_retry;
582 }
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
584 ret = -EIO;
585 return ret;
586out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500587 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400588 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500589 /*
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
592 */
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
595 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400596 return ret;
597}
598
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400599int
Trond Myklebust037fc982015-09-20 15:51:00 -0400600nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
602{
603 struct nfs4_exception exception = {
604 .state = state,
605 };
606
607 if (task->tk_status >= 0)
608 return 0;
609 if (timeout)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
612 task->tk_status,
613 &exception);
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
616 if (exception.retry)
617 return -EAGAIN;
618 return 0;
619}
620
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400621/*
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
624 */
625static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
626{
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400629}
Trond Myklebust65de8722008-12-23 15:21:44 -0500630
Trond Myklebust452e9352010-07-31 14:29:06 -0400631static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
637}
638
Trond Myklebust452e9352010-07-31 14:29:06 -0400639static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
640{
Trond Myklebustbe824162015-07-05 14:50:46 -0400641 struct nfs_client *clp = server->nfs_client;
642
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400645}
646
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400647struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
651};
652
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800653void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400654 struct nfs4_sequence_res *res, int cache_reply,
655 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400656{
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400659 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400660
661 res->sr_slot = NULL;
662}
663
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400664static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400665{
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
668
Chuck Lever3bd23842013-08-09 12:49:19 -0400669 tbl = slot->table;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
674
675 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400676}
677
678static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
680{
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400683 return 1;
684}
685
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686#if defined(CONFIG_NFS_V4_1)
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400689{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500690 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400691 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500692 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400693
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400694 if (!slot)
695 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500698
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
703
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500704 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
707 */
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
710
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500712 send_new_highest_used_slotid = false;
713 goto out_unlock;
714 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500716
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500721 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400722 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400725}
726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
728{
729 nfs41_release_slot(res->sr_slot);
730 res->sr_slot = NULL;
731}
732
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400733static int nfs41_sequence_process(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400735{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500737 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500739 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500740 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (slot == NULL)
743 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400744 /* don't increment the sequence number if the task wasn't sent */
745 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400746 goto out;
747
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500748 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500749
Trond Myklebustac20d162012-12-15 15:36:07 -0500750 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400751 if (res->sr_status != -NFS4ERR_DELAY)
752 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 interrupted = true;
754 }
755
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400756 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500757 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 switch (res->sr_status) {
759 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400760 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400761 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500762 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500764 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400765 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
766 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500767 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500769 case 1:
770 /*
771 * sr_status remains 1 if an RPC level error occurred.
772 * The server may or may not have processed the sequence
773 * operation..
774 * Mark the slot as having hosted an interrupted RPC call.
775 */
776 slot->interrupted = 1;
777 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 case -NFS4ERR_DELAY:
779 /* The server detected a resend of the RPC call and
780 * returned NFS4ERR_DELAY as per Section 2.10.6.2
781 * of RFC5661.
782 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400784 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500785 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500786 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400787 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400788 case -NFS4ERR_RETRY_UNCACHED_REP:
789 case -NFS4ERR_SEQ_FALSE_RETRY:
790 /*
791 * The server thinks we tried to replay a request.
792 * Retry the call after bumping the sequence ID.
793 */
794 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500795 case -NFS4ERR_BADSLOT:
796 /*
797 * The slot id we used was probably retired. Try again
798 * using a different slot id.
799 */
Trond Myklebust99589102018-06-09 12:50:50 -0400800 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400801 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 goto retry_nowait;
803 case -NFS4ERR_SEQ_MISORDERED:
804 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500805 * Was the last operation on this sequence interrupted?
806 * If so, retry after bumping the sequence number.
807 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400808 if (interrupted)
809 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500810 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500811 * Could this slot have been previously retired?
812 * If so, then the server may be expecting seq_nr = 1!
813 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500814 if (slot->seq_nr != 1) {
815 slot->seq_nr = 1;
816 goto retry_nowait;
817 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400818 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 default:
820 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400821 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 }
823out:
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500826out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500827 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400828session_recover:
829 nfs4_schedule_session_recovery(session, res->sr_status);
830 goto retry_nowait;
831retry_new_seq:
832 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500833retry_nowait:
834 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500836 task->tk_status = 0;
837 ret = 0;
838 }
839 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400840out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400841 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400842 goto out;
843 rpc_delay(task, NFS4_POLL_RETRY_MAX);
844 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400845}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400846
847int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (!nfs41_sequence_process(task, res))
850 return 0;
851 if (res->sr_slot != NULL)
852 nfs41_sequence_free_slot(res);
853 return 1;
854
855}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500856EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400857
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400858static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859{
860 if (res->sr_slot == NULL)
861 return 1;
862 if (res->sr_slot->table->session != NULL)
863 return nfs41_sequence_process(task, res);
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL) {
870 if (res->sr_slot->table->session != NULL)
871 nfs41_sequence_free_slot(res);
872 else
873 nfs40_sequence_free_slot(res);
874 }
875}
876
Peng Tao2c4b1312014-06-11 05:24:16 +0800877int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400878{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500879 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400880 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400881 if (!res->sr_slot->table->session)
882 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400883 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400884}
Peng Tao2c4b1312014-06-11 05:24:16 +0800885EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400886
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
888{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400889 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400890
Trond Myklebust035168ab2010-06-16 09:52:26 -0400891 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
892
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500893 nfs4_setup_sequence(data->seq_server->nfs_client,
894 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895}
896
Andy Adamson69ab40c2009-04-01 09:22:19 -0400897static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
898{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400899 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400900
Trond Myklebust14516c32010-07-31 14:29:06 -0400901 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400902}
903
Trond Myklebust17280172012-03-11 13:11:00 -0400904static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400905 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400906 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907};
908
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909static void
910nfs4_sequence_process_interrupted(struct nfs_client *client,
911 struct nfs4_slot *slot, struct rpc_cred *cred)
912{
913 struct rpc_task *task;
914
915 task = _nfs41_proc_sequence(client, cred, slot, true);
916 if (!IS_ERR(task))
917 rpc_put_task_async(task);
918}
919
Chuck Lever3bd23842013-08-09 12:49:19 -0400920#else /* !CONFIG_NFS_V4_1 */
921
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400922static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
923{
924 return nfs40_sequence_done(task, res);
925}
926
927static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928{
929 if (res->sr_slot != NULL)
930 nfs40_sequence_free_slot(res);
931}
932
Peng Tao2c4b1312014-06-11 05:24:16 +0800933int nfs4_sequence_done(struct rpc_task *task,
934 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400935{
Chuck Lever3bd23842013-08-09 12:49:19 -0400936 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400937}
Peng Tao2c4b1312014-06-11 05:24:16 +0800938EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400939
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400940static void
941nfs4_sequence_process_interrupted(struct nfs_client *client,
942 struct nfs4_slot *slot, struct rpc_cred *cred)
943{
944 WARN_ON_ONCE(1);
945 slot->interrupted = 0;
946}
947
Chuck Lever3bd23842013-08-09 12:49:19 -0400948#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400949
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400950static
951void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct nfs4_slot *slot)
954{
955 if (!slot)
956 return;
957 slot->privileged = args->sa_privileged ? 1 : 0;
958 args->sa_slot = slot;
959
960 res->sr_slot = slot;
961 res->sr_timestamp = jiffies;
962 res->sr_status_flags = 0;
963 res->sr_status = 1;
964
965}
966
967int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500968 struct nfs4_sequence_args *args,
969 struct nfs4_sequence_res *res,
970 struct rpc_task *task)
971{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500972 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500973 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500974 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500975
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500976 /* slot already allocated? */
977 if (res->sr_slot != NULL)
978 goto out_start;
979
Anna Schumaker76ee0352017-01-10 16:49:31 -0500980 if (session) {
981 tbl = &session->fc_slot_table;
982 task->tk_timeout = 0;
983 }
984
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400985 for (;;) {
986 spin_lock(&tbl->slot_tbl_lock);
987 /* The state manager will wait until the slot table is empty */
988 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
989 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500990
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400991 slot = nfs4_alloc_slot(tbl);
992 if (IS_ERR(slot)) {
993 /* Try again in 1/4 second */
994 if (slot == ERR_PTR(-ENOMEM))
995 task->tk_timeout = HZ >> 2;
996 goto out_sleep;
997 }
998 spin_unlock(&tbl->slot_tbl_lock);
999
1000 if (likely(!slot->interrupted))
1001 break;
1002 nfs4_sequence_process_interrupted(client,
1003 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -05001004 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001005
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001006 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001007
Anna Schumakerad05cc02017-01-11 13:37:06 -05001008 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001009out_start:
1010 rpc_call_start(task);
1011 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001012
1013out_sleep:
1014 if (args->sa_privileged)
1015 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1016 NULL, RPC_PRIORITY_PRIVILEGED);
1017 else
1018 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1019 spin_unlock(&tbl->slot_tbl_lock);
1020 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001021}
1022EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1023
Chuck Lever9915ea72013-08-09 12:48:27 -04001024static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025{
1026 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001027 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001028 data->seq_args, data->seq_res, task);
1029}
1030
1031static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032{
1033 struct nfs4_call_sync_data *data = calldata;
1034 nfs4_sequence_done(task, data->seq_res);
1035}
1036
1037static const struct rpc_call_ops nfs40_call_sync_ops = {
1038 .rpc_call_prepare = nfs40_call_sync_prepare,
1039 .rpc_call_done = nfs40_call_sync_done,
1040};
1041
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001043 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001047{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 int ret;
1049 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001050 struct nfs_client *clp = server->nfs_client;
1051 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .seq_server = server,
1053 .seq_args = args,
1054 .seq_res = res,
1055 };
1056 struct rpc_task_setup task_setup = {
1057 .rpc_client = clnt,
1058 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001059 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060 .callback_data = &data
1061 };
1062
1063 task = rpc_run_task(&task_setup);
1064 if (IS_ERR(task))
1065 ret = PTR_ERR(task);
1066 else {
1067 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001068 rpc_put_task(task);
1069 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 return ret;
1071}
1072
Bryan Schumaker7c513052011-03-24 17:12:24 +00001073int nfs4_call_sync(struct rpc_clnt *clnt,
1074 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001075 struct rpc_message *msg,
1076 struct nfs4_sequence_args *args,
1077 struct nfs4_sequence_res *res,
1078 int cache_reply)
1079{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001080 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001081 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Trond Myklebust3c591172018-07-31 15:54:10 -04001084static void
1085nfs4_inc_nlink_locked(struct inode *inode)
1086{
1087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1088 inc_nlink(inode);
1089}
1090
1091static void
1092nfs4_dec_nlink_locked(struct inode *inode)
1093{
1094 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1095 drop_nlink(inode);
1096}
1097
1098static void
1099update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001100 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct nfs_inode *nfsi = NFS_I(dir);
1103
Trond Myklebust16e14372018-03-20 16:53:31 -04001104 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1105 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001106 | NFS_INO_INVALID_DATA
1107 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001108 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001109 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1110 nfsi->attrtimeo_timestamp = jiffies;
1111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001113 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001114 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1115 NFS_INO_INVALID_ACL;
1116 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001117 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001118 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001119 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001120 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001121 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001122}
1123
1124static void
1125update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001126 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001127{
1128 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001129 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_unlock(&dir->i_lock);
1131}
1132
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001133struct nfs4_open_createattrs {
1134 struct nfs4_label *label;
1135 struct iattr *sattr;
1136 const __u32 verf[2];
1137};
1138
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001139static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1140 int err, struct nfs4_exception *exception)
1141{
1142 if (err != -EINVAL)
1143 return false;
1144 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1145 return false;
1146 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1147 exception->retry = 1;
1148 return true;
1149}
1150
Trond Myklebust6ae37332015-01-30 14:21:14 -05001151static u32
1152nfs4_map_atomic_open_share(struct nfs_server *server,
1153 fmode_t fmode, int openflags)
1154{
1155 u32 res = 0;
1156
1157 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1158 case FMODE_READ:
1159 res = NFS4_SHARE_ACCESS_READ;
1160 break;
1161 case FMODE_WRITE:
1162 res = NFS4_SHARE_ACCESS_WRITE;
1163 break;
1164 case FMODE_READ|FMODE_WRITE:
1165 res = NFS4_SHARE_ACCESS_BOTH;
1166 }
1167 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1168 goto out;
1169 /* Want no delegation if we're using O_DIRECT */
1170 if (openflags & O_DIRECT)
1171 res |= NFS4_SHARE_WANT_NO_DELEG;
1172out:
1173 return res;
1174}
1175
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001176static enum open_claim_type4
1177nfs4_map_atomic_open_claim(struct nfs_server *server,
1178 enum open_claim_type4 claim)
1179{
1180 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1181 return claim;
1182 switch (claim) {
1183 default:
1184 return claim;
1185 case NFS4_OPEN_CLAIM_FH:
1186 return NFS4_OPEN_CLAIM_NULL;
1187 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1188 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1189 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1190 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1191 }
1192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195{
1196 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001197 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 p->o_res.seqid = p->o_arg.seqid;
1199 p->c_res.seqid = p->c_arg.seqid;
1200 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001201 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001203 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204}
1205
Al Viro82a2c1b2011-06-22 18:30:55 -04001206static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001208 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001209 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211{
Al Viro82a2c1b2011-06-22 18:30:55 -04001212 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001213 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001215 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001216 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 struct nfs4_opendata *p;
1218
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001219 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 if (p == NULL)
1221 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001222
1223 p->f_label = nfs4_label_alloc(server, gfp_mask);
1224 if (IS_ERR(p->f_label))
1225 goto err_free_p;
1226
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->a_label = nfs4_label_alloc(server, gfp_mask);
1228 if (IS_ERR(p->a_label))
1229 goto err_free_f;
1230
Trond Myklebust63f5f792015-01-23 19:19:25 -05001231 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1232 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001233 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001234 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 nfs_sb_active(dentry->d_sb);
1236 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 p->dir = parent;
1238 p->owner = sp;
1239 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 p->o_arg.open_flags = flags;
1241 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001242 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001243 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001244 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1245 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001246 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1247 * will return permission denied for all bits until close */
1248 if (!(flags & O_EXCL)) {
1249 /* ask server to check for all possible rights as results
1250 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001251 switch (p->o_arg.claim) {
1252 default:
1253 break;
1254 case NFS4_OPEN_CLAIM_NULL:
1255 case NFS4_OPEN_CLAIM_FH:
1256 p->o_arg.access = NFS4_ACCESS_READ |
1257 NFS4_ACCESS_MODIFY |
1258 NFS4_ACCESS_EXTEND |
1259 NFS4_ACCESS_EXECUTE;
1260 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001261 }
David Howells7539bba2006-08-22 20:06:09 -04001262 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001263 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1264 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001265 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001267 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001268 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001269 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001270 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001271 case NFS4_OPEN_CLAIM_NULL:
1272 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1273 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1274 p->o_arg.fh = NFS_FH(dir);
1275 break;
1276 case NFS4_OPEN_CLAIM_PREVIOUS:
1277 case NFS4_OPEN_CLAIM_FH:
1278 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1279 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001280 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001281 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001282 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001284 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001285
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001286 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001287 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001289 p->c_arg.fh = &p->o_res.fh;
1290 p->c_arg.stateid = &p->o_res.stateid;
1291 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001297 nfs4_label_free(p->a_label);
1298err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001299 nfs4_label_free(p->f_label);
1300err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 kfree(p);
1302err:
1303 dput(parent);
1304 return NULL;
1305}
1306
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 struct nfs4_opendata *p = container_of(kref,
1310 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001311 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312
Fred Isaman30ae2412016-10-18 13:39:51 -04001313 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001315 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 if (p->state != NULL)
1317 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001319
Kinglong Meea49c2692015-07-27 15:31:38 +08001320 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001321 nfs4_label_free(p->f_label);
1322
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001324 dput(p->dentry);
1325 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001326 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001327 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001328 kfree(p);
1329}
1330
1331static void nfs4_opendata_put(struct nfs4_opendata *p)
1332{
1333 if (p != NULL)
1334 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001335}
1336
Trond Myklebust24311f82015-09-20 10:50:17 -04001337static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1338 fmode_t fmode)
1339{
1340 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1341 case FMODE_READ|FMODE_WRITE:
1342 return state->n_rdwr != 0;
1343 case FMODE_WRITE:
1344 return state->n_wronly != 0;
1345 case FMODE_READ:
1346 return state->n_rdonly != 0;
1347 }
1348 WARN_ON_ONCE(1);
1349 return false;
1350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001353{
1354 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355
Trond Myklebust536e43d2012-01-17 22:04:26 -05001356 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 goto out;
1358 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001360 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1361 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001362 break;
1363 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001364 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1365 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 break;
1367 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001368 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1369 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001372 return ret;
1373}
1374
Trond Myklebust2a606182015-08-19 22:30:00 -05001375static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1376 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001377{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001378 if (delegation == NULL)
1379 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001380 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001381 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001382 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1383 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001384 switch (claim) {
1385 case NFS4_OPEN_CLAIM_NULL:
1386 case NFS4_OPEN_CLAIM_FH:
1387 break;
1388 case NFS4_OPEN_CLAIM_PREVIOUS:
1389 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1390 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001391 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001392 default:
1393 return 0;
1394 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001395 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001396 return 1;
1397}
1398
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001399static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001400{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001402 case FMODE_WRITE:
1403 state->n_wronly++;
1404 break;
1405 case FMODE_READ:
1406 state->n_rdonly++;
1407 break;
1408 case FMODE_READ|FMODE_WRITE:
1409 state->n_rdwr++;
1410 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001411 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001412}
1413
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001414#ifdef CONFIG_NFS_V4_1
1415static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1416{
1417 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1418 return true;
1419 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1420 return true;
1421 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1422 return true;
1423 return false;
1424}
1425#endif /* CONFIG_NFS_V4_1 */
1426
Trond Myklebustc9399f22017-11-06 15:28:01 -05001427static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1428{
1429 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1430 wake_up_all(&state->waitq);
1431}
1432
1433static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1434 const nfs4_stateid *stateid)
1435{
1436 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1437 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1438
1439 if (stateid_seqid == state_seqid + 1U ||
1440 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1441 nfs_state_log_update_open_stateid(state);
1442 else
1443 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1444}
1445
Trond Myklebust4f14c192014-02-12 19:15:06 -05001446static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001447{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001448 struct nfs_client *clp = state->owner->so_server->nfs_client;
1449 bool need_recover = false;
1450
1451 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1452 need_recover = true;
1453 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1454 need_recover = true;
1455 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1456 need_recover = true;
1457 if (need_recover)
1458 nfs4_state_mark_reclaim_nograce(clp, state);
1459}
1460
Trond Myklebustc9399f22017-11-06 15:28:01 -05001461/*
1462 * Check for whether or not the caller may update the open stateid
1463 * to the value passed in by stateid.
1464 *
1465 * Note: This function relies heavily on the server implementing
1466 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1467 * correctly.
1468 * i.e. The stateid seqids have to be initialised to 1, and
1469 * are then incremented on every state transition.
1470 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001471static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001472 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001473{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001474 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1475 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1476 if (stateid->seqid == cpu_to_be32(1))
1477 nfs_state_log_update_open_stateid(state);
1478 else
1479 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001480 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001481 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001482
1483 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1484 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001485 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001486 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001487 return false;
1488}
1489
Trond Myklebustf95549c2015-01-23 18:06:09 -05001490static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1491{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001492 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1493 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001494 if (state->n_wronly)
1495 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1496 if (state->n_rdonly)
1497 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1498 if (state->n_rdwr)
1499 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001500 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001501}
1502
Trond Myklebust226056c2014-02-11 10:41:07 -05001503static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1504 nfs4_stateid *stateid, fmode_t fmode)
1505{
1506 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1507 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1508 case FMODE_WRITE:
1509 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1510 break;
1511 case FMODE_READ:
1512 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1513 break;
1514 case 0:
1515 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1516 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 clear_bit(NFS_OPEN_STATE, &state->flags);
1518 }
1519 if (stateid == NULL)
1520 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001521 /* Handle OPEN+OPEN_DOWNGRADE races */
1522 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1523 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001524 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001525 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001526 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001527 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001528 nfs4_stateid_copy(&state->stateid, stateid);
1529 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001530 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531out:
1532 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001533}
1534
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001535static void nfs_clear_open_stateid(struct nfs4_state *state,
1536 nfs4_stateid *arg_stateid,
1537 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001538{
1539 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001540 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1541 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1542 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001543 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001544 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1545 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001546}
1547
Trond Myklebust1393d962016-09-22 13:39:13 -04001548static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001549 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001550{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551 DEFINE_WAIT(wait);
1552 int status = 0;
1553 for (;;) {
1554
1555 if (!nfs_need_update_open_stateid(state, stateid))
1556 return;
1557 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001558 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001559 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001560 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001561 /* Rely on seqids for serialisation with NFSv4.0 */
1562 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1563 break;
1564
1565 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1566 /*
1567 * Ensure we process the state changes in the same order
1568 * in which the server processed them by delaying the
1569 * update of the stateid until we are in sequence.
1570 */
1571 write_sequnlock(&state->seqlock);
1572 spin_unlock(&state->owner->so_lock);
1573 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001574 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575 if (!signal_pending(current)) {
1576 if (schedule_timeout(5*HZ) == 0)
1577 status = -EAGAIN;
1578 else
1579 status = 0;
1580 } else
1581 status = -EINTR;
1582 finish_wait(&state->waitq, &wait);
1583 rcu_read_lock();
1584 spin_lock(&state->owner->so_lock);
1585 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001586 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001587
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001588 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1589 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001590 nfs4_stateid_copy(freeme, &state->open_stateid);
1591 nfs_test_and_clear_all_open_stateid(state);
1592 }
1593
Trond Myklebuste999e802014-02-10 18:20:47 -05001594 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1595 nfs4_stateid_copy(&state->stateid, stateid);
1596 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001597 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001598 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001599}
1600
Trond Myklebustc9399f22017-11-06 15:28:01 -05001601static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001602 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001603 fmode_t fmode,
1604 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001606 /*
1607 * Protect the call to nfs4_state_set_mode_locked and
1608 * serialise the stateid update
1609 */
1610 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001611 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1612 switch (fmode) {
1613 case FMODE_READ:
1614 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1615 break;
1616 case FMODE_WRITE:
1617 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1618 break;
1619 case FMODE_READ|FMODE_WRITE:
1620 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001621 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001622 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001623 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001624}
1625
1626static void nfs_state_set_delegation(struct nfs4_state *state,
1627 const nfs4_stateid *deleg_stateid,
1628 fmode_t fmode)
1629{
1630 /*
1631 * Protect the call to nfs4_state_set_mode_locked and
1632 * serialise the stateid update
1633 */
1634 write_seqlock(&state->seqlock);
1635 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1636 set_bit(NFS_DELEGATED_STATE, &state->flags);
1637 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Trond Myklebust1393d962016-09-22 13:39:13 -04001640static int update_open_stateid(struct nfs4_state *state,
1641 const nfs4_stateid *open_stateid,
1642 const nfs4_stateid *delegation,
1643 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001644{
Trond Myklebust1393d962016-09-22 13:39:13 -04001645 struct nfs_server *server = NFS_SERVER(state->inode);
1646 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001647 struct nfs_inode *nfsi = NFS_I(state->inode);
1648 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001649 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001650 int ret = 0;
1651
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001652 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001653
1654 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001655 spin_lock(&state->owner->so_lock);
1656 if (open_stateid != NULL) {
1657 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1658 ret = 1;
1659 }
1660
Trond Myklebust34310432008-12-23 15:21:38 -05001661 deleg_cur = rcu_dereference(nfsi->delegation);
1662 if (deleg_cur == NULL)
1663 goto no_delegation;
1664
1665 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001666 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001667 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001669 goto no_delegation_unlock;
1670
1671 if (delegation == NULL)
1672 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001673 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001674 goto no_delegation_unlock;
1675
Trond Myklebustb7391f42008-12-23 15:21:52 -05001676 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001677 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001678 ret = 1;
1679no_delegation_unlock:
1680 spin_unlock(&deleg_cur->lock);
1681no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001682 if (ret)
1683 update_open_stateflags(state, fmode);
1684 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001685 rcu_read_unlock();
1686
Trond Myklebust4f14c192014-02-12 19:15:06 -05001687 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001688 nfs4_schedule_state_manager(clp);
1689 if (freeme.type != 0)
1690 nfs4_test_and_free_stateid(server, &freeme,
1691 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001692
1693 return ret;
1694}
1695
Trond Myklebust39071e62015-01-24 15:07:56 -05001696static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1697 const nfs4_stateid *stateid)
1698{
1699 struct nfs4_state *state = lsp->ls_state;
1700 bool ret = false;
1701
1702 spin_lock(&state->state_lock);
1703 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1704 goto out_noupdate;
1705 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1706 goto out_noupdate;
1707 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1708 ret = true;
1709out_noupdate:
1710 spin_unlock(&state->state_lock);
1711 return ret;
1712}
Trond Myklebust34310432008-12-23 15:21:38 -05001713
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001714static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715{
1716 struct nfs_delegation *delegation;
1717
Trond Myklebustf5086242018-03-20 16:43:13 -04001718 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 rcu_read_lock();
1720 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001721 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722 rcu_read_unlock();
1723 return;
1724 }
1725 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001726 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001727}
1728
Trond Myklebust6ee41262007-07-08 14:11:36 -04001729static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730{
1731 struct nfs4_state *state = opendata->state;
1732 struct nfs_inode *nfsi = NFS_I(state->inode);
1733 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001734 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001735 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001736 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001737 nfs4_stateid stateid;
1738 int ret = -EAGAIN;
1739
Trond Myklebustaac00a82007-07-05 19:02:21 -04001740 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001741 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001742 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001743 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001744 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001745 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001746 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001747 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001748 rcu_read_lock();
1749 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001750 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001751 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001752 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001753 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001754 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001755 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001756 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001757 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001758 if (!opendata->is_recover) {
1759 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1760 if (ret != 0)
1761 goto out;
1762 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001763 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001764
1765 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001766 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001767 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001768 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769out:
1770 return ERR_PTR(ret);
1771out_return_state:
1772 atomic_inc(&state->count);
1773 return state;
1774}
1775
Andy Adamsone23008e2012-10-02 21:07:32 -04001776static void
1777nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1778{
1779 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1780 struct nfs_delegation *delegation;
1781 int delegation_flags = 0;
1782
1783 rcu_read_lock();
1784 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1785 if (delegation)
1786 delegation_flags = delegation->flags;
1787 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001788 switch (data->o_arg.claim) {
1789 default:
1790 break;
1791 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1792 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001793 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1794 "returning a delegation for "
1795 "OPEN(CLAIM_DELEGATE_CUR)\n",
1796 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001797 return;
1798 }
1799 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001800 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001801 data->owner->so_cred,
1802 data->o_res.delegation_type,
1803 &data->o_res.delegation,
1804 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001805 else
1806 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001807 data->owner->so_cred,
1808 data->o_res.delegation_type,
1809 &data->o_res.delegation,
1810 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001811
1812 if (data->o_res.do_recall)
1813 nfs_async_inode_return_delegation(state->inode,
1814 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001815}
1816
1817/*
1818 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1819 * and update the nfs4_state.
1820 */
1821static struct nfs4_state *
1822_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1823{
1824 struct inode *inode = data->state->inode;
1825 struct nfs4_state *state = data->state;
1826 int ret;
1827
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001828 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001829 if (data->rpc_status)
1830 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001831 /* cached opens have already been processed */
1832 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001833 }
1834
Andy Adamsone23008e2012-10-02 21:07:32 -04001835 ret = nfs_refresh_inode(inode, &data->f_attr);
1836 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001837 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001838
1839 if (data->o_res.delegation_type != 0)
1840 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001841update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001842 update_open_stateid(state, &data->o_res.stateid, NULL,
1843 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001844 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001845
1846 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001847}
1848
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001849static struct inode *
1850nfs4_opendata_get_inode(struct nfs4_opendata *data)
1851{
1852 struct inode *inode;
1853
1854 switch (data->o_arg.claim) {
1855 case NFS4_OPEN_CLAIM_NULL:
1856 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1857 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1858 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1859 return ERR_PTR(-EAGAIN);
1860 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1861 &data->f_attr, data->f_label);
1862 break;
1863 default:
1864 inode = d_inode(data->dentry);
1865 ihold(inode);
1866 nfs_refresh_inode(inode, &data->f_attr);
1867 }
1868 return inode;
1869}
1870
Andy Adamsone23008e2012-10-02 21:07:32 -04001871static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001872nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1873{
1874 struct nfs4_state *state;
1875 struct inode *inode;
1876
1877 inode = nfs4_opendata_get_inode(data);
1878 if (IS_ERR(inode))
1879 return ERR_CAST(inode);
1880 if (data->state != NULL && data->state->inode == inode) {
1881 state = data->state;
1882 atomic_inc(&state->count);
1883 } else
1884 state = nfs4_get_open_state(inode, data->owner);
1885 iput(inode);
1886 if (state == NULL)
1887 state = ERR_PTR(-ENOMEM);
1888 return state;
1889}
1890
Andy Adamsone23008e2012-10-02 21:07:32 -04001891static struct nfs4_state *
1892_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001893{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001894 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001895
Trond Myklebustaac00a82007-07-05 19:02:21 -04001896 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001897 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001898 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001899 goto out;
1900 }
1901
Trond Myklebust75e8c482017-08-08 10:38:07 -04001902 state = nfs4_opendata_find_nfs4_state(data);
1903 if (IS_ERR(state))
1904 goto out;
1905
Andy Adamsone23008e2012-10-02 21:07:32 -04001906 if (data->o_res.delegation_type != 0)
1907 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001908 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001909 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001910out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001911 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001912 return state;
1913}
1914
Andy Adamsone23008e2012-10-02 21:07:32 -04001915static struct nfs4_state *
1916nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1917{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001918 struct nfs4_state *ret;
1919
Andy Adamsone23008e2012-10-02 21:07:32 -04001920 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001921 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1922 else
1923 ret = _nfs4_opendata_to_nfs4_state(data);
1924 nfs4_sequence_free_slot(&data->o_res.seq_res);
1925 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001926}
1927
Trond Myklebust864472e2006-01-03 09:55:15 +01001928static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1929{
1930 struct nfs_inode *nfsi = NFS_I(state->inode);
1931 struct nfs_open_context *ctx;
1932
1933 spin_lock(&state->inode->i_lock);
1934 list_for_each_entry(ctx, &nfsi->open_files, list) {
1935 if (ctx->state != state)
1936 continue;
1937 get_nfs_open_context(ctx);
1938 spin_unlock(&state->inode->i_lock);
1939 return ctx;
1940 }
1941 spin_unlock(&state->inode->i_lock);
1942 return ERR_PTR(-ENOENT);
1943}
1944
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001945static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1946 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001947{
1948 struct nfs4_opendata *opendata;
1949
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001950 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001951 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001952 if (opendata == NULL)
1953 return ERR_PTR(-ENOMEM);
1954 opendata->state = state;
1955 atomic_inc(&state->count);
1956 return opendata;
1957}
1958
Trond Myklebust24311f82015-09-20 10:50:17 -04001959static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1960 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001961{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001962 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001963 int ret;
1964
Trond Myklebust24311f82015-09-20 10:50:17 -04001965 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001966 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001967 opendata->o_arg.open_flags = 0;
1968 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001969 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1970 NFS_SB(opendata->dentry->d_sb),
1971 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001972 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1973 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1974 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001975 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001976 if (ret != 0)
1977 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001978 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001979 if (IS_ERR(newstate))
1980 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001981 if (newstate != opendata->state)
1982 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001983 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001984 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001985}
1986
1987static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1988{
Trond Myklebust864472e2006-01-03 09:55:15 +01001989 int ret;
1990
Trond Myklebust4f14c192014-02-12 19:15:06 -05001991 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1992 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1993 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1994 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001995 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001996 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001997 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001998 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001999 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2000 if (ret != 0)
2001 return ret;
2002 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2003 if (ret != 0)
2004 return ret;
2005 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2006 if (ret != 0)
2007 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002008 /*
2009 * We may have performed cached opens for all three recoveries.
2010 * Check if we need to update the current stateid.
2011 */
2012 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002013 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002014 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002015 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002016 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002017 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002018 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002019 return 0;
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022/*
2023 * OPEN_RECLAIM:
2024 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002026static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002028 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002029 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002030 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int status;
2032
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002033 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2034 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002035 if (IS_ERR(opendata))
2036 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002037 rcu_read_lock();
2038 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002039 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002040 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002041 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002042 opendata->o_arg.u.delegation_type = delegation_type;
2043 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002044 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return status;
2046}
2047
Trond Myklebust539cd032007-06-05 11:46:42 -04002048static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct nfs_server *server = NFS_SERVER(state->inode);
2051 struct nfs4_exception exception = { };
2052 int err;
2053 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002054 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002055 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002056 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2057 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002058 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002059 break;
2060 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 } while (exception.retry);
2062 return err;
2063}
2064
Trond Myklebust864472e2006-01-03 09:55:15 +01002065static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2066{
2067 struct nfs_open_context *ctx;
2068 int ret;
2069
2070 ctx = nfs4_state_find_open_context(state);
2071 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002072 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002073 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002074 put_nfs_open_context(ctx);
2075 return ret;
2076}
2077
NeilBrowndce26302017-12-13 09:57:09 +11002078static 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 -07002079{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002080 switch (err) {
2081 default:
2082 printk(KERN_ERR "NFS: %s: unhandled error "
2083 "%d.\n", __func__, err);
2084 case 0:
2085 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002086 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002087 case -ESTALE:
2088 break;
2089 case -NFS4ERR_BADSESSION:
2090 case -NFS4ERR_BADSLOT:
2091 case -NFS4ERR_BAD_HIGH_SLOT:
2092 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2093 case -NFS4ERR_DEADSESSION:
2094 set_bit(NFS_DELEGATED_STATE, &state->flags);
2095 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2096 return -EAGAIN;
2097 case -NFS4ERR_STALE_CLIENTID:
2098 case -NFS4ERR_STALE_STATEID:
2099 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002100 /* Don't recall a delegation if it was lost */
2101 nfs4_schedule_lease_recovery(server->nfs_client);
2102 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002103 case -NFS4ERR_MOVED:
2104 nfs4_schedule_migration_recovery(server);
2105 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002106 case -NFS4ERR_LEASE_MOVED:
2107 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2108 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002109 case -NFS4ERR_DELEG_REVOKED:
2110 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002111 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002112 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002113 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002114 nfs_inode_find_state_and_recover(state->inode,
2115 stateid);
2116 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002117 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002118 case -NFS4ERR_DELAY:
2119 case -NFS4ERR_GRACE:
2120 set_bit(NFS_DELEGATED_STATE, &state->flags);
2121 ssleep(1);
2122 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002123 case -ENOMEM:
2124 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002125 if (fl) {
2126 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2127 if (lsp)
2128 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2129 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002130 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return err;
2133}
2134
Trond Myklebust24311f82015-09-20 10:50:17 -04002135int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2136 struct nfs4_state *state, const nfs4_stateid *stateid,
2137 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002138{
2139 struct nfs_server *server = NFS_SERVER(state->inode);
2140 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002141 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002142
2143 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2144 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2145 if (IS_ERR(opendata))
2146 return PTR_ERR(opendata);
2147 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002148 write_seqlock(&state->seqlock);
2149 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2150 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002151 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2152 switch (type & (FMODE_READ|FMODE_WRITE)) {
2153 case FMODE_READ|FMODE_WRITE:
2154 case FMODE_WRITE:
2155 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2156 if (err)
2157 break;
2158 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2159 if (err)
2160 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002161 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002162 case FMODE_READ:
2163 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2164 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002165 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002166 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002167}
2168
Chuck Leverbe05c862013-08-09 12:49:47 -04002169static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2170{
2171 struct nfs4_opendata *data = calldata;
2172
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002173 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2174 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002175}
2176
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002177static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2178{
2179 struct nfs4_opendata *data = calldata;
2180
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002181 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002182
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002183 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002184 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002185 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002186 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002187 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002188 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002189 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002190}
2191
2192static void nfs4_open_confirm_release(void *calldata)
2193{
2194 struct nfs4_opendata *data = calldata;
2195 struct nfs4_state *state = NULL;
2196
2197 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002198 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002199 goto out_free;
2200 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002201 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002202 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002203 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002204 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002205 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002206out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002207 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002208}
2209
2210static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002211 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002212 .rpc_call_done = nfs4_open_confirm_done,
2213 .rpc_release = nfs4_open_confirm_release,
2214};
2215
2216/*
2217 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2218 */
2219static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2220{
David Howells2b0143b2015-03-17 22:25:59 +00002221 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002222 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002223 struct rpc_message msg = {
2224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2225 .rpc_argp = &data->c_arg,
2226 .rpc_resp = &data->c_res,
2227 .rpc_cred = data->owner->so_cred,
2228 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002229 struct rpc_task_setup task_setup_data = {
2230 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002231 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002232 .callback_ops = &nfs4_open_confirm_ops,
2233 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002234 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002235 .flags = RPC_TASK_ASYNC,
2236 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 int status;
2238
Anna Schumakerfba83f32018-05-04 16:22:50 -04002239 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2240 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002241 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002242 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002243 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002244 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002245 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002246 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002247 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002248 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002249 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002250 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002251 smp_wmb();
2252 } else
2253 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002254 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return status;
2256}
2257
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002258static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260 struct nfs4_opendata *data = calldata;
2261 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002262 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002263 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002264
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002265 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002266 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002267 /*
2268 * Check if we still need to send an OPEN call, or if we can use
2269 * a delegation instead.
2270 */
2271 if (data->state != NULL) {
2272 struct nfs_delegation *delegation;
2273
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002274 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002275 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002276 rcu_read_lock();
2277 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002278 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002279 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002280 rcu_read_unlock();
2281 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002282 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002283 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002284 switch (claim) {
2285 default:
2286 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002287 case NFS4_OPEN_CLAIM_PREVIOUS:
2288 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2289 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002290 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002291 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002292 case NFS4_OPEN_CLAIM_FH:
2293 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002294 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002295 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002296 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002297 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002298 &data->o_res.seq_res,
2299 task) != 0)
2300 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002301
2302 /* Set the create mode (note dependency on the session type) */
2303 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2304 if (data->o_arg.open_flags & O_EXCL) {
2305 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2306 if (nfs4_has_persistent_session(clp))
2307 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2308 else if (clp->cl_mvops->minor_version > 0)
2309 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2310 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002311 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002312unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002313 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002314 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002315out_no_action:
2316 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002317out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002318 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002319}
2320
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321static void nfs4_open_done(struct rpc_task *task, void *calldata)
2322{
2323 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002325 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002326
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002327 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002328 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002329
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002330 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002331 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2332 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002333 case S_IFREG:
2334 break;
2335 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002336 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002337 break;
2338 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002339 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002340 break;
2341 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002342 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002343 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002344 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002345 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002346 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002348 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002350}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002351
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002352static void nfs4_open_release(void *calldata)
2353{
2354 struct nfs4_opendata *data = calldata;
2355 struct nfs4_state *state = NULL;
2356
2357 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002358 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002359 goto out_free;
2360 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002361 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002362 goto out_free;
2363 /* In case we need an open_confirm, no cleanup! */
2364 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2365 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002366 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002367 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002368 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002369out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002370 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371}
2372
2373static const struct rpc_call_ops nfs4_open_ops = {
2374 .rpc_call_prepare = nfs4_open_prepare,
2375 .rpc_call_done = nfs4_open_done,
2376 .rpc_release = nfs4_open_release,
2377};
2378
Fred Isaman3b65a302016-09-19 10:06:49 -04002379static int nfs4_run_open_task(struct nfs4_opendata *data,
2380 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002381{
David Howells2b0143b2015-03-17 22:25:59 +00002382 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383 struct nfs_server *server = NFS_SERVER(dir);
2384 struct nfs_openargs *o_arg = &data->o_arg;
2385 struct nfs_openres *o_res = &data->o_res;
2386 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2389 .rpc_argp = o_arg,
2390 .rpc_resp = o_res,
2391 .rpc_cred = data->owner->so_cred,
2392 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002393 struct rpc_task_setup task_setup_data = {
2394 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002395 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002396 .callback_ops = &nfs4_open_ops,
2397 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002398 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002399 .flags = RPC_TASK_ASYNC,
2400 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002401 int status;
2402
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002403 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002404 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002405 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002406 data->cancelled = false;
2407 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002408 if (!ctx) {
2409 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002410 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002411 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002412 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002413 pnfs_lgopen_prepare(data, ctx);
2414 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002415 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002416 if (IS_ERR(task))
2417 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002418 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002419 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002420 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002421 smp_wmb();
2422 } else
2423 status = data->rpc_status;
2424 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002425
2426 return status;
2427}
2428
2429static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2430{
David Howells2b0143b2015-03-17 22:25:59 +00002431 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002432 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002433 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002434
Fred Isaman3b65a302016-09-19 10:06:49 -04002435 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002436 if (status != 0 || !data->rpc_done)
2437 return status;
2438
Trond Myklebust6926afd12012-01-07 13:22:46 -05002439 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2440
Anna Schumakerd7e98252017-01-11 16:13:29 -05002441 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002442 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002443
2444 return status;
2445}
2446
Trond Myklebustf3792d62014-07-10 08:54:32 -04002447/*
2448 * Additional permission checks in order to distinguish between an
2449 * open for read, and an open for execute. This works around the
2450 * fact that NFSv4 OPEN treats read and execute permissions as being
2451 * the same.
2452 * Note that in the non-execute case, we want to turn off permission
2453 * checking if we just created a new file (POSIX open() semantics).
2454 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002455static int nfs4_opendata_access(struct rpc_cred *cred,
2456 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002457 struct nfs4_state *state, fmode_t fmode,
2458 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002459{
2460 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002461 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002462
2463 /* access call failed or for some reason the server doesn't
2464 * support any access modes -- defer access call until later */
2465 if (opendata->o_res.access_supported == 0)
2466 return 0;
2467
2468 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002469 /*
2470 * Use openflags to check for exec, because fmode won't
2471 * always have FMODE_EXEC set when file open for exec.
2472 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002473 if (openflags & __FMODE_EXEC) {
2474 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002475 if (S_ISDIR(state->inode->i_mode))
2476 mask = NFS4_ACCESS_LOOKUP;
2477 else
2478 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002479 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002480 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002481
2482 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002483 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2484 nfs_access_add_cache(state->inode, &cache);
2485
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002486 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2487 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002488 return 0;
2489
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002490 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002491}
2492
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002493/*
2494 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2495 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002496static int _nfs4_proc_open(struct nfs4_opendata *data,
2497 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002498{
David Howells2b0143b2015-03-17 22:25:59 +00002499 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002500 struct nfs_server *server = NFS_SERVER(dir);
2501 struct nfs_openargs *o_arg = &data->o_arg;
2502 struct nfs_openres *o_res = &data->o_res;
2503 int status;
2504
Fred Isaman3b65a302016-09-19 10:06:49 -04002505 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002506 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002507 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002508 if (status != 0) {
2509 if (status == -NFS4ERR_BADNAME &&
2510 !(o_arg->open_flags & O_CREAT))
2511 return -ENOENT;
2512 return status;
2513 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002514
Trond Myklebust6926afd12012-01-07 13:22:46 -05002515 nfs_fattr_map_and_free_names(server, &data->f_attr);
2516
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002517 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002518 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002519 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002520 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002521 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002522 if (data->file_created ||
2523 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002524 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002525 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002526 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002527 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2528 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002530 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002532 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002534 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2535 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002536 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2537 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002538 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542/*
2543 * OPEN_EXPIRED:
2544 * reclaim state on the server after a network partition.
2545 * Assumes caller holds the appropriate lock
2546 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002547static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002549 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002550 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002552 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002553 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002554 if (IS_ERR(opendata))
2555 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002556 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002557 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002558 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002559 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002560 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002563static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002564{
Trond Myklebust539cd032007-06-05 11:46:42 -04002565 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002566 struct nfs4_exception exception = { };
2567 int err;
2568
2569 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002570 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002571 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002572 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2573 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002574 switch (err) {
2575 default:
2576 goto out;
2577 case -NFS4ERR_GRACE:
2578 case -NFS4ERR_DELAY:
2579 nfs4_handle_exception(server, err, &exception);
2580 err = 0;
2581 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002582 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002583out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002584 return err;
2585}
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Trond Myklebust864472e2006-01-03 09:55:15 +01002592 ctx = nfs4_state_find_open_context(state);
2593 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002594 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002595 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002596 put_nfs_open_context(ctx);
2597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Trond Myklebust41020b62016-09-22 13:38:58 -04002600static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2601 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002602{
Trond Myklebust41020b62016-09-22 13:38:58 -04002603 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002604 write_seqlock(&state->seqlock);
2605 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2606 write_sequnlock(&state->seqlock);
2607 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2608}
2609
2610static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2611{
2612 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002613 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002614}
2615
2616static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2617{
2618 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2619 nfs40_clear_delegation_stateid(state);
2620 return nfs4_open_expired(sp, state);
2621}
2622
Trond Myklebust45870d62016-09-22 13:38:59 -04002623static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2624 nfs4_stateid *stateid,
2625 struct rpc_cred *cred)
2626{
2627 return -NFS4ERR_BAD_STATEID;
2628}
2629
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002630#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002631static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2632 nfs4_stateid *stateid,
2633 struct rpc_cred *cred)
2634{
2635 int status;
2636
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002637 switch (stateid->type) {
2638 default:
2639 break;
2640 case NFS4_INVALID_STATEID_TYPE:
2641 case NFS4_SPECIAL_STATEID_TYPE:
2642 return -NFS4ERR_BAD_STATEID;
2643 case NFS4_REVOKED_STATEID_TYPE:
2644 goto out_free;
2645 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002646
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002647 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002648 switch (status) {
2649 case -NFS4ERR_EXPIRED:
2650 case -NFS4ERR_ADMIN_REVOKED:
2651 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002652 break;
2653 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002654 return status;
2655 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002656out_free:
2657 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002658 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002659 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002660}
2661
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002662static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002663{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002664 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002665 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002666 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002667 struct rpc_cred *cred;
2668 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002669
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002670 /* Get the delegation credential for use by test/free_stateid */
2671 rcu_read_lock();
2672 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002673 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002674 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002675 return;
2676 }
2677
2678 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002679 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2680 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2681 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002682 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002683 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002684 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002685 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002686
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002687 cred = get_rpccred(delegation->cred);
2688 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002689 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002690 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002691 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002692 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002693
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002694 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002695}
2696
2697/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002698 * nfs41_check_expired_locks - possibly free a lock stateid
2699 *
2700 * @state: NFSv4 state for an inode
2701 *
2702 * Returns NFS_OK if recovery for this stateid is now finished.
2703 * Otherwise a negative NFS4ERR value is returned.
2704 */
2705static int nfs41_check_expired_locks(struct nfs4_state *state)
2706{
2707 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002708 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002709 struct nfs_server *server = NFS_SERVER(state->inode);
2710
2711 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2712 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002713
2714 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002715 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2716 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2717 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2718
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002719 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002720 spin_unlock(&state->state_lock);
2721
2722 nfs4_put_lock_state(prev);
2723 prev = lsp;
2724
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002725 status = nfs41_test_and_free_expired_stateid(server,
2726 &lsp->ls_stateid,
2727 cred);
2728 trace_nfs4_test_lock_stateid(state, lsp, status);
2729 if (status == -NFS4ERR_EXPIRED ||
2730 status == -NFS4ERR_BAD_STATEID) {
2731 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002732 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002733 if (!recover_lost_locks)
2734 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2735 } else if (status != NFS_OK) {
2736 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002737 nfs4_put_lock_state(prev);
2738 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002739 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002740 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002741 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002742 }
2743 spin_unlock(&state->state_lock);
2744 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002745out:
2746 return ret;
2747}
2748
2749/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002750 * nfs41_check_open_stateid - possibly free an open stateid
2751 *
2752 * @state: NFSv4 state for an inode
2753 *
2754 * Returns NFS_OK if recovery for this stateid is now finished.
2755 * Otherwise a negative NFS4ERR value is returned.
2756 */
2757static int nfs41_check_open_stateid(struct nfs4_state *state)
2758{
2759 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002760 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002761 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002762 int status;
2763
Trond Myklebustb134fc42016-09-22 13:39:16 -04002764 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002765 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2766 if (nfs4_have_delegation(state->inode, state->state))
2767 return NFS_OK;
2768 return -NFS4ERR_OPENMODE;
2769 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002770 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002771 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002772 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002773 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002774 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002775 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2776 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002778 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002779 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002780 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002781 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002782 if (nfs_open_stateid_recover_openmode(state))
2783 return -NFS4ERR_OPENMODE;
2784 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002785}
2786
2787static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2788{
Chuck Levereb64cf92012-07-11 16:30:05 -04002789 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002790
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002791 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002792 status = nfs41_check_expired_locks(state);
2793 if (status != NFS_OK)
2794 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002795 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002796 if (status != NFS_OK)
2797 status = nfs4_open_expired(sp, state);
2798 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002799}
2800#endif
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002803 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2804 * fields corresponding to attributes that were used to store the verifier.
2805 * Make sure we clobber those fields in the later setattr call
2806 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002807static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002808 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002809{
Trond Myklebust609339c2018-03-28 16:18:17 -04002810 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2811 __u32 attrset[3];
2812 unsigned ret;
2813 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002814
Trond Myklebust609339c2018-03-28 16:18:17 -04002815 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2816 attrset[i] = opendata->o_res.attrset[i];
2817 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2818 attrset[i] &= ~bitmask[i];
2819 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002820
Trond Myklebust609339c2018-03-28 16:18:17 -04002821 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2822 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002823
Trond Myklebust609339c2018-03-28 16:18:17 -04002824 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2825 if (sattr->ia_valid & ATTR_ATIME_SET)
2826 ret |= ATTR_ATIME_SET;
2827 else
2828 ret |= ATTR_ATIME;
2829 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002830
Trond Myklebust609339c2018-03-28 16:18:17 -04002831 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2832 if (sattr->ia_valid & ATTR_MTIME_SET)
2833 ret |= ATTR_MTIME_SET;
2834 else
2835 ret |= ATTR_MTIME;
2836 }
2837
2838 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002839 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002840 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002841}
2842
Trond Myklebustc21443c2013-02-07 14:26:21 -05002843static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2844 fmode_t fmode,
2845 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002846 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002847{
2848 struct nfs4_state_owner *sp = opendata->owner;
2849 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002850 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002851 struct nfs4_state *state;
2852 unsigned int seq;
2853 int ret;
2854
2855 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2856
Fred Isaman3b65a302016-09-19 10:06:49 -04002857 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002858 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002859 goto out;
2860
Trond Myklebustae55e592018-05-22 11:17:16 -04002861 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002862 ret = PTR_ERR(state);
2863 if (IS_ERR(state))
2864 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002865 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002866 if (server->caps & NFS_CAP_POSIX_LOCK)
2867 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002868 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2869 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002870
Trond Myklebust275bb302013-05-29 13:11:28 -04002871 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002872 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002873 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002874 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002875 alias = d_exact_alias(dentry, state->inode);
2876 if (!alias)
2877 alias = d_splice_alias(igrab(state->inode), dentry);
2878 /* d_splice_alias() can't fail here - it's a non-directory */
2879 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002880 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002881 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002882 }
2883 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002884 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002885 }
2886
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002887 /* Parse layoutget results before we check for access */
2888 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2889
Trond Myklebustc21443c2013-02-07 14:26:21 -05002890 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2891 if (ret != 0)
2892 goto out;
2893
David Howells2b0143b2015-03-17 22:25:59 +00002894 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002895 nfs_inode_attach_open_context(ctx);
2896 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2897 nfs4_schedule_stateid_recovery(server, state);
2898 }
Fred Isaman2409a972016-10-06 12:11:21 -04002899
Trond Myklebustc21443c2013-02-07 14:26:21 -05002900out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002901 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002902 return ret;
2903}
2904
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002905/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002906 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
Andy Adamson82be4172012-05-23 05:02:35 -04002908static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002909 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002910 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002911 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002912 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct nfs4_state_owner *sp;
2915 struct nfs4_state *state = NULL;
2916 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002917 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002918 struct dentry *dentry = ctx->dentry;
2919 struct rpc_cred *cred = ctx->cred;
2920 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2921 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002922 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002923 struct iattr *sattr = c->sattr;
2924 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002925 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002926 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002930 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2931 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2933 goto out_err;
2934 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002935 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002936 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002937 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002938 if (d_really_is_positive(dentry))
2939 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002940 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002941 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002942 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002943 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2944 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002945 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002946 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
David Quigley14c43f72013-05-22 12:50:43 -04002948 if (label) {
2949 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2950 if (IS_ERR(olabel)) {
2951 status = PTR_ERR(olabel);
2952 goto err_opendata_put;
2953 }
2954 }
2955
Trond Myklebuste911b812014-03-26 13:24:37 -07002956 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2957 if (!opendata->f_attr.mdsthreshold) {
2958 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2959 if (!opendata->f_attr.mdsthreshold)
2960 goto err_free_label;
2961 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002962 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002963 }
David Howells2b0143b2015-03-17 22:25:59 +00002964 if (d_really_is_positive(dentry))
2965 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002966
Trond Myklebust3efb9722013-05-29 13:17:04 -04002967 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002968 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002969 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002970 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002971
NeilBrownefcbc042015-07-30 13:00:56 +10002972 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002973 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002974 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002975 /*
2976 * send create attributes which was not set by open
2977 * with an extra setattr.
2978 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002979 if (attrs || label) {
2980 unsigned ia_old = sattr->ia_valid;
2981
2982 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002983 nfs_fattr_init(opendata->o_res.f_attr);
2984 status = nfs4_do_setattr(state->inode, cred,
2985 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002986 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002987 if (status == 0) {
2988 nfs_setattr_update_inode(state->inode, sattr,
2989 opendata->o_res.f_attr);
2990 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2991 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002992 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002993 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002994 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002995 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002996 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002997
Trond Myklebuste911b812014-03-26 13:24:37 -07002998 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002999 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003000 opendata->f_attr.mdsthreshold = NULL;
3001 }
Andy Adamson82be4172012-05-23 05:02:35 -04003002
David Quigley14c43f72013-05-22 12:50:43 -04003003 nfs4_label_free(olabel);
3004
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003005 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003008err_free_label:
3009 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003010err_opendata_put:
3011 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003012err_put_state_owner:
3013 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return status;
3016}
3017
3018
Andy Adamson82be4172012-05-23 05:02:35 -04003019static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003020 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003021 int flags,
3022 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003023 struct nfs4_label *label,
3024 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003026 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 struct nfs4_exception exception = { };
3028 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003029 struct nfs4_open_createattrs c = {
3030 .label = label,
3031 .sattr = sattr,
3032 .verf = {
3033 [0] = (__u32)jiffies,
3034 [1] = (__u32)current->pid,
3035 },
3036 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 int status;
3038
3039 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003040 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003041 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003042 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (status == 0)
3044 break;
3045 /* NOTE: BAD_SEQID means the server and client disagree about the
3046 * book-keeping w.r.t. state-changing operations
3047 * (OPEN/CLOSE/LOCK/LOCKU...)
3048 * It is actually a sign of a bug on the client or on the server.
3049 *
3050 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003051 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 * have unhashed the old state_owner for us, and that we can
3053 * therefore safely retry using a new one. We should still warn
3054 * the user though...
3055 */
3056 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003057 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003058 " returned a bad sequence-id error!\n",
3059 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 exception.retry = 1;
3061 continue;
3062 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003063 /*
3064 * BAD_STATEID on OPEN means that the server cancelled our
3065 * state before it received the OPEN_CONFIRM.
3066 * Recover by retrying the request as per the discussion
3067 * on Page 181 of RFC3530.
3068 */
3069 if (status == -NFS4ERR_BAD_STATEID) {
3070 exception.retry = 1;
3071 continue;
3072 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003073 if (status == -EAGAIN) {
3074 /* We must have found a delegation */
3075 exception.retry = 1;
3076 continue;
3077 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003078 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3079 continue;
3080 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 status, &exception));
3082 } while (exception.retry);
3083 return res;
3084}
3085
Trond Myklebust8487c472016-06-26 08:44:35 -04003086static int _nfs4_do_setattr(struct inode *inode,
3087 struct nfs_setattrargs *arg,
3088 struct nfs_setattrres *res,
3089 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003090 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003091{
3092 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003093 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3095 .rpc_argp = arg,
3096 .rpc_resp = res,
3097 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003098 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003099 struct rpc_cred *delegation_cred = NULL;
3100 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003101 bool truncate;
3102 int status;
3103
3104 nfs_fattr_init(res->fattr);
3105
3106 /* Servers should only apply open mode checks for file size changes */
3107 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003108 if (!truncate)
3109 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003110
Trond Myklebust991eedb2018-04-09 11:15:30 -04003111 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003112 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003113 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003114 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003115 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003116 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003117 l_ctx = nfs_get_lock_context(ctx);
3118 if (IS_ERR(l_ctx))
3119 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003120 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3121 &arg->stateid, &delegation_cred);
3122 nfs_put_lock_context(l_ctx);
3123 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003124 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003125 } else {
3126zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003127 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003128 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003129 if (delegation_cred)
3130 msg.rpc_cred = delegation_cred;
3131
3132 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3133
3134 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003135 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003136 renew_lease(server, timestamp);
3137 trace_nfs4_setattr(inode, &arg->stateid, status);
3138 return status;
3139}
3140
3141static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3142 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003143 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003144 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003146 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003147 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003148 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003149 struct nfs_setattrargs arg = {
3150 .fh = NFS_FH(inode),
3151 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003153 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003154 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003155 };
3156 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003158 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003160 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003161 struct nfs4_exception exception = {
3162 .state = state,
3163 .inode = inode,
3164 .stateid = &arg.stateid,
3165 };
3166 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003169 nfs4_bitmap_copy_adjust_setattr(bitmask,
3170 nfs4_bitmask(server, olabel),
3171 inode);
3172
NeilBrown29b59f92016-10-13 15:26:47 +11003173 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003174 switch (err) {
3175 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003176 if (!(sattr->ia_valid & ATTR_SIZE)) {
3177 pr_warn_once("NFSv4: server %s is incorrectly "
3178 "applying open mode checks to "
3179 "a SETATTR that is not "
3180 "changing file size.\n",
3181 server->nfs_client->cl_hostname);
3182 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003183 if (state && !(state->state & FMODE_WRITE)) {
3184 err = -EBADF;
3185 if (sattr->ia_valid & ATTR_OPEN)
3186 err = -EACCES;
3187 goto out;
3188 }
3189 }
3190 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003192out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return err;
3194}
3195
Peng Tao500d7012015-09-22 11:35:22 +08003196static bool
3197nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3198{
3199 if (inode == NULL || !nfs_have_layout(inode))
3200 return false;
3201
3202 return pnfs_wait_on_layoutreturn(inode, task);
3203}
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205struct nfs4_closedata {
3206 struct inode *inode;
3207 struct nfs4_state *state;
3208 struct nfs_closeargs arg;
3209 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003210 struct {
3211 struct nfs4_layoutreturn_args arg;
3212 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003213 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003214 u32 roc_barrier;
3215 bool roc;
3216 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003217 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003218 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219};
3220
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003221static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003222{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003223 struct nfs4_closedata *calldata = data;
3224 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003225 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003226
Trond Myklebustcf805162016-11-15 14:56:07 -05003227 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003228 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3229 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003230 nfs4_put_open_state(calldata->state);
3231 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003232 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003233 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003234 kfree(calldata);
3235}
3236
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003237static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003239 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003242 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003243 struct nfs4_exception exception = {
3244 .state = state,
3245 .inode = calldata->inode,
3246 .stateid = &calldata->arg.stateid,
3247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Chuck Levera3ca5652012-03-01 17:00:40 -05003249 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003250 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3251 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003252 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003253
3254 /* Handle Layoutreturn errors */
3255 if (calldata->arg.lr_args && task->tk_status != 0) {
3256 switch (calldata->res.lr_ret) {
3257 default:
3258 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3259 break;
3260 case 0:
3261 calldata->arg.lr_args = NULL;
3262 calldata->res.lr_res = NULL;
3263 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003264 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003265 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003266 calldata->inode))
3267 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003268 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003269 case -NFS4ERR_ADMIN_REVOKED:
3270 case -NFS4ERR_DELEG_REVOKED:
3271 case -NFS4ERR_EXPIRED:
3272 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003273 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3274 case -NFS4ERR_WRONG_CRED:
3275 calldata->arg.lr_args = NULL;
3276 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003277 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003278 }
3279 }
3280
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003281 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 * the state_owner. we keep this around to process errors
3283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 switch (task->tk_status) {
3285 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003286 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003287 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003288 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003289 case -NFS4ERR_ACCESS:
3290 if (calldata->arg.bitmask != NULL) {
3291 calldata->arg.bitmask = NULL;
3292 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003293 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003294
3295 }
3296 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003297 case -NFS4ERR_OLD_STATEID:
3298 /* Did we race with OPEN? */
3299 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003300 state))
3301 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003302 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003303 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003305 case -NFS4ERR_EXPIRED:
3306 nfs4_free_revoked_stateid(server,
3307 &calldata->arg.stateid,
3308 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003309 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003310 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003313 task->tk_status = nfs4_async_handle_exception(task,
3314 server, task->tk_status, &exception);
3315 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003316 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003318 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3319 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003320out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003321 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003322 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003323 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003324 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003325 return;
3326lr_restart:
3327 calldata->res.lr_ret = 0;
3328out_restart:
3329 task->tk_status = 0;
3330 rpc_restart_call_prepare(task);
3331 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003334static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003336 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003337 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003338 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003339 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003340 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003341 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003342
Chuck Levera3ca5652012-03-01 17:00:40 -05003343 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003344 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003345 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003346
Trond Myklebust88069f72009-12-08 08:33:16 -05003347 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003348 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003349 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3350 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3351 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003352 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003353 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003354 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003355 if (state->n_rdonly == 0)
3356 call_close |= is_rdonly;
3357 else if (is_rdonly)
3358 calldata->arg.fmode |= FMODE_READ;
3359 if (state->n_wronly == 0)
3360 call_close |= is_wronly;
3361 else if (is_wronly)
3362 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003363 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3364 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003365 } else if (is_rdwr)
3366 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3367
Trond Myklebust5cc78612016-11-14 11:19:56 -05003368 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003369 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003370 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003371 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003372
3373 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003374 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003375 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003376 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003377
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003378 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003379 nfs_release_seqid(calldata->arg.seqid);
3380 goto out_wait;
3381 }
3382
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003383 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3384 if (lo && !pnfs_layout_is_valid(lo)) {
3385 calldata->arg.lr_args = NULL;
3386 calldata->res.lr_res = NULL;
3387 }
3388
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003389 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003390 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003391
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003392 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003393 /* Close-to-open cache consistency revalidation */
3394 if (!nfs4_have_delegation(inode, FMODE_READ))
3395 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3396 else
3397 calldata->arg.bitmask = NULL;
3398 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003399
Trond Myklebust6ae37332015-01-30 14:21:14 -05003400 calldata->arg.share_access =
3401 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3402 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003403
Trond Myklebustd8d84982016-12-19 12:14:44 -05003404 if (calldata->res.fattr == NULL)
3405 calldata->arg.bitmask = NULL;
3406 else if (calldata->arg.bitmask == NULL)
3407 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003408 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003409 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003410 &calldata->arg.seq_args,
3411 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003412 task) != 0)
3413 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003414 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003415 return;
3416out_no_action:
3417 task->tk_action = NULL;
3418out_wait:
3419 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003422static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003423 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003424 .rpc_call_done = nfs4_close_done,
3425 .rpc_release = nfs4_free_closedata,
3426};
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428/*
3429 * It is possible for data to be read/written from a mem-mapped file
3430 * after the sys_close call (which hits the vfs layer as a flush).
3431 * This means that we can't safely call nfsv4 close on a file until
3432 * the inode is cleared. This in turn means that we are not good
3433 * NFSv4 citizens - we do not indicate to the server to update the file's
3434 * share state even when we are done with one of the three share
3435 * stateid's in the inode.
3436 *
3437 * NOTE: Caller must be holding the sp->so_owner semaphore!
3438 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003439int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003441 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003442 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003444 struct nfs4_state_owner *sp = state->owner;
3445 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3448 .rpc_cred = state->owner->so_cred,
3449 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003450 struct rpc_task_setup task_setup_data = {
3451 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003452 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003453 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003454 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003455 .flags = RPC_TASK_ASYNC,
3456 };
Trond Myklebust95121352005-10-18 14:20:12 -07003457 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003459 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3460 &task_setup_data.rpc_client, &msg);
3461
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003462 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003464 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003465 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003466 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003468 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003469 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3470 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003472 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3473 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003474 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003475 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003476 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003477 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003478 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003479 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003480 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003481 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003482 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003483 calldata->lr.roc = pnfs_roc(state->inode,
3484 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3485 if (calldata->lr.roc) {
3486 calldata->arg.lr_args = &calldata->lr.arg;
3487 calldata->res.lr_res = &calldata->lr.res;
3488 }
Al Viro643168c2011-06-22 18:20:23 -04003489 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003490
Trond Myklebust1174dd12010-12-21 10:52:24 -05003491 msg.rpc_argp = &calldata->arg;
3492 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003493 task_setup_data.callback_data = calldata;
3494 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003495 if (IS_ERR(task))
3496 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003497 status = 0;
3498 if (wait)
3499 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003500 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003501 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003502out_free_calldata:
3503 kfree(calldata);
3504out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003505 nfs4_put_open_state(state);
3506 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003507 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508}
3509
Trond Myklebust2b484292010-09-17 10:56:51 -04003510static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003511nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3512 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003515 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3516
3517 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Trond Myklebust565277f2007-10-15 18:17:53 -04003519 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003520 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003521
3522 nfs4_label_release_security(label);
3523
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003524 if (IS_ERR(state))
3525 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003526 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Trond Myklebust1185a552009-12-03 15:54:02 -05003529static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003530{
3531 if (ctx->state == NULL)
3532 return;
3533 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003534 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003535 else
Al Viro643168c2011-06-22 18:20:23 -04003536 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Trond Myklebustb944dba2013-11-04 15:20:20 -05003539#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3540#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003541#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3544{
Kinglong Mee8c612822015-08-26 21:12:58 +08003545 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003546 struct nfs4_server_caps_arg args = {
3547 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003548 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 struct nfs4_server_caps_res res = {};
3551 struct rpc_message msg = {
3552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003553 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 .rpc_resp = &res,
3555 };
3556 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003557 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Kinglong Mee8c612822015-08-26 21:12:58 +08003559 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3560 FATTR4_WORD0_FH_EXPIRE_TYPE |
3561 FATTR4_WORD0_LINK_SUPPORT |
3562 FATTR4_WORD0_SYMLINK_SUPPORT |
3563 FATTR4_WORD0_ACLSUPPORT;
3564 if (minorversion)
3565 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3566
Bryan Schumaker7c513052011-03-24 17:12:24 +00003567 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003569 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003570 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003571 case 0:
3572 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3573 res.attr_bitmask[2] = 0;
3574 break;
3575 case 1:
3576 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3577 break;
3578 case 2:
3579 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003582 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3583 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3584 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3585 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003586 NFS_CAP_CTIME|NFS_CAP_MTIME|
3587 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003588 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3589 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 server->caps |= NFS_CAP_ACLS;
3591 if (res.has_links != 0)
3592 server->caps |= NFS_CAP_HARDLINKS;
3593 if (res.has_symlinks != 0)
3594 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003595 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3596 server->caps |= NFS_CAP_FILEID;
3597 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3598 server->caps |= NFS_CAP_MODE;
3599 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3600 server->caps |= NFS_CAP_NLINK;
3601 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3602 server->caps |= NFS_CAP_OWNER;
3603 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3604 server->caps |= NFS_CAP_OWNER_GROUP;
3605 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3606 server->caps |= NFS_CAP_ATIME;
3607 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3608 server->caps |= NFS_CAP_CTIME;
3609 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3610 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003611#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3612 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3613 server->caps |= NFS_CAP_SECURITY_LABEL;
3614#endif
3615 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3616 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003617 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003618
Trond Myklebusta65318b2009-03-11 14:10:28 -04003619 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3620 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3621 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003622 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003623
3624 /* Avoid a regression due to buggy server */
3625 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3626 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003627 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3628 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003631 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 return status;
3635}
3636
Trond Myklebust55a97592006-06-09 09:34:19 -04003637int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
3639 struct nfs4_exception exception = { };
3640 int err;
3641 do {
3642 err = nfs4_handle_exception(server,
3643 _nfs4_server_capabilities(server, fhandle),
3644 &exception);
3645 } while (exception.retry);
3646 return err;
3647}
3648
3649static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3650 struct nfs_fsinfo *info)
3651{
David Quigleyaa9c2662013-05-22 12:50:44 -04003652 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003654 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 };
3656 struct nfs4_lookup_res res = {
3657 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003658 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 .fh = fhandle,
3660 };
3661 struct rpc_message msg = {
3662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3663 .rpc_argp = &args,
3664 .rpc_resp = &res,
3665 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003666
David Quigleyaa9c2662013-05-22 12:50:44 -04003667 bitmask[0] = nfs4_fattr_bitmap[0];
3668 bitmask[1] = nfs4_fattr_bitmap[1];
3669 /*
3670 * Process the label in the upcoming getfattr
3671 */
3672 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3673
Trond Myklebust0e574af2005-10-27 22:12:38 -04003674 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003675 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
3678static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3679 struct nfs_fsinfo *info)
3680{
3681 struct nfs4_exception exception = { };
3682 int err;
3683 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003684 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003685 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003686 switch (err) {
3687 case 0:
3688 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003689 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003690 default:
3691 err = nfs4_handle_exception(server, err, &exception);
3692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return err;
3696}
3697
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003698static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3699 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3700{
Trond Myklebustc2190662013-08-26 19:23:04 -04003701 struct rpc_auth_create_args auth_args = {
3702 .pseudoflavor = flavor,
3703 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003704 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003705
Trond Myklebustc2190662013-08-26 19:23:04 -04003706 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003707 if (IS_ERR(auth))
3708 return -EACCES;
3709 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003710}
3711
Chuck Lever9a744ba2013-03-16 15:56:02 -04003712/*
3713 * Retry pseudoroot lookup with various security flavors. We do this when:
3714 *
3715 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3716 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3717 *
3718 * Returns zero on success, or a negative NFS4ERR value, or a
3719 * negative errno value.
3720 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003721static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003722 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003724 /* Per 3530bis 15.33.5 */
3725 static const rpc_authflavor_t flav_array[] = {
3726 RPC_AUTH_GSS_KRB5P,
3727 RPC_AUTH_GSS_KRB5I,
3728 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003729 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003730 RPC_AUTH_NULL,
3731 };
3732 int status = -EPERM;
3733 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003735 if (server->auth_info.flavor_len > 0) {
3736 /* try each flavor specified by user */
3737 for (i = 0; i < server->auth_info.flavor_len; i++) {
3738 status = nfs4_lookup_root_sec(server, fhandle, info,
3739 server->auth_info.flavors[i]);
3740 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3741 continue;
3742 break;
3743 }
3744 } else {
3745 /* no flavors specified by user, try default list */
3746 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3747 status = nfs4_lookup_root_sec(server, fhandle, info,
3748 flav_array[i]);
3749 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3750 continue;
3751 break;
3752 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003753 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003754
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003755 /*
3756 * -EACCESS could mean that the user doesn't have correct permissions
3757 * to access the mount. It could also mean that we tried to mount
3758 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3759 * existing mount programs don't handle -EACCES very well so it should
3760 * be mapped to -EPERM instead.
3761 */
3762 if (status == -EACCES)
3763 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003764 return status;
3765}
3766
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003767/**
3768 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3769 * @server: initialized nfs_server handle
3770 * @fhandle: we fill in the pseudo-fs root file handle
3771 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003772 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003773 *
3774 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003775 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003776int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003777 struct nfs_fsinfo *info,
3778 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003779{
Andre Przywarac7757072015-04-23 17:17:40 +01003780 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003781
Andre Przywarac7757072015-04-23 17:17:40 +01003782 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003783 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003784
3785 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003786 status = server->nfs_client->cl_mvops->find_root_sec(server,
3787 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 if (status == 0)
3790 status = nfs4_server_capabilities(server, fhandle);
3791 if (status == 0)
3792 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003793
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003794 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
Bryan Schumakerbae36242012-05-10 15:07:31 -04003797static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3798 struct nfs_fsinfo *info)
3799{
3800 int error;
3801 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003802 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003803
3804 error = nfs4_server_capabilities(server, mntfh);
3805 if (error < 0) {
3806 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3807 return error;
3808 }
3809
David Quigley14c43f72013-05-22 12:50:43 -04003810 label = nfs4_label_alloc(server, GFP_KERNEL);
3811 if (IS_ERR(label))
3812 return PTR_ERR(label);
3813
Trond Myklebusta841b542018-04-07 13:50:59 -04003814 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003815 if (error < 0) {
3816 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003817 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003818 }
3819
3820 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3821 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3822 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3823
David Quigley14c43f72013-05-22 12:50:43 -04003824err_free_label:
3825 nfs4_label_free(label);
3826
Bryan Schumakerbae36242012-05-10 15:07:31 -04003827 return error;
3828}
3829
Manoj Naik6b97fd32006-06-09 09:34:29 -04003830/*
3831 * Get locations and (maybe) other attributes of a referral.
3832 * Note that we'll actually follow the referral later when
3833 * we detect fsid mismatch in inode revalidation
3834 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003835static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3836 const struct qstr *name, struct nfs_fattr *fattr,
3837 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003838{
3839 int status = -ENOMEM;
3840 struct page *page = NULL;
3841 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003842
3843 page = alloc_page(GFP_KERNEL);
3844 if (page == NULL)
3845 goto out;
3846 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3847 if (locations == NULL)
3848 goto out;
3849
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003850 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003851 if (status != 0)
3852 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003853
3854 /*
3855 * If the fsid didn't change, this is a migration event, not a
3856 * referral. Cause us to drop into the exception handler, which
3857 * will kick off migration recovery.
3858 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003859 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003860 dprintk("%s: server did not return a different fsid for"
3861 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003862 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003863 goto out;
3864 }
Andy Adamson533eb462011-06-13 18:25:56 -04003865 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3866 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003867
Andy Adamson533eb462011-06-13 18:25:56 -04003868 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003869 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003870 memset(fhandle, 0, sizeof(struct nfs_fh));
3871out:
3872 if (page)
3873 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003874 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003875 return status;
3876}
3877
David Quigley1775fd32013-05-22 12:50:42 -04003878static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003879 struct nfs_fattr *fattr, struct nfs4_label *label,
3880 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Trond Myklebust771734f2018-04-07 13:54:23 -04003882 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct nfs4_getattr_arg args = {
3884 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003885 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 };
3887 struct nfs4_getattr_res res = {
3888 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003889 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 .server = server,
3891 };
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3894 .rpc_argp = &args,
3895 .rpc_resp = &res,
3896 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003897
Trond Myklebust771734f2018-04-07 13:54:23 -04003898 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003899
Trond Myklebust0e574af2005-10-27 22:12:38 -04003900 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
David Quigley1775fd32013-05-22 12:50:42 -04003904static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003905 struct nfs_fattr *fattr, struct nfs4_label *label,
3906 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
3908 struct nfs4_exception exception = { };
3909 int err;
3910 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003911 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003912 trace_nfs4_getattr(server, fhandle, fattr, err);
3913 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 &exception);
3915 } while (exception.retry);
3916 return err;
3917}
3918
3919/*
3920 * The file is not closed if it is opened due to the a request to change
3921 * the size of the file. The open call will not be needed once the
3922 * VFS layer lookup-intents are implemented.
3923 *
3924 * Close is called when the inode is destroyed.
3925 * If we haven't opened the file for O_WRONLY, we
3926 * need to in the size_change case to obtain a stateid.
3927 *
3928 * Got race?
3929 * Because OPEN is always done by name in nfsv4, it is
3930 * possible that we opened a different file by the same
3931 * name. We can recognize this race condition, but we
3932 * can't do anything about it besides returning an error.
3933 *
3934 * This will be fixed with VFS changes (lookup-intent).
3935 */
3936static int
3937nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3938 struct iattr *sattr)
3939{
David Howells2b0143b2015-03-17 22:25:59 +00003940 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003941 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003942 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003943 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 int status;
3945
Peng Tao88ac8152014-09-12 11:04:10 +08003946 if (pnfs_ld_layoutret_on_setattr(inode) &&
3947 sattr->ia_valid & ATTR_SIZE &&
3948 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003949 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003950
Trond Myklebust0e574af2005-10-27 22:12:38 -04003951 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Andy Adamson26699402012-05-30 16:12:24 -04003953 /* Deal with open(O_TRUNC) */
3954 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003955 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003956
3957 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003958 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003959 return 0;
3960
Trond Myklebustd5308382005-11-04 15:33:38 -05003961 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003962 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003963
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003964 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003965 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003966 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003967 }
3968
David Quigley14c43f72013-05-22 12:50:43 -04003969 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3970 if (IS_ERR(label))
3971 return PTR_ERR(label);
3972
Trond Myklebust199366f2018-03-20 16:43:18 -04003973 /* Return any delegations if we're going to change ACLs */
3974 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003975 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003976
NeilBrown29b59f92016-10-13 15:26:47 +11003977 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003978 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003979 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003980 nfs_setsecurity(inode, fattr, label);
3981 }
David Quigley14c43f72013-05-22 12:50:43 -04003982 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return status;
3984}
3985
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003986static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3987 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003988 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003989{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003990 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003991 int status;
3992 struct nfs4_lookup_arg args = {
3993 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003994 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003995 .name = name,
3996 };
3997 struct nfs4_lookup_res res = {
3998 .server = server,
3999 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004000 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004001 .fh = fhandle,
4002 };
4003 struct rpc_message msg = {
4004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4005 .rpc_argp = &args,
4006 .rpc_resp = &res,
4007 };
4008
David Quigleyaa9c2662013-05-22 12:50:44 -04004009 args.bitmask = nfs4_bitmask(server, label);
4010
David Howells2b3de442006-08-22 20:06:09 -04004011 nfs_fattr_init(fattr);
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004014 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 dprintk("NFS reply lookup: %d\n", status);
4016 return status;
4017}
4018
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004019static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004020{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004021 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004022 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004023 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4024 fattr->nlink = 2;
4025}
4026
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004027static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004028 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004029 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004030{
4031 struct nfs4_exception exception = { };
4032 struct rpc_clnt *client = *clnt;
4033 int err;
4034 do {
David Quigley1775fd32013-05-22 12:50:42 -04004035 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004036 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004037 switch (err) {
4038 case -NFS4ERR_BADNAME:
4039 err = -ENOENT;
4040 goto out;
4041 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004042 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004043 if (err == -NFS4ERR_MOVED)
4044 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004045 goto out;
4046 case -NFS4ERR_WRONGSEC:
4047 err = -EPERM;
4048 if (client != *clnt)
4049 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004050 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004051 if (IS_ERR(client))
4052 return PTR_ERR(client);
4053
4054 exception.retry = 1;
4055 break;
4056 default:
4057 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4058 }
4059 } while (exception.retry);
4060
4061out:
4062 if (err == 0)
4063 *clnt = client;
4064 else if (client != *clnt)
4065 rpc_shutdown_client(client);
4066
4067 return err;
4068}
4069
Al Virobeffb8f2016-07-20 16:34:42 -04004070static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004071 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4072 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004074 int status;
4075 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004076
David Quigley1775fd32013-05-22 12:50:42 -04004077 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004078 if (client != NFS_CLIENT(dir)) {
4079 rpc_shutdown_client(client);
4080 nfs_fixup_secinfo_attributes(fattr);
4081 }
4082 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004085struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004086nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004087 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4088{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004089 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004090 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004091
David Quigley1775fd32013-05-22 12:50:42 -04004092 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004093 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004094 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004095 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004096}
4097
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004098static int _nfs4_proc_lookupp(struct inode *inode,
4099 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4100 struct nfs4_label *label)
4101{
4102 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4103 struct nfs_server *server = NFS_SERVER(inode);
4104 int status;
4105 struct nfs4_lookupp_arg args = {
4106 .bitmask = server->attr_bitmask,
4107 .fh = NFS_FH(inode),
4108 };
4109 struct nfs4_lookupp_res res = {
4110 .server = server,
4111 .fattr = fattr,
4112 .label = label,
4113 .fh = fhandle,
4114 };
4115 struct rpc_message msg = {
4116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4117 .rpc_argp = &args,
4118 .rpc_resp = &res,
4119 };
4120
4121 args.bitmask = nfs4_bitmask(server, label);
4122
4123 nfs_fattr_init(fattr);
4124
4125 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4126 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4127 &res.seq_res, 0);
4128 dprintk("NFS reply lookupp: %d\n", status);
4129 return status;
4130}
4131
4132static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4133 struct nfs_fattr *fattr, struct nfs4_label *label)
4134{
4135 struct nfs4_exception exception = { };
4136 int err;
4137 do {
4138 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4139 trace_nfs4_lookupp(inode, err);
4140 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4141 &exception);
4142 } while (exception.retry);
4143 return err;
4144}
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4147{
Trond Myklebust76b32992007-08-10 17:45:11 -04004148 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 struct nfs4_accessargs args = {
4150 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004151 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004153 struct nfs4_accessres res = {
4154 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 struct rpc_message msg = {
4157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4158 .rpc_argp = &args,
4159 .rpc_resp = &res,
4160 .rpc_cred = entry->cred,
4161 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004162 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Trond Myklebust7c672652018-06-04 15:00:53 -04004164 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004165 res.fattr = nfs_alloc_fattr();
4166 if (res.fattr == NULL)
4167 return -ENOMEM;
4168 args.bitmask = server->cache_consistency_bitmask;
4169 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004170
Bryan Schumaker7c513052011-03-24 17:12:24 +00004171 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004173 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004174 if (res.fattr)
4175 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004177 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return status;
4179}
4180
4181static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4182{
4183 struct nfs4_exception exception = { };
4184 int err;
4185 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004186 err = _nfs4_proc_access(inode, entry);
4187 trace_nfs4_access(inode, err);
4188 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 &exception);
4190 } while (exception.retry);
4191 return err;
4192}
4193
4194/*
4195 * TODO: For the time being, we don't try to get any attributes
4196 * along with any of the zero-copy operations READ, READDIR,
4197 * READLINK, WRITE.
4198 *
4199 * In the case of the first three, we want to put the GETATTR
4200 * after the read-type operation -- this is because it is hard
4201 * to predict the length of a GETATTR response in v4, and thus
4202 * align the READ data correctly. This means that the GETATTR
4203 * may end up partially falling into the page cache, and we should
4204 * shift it into the 'tail' of the xdr_buf before processing.
4205 * To do this efficiently, we need to know the total length
4206 * of data received, which doesn't seem to be available outside
4207 * of the RPC layer.
4208 *
4209 * In the case of WRITE, we also want to put the GETATTR after
4210 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004211 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 *
4213 * Both of these changes to the XDR layer would in fact be quite
4214 * minor, but I decided to leave them for a subsequent patch.
4215 */
4216static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4217 unsigned int pgbase, unsigned int pglen)
4218{
4219 struct nfs4_readlink args = {
4220 .fh = NFS_FH(inode),
4221 .pgbase = pgbase,
4222 .pglen = pglen,
4223 .pages = &page,
4224 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004225 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 struct rpc_message msg = {
4227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4228 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004229 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 };
4231
Bryan Schumaker7c513052011-03-24 17:12:24 +00004232 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 -07004233}
4234
4235static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4236 unsigned int pgbase, unsigned int pglen)
4237{
4238 struct nfs4_exception exception = { };
4239 int err;
4240 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004241 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4242 trace_nfs4_readlink(inode, err);
4243 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 &exception);
4245 } while (exception.retry);
4246 return err;
4247}
4248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004250 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252static int
4253nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004254 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004256 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004257 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004258 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 int status = 0;
4261
NeilBrown532d4de2016-10-13 15:26:47 +11004262 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004263 if (IS_ERR(ctx))
4264 return PTR_ERR(ctx);
4265
David Quigleyaa9c2662013-05-22 12:50:44 -04004266 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4267
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004268 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4269 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004270 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (IS_ERR(state)) {
4272 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004276 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004277 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return status;
4279}
4280
Trond Myklebust3c591172018-07-31 15:54:10 -04004281static int
4282_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
Trond Myklebust16e42952005-10-27 22:12:44 -04004284 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004285 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004287 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004289 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004290 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004291 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4294 .rpc_argp = &args,
4295 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004297 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004298 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004299
Bryan Schumaker7c513052011-03-24 17:12:24 +00004300 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004301 if (status == 0) {
4302 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004303 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004304 /* Removing a directory decrements nlink in the parent */
4305 if (ftype == NF4DIR && dir->i_nlink > 2)
4306 nfs4_dec_nlink_locked(dir);
4307 spin_unlock(&dir->i_lock);
4308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return status;
4310}
4311
Trond Myklebust912678d2018-03-20 16:43:15 -04004312static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4313{
4314 struct nfs4_exception exception = { };
4315 struct inode *inode = d_inode(dentry);
4316 int err;
4317
Trond Myklebustc01d3642018-03-20 16:43:20 -04004318 if (inode) {
4319 if (inode->i_nlink == 1)
4320 nfs4_inode_return_delegation(inode);
4321 else
4322 nfs4_inode_make_writeable(inode);
4323 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004324 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004325 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004326 trace_nfs4_remove(dir, &dentry->d_name, err);
4327 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4328 &exception);
4329 } while (exception.retry);
4330 return err;
4331}
4332
4333static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{
4335 struct nfs4_exception exception = { };
4336 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004339 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004340 trace_nfs4_remove(dir, name, err);
4341 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 &exception);
4343 } while (exception.retry);
4344 return err;
4345}
4346
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004347static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4348 struct dentry *dentry,
4349 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004351 struct nfs_removeargs *args = msg->rpc_argp;
4352 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Trond Myklebust977fcc22018-03-20 16:43:17 -04004354 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004356 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004357
4358 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004359
4360 if (inode)
4361 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362}
4363
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004364static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4365{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004366 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004367 &data->args.seq_args,
4368 &data->res.seq_res,
4369 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
4371
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004372static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004374 struct nfs_unlinkdata *data = task->tk_calldata;
4375 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004376
Trond Myklebust14516c32010-07-31 14:29:06 -04004377 if (!nfs4_sequence_done(task, &res->seq_res))
4378 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004379 if (nfs4_async_handle_error(task, res->server, NULL,
4380 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004381 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004382 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004383 update_changeattr(dir, &res->cinfo,
4384 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004385 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
4387
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004388static void nfs4_proc_rename_setup(struct rpc_message *msg,
4389 struct dentry *old_dentry,
4390 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004391{
Jeff Laytond3d41522010-09-17 17:31:57 -04004392 struct nfs_renameargs *arg = msg->rpc_argp;
4393 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004394 struct inode *old_inode = d_inode(old_dentry);
4395 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004396
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004397 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004398 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004399 if (new_inode)
4400 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004401 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004402 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004403 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004404}
4405
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004406static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4407{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004408 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004409 &data->args.seq_args,
4410 &data->res.seq_res,
4411 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004412}
4413
4414static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4415 struct inode *new_dir)
4416{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004417 struct nfs_renamedata *data = task->tk_calldata;
4418 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004419
4420 if (!nfs4_sequence_done(task, &res->seq_res))
4421 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004422 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004423 return 0;
4424
Trond Myklebustc733c492017-01-11 12:32:26 -05004425 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004426 if (new_dir != old_dir) {
4427 /* Note: If we moved a directory, nlink will change */
4428 update_changeattr(old_dir, &res->old_cinfo,
4429 res->old_fattr->time_start,
4430 NFS_INO_INVALID_OTHER);
4431 update_changeattr(new_dir, &res->new_cinfo,
4432 res->new_fattr->time_start,
4433 NFS_INO_INVALID_OTHER);
4434 } else
4435 update_changeattr(old_dir, &res->old_cinfo,
4436 res->old_fattr->time_start,
4437 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004438 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004439 return 1;
4440}
4441
Al Virobeffb8f2016-07-20 16:34:42 -04004442static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004444 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004445 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct nfs4_link_arg arg = {
4447 .fh = NFS_FH(inode),
4448 .dir_fh = NFS_FH(dir),
4449 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004450 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004452 struct nfs4_link_res res = {
4453 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004454 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4458 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004459 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004461 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Trond Myklebust136f2622010-04-16 16:22:49 -04004463 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004464 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004465 goto out;
4466
David Quigley14c43f72013-05-22 12:50:43 -04004467 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4468 if (IS_ERR(res.label)) {
4469 status = PTR_ERR(res.label);
4470 goto out;
4471 }
4472
Trond Myklebustc01d3642018-03-20 16:43:20 -04004473 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004474 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004475
Bryan Schumaker7c513052011-03-24 17:12:24 +00004476 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004477 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004478 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004479 status = nfs_post_op_update_inode(inode, res.fattr);
4480 if (!status)
4481 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004482 }
David Quigley14c43f72013-05-22 12:50:43 -04004483
4484
4485 nfs4_label_free(res.label);
4486
Trond Myklebust136f2622010-04-16 16:22:49 -04004487out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004488 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 return status;
4490}
4491
Al Virobeffb8f2016-07-20 16:34:42 -04004492static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
4494 struct nfs4_exception exception = { };
4495 int err;
4496 do {
4497 err = nfs4_handle_exception(NFS_SERVER(inode),
4498 _nfs4_proc_link(inode, dir, name),
4499 &exception);
4500 } while (exception.retry);
4501 return err;
4502}
4503
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004504struct nfs4_createdata {
4505 struct rpc_message msg;
4506 struct nfs4_create_arg arg;
4507 struct nfs4_create_res res;
4508 struct nfs_fh fh;
4509 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004510 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004511};
4512
4513static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004514 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004515{
4516 struct nfs4_createdata *data;
4517
4518 data = kzalloc(sizeof(*data), GFP_KERNEL);
4519 if (data != NULL) {
4520 struct nfs_server *server = NFS_SERVER(dir);
4521
David Quigley14c43f72013-05-22 12:50:43 -04004522 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4523 if (IS_ERR(data->label))
4524 goto out_free;
4525
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004526 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4527 data->msg.rpc_argp = &data->arg;
4528 data->msg.rpc_resp = &data->res;
4529 data->arg.dir_fh = NFS_FH(dir);
4530 data->arg.server = server;
4531 data->arg.name = name;
4532 data->arg.attrs = sattr;
4533 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004534 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004535 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004536 data->res.server = server;
4537 data->res.fh = &data->fh;
4538 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004539 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004540 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004541 }
4542 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004543out_free:
4544 kfree(data);
4545 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004546}
4547
4548static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4549{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004550 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004551 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004553 spin_lock(&dir->i_lock);
4554 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004555 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004556 /* Creating a directory bumps nlink in the parent */
4557 if (data->arg.ftype == NF4DIR)
4558 nfs4_inc_nlink_locked(dir);
4559 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004560 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004561 }
4562 return status;
4563}
4564
4565static void nfs4_free_createdata(struct nfs4_createdata *data)
4566{
David Quigley14c43f72013-05-22 12:50:43 -04004567 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004568 kfree(data);
4569}
4570
Chuck Lever4f390c12006-08-22 20:06:22 -04004571static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004572 struct page *page, unsigned int len, struct iattr *sattr,
4573 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004575 struct nfs4_createdata *data;
4576 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Chuck Lever94a6d752006-08-22 20:06:23 -04004578 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004579 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004580
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004581 status = -ENOMEM;
4582 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4583 if (data == NULL)
4584 goto out;
4585
4586 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4587 data->arg.u.symlink.pages = &page;
4588 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004589 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004591 status = nfs4_do_create(dir, dentry, data);
4592
4593 nfs4_free_createdata(data);
4594out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return status;
4596}
4597
Chuck Lever4f390c12006-08-22 20:06:22 -04004598static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004599 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
4601 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004602 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004604
4605 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004608 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4609 trace_nfs4_symlink(dir, &dentry->d_name, err);
4610 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 &exception);
4612 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004613
4614 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 return err;
4616}
4617
4618static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004619 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004621 struct nfs4_createdata *data;
4622 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004624 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4625 if (data == NULL)
4626 goto out;
4627
David Quigley1775fd32013-05-22 12:50:42 -04004628 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004629 status = nfs4_do_create(dir, dentry, data);
4630
4631 nfs4_free_createdata(data);
4632out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 return status;
4634}
4635
4636static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4637 struct iattr *sattr)
4638{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004639 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004641 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004643
David Quigleyaa9c2662013-05-22 12:50:44 -04004644 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4645
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004646 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4647 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004649 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4650 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4651 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 &exception);
4653 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004654 nfs4_label_release_security(label);
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 return err;
4657}
4658
4659static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004660 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
David Howells2b0143b2015-03-17 22:25:59 +00004662 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 struct nfs4_readdir_arg args = {
4664 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004665 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 .pgbase = 0,
4667 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004668 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004669 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 };
4671 struct nfs4_readdir_res res;
4672 struct rpc_message msg = {
4673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4674 .rpc_argp = &args,
4675 .rpc_resp = &res,
4676 .rpc_cred = cred,
4677 };
4678 int status;
4679
Al Viro6de14722013-09-16 10:53:17 -04004680 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4681 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004682 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004683 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004685 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 -05004686 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004687 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004688 status += args.pgbase;
4689 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004690
4691 nfs_invalidate_atime(dir);
4692
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return status;
4695}
4696
4697static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004698 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
4700 struct nfs4_exception exception = { };
4701 int err;
4702 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004703 err = _nfs4_proc_readdir(dentry, cred, cookie,
4704 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004705 trace_nfs4_readdir(d_inode(dentry), err);
4706 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 &exception);
4708 } while (exception.retry);
4709 return err;
4710}
4711
4712static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004713 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004715 struct nfs4_createdata *data;
4716 int mode = sattr->ia_mode;
4717 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004719 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4720 if (data == NULL)
4721 goto out;
4722
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004724 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004726 data->arg.ftype = NF4BLK;
4727 data->arg.u.device.specdata1 = MAJOR(rdev);
4728 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
4730 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004731 data->arg.ftype = NF4CHR;
4732 data->arg.u.device.specdata1 = MAJOR(rdev);
4733 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004734 } else if (!S_ISSOCK(mode)) {
4735 status = -EINVAL;
4736 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
David Quigley1775fd32013-05-22 12:50:42 -04004738
David Quigleyaa9c2662013-05-22 12:50:44 -04004739 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004740 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004741out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004742 nfs4_free_createdata(data);
4743out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 return status;
4745}
4746
4747static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4748 struct iattr *sattr, dev_t rdev)
4749{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004750 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004752 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004754
David Quigleyaa9c2662013-05-22 12:50:44 -04004755 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4756
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004757 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4758 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004760 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4761 trace_nfs4_mknod(dir, &dentry->d_name, err);
4762 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 &exception);
4764 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004765
4766 nfs4_label_release_security(label);
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 return err;
4769}
4770
4771static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4772 struct nfs_fsstat *fsstat)
4773{
4774 struct nfs4_statfs_arg args = {
4775 .fh = fhandle,
4776 .bitmask = server->attr_bitmask,
4777 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004778 struct nfs4_statfs_res res = {
4779 .fsstat = fsstat,
4780 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 struct rpc_message msg = {
4782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4783 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004784 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 };
4786
Trond Myklebust0e574af2005-10-27 22:12:38 -04004787 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004788 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
4791static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4792{
4793 struct nfs4_exception exception = { };
4794 int err;
4795 do {
4796 err = nfs4_handle_exception(server,
4797 _nfs4_proc_statfs(server, fhandle, fsstat),
4798 &exception);
4799 } while (exception.retry);
4800 return err;
4801}
4802
4803static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4804 struct nfs_fsinfo *fsinfo)
4805{
4806 struct nfs4_fsinfo_arg args = {
4807 .fh = fhandle,
4808 .bitmask = server->attr_bitmask,
4809 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004810 struct nfs4_fsinfo_res res = {
4811 .fsinfo = fsinfo,
4812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 struct rpc_message msg = {
4814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4815 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004816 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 };
4818
Bryan Schumaker7c513052011-03-24 17:12:24 +00004819 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
4822static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4823{
4824 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004825 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 int err;
4827
4828 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004829 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004830 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004831 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004832 nfs4_set_lease_period(server->nfs_client,
4833 fsinfo->lease_time * HZ,
4834 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004835 break;
4836 }
4837 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 } while (exception.retry);
4839 return err;
4840}
4841
4842static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4843{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004844 int error;
4845
Trond Myklebust0e574af2005-10-27 22:12:38 -04004846 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004847 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004848 if (error == 0) {
4849 /* block layout checks this! */
4850 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004851 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004852 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004853
4854 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
4857static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4858 struct nfs_pathconf *pathconf)
4859{
4860 struct nfs4_pathconf_arg args = {
4861 .fh = fhandle,
4862 .bitmask = server->attr_bitmask,
4863 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004864 struct nfs4_pathconf_res res = {
4865 .pathconf = pathconf,
4866 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 struct rpc_message msg = {
4868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4869 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004870 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 };
4872
4873 /* None of the pathconf attributes are mandatory to implement */
4874 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4875 memset(pathconf, 0, sizeof(*pathconf));
4876 return 0;
4877 }
4878
Trond Myklebust0e574af2005-10-27 22:12:38 -04004879 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004880 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
4883static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4884 struct nfs_pathconf *pathconf)
4885{
4886 struct nfs4_exception exception = { };
4887 int err;
4888
4889 do {
4890 err = nfs4_handle_exception(server,
4891 _nfs4_proc_pathconf(server, fhandle, pathconf),
4892 &exception);
4893 } while (exception.retry);
4894 return err;
4895}
4896
Trond Myklebust5521abf2013-03-16 20:54:34 -04004897int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004898 const struct nfs_open_context *ctx,
4899 const struct nfs_lock_context *l_ctx,
4900 fmode_t fmode)
4901{
NeilBrown17393472016-10-13 15:26:47 +11004902 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004903}
4904EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4905
Trond Myklebust5521abf2013-03-16 20:54:34 -04004906static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4907 const struct nfs_open_context *ctx,
4908 const struct nfs_lock_context *l_ctx,
4909 fmode_t fmode)
4910{
4911 nfs4_stateid current_stateid;
4912
Trond Myklebuste1253be2014-03-05 08:44:23 -05004913 /* If the current stateid represents a lost lock, then exit */
4914 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4915 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004916 return nfs4_stateid_match(stateid, &current_stateid);
4917}
4918
4919static bool nfs4_error_stateid_expired(int err)
4920{
4921 switch (err) {
4922 case -NFS4ERR_DELEG_REVOKED:
4923 case -NFS4ERR_ADMIN_REVOKED:
4924 case -NFS4ERR_BAD_STATEID:
4925 case -NFS4ERR_STALE_STATEID:
4926 case -NFS4ERR_OLD_STATEID:
4927 case -NFS4ERR_OPENMODE:
4928 case -NFS4ERR_EXPIRED:
4929 return true;
4930 }
4931 return false;
4932}
4933
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004934static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004936 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004938 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004939 if (task->tk_status < 0) {
4940 struct nfs4_exception exception = {
4941 .inode = hdr->inode,
4942 .state = hdr->args.context->state,
4943 .stateid = &hdr->args.stateid,
4944 };
4945 task->tk_status = nfs4_async_handle_exception(task,
4946 server, task->tk_status, &exception);
4947 if (exception.retry) {
4948 rpc_restart_call_prepare(task);
4949 return -EAGAIN;
4950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004954 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956}
4957
Trond Myklebust5521abf2013-03-16 20:54:34 -04004958static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004959 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004960{
4961
4962 if (!nfs4_error_stateid_expired(task->tk_status) ||
4963 nfs4_stateid_is_current(&args->stateid,
4964 args->context,
4965 args->lock_context,
4966 FMODE_READ))
4967 return false;
4968 rpc_restart_call_prepare(task);
4969 return true;
4970}
4971
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004972static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004973{
4974
4975 dprintk("--> %s\n", __func__);
4976
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004977 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004978 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004979 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004980 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004981 if (task->tk_status > 0)
4982 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004983 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4984 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004985}
4986
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004987static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4988 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004990 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004991 if (!hdr->pgio_done_cb)
4992 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004993 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004994 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
4996
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004997static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4998 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004999{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005000 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005001 &hdr->args.seq_args,
5002 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005003 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005004 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005005 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5006 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005007 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005008 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005009 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005010 return -EIO;
5011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005014static int nfs4_write_done_cb(struct rpc_task *task,
5015 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005017 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005018
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005019 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005020 if (task->tk_status < 0) {
5021 struct nfs4_exception exception = {
5022 .inode = hdr->inode,
5023 .state = hdr->args.context->state,
5024 .stateid = &hdr->args.stateid,
5025 };
5026 task->tk_status = nfs4_async_handle_exception(task,
5027 NFS_SERVER(inode), task->tk_status,
5028 &exception);
5029 if (exception.retry) {
5030 rpc_restart_call_prepare(task);
5031 return -EAGAIN;
5032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005034 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005035 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005036 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005037 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039}
5040
Trond Myklebust5521abf2013-03-16 20:54:34 -04005041static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005042 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005043{
5044
5045 if (!nfs4_error_stateid_expired(task->tk_status) ||
5046 nfs4_stateid_is_current(&args->stateid,
5047 args->context,
5048 args->lock_context,
5049 FMODE_WRITE))
5050 return false;
5051 rpc_restart_call_prepare(task);
5052 return true;
5053}
5054
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005055static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005056{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005057 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005058 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005059 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005060 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005061 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5062 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005063}
5064
Trond Myklebust5a37f852012-04-28 14:55:16 -04005065static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005066bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005067{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005068 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005069 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005070 return false;
5071 /* Otherwise, request attributes if and only if we don't hold
5072 * a delegation
5073 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005074 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005075}
Fred Isamana69aef12011-03-03 15:13:47 +00005076
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005077static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005078 struct rpc_message *msg,
5079 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005081 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005082
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005083 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5084 hdr->args.bitmask = NULL;
5085 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005086 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005087 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005088
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005089 if (!hdr->pgio_done_cb)
5090 hdr->pgio_done_cb = nfs4_write_done_cb;
5091 hdr->res.server = server;
5092 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005094 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005095 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005096 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097}
5098
Fred Isaman0b7c0152012-04-20 14:47:39 -04005099static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5100{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005101 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005102 &data->args.seq_args,
5103 &data->res.seq_res,
5104 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
5106
Fred Isaman0b7c0152012-04-20 14:47:39 -04005107static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005110
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005111 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005112 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5113 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005114 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005115 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118}
5119
Fred Isaman0b7c0152012-04-20 14:47:39 -04005120static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005121{
5122 if (!nfs4_sequence_done(task, &data->res.seq_res))
5123 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005124 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005125}
5126
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005127static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5128 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005130 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005131
Fred Isaman0b7c0152012-04-20 14:47:39 -04005132 if (data->commit_done_cb == NULL)
5133 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005134 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005135 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005136 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005137 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138}
5139
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005140struct nfs4_renewdata {
5141 struct nfs_client *client;
5142 unsigned long timestamp;
5143};
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145/*
5146 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5147 * standalone procedure for queueing an asynchronous RENEW.
5148 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005149static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005150{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005151 struct nfs4_renewdata *data = calldata;
5152 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005153
Elena Reshetova212bf412017-10-20 12:53:38 +03005154 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005155 nfs4_schedule_state_renewal(clp);
5156 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005157 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005158}
5159
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005160static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005162 struct nfs4_renewdata *data = calldata;
5163 struct nfs_client *clp = data->client;
5164 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005166 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005167 switch (task->tk_status) {
5168 case 0:
5169 break;
5170 case -NFS4ERR_LEASE_MOVED:
5171 nfs4_schedule_lease_moved_recovery(clp);
5172 break;
5173 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005174 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005175 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5176 return;
5177 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005178 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005179 return;
5180 }
5181 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005183 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184}
5185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005186static const struct rpc_call_ops nfs4_renew_ops = {
5187 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005188 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005189};
5190
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005191static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192{
5193 struct rpc_message msg = {
5194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5195 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005196 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005198 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005200 if (renew_flags == 0)
5201 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005202 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005203 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005204 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005205 if (data == NULL) {
5206 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005207 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005208 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005209 data->client = clp;
5210 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005211 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005212 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005215static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216{
5217 struct rpc_message msg = {
5218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5219 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005220 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 };
5222 unsigned long now = jiffies;
5223 int status;
5224
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005225 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 if (status < 0)
5227 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005228 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 return 0;
5230}
5231
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005232static inline int nfs4_server_supports_acls(struct nfs_server *server)
5233{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005234 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005235}
5236
Trond Myklebust21f498c2012-08-24 10:59:25 -04005237/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5238 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005239 * the stack.
5240 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005241#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005242
Neil Hormane9e3d722011-03-04 19:26:03 -05005243static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005244 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005245{
5246 struct page *newpage, **spages;
5247 int rc = 0;
5248 size_t len;
5249 spages = pages;
5250
5251 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005252 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005253 newpage = alloc_page(GFP_KERNEL);
5254
5255 if (newpage == NULL)
5256 goto unwind;
5257 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005258 buf += len;
5259 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005260 *pages++ = newpage;
5261 rc++;
5262 } while (buflen != 0);
5263
5264 return rc;
5265
5266unwind:
5267 for(; rc > 0; rc--)
5268 __free_page(spages[rc-1]);
5269 return -ENOMEM;
5270}
5271
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005272struct nfs4_cached_acl {
5273 int cached;
5274 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005275 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005276};
5277
5278static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005279{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005280 struct nfs_inode *nfsi = NFS_I(inode);
5281
5282 spin_lock(&inode->i_lock);
5283 kfree(nfsi->nfs4_acl);
5284 nfsi->nfs4_acl = acl;
5285 spin_unlock(&inode->i_lock);
5286}
5287
5288static void nfs4_zap_acl_attr(struct inode *inode)
5289{
5290 nfs4_set_cached_acl(inode, NULL);
5291}
5292
5293static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5294{
5295 struct nfs_inode *nfsi = NFS_I(inode);
5296 struct nfs4_cached_acl *acl;
5297 int ret = -ENOENT;
5298
5299 spin_lock(&inode->i_lock);
5300 acl = nfsi->nfs4_acl;
5301 if (acl == NULL)
5302 goto out;
5303 if (buf == NULL) /* user is just asking for length */
5304 goto out_len;
5305 if (acl->cached == 0)
5306 goto out;
5307 ret = -ERANGE; /* see getxattr(2) man page */
5308 if (acl->len > buflen)
5309 goto out;
5310 memcpy(buf, acl->data, acl->len);
5311out_len:
5312 ret = acl->len;
5313out:
5314 spin_unlock(&inode->i_lock);
5315 return ret;
5316}
5317
Sachin Prabhu5794d212012-04-17 14:36:40 +01005318static 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 +00005319{
5320 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005321 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005322
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005323 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005324 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005325 if (acl == NULL)
5326 goto out;
5327 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005328 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005329 } else {
5330 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5331 if (acl == NULL)
5332 goto out;
5333 acl->cached = 0;
5334 }
5335 acl->len = acl_len;
5336out:
5337 nfs4_set_cached_acl(inode, acl);
5338}
5339
Andy Adamsonbf118a32011-12-07 11:55:27 -05005340/*
5341 * The getxattr API returns the required buffer length when called with a
5342 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5343 * the required buf. On a NULL buf, we send a page of data to the server
5344 * guessing that the ACL request can be serviced by a page. If so, we cache
5345 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5346 * the cache. If not so, we throw away the page, and cache the required
5347 * length. The next getxattr call will then produce another round trip to
5348 * the server, this time with the input buf of the required size.
5349 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005350static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005351{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005352 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005353 struct nfs_getaclargs args = {
5354 .fh = NFS_FH(inode),
5355 .acl_pages = pages,
5356 .acl_len = buflen,
5357 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005358 struct nfs_getaclres res = {
5359 .acl_len = buflen,
5360 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005361 struct rpc_message msg = {
5362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5363 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005364 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005365 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005366 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005367 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005368
Trond Myklebust21f498c2012-08-24 10:59:25 -04005369 if (npages > ARRAY_SIZE(pages))
5370 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005371
Andy Adamsonbf118a32011-12-07 11:55:27 -05005372 for (i = 0; i < npages; i++) {
5373 pages[i] = alloc_page(GFP_KERNEL);
5374 if (!pages[i])
5375 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005376 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005377
5378 /* for decoding across pages */
5379 res.acl_scratch = alloc_page(GFP_KERNEL);
5380 if (!res.acl_scratch)
5381 goto out_free;
5382
Andy Adamsonbf118a32011-12-07 11:55:27 -05005383 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005384
Peng Taode040be2012-01-10 22:42:47 +08005385 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005386 __func__, buf, buflen, npages, args.acl_len);
5387 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5388 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005389 if (ret)
5390 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005391
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005392 /* Handle the case where the passed-in buffer is too short */
5393 if (res.acl_flags & NFS4_ACL_TRUNC) {
5394 /* Did the user only issue a request for the acl length? */
5395 if (buf == NULL)
5396 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005397 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005398 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005399 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005400 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005401 if (buf) {
5402 if (res.acl_len > buflen) {
5403 ret = -ERANGE;
5404 goto out_free;
5405 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005406 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005407 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005408out_ok:
5409 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005410out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005411 for (i = 0; i < npages; i++)
5412 if (pages[i])
5413 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005414 if (res.acl_scratch)
5415 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005416 return ret;
5417}
5418
Trond Myklebust16b42892006-08-24 12:27:15 -04005419static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5420{
5421 struct nfs4_exception exception = { };
5422 ssize_t ret;
5423 do {
5424 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005425 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005426 if (ret >= 0)
5427 break;
5428 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5429 } while (exception.retry);
5430 return ret;
5431}
5432
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005433static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5434{
5435 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005436 int ret;
5437
5438 if (!nfs4_server_supports_acls(server))
5439 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005440 ret = nfs_revalidate_inode(server, inode);
5441 if (ret < 0)
5442 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005443 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5444 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005445 ret = nfs4_read_cached_acl(inode, buf, buflen);
5446 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005447 /* -ENOENT is returned if there is no ACL or if there is an ACL
5448 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005449 return ret;
5450 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005451}
5452
Trond Myklebust16b42892006-08-24 12:27:15 -04005453static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005454{
5455 struct nfs_server *server = NFS_SERVER(inode);
5456 struct page *pages[NFS4ACL_MAXPAGES];
5457 struct nfs_setaclargs arg = {
5458 .fh = NFS_FH(inode),
5459 .acl_pages = pages,
5460 .acl_len = buflen,
5461 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005462 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005463 struct rpc_message msg = {
5464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5465 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005466 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005467 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005468 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005469 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005470
5471 if (!nfs4_server_supports_acls(server))
5472 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005473 if (npages > ARRAY_SIZE(pages))
5474 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005475 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005476 if (i < 0)
5477 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005478 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005479 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005480
5481 /*
5482 * Free each page after tx, so the only ref left is
5483 * held by the network stack
5484 */
5485 for (; i > 0; i--)
5486 put_page(pages[i-1]);
5487
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005488 /*
5489 * Acl update can result in inode attribute update.
5490 * so mark the attribute cache invalid.
5491 */
5492 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005493 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005494 | NFS_INO_INVALID_CTIME
5495 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005496 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005497 nfs_access_zap_cache(inode);
5498 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005499 return ret;
5500}
5501
Trond Myklebust16b42892006-08-24 12:27:15 -04005502static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5503{
5504 struct nfs4_exception exception = { };
5505 int err;
5506 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005507 err = __nfs4_proc_set_acl(inode, buf, buflen);
5508 trace_nfs4_set_acl(inode, err);
5509 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005510 &exception);
5511 } while (exception.retry);
5512 return err;
5513}
5514
David Quigleyaa9c2662013-05-22 12:50:44 -04005515#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5516static int _nfs4_get_security_label(struct inode *inode, void *buf,
5517 size_t buflen)
5518{
5519 struct nfs_server *server = NFS_SERVER(inode);
5520 struct nfs_fattr fattr;
5521 struct nfs4_label label = {0, 0, buflen, buf};
5522
5523 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005524 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005525 .fh = NFS_FH(inode),
5526 .bitmask = bitmask,
5527 };
5528 struct nfs4_getattr_res res = {
5529 .fattr = &fattr,
5530 .label = &label,
5531 .server = server,
5532 };
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005535 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005536 .rpc_resp = &res,
5537 };
5538 int ret;
5539
5540 nfs_fattr_init(&fattr);
5541
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005542 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005543 if (ret)
5544 return ret;
5545 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5546 return -ENOENT;
5547 if (buflen < label.len)
5548 return -ERANGE;
5549 return 0;
5550}
5551
5552static int nfs4_get_security_label(struct inode *inode, void *buf,
5553 size_t buflen)
5554{
5555 struct nfs4_exception exception = { };
5556 int err;
5557
5558 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5559 return -EOPNOTSUPP;
5560
5561 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005562 err = _nfs4_get_security_label(inode, buf, buflen);
5563 trace_nfs4_get_security_label(inode, err);
5564 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005565 &exception);
5566 } while (exception.retry);
5567 return err;
5568}
5569
5570static int _nfs4_do_set_security_label(struct inode *inode,
5571 struct nfs4_label *ilabel,
5572 struct nfs_fattr *fattr,
5573 struct nfs4_label *olabel)
5574{
5575
5576 struct iattr sattr = {0};
5577 struct nfs_server *server = NFS_SERVER(inode);
5578 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005579 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005580 .fh = NFS_FH(inode),
5581 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005582 .server = server,
5583 .bitmask = bitmask,
5584 .label = ilabel,
5585 };
5586 struct nfs_setattrres res = {
5587 .fattr = fattr,
5588 .label = olabel,
5589 .server = server,
5590 };
5591 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5593 .rpc_argp = &arg,
5594 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005595 };
5596 int status;
5597
Jeff Layton12207f62013-11-01 10:49:32 -04005598 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005599
Jeff Layton12207f62013-11-01 10:49:32 -04005600 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005601 if (status)
5602 dprintk("%s failed: %d\n", __func__, status);
5603
5604 return status;
5605}
5606
5607static int nfs4_do_set_security_label(struct inode *inode,
5608 struct nfs4_label *ilabel,
5609 struct nfs_fattr *fattr,
5610 struct nfs4_label *olabel)
5611{
5612 struct nfs4_exception exception = { };
5613 int err;
5614
5615 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005616 err = _nfs4_do_set_security_label(inode, ilabel,
5617 fattr, olabel);
5618 trace_nfs4_set_security_label(inode, err);
5619 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005620 &exception);
5621 } while (exception.retry);
5622 return err;
5623}
5624
5625static int
Al Viro59301222016-05-27 10:19:30 -04005626nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005627{
5628 struct nfs4_label ilabel, *olabel = NULL;
5629 struct nfs_fattr fattr;
5630 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005631 int status;
5632
5633 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5634 return -EOPNOTSUPP;
5635
5636 nfs_fattr_init(&fattr);
5637
5638 ilabel.pi = 0;
5639 ilabel.lfs = 0;
5640 ilabel.label = (char *)buf;
5641 ilabel.len = buflen;
5642
5643 cred = rpc_lookup_cred();
5644 if (IS_ERR(cred))
5645 return PTR_ERR(cred);
5646
5647 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5648 if (IS_ERR(olabel)) {
5649 status = -PTR_ERR(olabel);
5650 goto out;
5651 }
5652
5653 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5654 if (status == 0)
5655 nfs_setsecurity(inode, &fattr, olabel);
5656
5657 nfs4_label_free(olabel);
5658out:
5659 put_rpccred(cred);
5660 return status;
5661}
5662#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5663
5664
Chuck Leverf0920752012-05-21 22:45:41 -04005665static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5666 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005667{
5668 __be32 verf[2];
5669
Chuck Lever2c820d92012-05-21 22:45:33 -04005670 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5671 /* An impossible timestamp guarantees this value
5672 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005673 verf[0] = cpu_to_be32(U32_MAX);
5674 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005675 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005676 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005677 u64 ns = ktime_to_ns(nn->boot_time);
5678
5679 verf[0] = cpu_to_be32(ns >> 32);
5680 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005681 }
Chuck Levercd937102012-03-02 17:14:31 -05005682 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5683}
5684
Jeff Laytona3192682015-06-09 19:43:59 -04005685static int
5686nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005687{
Jeff Laytona3192682015-06-09 19:43:59 -04005688 size_t len;
5689 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005690
Trond Myklebustceb3a162015-01-03 15:16:04 -05005691 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005692 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005693
Jeff Laytona3192682015-06-09 19:43:59 -04005694 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005695 len = 14 +
5696 strlen(clp->cl_rpcclient->cl_nodename) +
5697 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005698 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005699 1;
5700 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005701 if (nfs4_client_id_uniquifier[0] != '\0')
5702 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005703 if (len > NFS4_OPAQUE_LIMIT + 1)
5704 return -EINVAL;
5705
5706 /*
5707 * Since this string is allocated at mount time, and held until the
5708 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5709 * about a memory-reclaim deadlock.
5710 */
5711 str = kmalloc(len, GFP_KERNEL);
5712 if (!str)
5713 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005714
Chuck Levere984a552012-09-14 17:24:21 -04005715 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005716 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005717 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005718 clp->cl_rpcclient->cl_nodename,
5719 nfs4_client_id_uniquifier,
5720 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005721 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005722 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005723 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005724 clp->cl_rpcclient->cl_nodename,
5725 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005726 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005727 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005728
Jeff Laytona3192682015-06-09 19:43:59 -04005729 clp->cl_owner_id = str;
5730 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005731}
5732
Jeff Layton873e3852015-06-09 19:44:00 -04005733static int
5734nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005735{
Jeff Layton873e3852015-06-09 19:44:00 -04005736 size_t len;
5737 char *str;
5738
5739 len = 10 + 10 + 1 + 10 + 1 +
5740 strlen(nfs4_client_id_uniquifier) + 1 +
5741 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5742
5743 if (len > NFS4_OPAQUE_LIMIT + 1)
5744 return -EINVAL;
5745
5746 /*
5747 * Since this string is allocated at mount time, and held until the
5748 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5749 * about a memory-reclaim deadlock.
5750 */
5751 str = kmalloc(len, GFP_KERNEL);
5752 if (!str)
5753 return -ENOMEM;
5754
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005755 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005756 clp->rpc_ops->version, clp->cl_minorversion,
5757 nfs4_client_id_uniquifier,
5758 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005759 clp->cl_owner_id = str;
5760 return 0;
5761}
5762
5763static int
5764nfs4_init_uniform_client_string(struct nfs_client *clp)
5765{
Jeff Layton873e3852015-06-09 19:44:00 -04005766 size_t len;
5767 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005768
5769 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005770 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005771
5772 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005773 return nfs4_init_uniquifier_client_string(clp);
5774
5775 len = 10 + 10 + 1 + 10 + 1 +
5776 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5777
5778 if (len > NFS4_OPAQUE_LIMIT + 1)
5779 return -EINVAL;
5780
5781 /*
5782 * Since this string is allocated at mount time, and held until the
5783 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5784 * about a memory-reclaim deadlock.
5785 */
5786 str = kmalloc(len, GFP_KERNEL);
5787 if (!str)
5788 return -ENOMEM;
5789
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005790 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005791 clp->rpc_ops->version, clp->cl_minorversion,
5792 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005793 clp->cl_owner_id = str;
5794 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005795}
5796
Chuck Lever706cb8d2014-03-12 12:51:47 -04005797/*
5798 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5799 * services. Advertise one based on the address family of the
5800 * clientaddr.
5801 */
5802static unsigned int
5803nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5804{
5805 if (strchr(clp->cl_ipaddr, ':') != NULL)
5806 return scnprintf(buf, len, "tcp6");
5807 else
5808 return scnprintf(buf, len, "tcp");
5809}
5810
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005811static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5812{
5813 struct nfs4_setclientid *sc = calldata;
5814
5815 if (task->tk_status == 0)
5816 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5817}
5818
5819static const struct rpc_call_ops nfs4_setclientid_ops = {
5820 .rpc_call_done = nfs4_setclientid_done,
5821};
5822
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005823/**
5824 * nfs4_proc_setclientid - Negotiate client ID
5825 * @clp: state data structure
5826 * @program: RPC program for NFSv4 callback service
5827 * @port: IP port number for NFS4 callback service
5828 * @cred: RPC credential to use for this call
5829 * @res: where to place the result
5830 *
5831 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5832 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005833int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5834 unsigned short port, struct rpc_cred *cred,
5835 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
5837 nfs4_verifier sc_verifier;
5838 struct nfs4_setclientid setclientid = {
5839 .sc_verifier = &sc_verifier,
5840 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005841 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 };
5843 struct rpc_message msg = {
5844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5845 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005846 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005847 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005849 struct rpc_task *task;
5850 struct rpc_task_setup task_setup_data = {
5851 .rpc_client = clp->cl_rpcclient,
5852 .rpc_message = &msg,
5853 .callback_ops = &nfs4_setclientid_ops,
5854 .callback_data = &setclientid,
5855 .flags = RPC_TASK_TIMEOUT,
5856 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005857 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Chuck Leverde734832012-07-11 16:30:50 -04005859 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005860 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005861
5862 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5863 status = nfs4_init_uniform_client_string(clp);
5864 else
Jeff Laytona3192682015-06-09 19:43:59 -04005865 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005866
5867 if (status)
5868 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005869
Chuck Leverde734832012-07-11 16:30:50 -04005870 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005871 setclientid.sc_netid_len =
5872 nfs4_init_callback_netid(clp,
5873 setclientid.sc_netid,
5874 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005875 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005876 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 clp->cl_ipaddr, port >> 8, port & 255);
5878
Jeff Layton3a6bb732015-06-09 19:43:57 -04005879 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005880 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005881 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005882 task = rpc_run_task(&task_setup_data);
5883 if (IS_ERR(task)) {
5884 status = PTR_ERR(task);
5885 goto out;
5886 }
5887 status = task->tk_status;
5888 if (setclientid.sc_cred) {
5889 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5890 put_rpccred(setclientid.sc_cred);
5891 }
5892 rpc_put_task(task);
5893out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005894 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005895 dprintk("NFS reply setclientid: %d\n", status);
5896 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897}
5898
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005899/**
5900 * nfs4_proc_setclientid_confirm - Confirm client ID
5901 * @clp: state data structure
5902 * @res: result of a previous SETCLIENTID
5903 * @cred: RPC credential to use for this call
5904 *
5905 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5906 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005907int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005908 struct nfs4_setclientid_res *arg,
5909 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 struct rpc_message msg = {
5912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005913 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005914 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 int status;
5917
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005918 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5919 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5920 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005921 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005922 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005923 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 return status;
5925}
5926
Trond Myklebustfe650402006-01-03 09:55:18 +01005927struct nfs4_delegreturndata {
5928 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005929 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005930 struct nfs_fh fh;
5931 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005932 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005933 struct {
5934 struct nfs4_layoutreturn_args arg;
5935 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005936 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005937 u32 roc_barrier;
5938 bool roc;
5939 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005940 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005941 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005942 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005943};
5944
Trond Myklebustfe650402006-01-03 09:55:18 +01005945static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5946{
5947 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005948 struct nfs4_exception exception = {
5949 .inode = data->inode,
5950 .stateid = &data->stateid,
5951 };
Andy Adamson938e1012009-04-01 09:22:28 -04005952
Trond Myklebust14516c32010-07-31 14:29:06 -04005953 if (!nfs4_sequence_done(task, &data->res.seq_res))
5954 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005955
Trond Myklebustca8acf82013-08-13 10:36:56 -04005956 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005957
5958 /* Handle Layoutreturn errors */
5959 if (data->args.lr_args && task->tk_status != 0) {
5960 switch(data->res.lr_ret) {
5961 default:
5962 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5963 break;
5964 case 0:
5965 data->args.lr_args = NULL;
5966 data->res.lr_res = NULL;
5967 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005968 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04005969 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005970 data->inode))
5971 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005972 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005973 case -NFS4ERR_ADMIN_REVOKED:
5974 case -NFS4ERR_DELEG_REVOKED:
5975 case -NFS4ERR_EXPIRED:
5976 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005977 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5978 case -NFS4ERR_WRONG_CRED:
5979 data->args.lr_args = NULL;
5980 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005981 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005982 }
5983 }
5984
Ricardo Labiaga79708862009-12-07 09:23:21 -05005985 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005986 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005987 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005988 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005989 case -NFS4ERR_ADMIN_REVOKED:
5990 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005991 case -NFS4ERR_EXPIRED:
5992 nfs4_free_revoked_stateid(data->res.server,
5993 data->args.stateid,
5994 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005995 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005996 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005997 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005998 task->tk_status = 0;
5999 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006000 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006001 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6002 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006003 task->tk_status = 0;
6004 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006005 case -NFS4ERR_ACCESS:
6006 if (data->args.bitmask) {
6007 data->args.bitmask = NULL;
6008 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006009 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006010 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006011 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006012 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006013 task->tk_status = nfs4_async_handle_exception(task,
6014 data->res.server, task->tk_status,
6015 &exception);
6016 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006017 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006018 }
6019 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006020 return;
6021lr_restart:
6022 data->res.lr_ret = 0;
6023out_restart:
6024 task->tk_status = 0;
6025 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006026}
6027
6028static void nfs4_delegreturn_release(void *calldata)
6029{
Peng Tao039b7562014-07-03 13:05:02 +08006030 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006031 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006032
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006033 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006034 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006035 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6036 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006037 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006038 nfs_iput_and_deactive(inode);
6039 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006040 kfree(calldata);
6041}
6042
Andy Adamson938e1012009-04-01 09:22:28 -04006043static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6044{
6045 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006046 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006047
6048 d_data = (struct nfs4_delegreturndata *)data;
6049
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006050 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006051 return;
6052
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006053 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6054 if (lo && !pnfs_layout_is_valid(lo)) {
6055 d_data->args.lr_args = NULL;
6056 d_data->res.lr_res = NULL;
6057 }
6058
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006059 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006060 &d_data->args.seq_args,
6061 &d_data->res.seq_res,
6062 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006063}
Andy Adamson938e1012009-04-01 09:22:28 -04006064
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006065static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006066 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006067 .rpc_call_done = nfs4_delegreturn_done,
6068 .rpc_release = nfs4_delegreturn_release,
6069};
6070
Trond Myklebuste6f81072008-01-24 18:14:34 -05006071static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006072{
6073 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006074 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006075 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006076 struct rpc_message msg = {
6077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6078 .rpc_cred = cred,
6079 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006080 struct rpc_task_setup task_setup_data = {
6081 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006082 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006083 .callback_ops = &nfs4_delegreturn_ops,
6084 .flags = RPC_TASK_ASYNC,
6085 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006086 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006087
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006088 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006089 if (data == NULL)
6090 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006091 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006092
6093 nfs4_state_protect(server->nfs_client,
6094 NFS_SP4_MACH_CRED_CLEANUP,
6095 &task_setup_data.rpc_client, &msg);
6096
Trond Myklebustfe650402006-01-03 09:55:18 +01006097 data->args.fhandle = &data->fh;
6098 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006099 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006100 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006101 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006102 data->res.fattr = &data->fattr;
6103 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006104 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006105 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006106 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006107 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006108 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006109 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006110 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006111 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006112 if (data->lr.roc) {
6113 data->args.lr_args = &data->lr.arg;
6114 data->res.lr_res = &data->lr.res;
6115 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006116 } else if (data->lr.roc) {
6117 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6118 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006119 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006120
Trond Myklebustc970aa82007-07-14 15:39:59 -04006121 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006122 msg.rpc_argp = &data->args;
6123 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006124 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006125 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006126 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006127 if (!issync)
6128 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006129 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006130 if (status != 0)
6131 goto out;
6132 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006133out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006134 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006135 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136}
6137
Trond Myklebuste6f81072008-01-24 18:14:34 -05006138int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
6140 struct nfs_server *server = NFS_SERVER(inode);
6141 struct nfs4_exception exception = { };
6142 int err;
6143 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006144 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006145 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 switch (err) {
6147 case -NFS4ERR_STALE_STATEID:
6148 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 case 0:
6150 return 0;
6151 }
6152 err = nfs4_handle_exception(server, err, &exception);
6153 } while (exception.retry);
6154 return err;
6155}
6156
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6158{
6159 struct inode *inode = state->inode;
6160 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006161 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006162 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006164 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006166 struct nfs_lockt_res res = {
6167 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 };
6169 struct rpc_message msg = {
6170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006171 .rpc_argp = &arg,
6172 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 .rpc_cred = state->owner->so_cred,
6174 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 struct nfs4_lock_state *lsp;
6176 int status;
6177
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006178 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006179 status = nfs4_set_lock_state(state, request);
6180 if (status != 0)
6181 goto out;
6182 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006183 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006184 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006185 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006186 switch (status) {
6187 case 0:
6188 request->fl_type = F_UNLCK;
6189 break;
6190 case -NFS4ERR_DENIED:
6191 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006193 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006194 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006195out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return status;
6197}
6198
6199static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6200{
6201 struct nfs4_exception exception = { };
6202 int err;
6203
6204 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006205 err = _nfs4_proc_getlk(state, cmd, request);
6206 trace_nfs4_get_lock(request, state, cmd, err);
6207 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 &exception);
6209 } while (exception.retry);
6210 return err;
6211}
6212
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006213struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006214 struct nfs_locku_args arg;
6215 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006216 struct nfs4_lock_state *lsp;
6217 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006218 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006219 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006220 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006221 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006222};
6223
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006224static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6225 struct nfs_open_context *ctx,
6226 struct nfs4_lock_state *lsp,
6227 struct nfs_seqid *seqid)
6228{
6229 struct nfs4_unlockdata *p;
6230 struct inode *inode = lsp->ls_state->inode;
6231
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006232 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006233 if (p == NULL)
6234 return NULL;
6235 p->arg.fh = NFS_FH(inode);
6236 p->arg.fl = &p->fl;
6237 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006238 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006239 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006240 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006241 /* Ensure we don't close file until we're done freeing locks! */
6242 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006243 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006244 memcpy(&p->fl, fl, sizeof(p->fl));
6245 p->server = NFS_SERVER(inode);
6246 return p;
6247}
6248
Trond Myklebust06f814a2006-01-03 09:55:07 +01006249static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006250{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006251 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006252 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006253 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006254 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006255 put_nfs_open_context(calldata->ctx);
6256 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006257}
6258
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006259static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006260{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006261 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006262 struct nfs4_exception exception = {
6263 .inode = calldata->lsp->ls_state->inode,
6264 .stateid = &calldata->arg.stateid,
6265 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006266
Trond Myklebust14516c32010-07-31 14:29:06 -04006267 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6268 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006269 switch (task->tk_status) {
6270 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006271 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006272 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006273 if (nfs4_update_lock_stateid(calldata->lsp,
6274 &calldata->res.stateid))
6275 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006276 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006277 case -NFS4ERR_ADMIN_REVOKED:
6278 case -NFS4ERR_EXPIRED:
6279 nfs4_free_revoked_stateid(calldata->server,
6280 &calldata->arg.stateid,
6281 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006282 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006283 case -NFS4ERR_BAD_STATEID:
6284 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006285 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006286 if (!nfs4_stateid_match(&calldata->arg.stateid,
6287 &calldata->lsp->ls_stateid))
6288 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006289 break;
6290 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006291 task->tk_status = nfs4_async_handle_exception(task,
6292 calldata->server, task->tk_status,
6293 &exception);
6294 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006295 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006296 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006297 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006298}
6299
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006300static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006301{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006302 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006304 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6305 nfs_async_iocounter_wait(task, calldata->l_ctx))
6306 return;
6307
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006308 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006309 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006310 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006311 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006312 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006313 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006314 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006315 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006316 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006317 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006318 &calldata->res.seq_res,
6319 task) != 0)
6320 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006321 return;
6322out_no_action:
6323 task->tk_action = NULL;
6324out_wait:
6325 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326}
6327
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006328static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006329 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006330 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006331 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006332};
6333
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006334static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6335 struct nfs_open_context *ctx,
6336 struct nfs4_lock_state *lsp,
6337 struct nfs_seqid *seqid)
6338{
6339 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006340 struct rpc_message msg = {
6341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6342 .rpc_cred = ctx->cred,
6343 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006344 struct rpc_task_setup task_setup_data = {
6345 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006346 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006347 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006349 .flags = RPC_TASK_ASYNC,
6350 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006352 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6353 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6354
Frank Filz137d6ac2007-07-09 15:32:29 -07006355 /* Ensure this is an unlock - when canceling a lock, the
6356 * canceled lock is passed in, and it won't be an unlock.
6357 */
6358 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006359 if (fl->fl_flags & FL_CLOSE)
6360 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006361
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006362 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6363 if (data == NULL) {
6364 nfs_free_seqid(seqid);
6365 return ERR_PTR(-ENOMEM);
6366 }
6367
Anna Schumakerfba83f32018-05-04 16:22:50 -04006368 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006369 msg.rpc_argp = &data->arg;
6370 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006371 task_setup_data.callback_data = data;
6372 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006373}
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6376{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006377 struct inode *inode = state->inode;
6378 struct nfs4_state_owner *sp = state->owner;
6379 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006380 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006381 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006382 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006383 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006384 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006385 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
Trond Myklebust9b073572006-06-29 16:38:34 -04006387 status = nfs4_set_lock_state(state, request);
6388 /* Unlock _before_ we do the RPC call */
6389 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006390 /* Exclude nfs_delegation_claim_locks() */
6391 mutex_lock(&sp->so_delegreturn_mutex);
6392 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006393 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006394 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006395 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006396 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006397 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006398 }
6399 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006400 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006401 if (status != 0)
6402 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006403 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006404 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006405 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6406 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006407 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6408 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006409 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006410 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006411 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006412 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006413 status = PTR_ERR(task);
6414 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006415 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006416 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006417 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006418out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006419 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006420 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006421 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422}
6423
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006424struct nfs4_lockdata {
6425 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006426 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006427 struct nfs4_lock_state *lsp;
6428 struct nfs_open_context *ctx;
6429 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006430 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006431 int rpc_status;
6432 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006433 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006434};
6435
6436static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006437 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6438 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006439{
6440 struct nfs4_lockdata *p;
6441 struct inode *inode = lsp->ls_state->inode;
6442 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006443 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006444
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006445 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006446 if (p == NULL)
6447 return NULL;
6448
6449 p->arg.fh = NFS_FH(inode);
6450 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006451 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006452 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006453 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006454 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6455 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006456 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006457 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006458 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006459 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006460 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006461 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006462 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006463 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006464 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006465 p->ctx = get_nfs_open_context(ctx);
6466 memcpy(&p->fl, fl, sizeof(p->fl));
6467 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006468out_free_seqid:
6469 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006470out_free:
6471 kfree(p);
6472 return NULL;
6473}
6474
6475static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6476{
6477 struct nfs4_lockdata *data = calldata;
6478 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
Harvey Harrison3110ff82008-05-02 13:42:44 -07006480 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006481 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006482 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006483 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006484 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006485 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006486 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006487 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006488 nfs4_stateid_copy(&data->arg.open_stateid,
6489 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006490 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006491 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006492 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006493 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006494 nfs4_stateid_copy(&data->arg.lock_stateid,
6495 &data->lsp->ls_stateid);
6496 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006497 if (!nfs4_valid_open_stateid(state)) {
6498 data->rpc_status = -EBADF;
6499 task->tk_action = NULL;
6500 goto out_release_open_seqid;
6501 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006502 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006503 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006504 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006505 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006506 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006507 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006508out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006509 nfs_release_seqid(data->arg.open_seqid);
6510out_release_lock_seqid:
6511 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006512out_wait:
6513 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006514 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006515}
6516
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006517static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6518{
6519 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006520 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006521
Harvey Harrison3110ff82008-05-02 13:42:44 -07006522 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006523
Trond Myklebust14516c32010-07-31 14:29:06 -04006524 if (!nfs4_sequence_done(task, &data->res.seq_res))
6525 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006526
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006527 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006528 switch (task->tk_status) {
6529 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006530 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006531 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006532 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006533 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006534 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebustc69899a2015-01-24 16:03:52 -05006535 break;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006536 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006537
Trond Myklebust39071e62015-01-24 15:07:56 -05006538 if (data->arg.new_lock_owner != 0) {
6539 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6540 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6541 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006542 goto out_done;
6543 } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
6544 goto out_done;
6545
Trond Myklebust425c1d42015-01-24 14:57:53 -05006546 break;
6547 case -NFS4ERR_BAD_STATEID:
6548 case -NFS4ERR_OLD_STATEID:
6549 case -NFS4ERR_STALE_STATEID:
6550 case -NFS4ERR_EXPIRED:
6551 if (data->arg.new_lock_owner != 0) {
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006552 if (nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006553 &lsp->ls_state->open_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006554 goto out_done;
6555 } else if (nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006556 &lsp->ls_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006557 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006558 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006559 if (!data->cancelled)
6560 rpc_restart_call_prepare(task);
6561out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006562 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006563}
6564
6565static void nfs4_lock_release(void *calldata)
6566{
6567 struct nfs4_lockdata *data = calldata;
6568
Harvey Harrison3110ff82008-05-02 13:42:44 -07006569 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006570 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006571 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006572 struct rpc_task *task;
6573 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6574 data->arg.lock_seqid);
6575 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006576 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006577 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006578 } else
6579 nfs_free_seqid(data->arg.lock_seqid);
6580 nfs4_put_lock_state(data->lsp);
6581 put_nfs_open_context(data->ctx);
6582 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006583 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006584}
6585
6586static const struct rpc_call_ops nfs4_lock_ops = {
6587 .rpc_call_prepare = nfs4_lock_prepare,
6588 .rpc_call_done = nfs4_lock_done,
6589 .rpc_release = nfs4_lock_release,
6590};
6591
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006592static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6593{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006594 switch (error) {
6595 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006596 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006597 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006598 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006599 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006600 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006601 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006602 break;
6603 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006604 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006605 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006606 };
6607}
6608
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006609static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006610{
6611 struct nfs4_lockdata *data;
6612 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006613 struct rpc_message msg = {
6614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6615 .rpc_cred = state->owner->so_cred,
6616 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006617 struct rpc_task_setup task_setup_data = {
6618 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006619 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006620 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006621 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006622 .flags = RPC_TASK_ASYNC,
6623 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006624 int ret;
6625
Harvey Harrison3110ff82008-05-02 13:42:44 -07006626 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006627 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006628 fl->fl_u.nfs4_fl.owner,
6629 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006630 if (data == NULL)
6631 return -ENOMEM;
6632 if (IS_SETLKW(cmd))
6633 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006634 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6635 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006636 msg.rpc_argp = &data->arg;
6637 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006638 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006639 if (recovery_type > NFS_LOCK_NEW) {
6640 if (recovery_type == NFS_LOCK_RECLAIM)
6641 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006642 } else
6643 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006644 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006645 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006646 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006647 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006648 if (ret == 0) {
6649 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006650 if (ret)
6651 nfs4_handle_setlk_error(data->server, data->lsp,
6652 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006653 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006654 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006655 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006656 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006657 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006658 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659}
6660
6661static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6662{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006663 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006664 struct nfs4_exception exception = {
6665 .inode = state->inode,
6666 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006667 int err;
6668
6669 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006670 /* Cache the lock if possible... */
6671 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6672 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006673 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006674 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006675 break;
6676 nfs4_handle_exception(server, err, &exception);
6677 } while (exception.retry);
6678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679}
6680
6681static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6682{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006683 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006684 struct nfs4_exception exception = {
6685 .inode = state->inode,
6686 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006687 int err;
6688
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006689 err = nfs4_set_lock_state(state, request);
6690 if (err != 0)
6691 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006692 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006693 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6694 return 0;
6695 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006696 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006697 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6698 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006699 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006700 switch (err) {
6701 default:
6702 goto out;
6703 case -NFS4ERR_GRACE:
6704 case -NFS4ERR_DELAY:
6705 nfs4_handle_exception(server, err, &exception);
6706 err = 0;
6707 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006708 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006709out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711}
6712
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006713#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006714static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6715{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006716 struct nfs4_lock_state *lsp;
6717 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006718
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006719 status = nfs4_set_lock_state(state, request);
6720 if (status != 0)
6721 return status;
6722 lsp = request->fl_u.nfs4_fl.owner;
6723 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6724 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6725 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006726 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006727}
6728#endif
6729
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6731{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006732 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006733 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006734 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006735 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
Trond Myklebust01c3b862006-06-29 16:38:39 -04006737 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006738 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006739 if (status < 0)
6740 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006741 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006742 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006743 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006744 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006745 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006746 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006747 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006748 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006749 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006750 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006751 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006752 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006753 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006754 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006755out:
6756 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 return status;
6758}
6759
6760static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6761{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006762 struct nfs4_exception exception = {
6763 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006764 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 int err;
6767
6768 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006769 err = _nfs4_proc_setlk(state, cmd, request);
6770 if (err == -NFS4ERR_DENIED)
6771 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006773 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 } while (exception.retry);
6775 return err;
6776}
6777
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006778#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6779#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6780
6781static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006782nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6783 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006784{
6785 int status = -ERESTARTSYS;
6786 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6787
6788 while(!signalled()) {
6789 status = nfs4_proc_setlk(state, cmd, request);
6790 if ((status != -EAGAIN) || IS_SETLK(cmd))
6791 break;
6792 freezable_schedule_timeout_interruptible(timeout);
6793 timeout *= 2;
6794 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6795 status = -ERESTARTSYS;
6796 }
6797 return status;
6798}
6799
Jeff Laytona1d617d82016-09-17 18:17:39 -04006800#ifdef CONFIG_NFS_V4_1
6801struct nfs4_lock_waiter {
6802 struct task_struct *task;
6803 struct inode *inode;
6804 struct nfs_lowner *owner;
6805 bool notified;
6806};
6807
6808static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006809nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006810{
6811 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006812 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006813
Jeff Layton57174592018-03-18 08:37:03 -04006814 /* NULL key means to wake up everyone */
6815 if (key) {
6816 struct cb_notify_lock_args *cbnl = key;
6817 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6818 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006819
Jeff Layton57174592018-03-18 08:37:03 -04006820 /* Only wake if the callback was for the same owner. */
6821 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6822 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006823
Jeff Layton57174592018-03-18 08:37:03 -04006824 /* Make sure it's for the right inode */
6825 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6826 return 0;
6827
6828 waiter->notified = true;
6829 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006830
6831 /* override "private" so we can use default_wake_function */
6832 wait->private = waiter->task;
6833 ret = autoremove_wake_function(wait, mode, flags, key);
6834 wait->private = waiter;
6835 return ret;
6836}
6837
6838static int
6839nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6840{
6841 int status = -ERESTARTSYS;
6842 unsigned long flags;
6843 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6844 struct nfs_server *server = NFS_SERVER(state->inode);
6845 struct nfs_client *clp = server->nfs_client;
6846 wait_queue_head_t *q = &clp->cl_lock_waitq;
6847 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6848 .id = lsp->ls_seqid.owner_id,
6849 .s_dev = server->s_dev };
6850 struct nfs4_lock_waiter waiter = { .task = current,
6851 .inode = state->inode,
6852 .owner = &owner,
6853 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006854 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006855
6856 /* Don't bother with waitqueue if we don't expect a callback */
6857 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6858 return nfs4_retry_setlk_simple(state, cmd, request);
6859
6860 init_wait(&wait);
6861 wait.private = &waiter;
6862 wait.func = nfs4_wake_lock_waiter;
6863 add_wait_queue(q, &wait);
6864
6865 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006866 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006867 status = nfs4_proc_setlk(state, cmd, request);
6868 if ((status != -EAGAIN) || IS_SETLK(cmd))
6869 break;
6870
6871 status = -ERESTARTSYS;
6872 spin_lock_irqsave(&q->lock, flags);
6873 if (waiter.notified) {
6874 spin_unlock_irqrestore(&q->lock, flags);
6875 continue;
6876 }
6877 set_current_state(TASK_INTERRUPTIBLE);
6878 spin_unlock_irqrestore(&q->lock, flags);
6879
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006880 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006881 }
6882
6883 finish_wait(q, &wait);
6884 return status;
6885}
6886#else /* !CONFIG_NFS_V4_1 */
6887static inline int
6888nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6889{
6890 return nfs4_retry_setlk_simple(state, cmd, request);
6891}
6892#endif
6893
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894static int
6895nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6896{
6897 struct nfs_open_context *ctx;
6898 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 int status;
6900
6901 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006902 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 state = ctx->state;
6904
Trond Myklebustd9531262009-07-21 19:22:38 -04006905 if (IS_GETLK(cmd)) {
6906 if (state != NULL)
6907 return nfs4_proc_getlk(state, F_GETLK, request);
6908 return 0;
6909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
6911 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6912 return -EINVAL;
6913
Trond Myklebustd9531262009-07-21 19:22:38 -04006914 if (request->fl_type == F_UNLCK) {
6915 if (state != NULL)
6916 return nfs4_proc_unlck(state, cmd, request);
6917 return 0;
6918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
Trond Myklebustd9531262009-07-21 19:22:38 -04006920 if (state == NULL)
6921 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006922
6923 if ((request->fl_flags & FL_POSIX) &&
6924 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6925 return -ENOLCK;
6926
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006927 /*
6928 * Don't rely on the VFS having checked the file open mode,
6929 * since it won't do this for flock() locks.
6930 */
6931 switch (request->fl_type) {
6932 case F_RDLCK:
6933 if (!(filp->f_mode & FMODE_READ))
6934 return -EBADF;
6935 break;
6936 case F_WRLCK:
6937 if (!(filp->f_mode & FMODE_WRITE))
6938 return -EBADF;
6939 }
6940
Jeff Layton1ea67db2016-09-17 18:17:37 -04006941 status = nfs4_set_lock_state(state, request);
6942 if (status != 0)
6943 return status;
6944
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006945 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006948int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006949{
6950 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006951 int err;
6952
6953 err = nfs4_set_lock_state(state, fl);
6954 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006955 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006956 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006957 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006958}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006959
Trond Myklebustcf470c32012-03-07 13:49:12 -05006960struct nfs_release_lockowner_data {
6961 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006962 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006963 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006964 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006965 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006966};
6967
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006968static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6969{
6970 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006971 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006972 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6973 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006974 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006975 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006976}
6977
6978static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6979{
6980 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006981 struct nfs_server *server = data->server;
6982
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006983 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006984
6985 switch (task->tk_status) {
6986 case 0:
6987 renew_lease(server, data->timestamp);
6988 break;
6989 case -NFS4ERR_STALE_CLIENTID:
6990 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006991 nfs4_schedule_lease_recovery(server->nfs_client);
6992 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006993 case -NFS4ERR_LEASE_MOVED:
6994 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006995 if (nfs4_async_handle_error(task, server,
6996 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006997 rpc_restart_call_prepare(task);
6998 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006999}
7000
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007001static void nfs4_release_lockowner_release(void *calldata)
7002{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007003 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007004 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007005 kfree(calldata);
7006}
7007
Trond Myklebust17280172012-03-11 13:11:00 -04007008static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007009 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7010 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007011 .rpc_release = nfs4_release_lockowner_release,
7012};
7013
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007014static void
7015nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007016{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007017 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007018 struct rpc_message msg = {
7019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7020 };
7021
7022 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007023 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007024
Trond Myklebustcf470c32012-03-07 13:49:12 -05007025 data = kmalloc(sizeof(*data), GFP_NOFS);
7026 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007027 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007028 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007029 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007030 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7031 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7032 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007033
Trond Myklebustcf470c32012-03-07 13:49:12 -05007034 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007035 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007036 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007037 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007038}
7039
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007040#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7041
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007042static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007043 struct dentry *unused, struct inode *inode,
7044 const char *key, const void *buf,
7045 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007046{
Al Viro59301222016-05-27 10:19:30 -04007047 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007048}
7049
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007050static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007051 struct dentry *unused, struct inode *inode,
7052 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007053{
Al Virob2968212016-04-10 20:48:24 -04007054 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007055}
7056
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007057static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007058{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007059 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007060}
7061
David Quigleyc9bccef2013-05-22 12:50:45 -04007062#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007063
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007064static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007065 struct dentry *unused, struct inode *inode,
7066 const char *key, const void *buf,
7067 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007068{
7069 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007070 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007071
7072 return -EOPNOTSUPP;
7073}
7074
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007075static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007076 struct dentry *unused, struct inode *inode,
7077 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007078{
7079 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007080 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007081 return -EOPNOTSUPP;
7082}
7083
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007084static ssize_t
7085nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007086{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007087 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007088
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007089 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7090 len = security_inode_listsecurity(inode, list, list_len);
7091 if (list_len && len > list_len)
7092 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007093 }
7094 return len;
7095}
7096
7097static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7098 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007099 .get = nfs4_xattr_get_nfs4_label,
7100 .set = nfs4_xattr_set_nfs4_label,
7101};
David Quigleyc9bccef2013-05-22 12:50:45 -04007102
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007103#else
7104
7105static ssize_t
7106nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7107{
7108 return 0;
7109}
7110
7111#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007112
Andy Adamson533eb462011-06-13 18:25:56 -04007113/*
7114 * nfs_fhget will use either the mounted_on_fileid or the fileid
7115 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007116static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7117{
Andy Adamson533eb462011-06-13 18:25:56 -04007118 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7119 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7120 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007121 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007122 return;
7123
7124 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007125 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007126 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7127 fattr->nlink = 2;
7128}
7129
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007130static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7131 const struct qstr *name,
7132 struct nfs4_fs_locations *fs_locations,
7133 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007134{
7135 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007136 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007137 struct nfs4_fs_locations_arg args = {
7138 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007139 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007140 .page = page,
7141 .bitmask = bitmask,
7142 };
Benny Halevy22958462009-04-01 09:22:02 -04007143 struct nfs4_fs_locations_res res = {
7144 .fs_locations = fs_locations,
7145 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007146 struct rpc_message msg = {
7147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7148 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007149 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007150 };
7151 int status;
7152
Harvey Harrison3110ff82008-05-02 13:42:44 -07007153 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007154
Chuck Leverc05cefc2017-11-05 15:45:22 -05007155 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7156 bitmask[1] = nfs4_fattr_bitmap[1];
7157
Andy Adamson533eb462011-06-13 18:25:56 -04007158 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7159 * is not supported */
7160 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007161 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007162 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007163 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007164
Trond Myklebustc228fd32007-01-13 02:28:11 -05007165 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007166 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007167 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007168 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007169 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007170 return status;
7171}
7172
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007173int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7174 const struct qstr *name,
7175 struct nfs4_fs_locations *fs_locations,
7176 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007177{
7178 struct nfs4_exception exception = { };
7179 int err;
7180 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007181 err = _nfs4_proc_fs_locations(client, dir, name,
7182 fs_locations, page);
7183 trace_nfs4_get_fs_locations(dir, name, err);
7184 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007185 &exception);
7186 } while (exception.retry);
7187 return err;
7188}
7189
Chuck Leverb03d7352013-10-17 14:12:50 -04007190/*
7191 * This operation also signals the server that this client is
7192 * performing migration recovery. The server can stop returning
7193 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7194 * appended to this compound to identify the client ID which is
7195 * performing recovery.
7196 */
7197static int _nfs40_proc_get_locations(struct inode *inode,
7198 struct nfs4_fs_locations *locations,
7199 struct page *page, struct rpc_cred *cred)
7200{
7201 struct nfs_server *server = NFS_SERVER(inode);
7202 struct rpc_clnt *clnt = server->client;
7203 u32 bitmask[2] = {
7204 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7205 };
7206 struct nfs4_fs_locations_arg args = {
7207 .clientid = server->nfs_client->cl_clientid,
7208 .fh = NFS_FH(inode),
7209 .page = page,
7210 .bitmask = bitmask,
7211 .migration = 1, /* skip LOOKUP */
7212 .renew = 1, /* append RENEW */
7213 };
7214 struct nfs4_fs_locations_res res = {
7215 .fs_locations = locations,
7216 .migration = 1,
7217 .renew = 1,
7218 };
7219 struct rpc_message msg = {
7220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7221 .rpc_argp = &args,
7222 .rpc_resp = &res,
7223 .rpc_cred = cred,
7224 };
7225 unsigned long now = jiffies;
7226 int status;
7227
7228 nfs_fattr_init(&locations->fattr);
7229 locations->server = server;
7230 locations->nlocations = 0;
7231
Anna Schumakerfba83f32018-05-04 16:22:50 -04007232 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007233 status = nfs4_call_sync_sequence(clnt, server, &msg,
7234 &args.seq_args, &res.seq_res);
7235 if (status)
7236 return status;
7237
7238 renew_lease(server, now);
7239 return 0;
7240}
7241
7242#ifdef CONFIG_NFS_V4_1
7243
7244/*
7245 * This operation also signals the server that this client is
7246 * performing migration recovery. The server can stop asserting
7247 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7248 * performing this operation is identified in the SEQUENCE
7249 * operation in this compound.
7250 *
7251 * When the client supports GETATTR(fs_locations_info), it can
7252 * be plumbed in here.
7253 */
7254static int _nfs41_proc_get_locations(struct inode *inode,
7255 struct nfs4_fs_locations *locations,
7256 struct page *page, struct rpc_cred *cred)
7257{
7258 struct nfs_server *server = NFS_SERVER(inode);
7259 struct rpc_clnt *clnt = server->client;
7260 u32 bitmask[2] = {
7261 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7262 };
7263 struct nfs4_fs_locations_arg args = {
7264 .fh = NFS_FH(inode),
7265 .page = page,
7266 .bitmask = bitmask,
7267 .migration = 1, /* skip LOOKUP */
7268 };
7269 struct nfs4_fs_locations_res res = {
7270 .fs_locations = locations,
7271 .migration = 1,
7272 };
7273 struct rpc_message msg = {
7274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7275 .rpc_argp = &args,
7276 .rpc_resp = &res,
7277 .rpc_cred = cred,
7278 };
7279 int status;
7280
7281 nfs_fattr_init(&locations->fattr);
7282 locations->server = server;
7283 locations->nlocations = 0;
7284
Anna Schumakerfba83f32018-05-04 16:22:50 -04007285 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007286 status = nfs4_call_sync_sequence(clnt, server, &msg,
7287 &args.seq_args, &res.seq_res);
7288 if (status == NFS4_OK &&
7289 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7290 status = -NFS4ERR_LEASE_MOVED;
7291 return status;
7292}
7293
7294#endif /* CONFIG_NFS_V4_1 */
7295
7296/**
7297 * nfs4_proc_get_locations - discover locations for a migrated FSID
7298 * @inode: inode on FSID that is migrating
7299 * @locations: result of query
7300 * @page: buffer
7301 * @cred: credential to use for this operation
7302 *
7303 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7304 * operation failed, or a negative errno if a local error occurred.
7305 *
7306 * On success, "locations" is filled in, but if the server has
7307 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7308 * asserted.
7309 *
7310 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7311 * from this client that require migration recovery.
7312 */
7313int nfs4_proc_get_locations(struct inode *inode,
7314 struct nfs4_fs_locations *locations,
7315 struct page *page, struct rpc_cred *cred)
7316{
7317 struct nfs_server *server = NFS_SERVER(inode);
7318 struct nfs_client *clp = server->nfs_client;
7319 const struct nfs4_mig_recovery_ops *ops =
7320 clp->cl_mvops->mig_recovery_ops;
7321 struct nfs4_exception exception = { };
7322 int status;
7323
7324 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7325 (unsigned long long)server->fsid.major,
7326 (unsigned long long)server->fsid.minor,
7327 clp->cl_hostname);
7328 nfs_display_fhandle(NFS_FH(inode), __func__);
7329
7330 do {
7331 status = ops->get_locations(inode, locations, page, cred);
7332 if (status != -NFS4ERR_DELAY)
7333 break;
7334 nfs4_handle_exception(server, status, &exception);
7335 } while (exception.retry);
7336 return status;
7337}
7338
Chuck Lever44c99932013-10-17 14:13:30 -04007339/*
7340 * This operation also signals the server that this client is
7341 * performing "lease moved" recovery. The server can stop
7342 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7343 * is appended to this compound to identify the client ID which is
7344 * performing recovery.
7345 */
7346static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7347{
7348 struct nfs_server *server = NFS_SERVER(inode);
7349 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7350 struct rpc_clnt *clnt = server->client;
7351 struct nfs4_fsid_present_arg args = {
7352 .fh = NFS_FH(inode),
7353 .clientid = clp->cl_clientid,
7354 .renew = 1, /* append RENEW */
7355 };
7356 struct nfs4_fsid_present_res res = {
7357 .renew = 1,
7358 };
7359 struct rpc_message msg = {
7360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7361 .rpc_argp = &args,
7362 .rpc_resp = &res,
7363 .rpc_cred = cred,
7364 };
7365 unsigned long now = jiffies;
7366 int status;
7367
7368 res.fh = nfs_alloc_fhandle();
7369 if (res.fh == NULL)
7370 return -ENOMEM;
7371
Anna Schumakerfba83f32018-05-04 16:22:50 -04007372 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007373 status = nfs4_call_sync_sequence(clnt, server, &msg,
7374 &args.seq_args, &res.seq_res);
7375 nfs_free_fhandle(res.fh);
7376 if (status)
7377 return status;
7378
7379 do_renew_lease(clp, now);
7380 return 0;
7381}
7382
7383#ifdef CONFIG_NFS_V4_1
7384
7385/*
7386 * This operation also signals the server that this client is
7387 * performing "lease moved" recovery. The server can stop asserting
7388 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7389 * this operation is identified in the SEQUENCE operation in this
7390 * compound.
7391 */
7392static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7393{
7394 struct nfs_server *server = NFS_SERVER(inode);
7395 struct rpc_clnt *clnt = server->client;
7396 struct nfs4_fsid_present_arg args = {
7397 .fh = NFS_FH(inode),
7398 };
7399 struct nfs4_fsid_present_res res = {
7400 };
7401 struct rpc_message msg = {
7402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7403 .rpc_argp = &args,
7404 .rpc_resp = &res,
7405 .rpc_cred = cred,
7406 };
7407 int status;
7408
7409 res.fh = nfs_alloc_fhandle();
7410 if (res.fh == NULL)
7411 return -ENOMEM;
7412
Anna Schumakerfba83f32018-05-04 16:22:50 -04007413 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007414 status = nfs4_call_sync_sequence(clnt, server, &msg,
7415 &args.seq_args, &res.seq_res);
7416 nfs_free_fhandle(res.fh);
7417 if (status == NFS4_OK &&
7418 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7419 status = -NFS4ERR_LEASE_MOVED;
7420 return status;
7421}
7422
7423#endif /* CONFIG_NFS_V4_1 */
7424
7425/**
7426 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7427 * @inode: inode on FSID to check
7428 * @cred: credential to use for this operation
7429 *
7430 * Server indicates whether the FSID is present, moved, or not
7431 * recognized. This operation is necessary to clear a LEASE_MOVED
7432 * condition for this client ID.
7433 *
7434 * Returns NFS4_OK if the FSID is present on this server,
7435 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7436 * NFS4ERR code if some error occurred on the server, or a
7437 * negative errno if a local failure occurred.
7438 */
7439int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7440{
7441 struct nfs_server *server = NFS_SERVER(inode);
7442 struct nfs_client *clp = server->nfs_client;
7443 const struct nfs4_mig_recovery_ops *ops =
7444 clp->cl_mvops->mig_recovery_ops;
7445 struct nfs4_exception exception = { };
7446 int status;
7447
7448 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7449 (unsigned long long)server->fsid.major,
7450 (unsigned long long)server->fsid.minor,
7451 clp->cl_hostname);
7452 nfs_display_fhandle(NFS_FH(inode), __func__);
7453
7454 do {
7455 status = ops->fsid_present(inode, cred);
7456 if (status != -NFS4ERR_DELAY)
7457 break;
7458 nfs4_handle_exception(server, status, &exception);
7459 } while (exception.retry);
7460 return status;
7461}
7462
Andy Adamson5ec16a82013-08-08 10:57:55 -04007463/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007464 * If 'use_integrity' is true and the state managment nfs_client
7465 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7466 * and the machine credential as per RFC3530bis and RFC5661 Security
7467 * Considerations sections. Otherwise, just use the user cred with the
7468 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007469 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007470static 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 +00007471{
7472 int status;
7473 struct nfs4_secinfo_arg args = {
7474 .dir_fh = NFS_FH(dir),
7475 .name = name,
7476 };
7477 struct nfs4_secinfo_res res = {
7478 .flavors = flavors,
7479 };
7480 struct rpc_message msg = {
7481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7482 .rpc_argp = &args,
7483 .rpc_resp = &res,
7484 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007485 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007486 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007487
7488 if (use_integrity) {
7489 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007490 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7491 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007492 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007493
7494 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007495
7496 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7497 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7498
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007499 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7500 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007501 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007502
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007503 if (cred)
7504 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007505
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007506 return status;
7507}
7508
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007509int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7510 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007511{
7512 struct nfs4_exception exception = { };
7513 int err;
7514 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007515 err = -NFS4ERR_WRONGSEC;
7516
7517 /* try to use integrity protection with machine cred */
7518 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7519 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7520
7521 /*
7522 * if unable to use integrity protection, or SECINFO with
7523 * integrity protection returns NFS4ERR_WRONGSEC (which is
7524 * disallowed by spec, but exists in deployed servers) use
7525 * the current filesystem's rpc_client and the user cred.
7526 */
7527 if (err == -NFS4ERR_WRONGSEC)
7528 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7529
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007530 trace_nfs4_secinfo(dir, name, err);
7531 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007532 &exception);
7533 } while (exception.retry);
7534 return err;
7535}
7536
Andy Adamson557134a2009-04-01 09:21:53 -04007537#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007538/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007539 * Check the exchange flags returned by the server for invalid flags, having
7540 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7541 * DS flags set.
7542 */
7543static int nfs4_check_cl_exchange_flags(u32 flags)
7544{
7545 if (flags & ~EXCHGID4_FLAG_MASK_R)
7546 goto out_inval;
7547 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7548 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7549 goto out_inval;
7550 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7551 goto out_inval;
7552 return NFS_OK;
7553out_inval:
7554 return -NFS4ERR_INVAL;
7555}
7556
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007557static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007558nfs41_same_server_scope(struct nfs41_server_scope *a,
7559 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007560{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007561 if (a->server_scope_sz != b->server_scope_sz)
7562 return false;
7563 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007564}
7565
Andy Adamson02a95de2016-02-05 16:08:37 -05007566static void
7567nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7568{
7569}
7570
7571static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7572 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7573};
7574
Andy Adamson357f54d2010-12-14 10:11:57 -05007575/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007576 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007577 *
7578 * The 4.1 client currently uses the same TCP connection for the
7579 * fore and backchannel.
7580 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007581static
7582int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7583 struct rpc_xprt *xprt,
7584 struct nfs_client *clp,
7585 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007586{
7587 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007588 struct nfs41_bind_conn_to_session_args args = {
7589 .client = clp,
7590 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7591 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007592 struct nfs41_bind_conn_to_session_res res;
7593 struct rpc_message msg = {
7594 .rpc_proc =
7595 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007596 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007597 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007598 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007599 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007600 struct rpc_task_setup task_setup_data = {
7601 .rpc_client = clnt,
7602 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007603 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007604 .rpc_message = &msg,
7605 .flags = RPC_TASK_TIMEOUT,
7606 };
7607 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007608
Trond Myklebust71a097c2015-02-18 09:27:18 -08007609 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7610 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7611 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007612
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007613 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7614 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7615 args.dir = NFS4_CDFC4_FORE;
7616
7617 task = rpc_run_task(&task_setup_data);
7618 if (!IS_ERR(task)) {
7619 status = task->tk_status;
7620 rpc_put_task(task);
7621 } else
7622 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007623 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007624 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007625 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007626 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7627 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007628 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007629 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007630 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007631 dprintk("NFS: %s: Unexpected direction from server\n",
7632 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007633 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007634 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007635 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007636 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7637 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007638 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007639 }
7640 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007641
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007642 return status;
7643}
7644
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007645struct rpc_bind_conn_calldata {
7646 struct nfs_client *clp;
7647 struct rpc_cred *cred;
7648};
7649
7650static int
7651nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7652 struct rpc_xprt *xprt,
7653 void *calldata)
7654{
7655 struct rpc_bind_conn_calldata *p = calldata;
7656
7657 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7658}
7659
7660int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7661{
7662 struct rpc_bind_conn_calldata data = {
7663 .clp = clp,
7664 .cred = cred,
7665 };
7666 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7667 nfs4_proc_bind_conn_to_session_callback, &data);
7668}
7669
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007670/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007671 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7672 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007673 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007674static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7675 .how = SP4_MACH_CRED,
7676 .enforce.u.words = {
7677 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7678 1 << (OP_EXCHANGE_ID - 32) |
7679 1 << (OP_CREATE_SESSION - 32) |
7680 1 << (OP_DESTROY_SESSION - 32) |
7681 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007682 },
7683 .allow.u.words = {
7684 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007685 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007686 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007687 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007688 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007689 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007690 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007691 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007692 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007693 1 << (OP_FREE_STATEID - 32) |
7694 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007695 }
7696};
7697
7698/*
7699 * Select the state protection mode for client `clp' given the server results
7700 * from exchange_id in `sp'.
7701 *
7702 * Returns 0 on success, negative errno otherwise.
7703 */
7704static int nfs4_sp4_select_mode(struct nfs_client *clp,
7705 struct nfs41_state_protection *sp)
7706{
7707 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7708 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7709 1 << (OP_EXCHANGE_ID - 32) |
7710 1 << (OP_CREATE_SESSION - 32) |
7711 1 << (OP_DESTROY_SESSION - 32) |
7712 1 << (OP_DESTROY_CLIENTID - 32)
7713 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007714 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007715 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007716 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007717
7718 if (sp->how == SP4_MACH_CRED) {
7719 /* Print state protect result */
7720 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7721 for (i = 0; i <= LAST_NFS4_OP; i++) {
7722 if (test_bit(i, sp->enforce.u.longs))
7723 dfprintk(MOUNT, " enforce op %d\n", i);
7724 if (test_bit(i, sp->allow.u.longs))
7725 dfprintk(MOUNT, " allow op %d\n", i);
7726 }
7727
7728 /* make sure nothing is on enforce list that isn't supported */
7729 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7730 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7731 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007732 ret = -EINVAL;
7733 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007734 }
7735 }
7736
7737 /*
7738 * Minimal mode - state operations are allowed to use machine
7739 * credential. Note this already happens by default, so the
7740 * client doesn't have to do anything more than the negotiation.
7741 *
7742 * NOTE: we don't care if EXCHANGE_ID is in the list -
7743 * we're already using the machine cred for exchange_id
7744 * and will never use a different cred.
7745 */
7746 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7747 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7748 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7749 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7750 dfprintk(MOUNT, "sp4_mach_cred:\n");
7751 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007752 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007753 } else {
7754 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007755 ret = -EINVAL;
7756 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007757 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007758
7759 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007760 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7761 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007762 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7763 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007764 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007765 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007766
Andrew Elble99ade3c2015-12-02 09:39:51 -05007767 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7768 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007769 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007770 }
7771
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007772 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7773 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7774 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007775 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007776 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007777
7778 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7779 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7780 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007781 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007782 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007783
7784 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7785 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007786 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007787 }
7788
7789 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7790 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007791 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007792 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007793 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007794out:
7795 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007796 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007797}
7798
Andy Adamson8d89bd72016-09-09 09:22:18 -04007799struct nfs41_exchange_id_data {
7800 struct nfs41_exchange_id_res res;
7801 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007802};
7803
Andy Adamson8d89bd72016-09-09 09:22:18 -04007804static void nfs4_exchange_id_release(void *data)
7805{
7806 struct nfs41_exchange_id_data *cdata =
7807 (struct nfs41_exchange_id_data *)data;
7808
Olga Kornievskaia63513232017-03-13 10:36:19 -04007809 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007810 kfree(cdata->res.impl_id);
7811 kfree(cdata->res.server_scope);
7812 kfree(cdata->res.server_owner);
7813 kfree(cdata);
7814}
7815
7816static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007817 .rpc_release = nfs4_exchange_id_release,
7818};
7819
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007820/*
7821 * _nfs4_proc_exchange_id()
7822 *
7823 * Wrapper for EXCHANGE_ID operation.
7824 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007825static struct rpc_task *
7826nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007827 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007828{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007829 struct rpc_message msg = {
7830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007831 .rpc_cred = cred,
7832 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007833 struct rpc_task_setup task_setup_data = {
7834 .rpc_client = clp->cl_rpcclient,
7835 .callback_ops = &nfs4_exchange_id_call_ops,
7836 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007837 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007838 };
7839 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007840 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007841
Elena Reshetova212bf412017-10-20 12:53:38 +03007842 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007843 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007844
Trond Myklebust9c760d12017-07-31 18:38:50 -04007845 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007846 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007847 if (!calldata)
7848 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007849
Trond Myklebustfd405592017-08-01 16:02:47 -04007850 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007851
7852 status = nfs4_init_uniform_client_string(clp);
7853 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007854 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007855
Andy Adamson8d89bd72016-09-09 09:22:18 -04007856 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7857 GFP_NOFS);
7858 status = -ENOMEM;
7859 if (unlikely(calldata->res.server_owner == NULL))
7860 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007861
Andy Adamson8d89bd72016-09-09 09:22:18 -04007862 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007863 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007864 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007865 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007866
Andy Adamson8d89bd72016-09-09 09:22:18 -04007867 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7868 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007869 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007870
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007871 switch (sp4_how) {
7872 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007873 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007874 break;
7875
7876 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007877 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007878 break;
7879
7880 default:
7881 /* unsupported! */
7882 WARN_ON_ONCE(1);
7883 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007884 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007885 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007886 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007887 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007888 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007889 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7890 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007891 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007892 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007893 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7894 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007895#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007896 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007897#endif
7898 msg.rpc_argp = &calldata->args;
7899 msg.rpc_resp = &calldata->res;
7900 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007901
Trond Myklebust9c760d12017-07-31 18:38:50 -04007902 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007903
7904out_impl_id:
7905 kfree(calldata->res.impl_id);
7906out_server_scope:
7907 kfree(calldata->res.server_scope);
7908out_server_owner:
7909 kfree(calldata->res.server_owner);
7910out_calldata:
7911 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007912out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007913 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007914 return ERR_PTR(status);
7915}
7916
7917/*
7918 * _nfs4_proc_exchange_id()
7919 *
7920 * Wrapper for EXCHANGE_ID operation.
7921 */
7922static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7923 u32 sp4_how)
7924{
7925 struct rpc_task *task;
7926 struct nfs41_exchange_id_args *argp;
7927 struct nfs41_exchange_id_res *resp;
7928 int status;
7929
7930 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7931 if (IS_ERR(task))
7932 return PTR_ERR(task);
7933
7934 argp = task->tk_msg.rpc_argp;
7935 resp = task->tk_msg.rpc_resp;
7936 status = task->tk_status;
7937 if (status != 0)
7938 goto out;
7939
7940 status = nfs4_check_cl_exchange_flags(resp->flags);
7941 if (status != 0)
7942 goto out;
7943
7944 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7945 if (status != 0)
7946 goto out;
7947
7948 clp->cl_clientid = resp->clientid;
7949 clp->cl_exchange_flags = resp->flags;
7950 clp->cl_seqid = resp->seqid;
7951 /* Client ID is not confirmed */
7952 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7953 clear_bit(NFS4_SESSION_ESTABLISHED,
7954 &clp->cl_session->session_state);
7955
7956 if (clp->cl_serverscope != NULL &&
7957 !nfs41_same_server_scope(clp->cl_serverscope,
7958 resp->server_scope)) {
7959 dprintk("%s: server_scope mismatch detected\n",
7960 __func__);
7961 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7962 }
7963
7964 swap(clp->cl_serverowner, resp->server_owner);
7965 swap(clp->cl_serverscope, resp->server_scope);
7966 swap(clp->cl_implid, resp->impl_id);
7967
7968 /* Save the EXCHANGE_ID verifier session trunk tests */
7969 memcpy(clp->cl_confirm.data, argp->verifier.data,
7970 sizeof(clp->cl_confirm.data));
7971out:
7972 trace_nfs4_exchange_id(clp, status);
7973 rpc_put_task(task);
7974 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007975}
7976
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007977/*
7978 * nfs4_proc_exchange_id()
7979 *
7980 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7981 *
7982 * Since the clientid has expired, all compounds using sessions
7983 * associated with the stale clientid will be returning
7984 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7985 * be in some phase of session reset.
7986 *
7987 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7988 */
7989int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7990{
7991 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7992 int status;
7993
7994 /* try SP4_MACH_CRED if krb5i/p */
7995 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7996 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007997 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007998 if (!status)
7999 return 0;
8000 }
8001
8002 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008003 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008004}
8005
Andy Adamson04fa2c62016-09-09 09:22:29 -04008006/**
8007 * nfs4_test_session_trunk
8008 *
8009 * This is an add_xprt_test() test function called from
8010 * rpc_clnt_setup_test_and_add_xprt.
8011 *
8012 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8013 * and is dereferrenced in nfs4_exchange_id_release
8014 *
8015 * Upon success, add the new transport to the rpc_clnt
8016 *
8017 * @clnt: struct rpc_clnt to get new transport
8018 * @xprt: the rpc_xprt to test
8019 * @data: call data for _nfs4_proc_exchange_id.
8020 */
8021int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8022 void *data)
8023{
8024 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008025 struct rpc_task *task;
8026 int status;
8027
Andy Adamson04fa2c62016-09-09 09:22:29 -04008028 u32 sp4_how;
8029
8030 dprintk("--> %s try %s\n", __func__,
8031 xprt->address_strings[RPC_DISPLAY_ADDR]);
8032
8033 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8034
8035 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008036 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8037 if (IS_ERR(task))
8038 return PTR_ERR(task);
8039
8040 status = task->tk_status;
8041 if (status == 0)
8042 status = nfs4_detect_session_trunking(adata->clp,
8043 task->tk_msg.rpc_resp, xprt);
8044
8045 rpc_put_task(task);
8046 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04008047}
8048EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8049
Trond Myklebust66245532012-05-25 17:18:09 -04008050static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8051 struct rpc_cred *cred)
8052{
8053 struct rpc_message msg = {
8054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8055 .rpc_argp = clp,
8056 .rpc_cred = cred,
8057 };
8058 int status;
8059
8060 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008061 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008062 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008063 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008064 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8065 return status;
8066}
8067
8068static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8069 struct rpc_cred *cred)
8070{
8071 unsigned int loop;
8072 int ret;
8073
8074 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8075 ret = _nfs4_proc_destroy_clientid(clp, cred);
8076 switch (ret) {
8077 case -NFS4ERR_DELAY:
8078 case -NFS4ERR_CLIENTID_BUSY:
8079 ssleep(1);
8080 break;
8081 default:
8082 return ret;
8083 }
8084 }
8085 return 0;
8086}
8087
8088int nfs4_destroy_clientid(struct nfs_client *clp)
8089{
8090 struct rpc_cred *cred;
8091 int ret = 0;
8092
8093 if (clp->cl_mvops->minor_version < 1)
8094 goto out;
8095 if (clp->cl_exchange_flags == 0)
8096 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008097 if (clp->cl_preserve_clid)
8098 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008099 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008100 ret = nfs4_proc_destroy_clientid(clp, cred);
8101 if (cred)
8102 put_rpccred(cred);
8103 switch (ret) {
8104 case 0:
8105 case -NFS4ERR_STALE_CLIENTID:
8106 clp->cl_exchange_flags = 0;
8107 }
8108out:
8109 return ret;
8110}
8111
Andy Adamson2050f0c2009-04-01 09:22:30 -04008112struct nfs4_get_lease_time_data {
8113 struct nfs4_get_lease_time_args *args;
8114 struct nfs4_get_lease_time_res *res;
8115 struct nfs_client *clp;
8116};
8117
8118static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8119 void *calldata)
8120{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008121 struct nfs4_get_lease_time_data *data =
8122 (struct nfs4_get_lease_time_data *)calldata;
8123
8124 dprintk("--> %s\n", __func__);
8125 /* just setup sequence, do not trigger session recovery
8126 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008127 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008128 &data->args->la_seq_args,
8129 &data->res->lr_seq_res,
8130 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008131 dprintk("<-- %s\n", __func__);
8132}
8133
8134/*
8135 * Called from nfs4_state_manager thread for session setup, so don't recover
8136 * from sequence operation or clientid errors.
8137 */
8138static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8139{
8140 struct nfs4_get_lease_time_data *data =
8141 (struct nfs4_get_lease_time_data *)calldata;
8142
8143 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008144 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8145 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008146 switch (task->tk_status) {
8147 case -NFS4ERR_DELAY:
8148 case -NFS4ERR_GRACE:
8149 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8150 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8151 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008152 /* fall through */
8153 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008154 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008155 return;
8156 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008157 dprintk("<-- %s\n", __func__);
8158}
8159
Trond Myklebust17280172012-03-11 13:11:00 -04008160static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008161 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8162 .rpc_call_done = nfs4_get_lease_time_done,
8163};
8164
8165int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8166{
8167 struct rpc_task *task;
8168 struct nfs4_get_lease_time_args args;
8169 struct nfs4_get_lease_time_res res = {
8170 .lr_fsinfo = fsinfo,
8171 };
8172 struct nfs4_get_lease_time_data data = {
8173 .args = &args,
8174 .res = &res,
8175 .clp = clp,
8176 };
8177 struct rpc_message msg = {
8178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8179 .rpc_argp = &args,
8180 .rpc_resp = &res,
8181 };
8182 struct rpc_task_setup task_setup = {
8183 .rpc_client = clp->cl_rpcclient,
8184 .rpc_message = &msg,
8185 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008186 .callback_data = &data,
8187 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008188 };
8189 int status;
8190
Anna Schumakerfba83f32018-05-04 16:22:50 -04008191 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008192 task = rpc_run_task(&task_setup);
8193
8194 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008195 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008196
Anna Schumakerf6148712017-04-07 14:15:23 -04008197 status = task->tk_status;
8198 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008199 return status;
8200}
8201
Andy Adamsonfc931582009-04-01 09:22:31 -04008202/*
8203 * Initialize the values to be used by the client in CREATE_SESSION
8204 * If nfs4_init_session set the fore channel request and response sizes,
8205 * use them.
8206 *
8207 * Set the back channel max_resp_sz_cached to zero to force the client to
8208 * always set csa_cachethis to FALSE because the current implementation
8209 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8210 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008211static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8212 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008213{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008214 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008215 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008216
Andy Adamson18aad3d2013-06-26 12:21:49 -04008217 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8218 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8219
Andy Adamsonfc931582009-04-01 09:22:31 -04008220 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008221 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8222 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008223 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008224 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008225
8226 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008227 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008228 __func__,
8229 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008230 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008231
8232 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008233 args->bc_attrs.max_rqst_sz = max_bc_payload;
8234 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008235 args->bc_attrs.max_resp_sz_cached = 0;
8236 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008237 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008238
8239 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8240 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8241 __func__,
8242 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8243 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8244 args->bc_attrs.max_reqs);
8245}
8246
Trond Myklebust79969dd12015-02-18 11:30:18 -08008247static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8248 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008249{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008250 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008251 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008252
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008253 if (rcvd->max_resp_sz > sent->max_resp_sz)
8254 return -EINVAL;
8255 /*
8256 * Our requested max_ops is the minimum we need; we're not
8257 * prepared to break up compounds into smaller pieces than that.
8258 * So, no point even trying to continue if the server won't
8259 * cooperate:
8260 */
8261 if (rcvd->max_ops < sent->max_ops)
8262 return -EINVAL;
8263 if (rcvd->max_reqs == 0)
8264 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008265 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8266 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008267 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008268}
8269
Trond Myklebust79969dd12015-02-18 11:30:18 -08008270static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8271 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008272{
8273 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008274 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008275
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008276 if (!(res->flags & SESSION4_BACK_CHAN))
8277 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008278 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8279 return -EINVAL;
8280 if (rcvd->max_resp_sz < sent->max_resp_sz)
8281 return -EINVAL;
8282 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8283 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008284 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008285 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008286 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008287 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008288out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008289 return 0;
8290}
Andy Adamson8d353012009-04-01 09:22:32 -04008291
Andy Adamson8d353012009-04-01 09:22:32 -04008292static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008293 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008294{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008295 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008296
Trond Myklebust79969dd12015-02-18 11:30:18 -08008297 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008298 if (ret)
8299 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008300 return nfs4_verify_back_channel_attrs(args, res);
8301}
8302
8303static void nfs4_update_session(struct nfs4_session *session,
8304 struct nfs41_create_session_res *res)
8305{
8306 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008307 /* Mark client id and session as being confirmed */
8308 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8309 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008310 session->flags = res->flags;
8311 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008312 if (res->flags & SESSION4_BACK_CHAN)
8313 memcpy(&session->bc_attrs, &res->bc_attrs,
8314 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008315}
8316
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008317static int _nfs4_proc_create_session(struct nfs_client *clp,
8318 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008319{
8320 struct nfs4_session *session = clp->cl_session;
8321 struct nfs41_create_session_args args = {
8322 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008323 .clientid = clp->cl_clientid,
8324 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008325 .cb_program = NFS4_CALLBACK,
8326 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008327 struct nfs41_create_session_res res;
8328
Andy Adamsonfc931582009-04-01 09:22:31 -04008329 struct rpc_message msg = {
8330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8331 .rpc_argp = &args,
8332 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008333 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008334 };
8335 int status;
8336
Chuck Lever6b26cc82016-05-02 14:40:40 -04008337 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008338 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008339
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008340 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008341 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008342
Trond Myklebustb519d402016-09-11 14:50:01 -04008343 switch (status) {
8344 case -NFS4ERR_STALE_CLIENTID:
8345 case -NFS4ERR_DELAY:
8346 case -ETIMEDOUT:
8347 case -EACCES:
8348 case -EAGAIN:
8349 goto out;
8350 };
8351
8352 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008353 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008354 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008355 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008356 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008357 if (status)
8358 goto out;
8359 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008360 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008361out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008362 return status;
8363}
8364
8365/*
8366 * Issues a CREATE_SESSION operation to the server.
8367 * It is the responsibility of the caller to verify the session is
8368 * expired before calling this routine.
8369 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008370int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008371{
8372 int status;
8373 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008374 struct nfs4_session *session = clp->cl_session;
8375
8376 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8377
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008378 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008379 if (status)
8380 goto out;
8381
Andy Adamsonaacd5532011-11-09 13:58:21 -05008382 /* Init or reset the session slot tables */
8383 status = nfs4_setup_session_slot_tables(session);
8384 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008385 if (status)
8386 goto out;
8387
8388 ptr = (unsigned *)&session->sess_id.data[0];
8389 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8390 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008391out:
8392 dprintk("<-- %s\n", __func__);
8393 return status;
8394}
8395
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008396/*
8397 * Issue the over-the-wire RPC DESTROY_SESSION.
8398 * The caller must serialize access to this routine.
8399 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008400int nfs4_proc_destroy_session(struct nfs4_session *session,
8401 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008402{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008403 struct rpc_message msg = {
8404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8405 .rpc_argp = session,
8406 .rpc_cred = cred,
8407 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008408 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008409
8410 dprintk("--> nfs4_proc_destroy_session\n");
8411
8412 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008413 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8414 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008415
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008416 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008417 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008418
8419 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008420 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008421 "Session has been destroyed regardless...\n", status);
8422
8423 dprintk("<-- nfs4_proc_destroy_session\n");
8424 return status;
8425}
8426
Trond Myklebust7b38c362012-05-23 13:23:31 -04008427/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008428 * Renew the cl_session lease.
8429 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008430struct nfs4_sequence_data {
8431 struct nfs_client *clp;
8432 struct nfs4_sequence_args args;
8433 struct nfs4_sequence_res res;
8434};
8435
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008436static void nfs41_sequence_release(void *data)
8437{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008438 struct nfs4_sequence_data *calldata = data;
8439 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008440
Elena Reshetova212bf412017-10-20 12:53:38 +03008441 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008442 nfs4_schedule_state_renewal(clp);
8443 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008444 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008445}
8446
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008447static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8448{
8449 switch(task->tk_status) {
8450 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008451 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8452 return -EAGAIN;
8453 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008454 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008455 }
8456 return 0;
8457}
8458
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008459static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008460{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008461 struct nfs4_sequence_data *calldata = data;
8462 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008463
Trond Myklebust14516c32010-07-31 14:29:06 -04008464 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8465 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008466
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008467 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008468 if (task->tk_status < 0) {
8469 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008470 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008471 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008472
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008473 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8474 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008475 return;
8476 }
8477 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008478 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008479out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008480 dprintk("<-- %s\n", __func__);
8481}
8482
8483static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8484{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008485 struct nfs4_sequence_data *calldata = data;
8486 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008487 struct nfs4_sequence_args *args;
8488 struct nfs4_sequence_res *res;
8489
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008490 args = task->tk_msg.rpc_argp;
8491 res = task->tk_msg.rpc_resp;
8492
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008493 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008494}
8495
8496static const struct rpc_call_ops nfs41_sequence_ops = {
8497 .rpc_call_done = nfs41_sequence_call_done,
8498 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008499 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008500};
8501
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008502static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8503 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008504 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008505 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008506{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008507 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008508 struct rpc_message msg = {
8509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8510 .rpc_cred = cred,
8511 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008512 struct rpc_task_setup task_setup_data = {
8513 .rpc_client = clp->cl_rpcclient,
8514 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008515 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008516 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008517 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008518 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008519
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008520 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008521 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008522 goto out_err;
8523
8524 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008525 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008526 if (calldata == NULL)
8527 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008528 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008529 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008530 msg.rpc_argp = &calldata->args;
8531 msg.rpc_resp = &calldata->res;
8532 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008533 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008534
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008535 ret = rpc_run_task(&task_setup_data);
8536 if (IS_ERR(ret))
8537 goto out_err;
8538 return ret;
8539out_put_clp:
8540 nfs_put_client(clp);
8541out_err:
8542 nfs41_release_slot(slot);
8543 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008544}
8545
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008546static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008547{
8548 struct rpc_task *task;
8549 int ret = 0;
8550
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008551 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008552 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008553 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008554 if (IS_ERR(task))
8555 ret = PTR_ERR(task);
8556 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008557 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008558 dprintk("<-- %s status=%d\n", __func__, ret);
8559 return ret;
8560}
8561
8562static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8563{
8564 struct rpc_task *task;
8565 int ret;
8566
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008567 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008568 if (IS_ERR(task)) {
8569 ret = PTR_ERR(task);
8570 goto out;
8571 }
8572 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008573 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008574 ret = task->tk_status;
8575 rpc_put_task(task);
8576out:
8577 dprintk("<-- %s status=%d\n", __func__, ret);
8578 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008579}
8580
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008581struct nfs4_reclaim_complete_data {
8582 struct nfs_client *clp;
8583 struct nfs41_reclaim_complete_args arg;
8584 struct nfs41_reclaim_complete_res res;
8585};
8586
8587static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8588{
8589 struct nfs4_reclaim_complete_data *calldata = data;
8590
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008591 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008592 &calldata->arg.seq_args,
8593 &calldata->res.seq_res,
8594 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008595}
8596
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008597static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8598{
8599 switch(task->tk_status) {
8600 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008601 wake_up_all(&clp->cl_lock_waitq);
8602 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008603 case -NFS4ERR_COMPLETE_ALREADY:
8604 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8605 break;
8606 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008607 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008608 /* fall through */
8609 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008610 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008611 case -NFS4ERR_BADSESSION:
8612 case -NFS4ERR_DEADSESSION:
8613 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8614 nfs4_schedule_session_recovery(clp->cl_session,
8615 task->tk_status);
8616 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008617 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008618 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008619 }
8620 return 0;
8621}
8622
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008623static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8624{
8625 struct nfs4_reclaim_complete_data *calldata = data;
8626 struct nfs_client *clp = calldata->clp;
8627 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8628
8629 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008630 if (!nfs41_sequence_done(task, res))
8631 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008632
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008633 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008634 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8635 rpc_restart_call_prepare(task);
8636 return;
8637 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008638 dprintk("<-- %s\n", __func__);
8639}
8640
8641static void nfs4_free_reclaim_complete_data(void *data)
8642{
8643 struct nfs4_reclaim_complete_data *calldata = data;
8644
8645 kfree(calldata);
8646}
8647
8648static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8649 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8650 .rpc_call_done = nfs4_reclaim_complete_done,
8651 .rpc_release = nfs4_free_reclaim_complete_data,
8652};
8653
8654/*
8655 * Issue a global reclaim complete.
8656 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008657static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8658 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008659{
8660 struct nfs4_reclaim_complete_data *calldata;
8661 struct rpc_task *task;
8662 struct rpc_message msg = {
8663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008664 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008665 };
8666 struct rpc_task_setup task_setup_data = {
8667 .rpc_client = clp->cl_rpcclient,
8668 .rpc_message = &msg,
8669 .callback_ops = &nfs4_reclaim_complete_call_ops,
8670 .flags = RPC_TASK_ASYNC,
8671 };
8672 int status = -ENOMEM;
8673
8674 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008675 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008676 if (calldata == NULL)
8677 goto out;
8678 calldata->clp = clp;
8679 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008680
Anna Schumakerfba83f32018-05-04 16:22:50 -04008681 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008682 msg.rpc_argp = &calldata->arg;
8683 msg.rpc_resp = &calldata->res;
8684 task_setup_data.callback_data = calldata;
8685 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008686 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008687 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008688 goto out;
8689 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008690 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008691 if (status == 0)
8692 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008693 rpc_put_task(task);
8694out:
8695 dprintk("<-- %s status=%d\n", __func__, status);
8696 return status;
8697}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698
8699static void
8700nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8701{
8702 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008703 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008704
8705 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008706 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008707 &lgp->res.seq_res, task);
8708 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008709}
8710
8711static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8712{
8713 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008714
8715 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008716 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008717 dprintk("<-- %s\n", __func__);
8718}
8719
8720static int
8721nfs4_layoutget_handle_exception(struct rpc_task *task,
8722 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8723{
Trond Myklebustee314c22012-10-01 17:25:48 -07008724 struct inode *inode = lgp->args.inode;
8725 struct nfs_server *server = NFS_SERVER(inode);
8726 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008727 int nfs4err = task->tk_status;
8728 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008729 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008730
Boaz Harroshed7e5422014-01-22 20:34:54 +02008731 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008732
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008733 nfs4_sequence_free_slot(&lgp->res.seq_res);
8734
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008735 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008736 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008737 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008738
8739 /*
8740 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8741 * on the file. set tk_status to -ENODATA to tell upper layer to
8742 * retry go inband.
8743 */
8744 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008745 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008746 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008747 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008748 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8749 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8750 */
8751 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008752 status = -EOVERFLOW;
8753 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008754 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008755 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008756 * (or clients) writing to the same RAID stripe except when
8757 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008758 *
8759 * Treat it like we would RECALLCONFLICT -- we retry for a little
8760 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008761 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008762 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008763 if (lgp->args.minlength == 0) {
8764 status = -EOVERFLOW;
8765 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008766 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008767 status = -EBUSY;
8768 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008769 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008770 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008771 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008772 case -NFS4ERR_DELEG_REVOKED:
8773 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008774 case -NFS4ERR_EXPIRED:
8775 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008776 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008777 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008778 lo = NFS_I(inode)->layout;
8779 /* If the open stateid was bad, then recover it. */
8780 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008781 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008782 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008783 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008784 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008785 break;
8786 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008787
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008788 /*
8789 * Mark the bad layout state as invalid, then retry
8790 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008791 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008792 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008793 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008794 pnfs_free_lseg_list(&head);
8795 status = -EAGAIN;
8796 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008797 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008798
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008799 err = nfs4_handle_exception(server, nfs4err, exception);
8800 if (!status) {
8801 if (exception->retry)
8802 status = -EAGAIN;
8803 else
8804 status = err;
8805 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008806out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008807 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008808 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008809}
8810
Fred Isamandacb4522016-09-19 17:47:09 -04008811size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008812{
8813 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8814 return nfs_page_array_len(0, max_resp_sz);
8815}
8816
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008817static void nfs4_layoutget_release(void *calldata)
8818{
8819 struct nfs4_layoutget *lgp = calldata;
8820
8821 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008822 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008823 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008824 dprintk("<-- %s\n", __func__);
8825}
8826
8827static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8828 .rpc_call_prepare = nfs4_layoutget_prepare,
8829 .rpc_call_done = nfs4_layoutget_done,
8830 .rpc_release = nfs4_layoutget_release,
8831};
8832
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008833struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008834nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008835{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008836 struct inode *inode = lgp->args.inode;
8837 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008838 struct rpc_task *task;
8839 struct rpc_message msg = {
8840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8841 .rpc_argp = &lgp->args,
8842 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008843 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008844 };
8845 struct rpc_task_setup task_setup_data = {
8846 .rpc_client = server->client,
8847 .rpc_message = &msg,
8848 .callback_ops = &nfs4_layoutget_call_ops,
8849 .callback_data = lgp,
8850 .flags = RPC_TASK_ASYNC,
8851 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008852 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008853 struct nfs4_exception exception = {
8854 .inode = inode,
8855 .timeout = *timeout,
8856 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008857 int status = 0;
8858
8859 dprintk("--> %s\n", __func__);
8860
Peng Tao4bd5a982014-11-17 11:05:17 +08008861 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8862 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8863
Anna Schumakerfba83f32018-05-04 16:22:50 -04008864 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008865
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008866 task = rpc_run_task(&task_setup_data);
8867 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008868 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008869 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008870 if (status != 0)
8871 goto out;
8872
8873 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8874 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008875 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8876 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008877 } else
8878 lseg = pnfs_layout_process(lgp);
8879out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008880 trace_nfs4_layoutget(lgp->args.ctx,
8881 &lgp->args.range,
8882 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008883 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008884 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008885
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008886 rpc_put_task(task);
8887 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008888 if (status)
8889 return ERR_PTR(status);
8890 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008891}
8892
Benny Halevycbe82602011-05-22 19:52:37 +03008893static void
8894nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8895{
8896 struct nfs4_layoutreturn *lrp = calldata;
8897
8898 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008899 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008900 &lrp->args.seq_args,
8901 &lrp->res.seq_res,
8902 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008903 if (!pnfs_layout_is_valid(lrp->args.layout))
8904 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008905}
8906
8907static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8908{
8909 struct nfs4_layoutreturn *lrp = calldata;
8910 struct nfs_server *server;
8911
8912 dprintk("--> %s\n", __func__);
8913
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008914 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008915 return;
8916
8917 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008918 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008919 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04008920 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustff905142017-11-06 15:28:08 -05008921 lrp->args.inode))
8922 goto out_restart;
8923 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008924 default:
8925 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008926 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008927 case 0:
8928 break;
8929 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008930 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008931 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008932 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008933 }
Benny Halevycbe82602011-05-22 19:52:37 +03008934 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008935 return;
8936out_restart:
8937 task->tk_status = 0;
8938 nfs4_sequence_free_slot(&lrp->res.seq_res);
8939 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008940}
8941
8942static void nfs4_layoutreturn_release(void *calldata)
8943{
8944 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008945 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008946
8947 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008948 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008949 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008950 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008951 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8952 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008953 pnfs_put_layout_hdr(lrp->args.layout);
8954 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008955 kfree(calldata);
8956 dprintk("<-- %s\n", __func__);
8957}
8958
8959static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8960 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8961 .rpc_call_done = nfs4_layoutreturn_done,
8962 .rpc_release = nfs4_layoutreturn_release,
8963};
8964
Peng Tao6c166052014-11-17 09:30:40 +08008965int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008966{
8967 struct rpc_task *task;
8968 struct rpc_message msg = {
8969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8970 .rpc_argp = &lrp->args,
8971 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008972 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008973 };
8974 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04008975 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008976 .rpc_message = &msg,
8977 .callback_ops = &nfs4_layoutreturn_call_ops,
8978 .callback_data = lrp,
8979 };
Peng Tao6c166052014-11-17 09:30:40 +08008980 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008981
Andrew Elble99ade3c2015-12-02 09:39:51 -05008982 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8983 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8984 &task_setup_data.rpc_client, &msg);
8985
Benny Halevycbe82602011-05-22 19:52:37 +03008986 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008987 if (!sync) {
8988 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8989 if (!lrp->inode) {
8990 nfs4_layoutreturn_release(lrp);
8991 return -EAGAIN;
8992 }
8993 task_setup_data.flags |= RPC_TASK_ASYNC;
8994 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008995 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008996 task = rpc_run_task(&task_setup_data);
8997 if (IS_ERR(task))
8998 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008999 if (sync)
9000 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009001 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009002 dprintk("<-- %s status=%d\n", __func__, status);
9003 rpc_put_task(task);
9004 return status;
9005}
9006
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009007static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009008_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9009 struct pnfs_device *pdev,
9010 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009011{
9012 struct nfs4_getdeviceinfo_args args = {
9013 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009014 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9015 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009016 };
9017 struct nfs4_getdeviceinfo_res res = {
9018 .pdev = pdev,
9019 };
9020 struct rpc_message msg = {
9021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9022 .rpc_argp = &args,
9023 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009024 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009025 };
9026 int status;
9027
9028 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009029 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009030 if (res.notification & ~args.notify_types)
9031 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009032 if (res.notification != args.notify_types)
9033 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009034
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009035 dprintk("<-- %s status=%d\n", __func__, status);
9036
9037 return status;
9038}
9039
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009040int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9041 struct pnfs_device *pdev,
9042 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009043{
9044 struct nfs4_exception exception = { };
9045 int err;
9046
9047 do {
9048 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009049 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009050 &exception);
9051 } while (exception.retry);
9052 return err;
9053}
9054EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9055
Andy Adamson863a3c62011-03-23 13:27:54 +00009056static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9057{
9058 struct nfs4_layoutcommit_data *data = calldata;
9059 struct nfs_server *server = NFS_SERVER(data->args.inode);
9060
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009061 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009062 &data->args.seq_args,
9063 &data->res.seq_res,
9064 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009065}
9066
9067static void
9068nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9069{
9070 struct nfs4_layoutcommit_data *data = calldata;
9071 struct nfs_server *server = NFS_SERVER(data->args.inode);
9072
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009073 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009074 return;
9075
9076 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009077 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9078 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9079 case -NFS4ERR_BADLAYOUT: /* no layout */
9080 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009081 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009082 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009083 break;
9084 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009085 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009086 rpc_restart_call_prepare(task);
9087 return;
9088 }
9089 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009090}
9091
9092static void nfs4_layoutcommit_release(void *calldata)
9093{
9094 struct nfs4_layoutcommit_data *data = calldata;
9095
Andy Adamsondb29c082011-07-30 20:52:38 -04009096 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009097 nfs_post_op_update_inode_force_wcc(data->args.inode,
9098 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009099 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009100 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009101 kfree(data);
9102}
9103
9104static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9105 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9106 .rpc_call_done = nfs4_layoutcommit_done,
9107 .rpc_release = nfs4_layoutcommit_release,
9108};
9109
9110int
Andy Adamsonef311532011-03-12 02:58:10 -05009111nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009112{
9113 struct rpc_message msg = {
9114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9115 .rpc_argp = &data->args,
9116 .rpc_resp = &data->res,
9117 .rpc_cred = data->cred,
9118 };
9119 struct rpc_task_setup task_setup_data = {
9120 .task = &data->task,
9121 .rpc_client = NFS_CLIENT(data->args.inode),
9122 .rpc_message = &msg,
9123 .callback_ops = &nfs4_layoutcommit_ops,
9124 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009125 };
9126 struct rpc_task *task;
9127 int status = 0;
9128
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009129 dprintk("NFS: initiating layoutcommit call. sync %d "
9130 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009131 data->args.lastbytewritten,
9132 data->args.inode->i_ino);
9133
Trond Myklebust472e2592015-02-05 16:50:30 -05009134 if (!sync) {
9135 data->inode = nfs_igrab_and_active(data->args.inode);
9136 if (data->inode == NULL) {
9137 nfs4_layoutcommit_release(data);
9138 return -EAGAIN;
9139 }
9140 task_setup_data.flags = RPC_TASK_ASYNC;
9141 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009142 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009143 task = rpc_run_task(&task_setup_data);
9144 if (IS_ERR(task))
9145 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009146 if (sync)
9147 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009148 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009149 dprintk("%s: status %d\n", __func__, status);
9150 rpc_put_task(task);
9151 return status;
9152}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009153
Andy Adamson97431202013-08-08 10:57:56 -04009154/**
9155 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9156 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9157 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009158static int
9159_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009160 struct nfs_fsinfo *info,
9161 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009162{
9163 struct nfs41_secinfo_no_name_args args = {
9164 .style = SECINFO_STYLE_CURRENT_FH,
9165 };
9166 struct nfs4_secinfo_res res = {
9167 .flavors = flavors,
9168 };
9169 struct rpc_message msg = {
9170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9171 .rpc_argp = &args,
9172 .rpc_resp = &res,
9173 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009174 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009175 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009176 int status;
9177
9178 if (use_integrity) {
9179 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009180 cred = nfs4_get_clid_cred(server->nfs_client);
9181 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009182 }
9183
9184 dprintk("--> %s\n", __func__);
9185 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9186 &res.seq_res, 0);
9187 dprintk("<-- %s status=%d\n", __func__, status);
9188
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009189 if (cred)
9190 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009191
9192 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009193}
9194
9195static int
9196nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9197 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9198{
9199 struct nfs4_exception exception = { };
9200 int err;
9201 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009202 /* first try using integrity protection */
9203 err = -NFS4ERR_WRONGSEC;
9204
9205 /* try to use integrity protection with machine cred */
9206 if (_nfs4_is_integrity_protected(server->nfs_client))
9207 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9208 flavors, true);
9209
9210 /*
9211 * if unable to use integrity protection, or SECINFO with
9212 * integrity protection returns NFS4ERR_WRONGSEC (which is
9213 * disallowed by spec, but exists in deployed servers) use
9214 * the current filesystem's rpc_client and the user cred.
9215 */
9216 if (err == -NFS4ERR_WRONGSEC)
9217 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9218 flavors, false);
9219
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009220 switch (err) {
9221 case 0:
9222 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009223 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009224 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009225 default:
9226 err = nfs4_handle_exception(server, err, &exception);
9227 }
9228 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009229out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009230 return err;
9231}
9232
9233static int
9234nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9235 struct nfs_fsinfo *info)
9236{
9237 int err;
9238 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009239 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009240 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009241 struct nfs4_secinfo4 *secinfo;
9242 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009243
9244 page = alloc_page(GFP_KERNEL);
9245 if (!page) {
9246 err = -ENOMEM;
9247 goto out;
9248 }
9249
9250 flavors = page_address(page);
9251 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9252
9253 /*
9254 * Fall back on "guess and check" method if
9255 * the server doesn't support SECINFO_NO_NAME
9256 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009257 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009258 err = nfs4_find_root_sec(server, fhandle, info);
9259 goto out_freepage;
9260 }
9261 if (err)
9262 goto out_freepage;
9263
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009264 for (i = 0; i < flavors->num_flavors; i++) {
9265 secinfo = &flavors->flavors[i];
9266
9267 switch (secinfo->flavor) {
9268 case RPC_AUTH_NULL:
9269 case RPC_AUTH_UNIX:
9270 case RPC_AUTH_GSS:
9271 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9272 &secinfo->flavor_info);
9273 break;
9274 default:
9275 flavor = RPC_AUTH_MAXFLAVOR;
9276 break;
9277 }
9278
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009279 if (!nfs_auth_info_match(&server->auth_info, flavor))
9280 flavor = RPC_AUTH_MAXFLAVOR;
9281
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009282 if (flavor != RPC_AUTH_MAXFLAVOR) {
9283 err = nfs4_lookup_root_sec(server, fhandle,
9284 info, flavor);
9285 if (!err)
9286 break;
9287 }
9288 }
9289
9290 if (flavor == RPC_AUTH_MAXFLAVOR)
9291 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009292
9293out_freepage:
9294 put_page(page);
9295 if (err == -EACCES)
9296 return -EPERM;
9297out:
9298 return err;
9299}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009300
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009301static int _nfs41_test_stateid(struct nfs_server *server,
9302 nfs4_stateid *stateid,
9303 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009304{
9305 int status;
9306 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009307 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009308 };
9309 struct nfs41_test_stateid_res res;
9310 struct rpc_message msg = {
9311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9312 .rpc_argp = &args,
9313 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009314 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009315 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009316 struct rpc_clnt *rpc_client = server->client;
9317
9318 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9319 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009320
Chuck Lever38527b12012-07-11 16:30:23 -04009321 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009322 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009323 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009324 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009325 if (status != NFS_OK) {
9326 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009327 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009328 }
9329 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009330 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009331}
9332
Trond Myklebust43912bb2016-09-22 13:38:56 -04009333static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9334 int err, struct nfs4_exception *exception)
9335{
9336 exception->retry = 0;
9337 switch(err) {
9338 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009339 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009340 nfs4_handle_exception(server, err, exception);
9341 break;
9342 case -NFS4ERR_BADSESSION:
9343 case -NFS4ERR_BADSLOT:
9344 case -NFS4ERR_BAD_HIGH_SLOT:
9345 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9346 case -NFS4ERR_DEADSESSION:
9347 nfs4_do_handle_exception(server, err, exception);
9348 }
9349}
9350
Chuck Lever38527b12012-07-11 16:30:23 -04009351/**
9352 * nfs41_test_stateid - perform a TEST_STATEID operation
9353 *
9354 * @server: server / transport on which to perform the operation
9355 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009356 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009357 *
9358 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9359 * Otherwise a negative NFS4ERR value is returned if the operation
9360 * failed or the state ID is not currently valid.
9361 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009362static int nfs41_test_stateid(struct nfs_server *server,
9363 nfs4_stateid *stateid,
9364 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009365{
9366 struct nfs4_exception exception = { };
9367 int err;
9368 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009369 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009370 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009371 } while (exception.retry);
9372 return err;
9373}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009374
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009375struct nfs_free_stateid_data {
9376 struct nfs_server *server;
9377 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009378 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009379};
9380
9381static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9382{
9383 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009384 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009385 &data->args.seq_args,
9386 &data->res.seq_res,
9387 task);
9388}
9389
9390static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9391{
9392 struct nfs_free_stateid_data *data = calldata;
9393
9394 nfs41_sequence_done(task, &data->res.seq_res);
9395
9396 switch (task->tk_status) {
9397 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009398 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009399 rpc_restart_call_prepare(task);
9400 }
9401}
9402
9403static void nfs41_free_stateid_release(void *calldata)
9404{
9405 kfree(calldata);
9406}
9407
Trond Myklebust17f26b12013-08-21 15:48:42 -04009408static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009409 .rpc_call_prepare = nfs41_free_stateid_prepare,
9410 .rpc_call_done = nfs41_free_stateid_done,
9411 .rpc_release = nfs41_free_stateid_release,
9412};
9413
Anna Schumaker2f261022018-05-15 13:03:39 -04009414/**
9415 * nfs41_free_stateid - perform a FREE_STATEID operation
9416 *
9417 * @server: server / transport on which to perform the operation
9418 * @stateid: state ID to release
9419 * @cred: credential
9420 * @is_recovery: set to true if this call needs to be privileged
9421 *
9422 * Note: this function is always asynchronous.
9423 */
9424static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009425 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009426 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009427 bool privileged)
9428{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009429 struct rpc_message msg = {
9430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009431 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009432 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009433 struct rpc_task_setup task_setup = {
9434 .rpc_client = server->client,
9435 .rpc_message = &msg,
9436 .callback_ops = &nfs41_free_stateid_ops,
9437 .flags = RPC_TASK_ASYNC,
9438 };
9439 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009440 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009441
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009442 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9443 &task_setup.rpc_client, &msg);
9444
Chuck Lever38527b12012-07-11 16:30:23 -04009445 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009446 data = kmalloc(sizeof(*data), GFP_NOFS);
9447 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009448 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009449 data->server = server;
9450 nfs4_stateid_copy(&data->args.stateid, stateid);
9451
9452 task_setup.callback_data = data;
9453
9454 msg.rpc_argp = &data->args;
9455 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009456 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009457 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009458 if (IS_ERR(task))
9459 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009460 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009461 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009462}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009463
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009464static void
9465nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009466{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009467 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009468
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009469 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009470 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009471}
9472
Trond Myklebust36281ca2012-03-04 18:13:56 -05009473static bool nfs41_match_stateid(const nfs4_stateid *s1,
9474 const nfs4_stateid *s2)
9475{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009476 if (s1->type != s2->type)
9477 return false;
9478
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009479 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009480 return false;
9481
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009482 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009483 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009484
Anna Schumaker045c5512017-01-11 16:59:48 -05009485 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009486}
9487
Andy Adamson557134a2009-04-01 09:21:53 -04009488#endif /* CONFIG_NFS_V4_1 */
9489
Trond Myklebust36281ca2012-03-04 18:13:56 -05009490static bool nfs4_match_stateid(const nfs4_stateid *s1,
9491 const nfs4_stateid *s2)
9492{
Trond Myklebustf597c532012-03-04 18:13:56 -05009493 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009494}
9495
9496
Trond Myklebust17280172012-03-11 13:11:00 -04009497static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009498 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009499 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500 .recover_open = nfs4_open_reclaim,
9501 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009502 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009503 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504};
9505
Andy Adamson591d71c2009-04-01 09:22:47 -04009506#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009507static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009508 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9509 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9510 .recover_open = nfs4_open_reclaim,
9511 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009512 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009513 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009514 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009515};
9516#endif /* CONFIG_NFS_V4_1 */
9517
Trond Myklebust17280172012-03-11 13:11:00 -04009518static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009519 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009520 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009521 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009523 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524};
9525
Andy Adamson591d71c2009-04-01 09:22:47 -04009526#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009527static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009528 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9529 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009530 .recover_open = nfs41_open_expired,
9531 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009532 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009533};
9534#endif /* CONFIG_NFS_V4_1 */
9535
Trond Myklebust17280172012-03-11 13:11:00 -04009536static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009537 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009538 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009539 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009540};
9541
9542#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009543static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009544 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009545 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009546 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009547};
9548#endif
9549
Chuck Leverec011fe2013-10-17 14:12:39 -04009550static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009551 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009552 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009553};
9554
9555#if defined(CONFIG_NFS_V4_1)
9556static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009557 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009558 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009559};
9560#endif /* CONFIG_NFS_V4_1 */
9561
Trond Myklebust97dc1352010-06-16 09:52:26 -04009562static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9563 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009564 .init_caps = NFS_CAP_READDIRPLUS
9565 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009566 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009567 .init_client = nfs40_init_client,
9568 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009569 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009570 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009571 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009572 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009573 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009574 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009575 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9576 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9577 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009578 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009579};
9580
9581#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009582static struct nfs_seqid *
9583nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9584{
9585 return NULL;
9586}
9587
Trond Myklebust97dc1352010-06-16 09:52:26 -04009588static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9589 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009590 .init_caps = NFS_CAP_READDIRPLUS
9591 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009592 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009593 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009594 | NFS_CAP_ATOMIC_OPEN_V1
9595 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009596 .init_client = nfs41_init_client,
9597 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009598 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009599 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009600 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009601 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009602 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009603 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009604 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009605 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9606 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9607 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009608 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009609};
9610#endif
9611
Steve Dickson42c2c422013-05-22 12:50:38 -04009612#if defined(CONFIG_NFS_V4_2)
9613static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9614 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009615 .init_caps = NFS_CAP_READDIRPLUS
9616 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009617 | NFS_CAP_POSIX_LOCK
9618 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009619 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009620 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009621 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009622 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009623 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009624 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009625 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009626 | NFS_CAP_LAYOUTSTATS
9627 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009628 .init_client = nfs41_init_client,
9629 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009630 .match_stateid = nfs41_match_stateid,
9631 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009632 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009633 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009634 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009635 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009636 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009637 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9638 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9639 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009640 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009641};
9642#endif
9643
Trond Myklebust97dc1352010-06-16 09:52:26 -04009644const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9645 [0] = &nfs_v4_0_minor_ops,
9646#if defined(CONFIG_NFS_V4_1)
9647 [1] = &nfs_v4_1_minor_ops,
9648#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009649#if defined(CONFIG_NFS_V4_2)
9650 [2] = &nfs_v4_2_minor_ops,
9651#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009652};
9653
Trond Myklebust13997822016-07-24 17:10:52 -04009654static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009655{
9656 ssize_t error, error2;
9657
9658 error = generic_listxattr(dentry, list, size);
9659 if (error < 0)
9660 return error;
9661 if (list) {
9662 list += error;
9663 size -= error;
9664 }
9665
9666 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9667 if (error2 < 0)
9668 return error2;
9669 return error + error2;
9670}
9671
Trond Myklebust17f26b12013-08-21 15:48:42 -04009672static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009673 .create = nfs_create,
9674 .lookup = nfs_lookup,
9675 .atomic_open = nfs_atomic_open,
9676 .link = nfs_link,
9677 .unlink = nfs_unlink,
9678 .symlink = nfs_symlink,
9679 .mkdir = nfs_mkdir,
9680 .rmdir = nfs_rmdir,
9681 .mknod = nfs_mknod,
9682 .rename = nfs_rename,
9683 .permission = nfs_permission,
9684 .getattr = nfs_getattr,
9685 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009686 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009687};
9688
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009689static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009690 .permission = nfs_permission,
9691 .getattr = nfs_getattr,
9692 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009693 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009694};
9695
David Howells509de812006-08-22 20:06:11 -04009696const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697 .version = 4, /* protocol version */
9698 .dentry_ops = &nfs4_dentry_operations,
9699 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009700 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009701 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009703 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009704 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 .getattr = nfs4_proc_getattr,
9706 .setattr = nfs4_proc_setattr,
9707 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009708 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 .access = nfs4_proc_access,
9710 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 .create = nfs4_proc_create,
9712 .remove = nfs4_proc_remove,
9713 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009714 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009716 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009717 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009718 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 .link = nfs4_proc_link,
9720 .symlink = nfs4_proc_symlink,
9721 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009722 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723 .readdir = nfs4_proc_readdir,
9724 .mknod = nfs4_proc_mknod,
9725 .statfs = nfs4_proc_statfs,
9726 .fsinfo = nfs4_proc_fsinfo,
9727 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009728 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009730 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009732 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009734 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009736 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009737 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009739 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009740 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009741 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009742 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009743 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009744 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009745 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009746 .create_server = nfs4_create_server,
9747 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748};
9749
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009750static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009751 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009752 .list = nfs4_xattr_list_nfs4_acl,
9753 .get = nfs4_xattr_get_nfs4_acl,
9754 .set = nfs4_xattr_set_nfs4_acl,
9755};
9756
9757const struct xattr_handler *nfs4_xattr_handlers[] = {
9758 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009759#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9760 &nfs4_xattr_nfs4_label_handler,
9761#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009762 NULL
9763};
9764
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765/*
9766 * Local variables:
9767 * c-basic-offset: 8
9768 * End:
9769 */