blob: e39f59b5e6d2738990baf7339316f728c3141853 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040096static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 struct rpc_cred *cred,
103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400110
111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
112static inline struct nfs4_label *
113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115{
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
David Quigleyaa9c2662013-05-22 12:50:44 -0400124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130}
131static inline void
132nfs4_label_release_security(struct nfs4_label *label)
133{
134 if (label)
135 security_release_secctx(label->label, label->len);
136}
137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143}
144#else
145static inline struct nfs4_label *
146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148{ return NULL; }
149static inline void
150nfs4_label_release_security(struct nfs4_label *label)
151{ return; }
152static inline u32 *
153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154{ return server->attr_bitmask; }
155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500158static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400168 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700181 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400184 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This is our standard bitmap for GETATTR requests.
189 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400190const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Trond Myklebust1549210f2012-06-05 09:16:47 -0400211static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400230};
231
Andy Adamsone23008e2012-10-02 21:07:32 -0400232static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400234 | FATTR4_WORD0_FILEID,
235};
236
David Quigleya09df2c2013-05-22 12:50:41 -0400237const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250};
251
Fred Isamandae100c2011-07-30 20:52:37 -0400252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700256 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800259 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
David Quigleya09df2c2013-05-22 12:50:41 -0400262const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500263 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500268 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400276};
277
Trond Myklebust30846df2018-04-07 13:44:28 -0400278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280{
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297}
298
299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301{
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303}
304
Al Virobc4785c2006-10-19 23:28:51 -0700305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct nfs4_readdir_arg *readdir)
307{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700309 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000312 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322 /*
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
328 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800359 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Trond Myklebust26d36302016-09-22 13:39:05 -0400362static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 struct rpc_cred *cred)
365{
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369}
370
371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 struct rpc_cred *cred)
374{
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377}
378
379static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 struct rpc_cred *cred)
382{
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387}
388
NeilBrown8478eaa2014-09-18 16:09:27 +1000389static long nfs4_update_delay(long *timeout)
390{
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401}
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
404{
405 int res = 0;
406
407 might_sleep();
408
NeilBrown8478eaa2014-09-18 16:09:27 +1000409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 if (fatal_signal_pending(current))
412 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 return res;
414}
415
416/* This is the error handling routine for processes that are allowed
417 * to sleep.
418 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400419static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500421{
422 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500423 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400424 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500426 int ret = errorcode;
427
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400428 exception->delay = 0;
429 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500430 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400431
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
434
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 switch(errorcode) {
436 case 0:
437 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400438 case -NFS4ERR_BADHANDLE:
439 case -ESTALE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
442 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400445 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400446 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
449 stateid);
450 goto wait_on_recovery;
451 }
452 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400453 if (inode) {
454 int err;
455
456 err = nfs_async_inode_return_delegation(inode,
457 stateid);
458 if (err == 0)
459 goto wait_on_recovery;
460 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
461 exception->retry = 1;
462 break;
463 }
464 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500465 if (state == NULL)
466 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400467 ret = nfs4_schedule_stateid_recovery(server, state);
468 if (ret < 0)
469 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500470 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500472 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500473 nfs4_schedule_lease_recovery(clp);
474 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400475 case -NFS4ERR_MOVED:
476 ret = nfs4_schedule_migration_recovery(server);
477 if (ret < 0)
478 break;
479 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400480 case -NFS4ERR_LEASE_MOVED:
481 nfs4_schedule_lease_moved_recovery(clp);
482 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500483#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400484 case -NFS4ERR_BADSESSION:
485 case -NFS4ERR_BADSLOT:
486 case -NFS4ERR_BAD_HIGH_SLOT:
487 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
488 case -NFS4ERR_DEADSESSION:
489 case -NFS4ERR_SEQ_FALSE_RETRY:
490 case -NFS4ERR_SEQ_MISORDERED:
491 dprintk("%s ERROR: %d Reset session\n", __func__,
492 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400493 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400494 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500495#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500496 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500497 if (exception->timeout > HZ) {
498 /* We have retried a decent amount, time to
499 * fail
500 */
501 ret = -EBUSY;
502 break;
503 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400505 nfs_inc_server_stats(server, NFSIOS_DELAY);
506 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400507 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400508 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400509 exception->delay = 1;
510 return 0;
511
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400512 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500513 case -NFS4ERR_OLD_STATEID:
514 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800515 break;
516 case -NFS4ERR_BADOWNER:
517 /* The following works around a Linux server bug! */
518 case -NFS4ERR_BADNAME:
519 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
520 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
521 exception->retry = 1;
522 printk(KERN_WARNING "NFS: v4 server %s "
523 "does not accept raw "
524 "uid/gids. "
525 "Reenabling the idmapper.\n",
526 server->nfs_client->cl_hostname);
527 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500528 }
529 /* We failed to handle the error */
530 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500531wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400532 exception->recovering = 1;
533 return 0;
534}
535
536/* This is the error handling routine for processes that are allowed
537 * to sleep.
538 */
539int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
540{
541 struct nfs_client *clp = server->nfs_client;
542 int ret;
543
544 ret = nfs4_do_handle_exception(server, errorcode, exception);
545 if (exception->delay) {
546 ret = nfs4_delay(server->client, &exception->timeout);
547 goto out_retry;
548 }
549 if (exception->recovering) {
550 ret = nfs4_wait_clnt_recover(clp);
551 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
552 return -EIO;
553 goto out_retry;
554 }
555 return ret;
556out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500557 if (ret == 0)
558 exception->retry = 1;
559 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500560}
561
Trond Myklebust037fc982015-09-20 15:51:00 -0400562static int
563nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
564 int errorcode, struct nfs4_exception *exception)
565{
566 struct nfs_client *clp = server->nfs_client;
567 int ret;
568
569 ret = nfs4_do_handle_exception(server, errorcode, exception);
570 if (exception->delay) {
571 rpc_delay(task, nfs4_update_delay(&exception->timeout));
572 goto out_retry;
573 }
574 if (exception->recovering) {
575 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
576 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
577 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
578 goto out_retry;
579 }
580 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
581 ret = -EIO;
582 return ret;
583out_retry:
584 if (ret == 0)
585 exception->retry = 1;
586 return ret;
587}
588
589static int
590nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
591 struct nfs4_state *state, long *timeout)
592{
593 struct nfs4_exception exception = {
594 .state = state,
595 };
596
597 if (task->tk_status >= 0)
598 return 0;
599 if (timeout)
600 exception.timeout = *timeout;
601 task->tk_status = nfs4_async_handle_exception(task, server,
602 task->tk_status,
603 &exception);
604 if (exception.delay && timeout)
605 *timeout = exception.timeout;
606 if (exception.retry)
607 return -EAGAIN;
608 return 0;
609}
610
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400611/*
612 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
613 * or 'false' otherwise.
614 */
615static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
616{
617 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500618 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400619}
Trond Myklebust65de8722008-12-23 15:21:44 -0500620
Trond Myklebust452e9352010-07-31 14:29:06 -0400621static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 spin_lock(&clp->cl_lock);
624 if (time_before(clp->cl_last_renewal,timestamp))
625 clp->cl_last_renewal = timestamp;
626 spin_unlock(&clp->cl_lock);
627}
628
Trond Myklebust452e9352010-07-31 14:29:06 -0400629static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
630{
Trond Myklebustbe824162015-07-05 14:50:46 -0400631 struct nfs_client *clp = server->nfs_client;
632
633 if (!nfs4_has_session(clp))
634 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400635}
636
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400637struct nfs4_call_sync_data {
638 const struct nfs_server *seq_server;
639 struct nfs4_sequence_args *seq_args;
640 struct nfs4_sequence_res *seq_res;
641};
642
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800643void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400644 struct nfs4_sequence_res *res, int cache_reply,
645 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400646{
647 args->sa_slot = NULL;
648 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400649 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400650
651 res->sr_slot = NULL;
652}
653
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400654static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400655{
656 struct nfs4_slot *slot = res->sr_slot;
657 struct nfs4_slot_table *tbl;
658
Chuck Lever3bd23842013-08-09 12:49:19 -0400659 tbl = slot->table;
660 spin_lock(&tbl->slot_tbl_lock);
661 if (!nfs41_wake_and_assign_slot(tbl, slot))
662 nfs4_free_slot(tbl, slot);
663 spin_unlock(&tbl->slot_tbl_lock);
664
665 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400666}
667
668static int nfs40_sequence_done(struct rpc_task *task,
669 struct nfs4_sequence_res *res)
670{
671 if (res->sr_slot != NULL)
672 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400673 return 1;
674}
675
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400676#if defined(CONFIG_NFS_V4_1)
677
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400678static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400679{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500680 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400681 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500682 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400683
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400684 if (!slot)
685 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500686 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500687 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500688
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400689 /* Bump the slot sequence number */
690 if (slot->seq_done)
691 slot->seq_nr++;
692 slot->seq_done = 0;
693
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500694 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500695 /* Be nice to the server: try to ensure that the last transmitted
696 * value for highest_user_slotid <= target_highest_slotid
697 */
698 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
699 send_new_highest_used_slotid = true;
700
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500701 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500702 send_new_highest_used_slotid = false;
703 goto out_unlock;
704 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500706
707 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
708 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500709out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500710 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500711 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400712 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400713 if (waitqueue_active(&tbl->slot_waitq))
714 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400715}
716
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400717static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
718{
719 nfs41_release_slot(res->sr_slot);
720 res->sr_slot = NULL;
721}
722
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400723static int nfs41_sequence_process(struct rpc_task *task,
724 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400725{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500726 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500727 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500730 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400731
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500732 if (slot == NULL)
733 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400734 /* don't increment the sequence number if the task wasn't sent */
735 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400736 goto out;
737
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500739
Trond Myklebustac20d162012-12-15 15:36:07 -0500740 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400741 if (res->sr_status != -NFS4ERR_DELAY)
742 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500743 interrupted = true;
744 }
745
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400746 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500747 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400748 switch (res->sr_status) {
749 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400750 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400751 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500752 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500753 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500754 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400755 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
756 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500757 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500759 case 1:
760 /*
761 * sr_status remains 1 if an RPC level error occurred.
762 * The server may or may not have processed the sequence
763 * operation..
764 * Mark the slot as having hosted an interrupted RPC call.
765 */
766 slot->interrupted = 1;
767 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 case -NFS4ERR_DELAY:
769 /* The server detected a resend of the RPC call and
770 * returned NFS4ERR_DELAY as per Section 2.10.6.2
771 * of RFC5661.
772 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500773 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400774 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500775 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500776 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500778 case -NFS4ERR_BADSLOT:
779 /*
780 * The slot id we used was probably retired. Try again
781 * using a different slot id.
782 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400783 if (slot->seq_nr < slot->table->target_highest_slotid)
784 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500785 goto retry_nowait;
786 case -NFS4ERR_SEQ_MISORDERED:
787 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500788 * Was the last operation on this sequence interrupted?
789 * If so, retry after bumping the sequence number.
790 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400791 if (interrupted)
792 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500793 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 * Could this slot have been previously retired?
795 * If so, then the server may be expecting seq_nr = 1!
796 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500797 if (slot->seq_nr != 1) {
798 slot->seq_nr = 1;
799 goto retry_nowait;
800 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400801 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400803 if (interrupted)
804 goto retry_new_seq;
805 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400806 default:
807 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400808 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400809 }
810out:
811 /* The session may be reset by one of the error handlers. */
812 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500813out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500814 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400815session_recover:
816 nfs4_schedule_session_recovery(session, res->sr_status);
817 goto retry_nowait;
818retry_new_seq:
819 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820retry_nowait:
821 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400822 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500823 task->tk_status = 0;
824 ret = 0;
825 }
826 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400827out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400828 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 goto out;
830 rpc_delay(task, NFS4_POLL_RETRY_MAX);
831 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400832}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400833
834int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
835{
836 if (!nfs41_sequence_process(task, res))
837 return 0;
838 if (res->sr_slot != NULL)
839 nfs41_sequence_free_slot(res);
840 return 1;
841
842}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500843EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
846{
847 if (res->sr_slot == NULL)
848 return 1;
849 if (res->sr_slot->table->session != NULL)
850 return nfs41_sequence_process(task, res);
851 return nfs40_sequence_done(task, res);
852}
853
854static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
855{
856 if (res->sr_slot != NULL) {
857 if (res->sr_slot->table->session != NULL)
858 nfs41_sequence_free_slot(res);
859 else
860 nfs40_sequence_free_slot(res);
861 }
862}
863
Peng Tao2c4b1312014-06-11 05:24:16 +0800864int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400865{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500866 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400867 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400868 if (!res->sr_slot->table->session)
869 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400870 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400871}
Peng Tao2c4b1312014-06-11 05:24:16 +0800872EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873
Andy Adamsonce5039c2009-04-01 09:22:13 -0400874static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
875{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400876 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877
Trond Myklebust035168ab2010-06-16 09:52:26 -0400878 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
879
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500880 nfs4_setup_sequence(data->seq_server->nfs_client,
881 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400882}
883
Andy Adamson69ab40c2009-04-01 09:22:19 -0400884static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
885{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400886 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400887
Trond Myklebust14516c32010-07-31 14:29:06 -0400888 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400889}
890
Trond Myklebust17280172012-03-11 13:11:00 -0400891static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400892 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400893 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894};
895
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400896static void
897nfs4_sequence_process_interrupted(struct nfs_client *client,
898 struct nfs4_slot *slot, struct rpc_cred *cred)
899{
900 struct rpc_task *task;
901
902 task = _nfs41_proc_sequence(client, cred, slot, true);
903 if (!IS_ERR(task))
904 rpc_put_task_async(task);
905}
906
Chuck Lever3bd23842013-08-09 12:49:19 -0400907#else /* !CONFIG_NFS_V4_1 */
908
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400909static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
910{
911 return nfs40_sequence_done(task, res);
912}
913
914static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
915{
916 if (res->sr_slot != NULL)
917 nfs40_sequence_free_slot(res);
918}
919
Peng Tao2c4b1312014-06-11 05:24:16 +0800920int nfs4_sequence_done(struct rpc_task *task,
921 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400922{
Chuck Lever3bd23842013-08-09 12:49:19 -0400923 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400924}
Peng Tao2c4b1312014-06-11 05:24:16 +0800925EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400926
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400927static void
928nfs4_sequence_process_interrupted(struct nfs_client *client,
929 struct nfs4_slot *slot, struct rpc_cred *cred)
930{
931 WARN_ON_ONCE(1);
932 slot->interrupted = 0;
933}
934
Chuck Lever3bd23842013-08-09 12:49:19 -0400935#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400936
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400937static
938void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
939 struct nfs4_sequence_res *res,
940 struct nfs4_slot *slot)
941{
942 if (!slot)
943 return;
944 slot->privileged = args->sa_privileged ? 1 : 0;
945 args->sa_slot = slot;
946
947 res->sr_slot = slot;
948 res->sr_timestamp = jiffies;
949 res->sr_status_flags = 0;
950 res->sr_status = 1;
951
952}
953
954int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500955 struct nfs4_sequence_args *args,
956 struct nfs4_sequence_res *res,
957 struct rpc_task *task)
958{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500959 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500960 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500961 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500962
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500963 /* slot already allocated? */
964 if (res->sr_slot != NULL)
965 goto out_start;
966
Anna Schumaker76ee0352017-01-10 16:49:31 -0500967 if (session) {
968 tbl = &session->fc_slot_table;
969 task->tk_timeout = 0;
970 }
971
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400972 for (;;) {
973 spin_lock(&tbl->slot_tbl_lock);
974 /* The state manager will wait until the slot table is empty */
975 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
976 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500977
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400978 slot = nfs4_alloc_slot(tbl);
979 if (IS_ERR(slot)) {
980 /* Try again in 1/4 second */
981 if (slot == ERR_PTR(-ENOMEM))
982 task->tk_timeout = HZ >> 2;
983 goto out_sleep;
984 }
985 spin_unlock(&tbl->slot_tbl_lock);
986
987 if (likely(!slot->interrupted))
988 break;
989 nfs4_sequence_process_interrupted(client,
990 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500991 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500992
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400993 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500994
Anna Schumakerad05cc02017-01-11 13:37:06 -0500995 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500996out_start:
997 rpc_call_start(task);
998 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500999
1000out_sleep:
1001 if (args->sa_privileged)
1002 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1003 NULL, RPC_PRIORITY_PRIVILEGED);
1004 else
1005 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1006 spin_unlock(&tbl->slot_tbl_lock);
1007 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001008}
1009EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1010
Chuck Lever9915ea72013-08-09 12:48:27 -04001011static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1012{
1013 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001014 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001015 data->seq_args, data->seq_res, task);
1016}
1017
1018static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1019{
1020 struct nfs4_call_sync_data *data = calldata;
1021 nfs4_sequence_done(task, data->seq_res);
1022}
1023
1024static const struct rpc_call_ops nfs40_call_sync_ops = {
1025 .rpc_call_prepare = nfs40_call_sync_prepare,
1026 .rpc_call_done = nfs40_call_sync_done,
1027};
1028
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001029static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001030 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 struct nfs4_sequence_args *args,
1033 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001034{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 int ret;
1036 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001037 struct nfs_client *clp = server->nfs_client;
1038 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001039 .seq_server = server,
1040 .seq_args = args,
1041 .seq_res = res,
1042 };
1043 struct rpc_task_setup task_setup = {
1044 .rpc_client = clnt,
1045 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001046 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 .callback_data = &data
1048 };
1049
1050 task = rpc_run_task(&task_setup);
1051 if (IS_ERR(task))
1052 ret = PTR_ERR(task);
1053 else {
1054 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001055 rpc_put_task(task);
1056 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001057 return ret;
1058}
1059
Bryan Schumaker7c513052011-03-24 17:12:24 +00001060int nfs4_call_sync(struct rpc_clnt *clnt,
1061 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001062 struct rpc_message *msg,
1063 struct nfs4_sequence_args *args,
1064 struct nfs4_sequence_res *res,
1065 int cache_reply)
1066{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001067 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001068 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001071static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1072 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct nfs_inode *nfsi = NFS_I(dir);
1075
1076 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001077 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1078 | NFS_INO_INVALID_MTIME
1079 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001080 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001081 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1082 nfsi->attrtimeo_timestamp = jiffies;
1083 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001085 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001086 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1087 NFS_INO_INVALID_ACL;
1088 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001089 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001090 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001091 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001092 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 spin_unlock(&dir->i_lock);
1094}
1095
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001096struct nfs4_open_createattrs {
1097 struct nfs4_label *label;
1098 struct iattr *sattr;
1099 const __u32 verf[2];
1100};
1101
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001102static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1103 int err, struct nfs4_exception *exception)
1104{
1105 if (err != -EINVAL)
1106 return false;
1107 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1108 return false;
1109 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1110 exception->retry = 1;
1111 return true;
1112}
1113
Trond Myklebust6ae37332015-01-30 14:21:14 -05001114static u32
1115nfs4_map_atomic_open_share(struct nfs_server *server,
1116 fmode_t fmode, int openflags)
1117{
1118 u32 res = 0;
1119
1120 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1121 case FMODE_READ:
1122 res = NFS4_SHARE_ACCESS_READ;
1123 break;
1124 case FMODE_WRITE:
1125 res = NFS4_SHARE_ACCESS_WRITE;
1126 break;
1127 case FMODE_READ|FMODE_WRITE:
1128 res = NFS4_SHARE_ACCESS_BOTH;
1129 }
1130 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1131 goto out;
1132 /* Want no delegation if we're using O_DIRECT */
1133 if (openflags & O_DIRECT)
1134 res |= NFS4_SHARE_WANT_NO_DELEG;
1135out:
1136 return res;
1137}
1138
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001139static enum open_claim_type4
1140nfs4_map_atomic_open_claim(struct nfs_server *server,
1141 enum open_claim_type4 claim)
1142{
1143 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1144 return claim;
1145 switch (claim) {
1146 default:
1147 return claim;
1148 case NFS4_OPEN_CLAIM_FH:
1149 return NFS4_OPEN_CLAIM_NULL;
1150 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1151 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1152 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1153 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1154 }
1155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001158{
1159 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001160 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 p->o_res.seqid = p->o_arg.seqid;
1162 p->c_res.seqid = p->c_arg.seqid;
1163 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001164 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001166 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001167}
1168
Al Viro82a2c1b2011-06-22 18:30:55 -04001169static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001171 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001172 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001173 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174{
Al Viro82a2c1b2011-06-22 18:30:55 -04001175 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001176 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001178 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001179 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 struct nfs4_opendata *p;
1181
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001182 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 if (p == NULL)
1184 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001185
1186 p->f_label = nfs4_label_alloc(server, gfp_mask);
1187 if (IS_ERR(p->f_label))
1188 goto err_free_p;
1189
Kinglong Meea49c2692015-07-27 15:31:38 +08001190 p->a_label = nfs4_label_alloc(server, gfp_mask);
1191 if (IS_ERR(p->a_label))
1192 goto err_free_f;
1193
Trond Myklebust63f5f792015-01-23 19:19:25 -05001194 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1195 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001196 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001197 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001198 nfs_sb_active(dentry->d_sb);
1199 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 p->dir = parent;
1201 p->owner = sp;
1202 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203 p->o_arg.open_flags = flags;
1204 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001205 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001206 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001207 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1208 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001209 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1210 * will return permission denied for all bits until close */
1211 if (!(flags & O_EXCL)) {
1212 /* ask server to check for all possible rights as results
1213 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001214 switch (p->o_arg.claim) {
1215 default:
1216 break;
1217 case NFS4_OPEN_CLAIM_NULL:
1218 case NFS4_OPEN_CLAIM_FH:
1219 p->o_arg.access = NFS4_ACCESS_READ |
1220 NFS4_ACCESS_MODIFY |
1221 NFS4_ACCESS_EXTEND |
1222 NFS4_ACCESS_EXECUTE;
1223 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001224 }
David Howells7539bba2006-08-22 20:06:09 -04001225 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001226 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1227 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001228 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001229 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001230 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001231 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001232 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001233 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001234 case NFS4_OPEN_CLAIM_NULL:
1235 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1236 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1237 p->o_arg.fh = NFS_FH(dir);
1238 break;
1239 case NFS4_OPEN_CLAIM_PREVIOUS:
1240 case NFS4_OPEN_CLAIM_FH:
1241 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1242 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001243 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001244 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001245 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001247 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001248
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001249 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001250 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001252 p->c_arg.fh = &p->o_res.fh;
1253 p->c_arg.stateid = &p->o_res.stateid;
1254 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001255 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001256 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001258
1259err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001260 nfs4_label_free(p->a_label);
1261err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001262 nfs4_label_free(p->f_label);
1263err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264 kfree(p);
1265err:
1266 dput(parent);
1267 return NULL;
1268}
1269
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 struct nfs4_opendata *p = container_of(kref,
1273 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001274 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275
Fred Isaman30ae2412016-10-18 13:39:51 -04001276 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001278 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001279 if (p->state != NULL)
1280 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001282
Kinglong Meea49c2692015-07-27 15:31:38 +08001283 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001284 nfs4_label_free(p->f_label);
1285
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001286 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001287 dput(p->dentry);
1288 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001289 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001290 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291 kfree(p);
1292}
1293
1294static void nfs4_opendata_put(struct nfs4_opendata *p)
1295{
1296 if (p != NULL)
1297 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001298}
1299
Trond Myklebust24311f82015-09-20 10:50:17 -04001300static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1301 fmode_t fmode)
1302{
1303 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1304 case FMODE_READ|FMODE_WRITE:
1305 return state->n_rdwr != 0;
1306 case FMODE_WRITE:
1307 return state->n_wronly != 0;
1308 case FMODE_READ:
1309 return state->n_rdonly != 0;
1310 }
1311 WARN_ON_ONCE(1);
1312 return false;
1313}
1314
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316{
1317 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318
Trond Myklebust536e43d2012-01-17 22:04:26 -05001319 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001320 goto out;
1321 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001323 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1324 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 break;
1326 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001327 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1328 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001329 break;
1330 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001331 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1332 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001333 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001335 return ret;
1336}
1337
Trond Myklebust2a606182015-08-19 22:30:00 -05001338static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1339 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001340{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001341 if (delegation == NULL)
1342 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001343 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001344 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001345 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1346 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001347 switch (claim) {
1348 case NFS4_OPEN_CLAIM_NULL:
1349 case NFS4_OPEN_CLAIM_FH:
1350 break;
1351 case NFS4_OPEN_CLAIM_PREVIOUS:
1352 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1353 break;
1354 default:
1355 return 0;
1356 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001357 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 return 1;
1359}
1360
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001361static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001362{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001363 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001364 case FMODE_WRITE:
1365 state->n_wronly++;
1366 break;
1367 case FMODE_READ:
1368 state->n_rdonly++;
1369 break;
1370 case FMODE_READ|FMODE_WRITE:
1371 state->n_rdwr++;
1372 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001373 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001374}
1375
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001376#ifdef CONFIG_NFS_V4_1
1377static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1378{
1379 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1380 return true;
1381 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1382 return true;
1383 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1384 return true;
1385 return false;
1386}
1387#endif /* CONFIG_NFS_V4_1 */
1388
Trond Myklebustc9399f22017-11-06 15:28:01 -05001389static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1390{
1391 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1392 wake_up_all(&state->waitq);
1393}
1394
1395static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1396 const nfs4_stateid *stateid)
1397{
1398 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1399 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1400
1401 if (stateid_seqid == state_seqid + 1U ||
1402 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1403 nfs_state_log_update_open_stateid(state);
1404 else
1405 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1406}
1407
Trond Myklebust4f14c192014-02-12 19:15:06 -05001408static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001409{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001410 struct nfs_client *clp = state->owner->so_server->nfs_client;
1411 bool need_recover = false;
1412
1413 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1414 need_recover = true;
1415 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1416 need_recover = true;
1417 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1418 need_recover = true;
1419 if (need_recover)
1420 nfs4_state_mark_reclaim_nograce(clp, state);
1421}
1422
Trond Myklebustc9399f22017-11-06 15:28:01 -05001423/*
1424 * Check for whether or not the caller may update the open stateid
1425 * to the value passed in by stateid.
1426 *
1427 * Note: This function relies heavily on the server implementing
1428 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1429 * correctly.
1430 * i.e. The stateid seqids have to be initialised to 1, and
1431 * are then incremented on every state transition.
1432 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001433static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001434 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001435{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001436 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1437 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1438 if (stateid->seqid == cpu_to_be32(1))
1439 nfs_state_log_update_open_stateid(state);
1440 else
1441 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001442 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001443 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001444
1445 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1446 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001447 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001448 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001449 return false;
1450}
1451
Trond Myklebustf95549c2015-01-23 18:06:09 -05001452static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1453{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001454 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1455 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001456 if (state->n_wronly)
1457 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1458 if (state->n_rdonly)
1459 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 if (state->n_rdwr)
1461 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001462 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001463}
1464
Trond Myklebust226056c2014-02-11 10:41:07 -05001465static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1466 nfs4_stateid *stateid, fmode_t fmode)
1467{
1468 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1469 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1470 case FMODE_WRITE:
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1472 break;
1473 case FMODE_READ:
1474 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1475 break;
1476 case 0:
1477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1479 clear_bit(NFS_OPEN_STATE, &state->flags);
1480 }
1481 if (stateid == NULL)
1482 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001483 /* Handle OPEN+OPEN_DOWNGRADE races */
1484 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1485 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001486 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001487 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001488 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001489 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001490 nfs4_stateid_copy(&state->stateid, stateid);
1491 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001492 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001493out:
1494 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001495}
1496
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001497static void nfs_clear_open_stateid(struct nfs4_state *state,
1498 nfs4_stateid *arg_stateid,
1499 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001500{
1501 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001502 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1503 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1504 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001505 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001506 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1507 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001508}
1509
Trond Myklebust1393d962016-09-22 13:39:13 -04001510static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001511 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001512{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001513 DEFINE_WAIT(wait);
1514 int status = 0;
1515 for (;;) {
1516
1517 if (!nfs_need_update_open_stateid(state, stateid))
1518 return;
1519 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001520 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001521 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001522 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001523 /* Rely on seqids for serialisation with NFSv4.0 */
1524 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1525 break;
1526
1527 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1528 /*
1529 * Ensure we process the state changes in the same order
1530 * in which the server processed them by delaying the
1531 * update of the stateid until we are in sequence.
1532 */
1533 write_sequnlock(&state->seqlock);
1534 spin_unlock(&state->owner->so_lock);
1535 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001536 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001537 if (!signal_pending(current)) {
1538 if (schedule_timeout(5*HZ) == 0)
1539 status = -EAGAIN;
1540 else
1541 status = 0;
1542 } else
1543 status = -EINTR;
1544 finish_wait(&state->waitq, &wait);
1545 rcu_read_lock();
1546 spin_lock(&state->owner->so_lock);
1547 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001548 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001549
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001550 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1551 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001552 nfs4_stateid_copy(freeme, &state->open_stateid);
1553 nfs_test_and_clear_all_open_stateid(state);
1554 }
1555
Trond Myklebuste999e802014-02-10 18:20:47 -05001556 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1557 nfs4_stateid_copy(&state->stateid, stateid);
1558 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001559 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001560 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001561}
1562
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001564 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001565 fmode_t fmode,
1566 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001568 /*
1569 * Protect the call to nfs4_state_set_mode_locked and
1570 * serialise the stateid update
1571 */
1572 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001573 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1574 switch (fmode) {
1575 case FMODE_READ:
1576 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1577 break;
1578 case FMODE_WRITE:
1579 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1580 break;
1581 case FMODE_READ|FMODE_WRITE:
1582 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001583 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001584 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001585 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001586}
1587
1588static void nfs_state_set_delegation(struct nfs4_state *state,
1589 const nfs4_stateid *deleg_stateid,
1590 fmode_t fmode)
1591{
1592 /*
1593 * Protect the call to nfs4_state_set_mode_locked and
1594 * serialise the stateid update
1595 */
1596 write_seqlock(&state->seqlock);
1597 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1598 set_bit(NFS_DELEGATED_STATE, &state->flags);
1599 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Trond Myklebust1393d962016-09-22 13:39:13 -04001602static int update_open_stateid(struct nfs4_state *state,
1603 const nfs4_stateid *open_stateid,
1604 const nfs4_stateid *delegation,
1605 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001606{
Trond Myklebust1393d962016-09-22 13:39:13 -04001607 struct nfs_server *server = NFS_SERVER(state->inode);
1608 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001609 struct nfs_inode *nfsi = NFS_I(state->inode);
1610 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001611 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001612 int ret = 0;
1613
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001614 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001615
1616 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001617 spin_lock(&state->owner->so_lock);
1618 if (open_stateid != NULL) {
1619 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1620 ret = 1;
1621 }
1622
Trond Myklebust34310432008-12-23 15:21:38 -05001623 deleg_cur = rcu_dereference(nfsi->delegation);
1624 if (deleg_cur == NULL)
1625 goto no_delegation;
1626
1627 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001628 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001629 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001630 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001631 goto no_delegation_unlock;
1632
1633 if (delegation == NULL)
1634 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001635 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001636 goto no_delegation_unlock;
1637
Trond Myklebustb7391f42008-12-23 15:21:52 -05001638 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001639 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001640 ret = 1;
1641no_delegation_unlock:
1642 spin_unlock(&deleg_cur->lock);
1643no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001644 if (ret)
1645 update_open_stateflags(state, fmode);
1646 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001647 rcu_read_unlock();
1648
Trond Myklebust4f14c192014-02-12 19:15:06 -05001649 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001650 nfs4_schedule_state_manager(clp);
1651 if (freeme.type != 0)
1652 nfs4_test_and_free_stateid(server, &freeme,
1653 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001654
1655 return ret;
1656}
1657
Trond Myklebust39071e62015-01-24 15:07:56 -05001658static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1659 const nfs4_stateid *stateid)
1660{
1661 struct nfs4_state *state = lsp->ls_state;
1662 bool ret = false;
1663
1664 spin_lock(&state->state_lock);
1665 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1666 goto out_noupdate;
1667 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1668 goto out_noupdate;
1669 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1670 ret = true;
1671out_noupdate:
1672 spin_unlock(&state->state_lock);
1673 return ret;
1674}
Trond Myklebust34310432008-12-23 15:21:38 -05001675
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001676static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001677{
1678 struct nfs_delegation *delegation;
1679
Trond Myklebustf5086242018-03-20 16:43:13 -04001680 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681 rcu_read_lock();
1682 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001683 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684 rcu_read_unlock();
1685 return;
1686 }
1687 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001688 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689}
1690
Trond Myklebust6ee41262007-07-08 14:11:36 -04001691static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692{
1693 struct nfs4_state *state = opendata->state;
1694 struct nfs_inode *nfsi = NFS_I(state->inode);
1695 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001696 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001697 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001698 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001699 nfs4_stateid stateid;
1700 int ret = -EAGAIN;
1701
Trond Myklebustaac00a82007-07-05 19:02:21 -04001702 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001703 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001704 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001705 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001706 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001707 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001708 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001709 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001710 rcu_read_lock();
1711 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001712 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001713 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001714 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001715 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001716 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001717 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001718 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001719 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001720 if (!opendata->is_recover) {
1721 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1722 if (ret != 0)
1723 goto out;
1724 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001725 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001726
1727 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001728 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001729 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001731out:
1732 return ERR_PTR(ret);
1733out_return_state:
1734 atomic_inc(&state->count);
1735 return state;
1736}
1737
Andy Adamsone23008e2012-10-02 21:07:32 -04001738static void
1739nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1740{
1741 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1742 struct nfs_delegation *delegation;
1743 int delegation_flags = 0;
1744
1745 rcu_read_lock();
1746 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1747 if (delegation)
1748 delegation_flags = delegation->flags;
1749 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001750 switch (data->o_arg.claim) {
1751 default:
1752 break;
1753 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1754 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001755 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1756 "returning a delegation for "
1757 "OPEN(CLAIM_DELEGATE_CUR)\n",
1758 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001759 return;
1760 }
1761 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001762 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001763 data->owner->so_cred,
1764 data->o_res.delegation_type,
1765 &data->o_res.delegation,
1766 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001767 else
1768 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001769 data->owner->so_cred,
1770 data->o_res.delegation_type,
1771 &data->o_res.delegation,
1772 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001773}
1774
1775/*
1776 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1777 * and update the nfs4_state.
1778 */
1779static struct nfs4_state *
1780_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1781{
1782 struct inode *inode = data->state->inode;
1783 struct nfs4_state *state = data->state;
1784 int ret;
1785
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001786 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001787 if (data->rpc_status)
1788 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001789 /* cached opens have already been processed */
1790 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001791 }
1792
Andy Adamsone23008e2012-10-02 21:07:32 -04001793 ret = nfs_refresh_inode(inode, &data->f_attr);
1794 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001795 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001796
1797 if (data->o_res.delegation_type != 0)
1798 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001799update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001800 update_open_stateid(state, &data->o_res.stateid, NULL,
1801 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001802 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001803
1804 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001805}
1806
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001807static struct inode *
1808nfs4_opendata_get_inode(struct nfs4_opendata *data)
1809{
1810 struct inode *inode;
1811
1812 switch (data->o_arg.claim) {
1813 case NFS4_OPEN_CLAIM_NULL:
1814 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1815 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1816 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1817 return ERR_PTR(-EAGAIN);
1818 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1819 &data->f_attr, data->f_label);
1820 break;
1821 default:
1822 inode = d_inode(data->dentry);
1823 ihold(inode);
1824 nfs_refresh_inode(inode, &data->f_attr);
1825 }
1826 return inode;
1827}
1828
Andy Adamsone23008e2012-10-02 21:07:32 -04001829static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001830nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1831{
1832 struct nfs4_state *state;
1833 struct inode *inode;
1834
1835 inode = nfs4_opendata_get_inode(data);
1836 if (IS_ERR(inode))
1837 return ERR_CAST(inode);
1838 if (data->state != NULL && data->state->inode == inode) {
1839 state = data->state;
1840 atomic_inc(&state->count);
1841 } else
1842 state = nfs4_get_open_state(inode, data->owner);
1843 iput(inode);
1844 if (state == NULL)
1845 state = ERR_PTR(-ENOMEM);
1846 return state;
1847}
1848
Andy Adamsone23008e2012-10-02 21:07:32 -04001849static struct nfs4_state *
1850_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001852 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001853
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001855 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001856 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857 goto out;
1858 }
1859
Trond Myklebust75e8c482017-08-08 10:38:07 -04001860 state = nfs4_opendata_find_nfs4_state(data);
1861 if (IS_ERR(state))
1862 goto out;
1863
Andy Adamsone23008e2012-10-02 21:07:32 -04001864 if (data->o_res.delegation_type != 0)
1865 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001866 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001867 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001868out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001869 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001870 return state;
1871}
1872
Andy Adamsone23008e2012-10-02 21:07:32 -04001873static struct nfs4_state *
1874nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1875{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001876 struct nfs4_state *ret;
1877
Andy Adamsone23008e2012-10-02 21:07:32 -04001878 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001879 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1880 else
1881 ret = _nfs4_opendata_to_nfs4_state(data);
1882 nfs4_sequence_free_slot(&data->o_res.seq_res);
1883 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001884}
1885
Trond Myklebust864472e2006-01-03 09:55:15 +01001886static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1887{
1888 struct nfs_inode *nfsi = NFS_I(state->inode);
1889 struct nfs_open_context *ctx;
1890
1891 spin_lock(&state->inode->i_lock);
1892 list_for_each_entry(ctx, &nfsi->open_files, list) {
1893 if (ctx->state != state)
1894 continue;
1895 get_nfs_open_context(ctx);
1896 spin_unlock(&state->inode->i_lock);
1897 return ctx;
1898 }
1899 spin_unlock(&state->inode->i_lock);
1900 return ERR_PTR(-ENOENT);
1901}
1902
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001903static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1904 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001905{
1906 struct nfs4_opendata *opendata;
1907
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001908 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001909 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001910 if (opendata == NULL)
1911 return ERR_PTR(-ENOMEM);
1912 opendata->state = state;
1913 atomic_inc(&state->count);
1914 return opendata;
1915}
1916
Trond Myklebust24311f82015-09-20 10:50:17 -04001917static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1918 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001919{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001920 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001921 int ret;
1922
Trond Myklebust24311f82015-09-20 10:50:17 -04001923 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001924 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001925 opendata->o_arg.open_flags = 0;
1926 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001927 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1928 NFS_SB(opendata->dentry->d_sb),
1929 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001930 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1931 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1932 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001933 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001934 if (ret != 0)
1935 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001936 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001937 if (IS_ERR(newstate))
1938 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001939 if (newstate != opendata->state)
1940 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001941 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001942 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001943}
1944
1945static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1946{
Trond Myklebust864472e2006-01-03 09:55:15 +01001947 int ret;
1948
Trond Myklebust4f14c192014-02-12 19:15:06 -05001949 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1950 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1951 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1952 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001953 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001954 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001955 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001956 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001957 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1958 if (ret != 0)
1959 return ret;
1960 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1961 if (ret != 0)
1962 return ret;
1963 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1964 if (ret != 0)
1965 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001966 /*
1967 * We may have performed cached opens for all three recoveries.
1968 * Check if we need to update the current stateid.
1969 */
1970 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001971 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001972 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001973 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001974 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001975 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001976 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001977 return 0;
1978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980/*
1981 * OPEN_RECLAIM:
1982 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001984static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001986 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001987 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001988 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 int status;
1990
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001991 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1992 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001993 if (IS_ERR(opendata))
1994 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001995 rcu_read_lock();
1996 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001997 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001998 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001999 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002000 opendata->o_arg.u.delegation_type = delegation_type;
2001 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002002 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return status;
2004}
2005
Trond Myklebust539cd032007-06-05 11:46:42 -04002006static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct nfs_server *server = NFS_SERVER(state->inode);
2009 struct nfs4_exception exception = { };
2010 int err;
2011 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002012 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002013 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002014 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2015 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002016 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002017 break;
2018 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 } while (exception.retry);
2020 return err;
2021}
2022
Trond Myklebust864472e2006-01-03 09:55:15 +01002023static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2024{
2025 struct nfs_open_context *ctx;
2026 int ret;
2027
2028 ctx = nfs4_state_find_open_context(state);
2029 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002030 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002031 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002032 put_nfs_open_context(ctx);
2033 return ret;
2034}
2035
NeilBrowndce26302017-12-13 09:57:09 +11002036static 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 -07002037{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002038 switch (err) {
2039 default:
2040 printk(KERN_ERR "NFS: %s: unhandled error "
2041 "%d.\n", __func__, err);
2042 case 0:
2043 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002044 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002045 case -ESTALE:
2046 break;
2047 case -NFS4ERR_BADSESSION:
2048 case -NFS4ERR_BADSLOT:
2049 case -NFS4ERR_BAD_HIGH_SLOT:
2050 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2051 case -NFS4ERR_DEADSESSION:
2052 set_bit(NFS_DELEGATED_STATE, &state->flags);
2053 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2054 return -EAGAIN;
2055 case -NFS4ERR_STALE_CLIENTID:
2056 case -NFS4ERR_STALE_STATEID:
2057 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 /* Don't recall a delegation if it was lost */
2059 nfs4_schedule_lease_recovery(server->nfs_client);
2060 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002061 case -NFS4ERR_MOVED:
2062 nfs4_schedule_migration_recovery(server);
2063 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002064 case -NFS4ERR_LEASE_MOVED:
2065 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2066 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002067 case -NFS4ERR_DELEG_REVOKED:
2068 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002069 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002070 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002071 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002072 nfs_inode_find_state_and_recover(state->inode,
2073 stateid);
2074 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002075 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002076 case -NFS4ERR_DELAY:
2077 case -NFS4ERR_GRACE:
2078 set_bit(NFS_DELEGATED_STATE, &state->flags);
2079 ssleep(1);
2080 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002081 case -ENOMEM:
2082 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002083 if (fl) {
2084 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2085 if (lsp)
2086 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2087 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002088 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return err;
2091}
2092
Trond Myklebust24311f82015-09-20 10:50:17 -04002093int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2094 struct nfs4_state *state, const nfs4_stateid *stateid,
2095 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002096{
2097 struct nfs_server *server = NFS_SERVER(state->inode);
2098 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002099 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002100
2101 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2102 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2103 if (IS_ERR(opendata))
2104 return PTR_ERR(opendata);
2105 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002106 write_seqlock(&state->seqlock);
2107 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2108 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002109 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2110 switch (type & (FMODE_READ|FMODE_WRITE)) {
2111 case FMODE_READ|FMODE_WRITE:
2112 case FMODE_WRITE:
2113 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2114 if (err)
2115 break;
2116 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2117 if (err)
2118 break;
2119 case FMODE_READ:
2120 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2121 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002122 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002123 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002124}
2125
Chuck Leverbe05c862013-08-09 12:49:47 -04002126static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2127{
2128 struct nfs4_opendata *data = calldata;
2129
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002130 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2131 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002132}
2133
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002134static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2135{
2136 struct nfs4_opendata *data = calldata;
2137
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002138 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002139
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002140 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002141 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002142 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002143 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002144 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002145 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002146 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147}
2148
2149static void nfs4_open_confirm_release(void *calldata)
2150{
2151 struct nfs4_opendata *data = calldata;
2152 struct nfs4_state *state = NULL;
2153
2154 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002155 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002156 goto out_free;
2157 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002158 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002159 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002160 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002161 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002162 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002163out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002164 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002165}
2166
2167static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002168 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002169 .rpc_call_done = nfs4_open_confirm_done,
2170 .rpc_release = nfs4_open_confirm_release,
2171};
2172
2173/*
2174 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2175 */
2176static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2177{
David Howells2b0143b2015-03-17 22:25:59 +00002178 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002179 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002180 struct rpc_message msg = {
2181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2182 .rpc_argp = &data->c_arg,
2183 .rpc_resp = &data->c_res,
2184 .rpc_cred = data->owner->so_cred,
2185 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 struct rpc_task_setup task_setup_data = {
2187 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002188 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 .callback_ops = &nfs4_open_confirm_ops,
2190 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002191 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002192 .flags = RPC_TASK_ASYNC,
2193 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int status;
2195
Anna Schumakerfba83f32018-05-04 16:22:50 -04002196 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2197 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002198 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002199 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002200 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002201 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002202 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002203 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002204 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002205 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002206 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002207 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002208 smp_wmb();
2209 } else
2210 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002211 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return status;
2213}
2214
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002215static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 struct nfs4_opendata *data = calldata;
2218 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002219 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002220 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002221
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002222 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002223 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002224 /*
2225 * Check if we still need to send an OPEN call, or if we can use
2226 * a delegation instead.
2227 */
2228 if (data->state != NULL) {
2229 struct nfs_delegation *delegation;
2230
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002231 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002232 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002233 rcu_read_lock();
2234 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002235 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002236 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002237 rcu_read_unlock();
2238 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002239 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002240 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002241 switch (claim) {
2242 default:
2243 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002244 case NFS4_OPEN_CLAIM_PREVIOUS:
2245 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2246 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002247 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002248 case NFS4_OPEN_CLAIM_FH:
2249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002250 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002251 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002252 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002253 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002254 &data->o_res.seq_res,
2255 task) != 0)
2256 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002257
2258 /* Set the create mode (note dependency on the session type) */
2259 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2260 if (data->o_arg.open_flags & O_EXCL) {
2261 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2262 if (nfs4_has_persistent_session(clp))
2263 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2264 else if (clp->cl_mvops->minor_version > 0)
2265 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2266 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002267 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002268unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002269 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002270 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002271out_no_action:
2272 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002273out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002274 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002275}
2276
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277static void nfs4_open_done(struct rpc_task *task, void *calldata)
2278{
2279 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002282
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002283 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002284 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002285
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002286 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002287 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2288 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002289 case S_IFREG:
2290 break;
2291 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002292 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002293 break;
2294 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002296 break;
2297 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002299 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002300 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002301 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002302 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2303 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002304 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002305 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002306}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002307
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308static void nfs4_open_release(void *calldata)
2309{
2310 struct nfs4_opendata *data = calldata;
2311 struct nfs4_state *state = NULL;
2312
2313 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002314 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315 goto out_free;
2316 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002317 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318 goto out_free;
2319 /* In case we need an open_confirm, no cleanup! */
2320 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2321 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002323 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002324 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002325out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002326 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002327}
2328
2329static const struct rpc_call_ops nfs4_open_ops = {
2330 .rpc_call_prepare = nfs4_open_prepare,
2331 .rpc_call_done = nfs4_open_done,
2332 .rpc_release = nfs4_open_release,
2333};
2334
Fred Isaman3b65a302016-09-19 10:06:49 -04002335static int nfs4_run_open_task(struct nfs4_opendata *data,
2336 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002337{
David Howells2b0143b2015-03-17 22:25:59 +00002338 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002339 struct nfs_server *server = NFS_SERVER(dir);
2340 struct nfs_openargs *o_arg = &data->o_arg;
2341 struct nfs_openres *o_res = &data->o_res;
2342 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002343 struct rpc_message msg = {
2344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2345 .rpc_argp = o_arg,
2346 .rpc_resp = o_res,
2347 .rpc_cred = data->owner->so_cred,
2348 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002349 struct rpc_task_setup task_setup_data = {
2350 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002351 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002352 .callback_ops = &nfs4_open_ops,
2353 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002354 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002355 .flags = RPC_TASK_ASYNC,
2356 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002357 int status;
2358
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002359 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002360 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002361 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002362 data->cancelled = false;
2363 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002364 if (!ctx) {
2365 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002366 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002367 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002368 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002369 pnfs_lgopen_prepare(data, ctx);
2370 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002371 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002372 if (IS_ERR(task))
2373 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002374 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002375 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002376 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002377 smp_wmb();
2378 } else
2379 status = data->rpc_status;
2380 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002381
2382 return status;
2383}
2384
2385static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2386{
David Howells2b0143b2015-03-17 22:25:59 +00002387 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002388 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002389 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002390
Fred Isaman3b65a302016-09-19 10:06:49 -04002391 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002392 if (status != 0 || !data->rpc_done)
2393 return status;
2394
Trond Myklebust6926afd12012-01-07 13:22:46 -05002395 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2396
Anna Schumakerd7e98252017-01-11 16:13:29 -05002397 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002398 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002399
2400 return status;
2401}
2402
Trond Myklebustf3792d62014-07-10 08:54:32 -04002403/*
2404 * Additional permission checks in order to distinguish between an
2405 * open for read, and an open for execute. This works around the
2406 * fact that NFSv4 OPEN treats read and execute permissions as being
2407 * the same.
2408 * Note that in the non-execute case, we want to turn off permission
2409 * checking if we just created a new file (POSIX open() semantics).
2410 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002411static int nfs4_opendata_access(struct rpc_cred *cred,
2412 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002413 struct nfs4_state *state, fmode_t fmode,
2414 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002415{
2416 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002417 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002418
2419 /* access call failed or for some reason the server doesn't
2420 * support any access modes -- defer access call until later */
2421 if (opendata->o_res.access_supported == 0)
2422 return 0;
2423
2424 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002425 /*
2426 * Use openflags to check for exec, because fmode won't
2427 * always have FMODE_EXEC set when file open for exec.
2428 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002429 if (openflags & __FMODE_EXEC) {
2430 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002431 if (S_ISDIR(state->inode->i_mode))
2432 mask = NFS4_ACCESS_LOOKUP;
2433 else
2434 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002435 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002436 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002437
2438 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002439 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2440 nfs_access_add_cache(state->inode, &cache);
2441
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002442 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2443 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002444 return 0;
2445
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002446 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002447}
2448
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002449/*
2450 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2451 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002452static int _nfs4_proc_open(struct nfs4_opendata *data,
2453 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002454{
David Howells2b0143b2015-03-17 22:25:59 +00002455 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002456 struct nfs_server *server = NFS_SERVER(dir);
2457 struct nfs_openargs *o_arg = &data->o_arg;
2458 struct nfs_openres *o_res = &data->o_res;
2459 int status;
2460
Fred Isaman3b65a302016-09-19 10:06:49 -04002461 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002462 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002463 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002464 if (status != 0) {
2465 if (status == -NFS4ERR_BADNAME &&
2466 !(o_arg->open_flags & O_CREAT))
2467 return -ENOENT;
2468 return status;
2469 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002470
Trond Myklebust6926afd12012-01-07 13:22:46 -05002471 nfs_fattr_map_and_free_names(server, &data->f_attr);
2472
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002473 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002474 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002475 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002476 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002477 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002478 if (data->file_created ||
2479 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002480 update_changeattr(dir, &o_res->cinfo,
2481 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002482 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002483 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2484 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002486 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002490 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2491 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002492 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2493 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002494 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498/*
2499 * OPEN_EXPIRED:
2500 * reclaim state on the server after a network partition.
2501 * Assumes caller holds the appropriate lock
2502 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002503static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002505 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002508 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002509 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002510 if (IS_ERR(opendata))
2511 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002512 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002513 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002514 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002515 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002516 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002519static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002520{
Trond Myklebust539cd032007-06-05 11:46:42 -04002521 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002522 struct nfs4_exception exception = { };
2523 int err;
2524
2525 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002526 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002527 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002528 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2529 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002530 switch (err) {
2531 default:
2532 goto out;
2533 case -NFS4ERR_GRACE:
2534 case -NFS4ERR_DELAY:
2535 nfs4_handle_exception(server, err, &exception);
2536 err = 0;
2537 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002538 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002539out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002540 return err;
2541}
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002546 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Trond Myklebust864472e2006-01-03 09:55:15 +01002548 ctx = nfs4_state_find_open_context(state);
2549 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002550 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002551 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002552 put_nfs_open_context(ctx);
2553 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
Trond Myklebust41020b62016-09-22 13:38:58 -04002556static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2557 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002558{
Trond Myklebust41020b62016-09-22 13:38:58 -04002559 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002560 write_seqlock(&state->seqlock);
2561 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2562 write_sequnlock(&state->seqlock);
2563 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2564}
2565
2566static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2567{
2568 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002569 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002570}
2571
2572static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2573{
2574 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2575 nfs40_clear_delegation_stateid(state);
2576 return nfs4_open_expired(sp, state);
2577}
2578
Trond Myklebust45870d62016-09-22 13:38:59 -04002579static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2580 nfs4_stateid *stateid,
2581 struct rpc_cred *cred)
2582{
2583 return -NFS4ERR_BAD_STATEID;
2584}
2585
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002586#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002587static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2588 nfs4_stateid *stateid,
2589 struct rpc_cred *cred)
2590{
2591 int status;
2592
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002593 switch (stateid->type) {
2594 default:
2595 break;
2596 case NFS4_INVALID_STATEID_TYPE:
2597 case NFS4_SPECIAL_STATEID_TYPE:
2598 return -NFS4ERR_BAD_STATEID;
2599 case NFS4_REVOKED_STATEID_TYPE:
2600 goto out_free;
2601 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002602
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002603 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002604 switch (status) {
2605 case -NFS4ERR_EXPIRED:
2606 case -NFS4ERR_ADMIN_REVOKED:
2607 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002608 break;
2609 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002610 return status;
2611 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002612out_free:
2613 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002614 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002615 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002616}
2617
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002618static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002619{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002620 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002621 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002622 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002623 struct rpc_cred *cred;
2624 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002626 /* Get the delegation credential for use by test/free_stateid */
2627 rcu_read_lock();
2628 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002629 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002630 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002631 return;
2632 }
2633
2634 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002635 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2636 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2637 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002638 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002639 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002640 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002641 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002642
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002643 cred = get_rpccred(delegation->cred);
2644 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002645 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002646 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002647 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002648 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002649
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002650 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002651}
2652
2653/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002654 * nfs41_check_expired_locks - possibly free a lock stateid
2655 *
2656 * @state: NFSv4 state for an inode
2657 *
2658 * Returns NFS_OK if recovery for this stateid is now finished.
2659 * Otherwise a negative NFS4ERR value is returned.
2660 */
2661static int nfs41_check_expired_locks(struct nfs4_state *state)
2662{
2663 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002664 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002665 struct nfs_server *server = NFS_SERVER(state->inode);
2666
2667 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2668 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002669
2670 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002671 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2672 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2673 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2674
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002675 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002676 spin_unlock(&state->state_lock);
2677
2678 nfs4_put_lock_state(prev);
2679 prev = lsp;
2680
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002681 status = nfs41_test_and_free_expired_stateid(server,
2682 &lsp->ls_stateid,
2683 cred);
2684 trace_nfs4_test_lock_stateid(state, lsp, status);
2685 if (status == -NFS4ERR_EXPIRED ||
2686 status == -NFS4ERR_BAD_STATEID) {
2687 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002688 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002689 if (!recover_lost_locks)
2690 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2691 } else if (status != NFS_OK) {
2692 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002693 nfs4_put_lock_state(prev);
2694 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002695 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002696 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002697 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002698 }
2699 spin_unlock(&state->state_lock);
2700 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002701out:
2702 return ret;
2703}
2704
2705/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002706 * nfs41_check_open_stateid - possibly free an open stateid
2707 *
2708 * @state: NFSv4 state for an inode
2709 *
2710 * Returns NFS_OK if recovery for this stateid is now finished.
2711 * Otherwise a negative NFS4ERR value is returned.
2712 */
2713static int nfs41_check_open_stateid(struct nfs4_state *state)
2714{
2715 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002716 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002717 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002718 int status;
2719
Trond Myklebustb134fc42016-09-22 13:39:16 -04002720 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002721 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2722 if (nfs4_have_delegation(state->inode, state->state))
2723 return NFS_OK;
2724 return -NFS4ERR_OPENMODE;
2725 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002726 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002727 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002728 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002729 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002730 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002731 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2732 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2733 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002734 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002735 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002736 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002737 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002738 if (nfs_open_stateid_recover_openmode(state))
2739 return -NFS4ERR_OPENMODE;
2740 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002741}
2742
2743static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2744{
Chuck Levereb64cf92012-07-11 16:30:05 -04002745 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002746
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002747 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002748 status = nfs41_check_expired_locks(state);
2749 if (status != NFS_OK)
2750 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002751 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002752 if (status != NFS_OK)
2753 status = nfs4_open_expired(sp, state);
2754 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002755}
2756#endif
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002759 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2760 * fields corresponding to attributes that were used to store the verifier.
2761 * Make sure we clobber those fields in the later setattr call
2762 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002763static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002764 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002765{
Trond Myklebust609339c2018-03-28 16:18:17 -04002766 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2767 __u32 attrset[3];
2768 unsigned ret;
2769 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002770
Trond Myklebust609339c2018-03-28 16:18:17 -04002771 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2772 attrset[i] = opendata->o_res.attrset[i];
2773 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2774 attrset[i] &= ~bitmask[i];
2775 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002776
Trond Myklebust609339c2018-03-28 16:18:17 -04002777 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2778 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002779
Trond Myklebust609339c2018-03-28 16:18:17 -04002780 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2781 if (sattr->ia_valid & ATTR_ATIME_SET)
2782 ret |= ATTR_ATIME_SET;
2783 else
2784 ret |= ATTR_ATIME;
2785 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002786
Trond Myklebust609339c2018-03-28 16:18:17 -04002787 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2788 if (sattr->ia_valid & ATTR_MTIME_SET)
2789 ret |= ATTR_MTIME_SET;
2790 else
2791 ret |= ATTR_MTIME;
2792 }
2793
2794 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002795 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002796 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002797}
2798
Trond Myklebustc21443c2013-02-07 14:26:21 -05002799static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2800 fmode_t fmode,
2801 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002802 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002803{
2804 struct nfs4_state_owner *sp = opendata->owner;
2805 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002806 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002807 struct nfs4_state *state;
2808 unsigned int seq;
2809 int ret;
2810
2811 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2812
Fred Isaman3b65a302016-09-19 10:06:49 -04002813 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002814 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002815 goto out;
2816
Trond Myklebustae55e592018-05-22 11:17:16 -04002817 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002818 ret = PTR_ERR(state);
2819 if (IS_ERR(state))
2820 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002821 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002822 if (server->caps & NFS_CAP_POSIX_LOCK)
2823 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002824 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2825 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002826
Trond Myklebust275bb302013-05-29 13:11:28 -04002827 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002828 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002829 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002830 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002831 alias = d_exact_alias(dentry, state->inode);
2832 if (!alias)
2833 alias = d_splice_alias(igrab(state->inode), dentry);
2834 /* d_splice_alias() can't fail here - it's a non-directory */
2835 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002836 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002837 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002838 }
2839 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002840 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002841 }
2842
Trond Myklebustc21443c2013-02-07 14:26:21 -05002843 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2844 if (ret != 0)
2845 goto out;
2846
David Howells2b0143b2015-03-17 22:25:59 +00002847 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002848 nfs_inode_attach_open_context(ctx);
2849 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2850 nfs4_schedule_stateid_recovery(server, state);
Fred Isaman2409a972016-10-06 12:11:21 -04002851 else
2852 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002853 }
Fred Isaman2409a972016-10-06 12:11:21 -04002854
Trond Myklebustc21443c2013-02-07 14:26:21 -05002855out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002856 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002857 return ret;
2858}
2859
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002860/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002861 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 */
Andy Adamson82be4172012-05-23 05:02:35 -04002863static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002864 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002865 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002866 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002867 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 struct nfs4_state_owner *sp;
2870 struct nfs4_state *state = NULL;
2871 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002872 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002873 struct dentry *dentry = ctx->dentry;
2874 struct rpc_cred *cred = ctx->cred;
2875 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2876 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002877 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002878 struct iattr *sattr = c->sattr;
2879 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002880 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002881 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002885 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2886 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2888 goto out_err;
2889 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002890 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002891 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002892 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002893 if (d_really_is_positive(dentry))
2894 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002895 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002896 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002897 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002898 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2899 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002900 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002901 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
David Quigley14c43f72013-05-22 12:50:43 -04002903 if (label) {
2904 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2905 if (IS_ERR(olabel)) {
2906 status = PTR_ERR(olabel);
2907 goto err_opendata_put;
2908 }
2909 }
2910
Trond Myklebuste911b812014-03-26 13:24:37 -07002911 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2912 if (!opendata->f_attr.mdsthreshold) {
2913 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2914 if (!opendata->f_attr.mdsthreshold)
2915 goto err_free_label;
2916 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002917 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002918 }
David Howells2b0143b2015-03-17 22:25:59 +00002919 if (d_really_is_positive(dentry))
2920 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002921
Trond Myklebust3efb9722013-05-29 13:17:04 -04002922 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002923 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002924 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002925 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002926
NeilBrownefcbc042015-07-30 13:00:56 +10002927 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002928 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002929 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002930 /*
2931 * send create attributes which was not set by open
2932 * with an extra setattr.
2933 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002934 if (attrs || label) {
2935 unsigned ia_old = sattr->ia_valid;
2936
2937 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002938 nfs_fattr_init(opendata->o_res.f_attr);
2939 status = nfs4_do_setattr(state->inode, cred,
2940 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002941 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002942 if (status == 0) {
2943 nfs_setattr_update_inode(state->inode, sattr,
2944 opendata->o_res.f_attr);
2945 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2946 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002947 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002948 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002949 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002950 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002951 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002952
Trond Myklebuste911b812014-03-26 13:24:37 -07002953 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002954 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002955 opendata->f_attr.mdsthreshold = NULL;
2956 }
Andy Adamson82be4172012-05-23 05:02:35 -04002957
David Quigley14c43f72013-05-22 12:50:43 -04002958 nfs4_label_free(olabel);
2959
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002960 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002963err_free_label:
2964 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002965err_opendata_put:
2966 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002967err_put_state_owner:
2968 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return status;
2971}
2972
2973
Andy Adamson82be4172012-05-23 05:02:35 -04002974static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002975 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002976 int flags,
2977 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002978 struct nfs4_label *label,
2979 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002981 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 struct nfs4_exception exception = { };
2983 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002984 struct nfs4_open_createattrs c = {
2985 .label = label,
2986 .sattr = sattr,
2987 .verf = {
2988 [0] = (__u32)jiffies,
2989 [1] = (__u32)current->pid,
2990 },
2991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 int status;
2993
2994 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002995 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002996 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002997 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (status == 0)
2999 break;
3000 /* NOTE: BAD_SEQID means the server and client disagree about the
3001 * book-keeping w.r.t. state-changing operations
3002 * (OPEN/CLOSE/LOCK/LOCKU...)
3003 * It is actually a sign of a bug on the client or on the server.
3004 *
3005 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003006 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 * have unhashed the old state_owner for us, and that we can
3008 * therefore safely retry using a new one. We should still warn
3009 * the user though...
3010 */
3011 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003012 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003013 " returned a bad sequence-id error!\n",
3014 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 exception.retry = 1;
3016 continue;
3017 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003018 /*
3019 * BAD_STATEID on OPEN means that the server cancelled our
3020 * state before it received the OPEN_CONFIRM.
3021 * Recover by retrying the request as per the discussion
3022 * on Page 181 of RFC3530.
3023 */
3024 if (status == -NFS4ERR_BAD_STATEID) {
3025 exception.retry = 1;
3026 continue;
3027 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003028 if (status == -EAGAIN) {
3029 /* We must have found a delegation */
3030 exception.retry = 1;
3031 continue;
3032 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003033 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3034 continue;
3035 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 status, &exception));
3037 } while (exception.retry);
3038 return res;
3039}
3040
Trond Myklebust8487c472016-06-26 08:44:35 -04003041static int _nfs4_do_setattr(struct inode *inode,
3042 struct nfs_setattrargs *arg,
3043 struct nfs_setattrres *res,
3044 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003045 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003046{
3047 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003048 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3050 .rpc_argp = arg,
3051 .rpc_resp = res,
3052 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003053 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003054 struct rpc_cred *delegation_cred = NULL;
3055 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003056 bool truncate;
3057 int status;
3058
3059 nfs_fattr_init(res->fattr);
3060
3061 /* Servers should only apply open mode checks for file size changes */
3062 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003063 if (!truncate)
3064 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003065
Trond Myklebust991eedb2018-04-09 11:15:30 -04003066 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003067 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003068 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003069 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003070 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003071 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003072 l_ctx = nfs_get_lock_context(ctx);
3073 if (IS_ERR(l_ctx))
3074 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003075 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3076 &arg->stateid, &delegation_cred);
3077 nfs_put_lock_context(l_ctx);
3078 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003079 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003080 } else {
3081zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003082 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003083 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003084 if (delegation_cred)
3085 msg.rpc_cred = delegation_cred;
3086
3087 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3088
3089 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003090 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003091 renew_lease(server, timestamp);
3092 trace_nfs4_setattr(inode, &arg->stateid, status);
3093 return status;
3094}
3095
3096static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3097 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003099 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003101 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003102 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003103 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003104 struct nfs_setattrargs arg = {
3105 .fh = NFS_FH(inode),
3106 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003108 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003109 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003110 };
3111 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003113 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003115 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003116 struct nfs4_exception exception = {
3117 .state = state,
3118 .inode = inode,
3119 .stateid = &arg.stateid,
3120 };
3121 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003124 nfs4_bitmap_copy_adjust_setattr(bitmask,
3125 nfs4_bitmask(server, olabel),
3126 inode);
3127
NeilBrown29b59f92016-10-13 15:26:47 +11003128 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003129 switch (err) {
3130 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003131 if (!(sattr->ia_valid & ATTR_SIZE)) {
3132 pr_warn_once("NFSv4: server %s is incorrectly "
3133 "applying open mode checks to "
3134 "a SETATTR that is not "
3135 "changing file size.\n",
3136 server->nfs_client->cl_hostname);
3137 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003138 if (state && !(state->state & FMODE_WRITE)) {
3139 err = -EBADF;
3140 if (sattr->ia_valid & ATTR_OPEN)
3141 err = -EACCES;
3142 goto out;
3143 }
3144 }
3145 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003147out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return err;
3149}
3150
Peng Tao500d7012015-09-22 11:35:22 +08003151static bool
3152nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3153{
3154 if (inode == NULL || !nfs_have_layout(inode))
3155 return false;
3156
3157 return pnfs_wait_on_layoutreturn(inode, task);
3158}
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160struct nfs4_closedata {
3161 struct inode *inode;
3162 struct nfs4_state *state;
3163 struct nfs_closeargs arg;
3164 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003165 struct {
3166 struct nfs4_layoutreturn_args arg;
3167 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003168 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003169 u32 roc_barrier;
3170 bool roc;
3171 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003172 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003173 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174};
3175
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003177{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 struct nfs4_closedata *calldata = data;
3179 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003180 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003181
Trond Myklebustcf805162016-11-15 14:56:07 -05003182 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003183 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3184 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003185 nfs4_put_open_state(calldata->state);
3186 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003187 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003188 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003189 kfree(calldata);
3190}
3191
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003192static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003194 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003197 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003198 struct nfs4_exception exception = {
3199 .state = state,
3200 .inode = calldata->inode,
3201 .stateid = &calldata->arg.stateid,
3202 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Chuck Levera3ca5652012-03-01 17:00:40 -05003204 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003205 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3206 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003207 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003208
3209 /* Handle Layoutreturn errors */
3210 if (calldata->arg.lr_args && task->tk_status != 0) {
3211 switch (calldata->res.lr_ret) {
3212 default:
3213 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3214 break;
3215 case 0:
3216 calldata->arg.lr_args = NULL;
3217 calldata->res.lr_res = NULL;
3218 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003219 case -NFS4ERR_OLD_STATEID:
3220 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003221 calldata->inode))
3222 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003223 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003224 case -NFS4ERR_ADMIN_REVOKED:
3225 case -NFS4ERR_DELEG_REVOKED:
3226 case -NFS4ERR_EXPIRED:
3227 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003228 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3229 case -NFS4ERR_WRONG_CRED:
3230 calldata->arg.lr_args = NULL;
3231 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003232 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003233 }
3234 }
3235
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003236 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 * the state_owner. we keep this around to process errors
3238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 switch (task->tk_status) {
3240 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003241 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003242 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003243 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003244 case -NFS4ERR_ACCESS:
3245 if (calldata->arg.bitmask != NULL) {
3246 calldata->arg.bitmask = NULL;
3247 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003248 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003249
3250 }
3251 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003252 case -NFS4ERR_OLD_STATEID:
3253 /* Did we race with OPEN? */
3254 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003255 state))
3256 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003257 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003258 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003260 case -NFS4ERR_EXPIRED:
3261 nfs4_free_revoked_stateid(server,
3262 &calldata->arg.stateid,
3263 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003264 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003265 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003268 task->tk_status = nfs4_async_handle_exception(task,
3269 server, task->tk_status, &exception);
3270 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003271 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003273 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3274 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003275out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003276 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003277 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003278 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003279 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003280 return;
3281lr_restart:
3282 calldata->res.lr_ret = 0;
3283out_restart:
3284 task->tk_status = 0;
3285 rpc_restart_call_prepare(task);
3286 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003289static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003291 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003292 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003293 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003294 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003295 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003296
Chuck Levera3ca5652012-03-01 17:00:40 -05003297 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003298 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003299 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003300
Trond Myklebust88069f72009-12-08 08:33:16 -05003301 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003302 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003303 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3304 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3305 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003306 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003307 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003308 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003309 if (state->n_rdonly == 0)
3310 call_close |= is_rdonly;
3311 else if (is_rdonly)
3312 calldata->arg.fmode |= FMODE_READ;
3313 if (state->n_wronly == 0)
3314 call_close |= is_wronly;
3315 else if (is_wronly)
3316 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003317 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3318 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003319 } else if (is_rdwr)
3320 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3321
Trond Myklebust5cc78612016-11-14 11:19:56 -05003322 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003323 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003324 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003325 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003326
3327 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003328 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003329 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003330 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003331
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003332 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003333 nfs_release_seqid(calldata->arg.seqid);
3334 goto out_wait;
3335 }
3336
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003337 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003338 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003339
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003340 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003341 /* Close-to-open cache consistency revalidation */
3342 if (!nfs4_have_delegation(inode, FMODE_READ))
3343 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3344 else
3345 calldata->arg.bitmask = NULL;
3346 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003347
Trond Myklebust6ae37332015-01-30 14:21:14 -05003348 calldata->arg.share_access =
3349 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3350 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003351
Trond Myklebustd8d84982016-12-19 12:14:44 -05003352 if (calldata->res.fattr == NULL)
3353 calldata->arg.bitmask = NULL;
3354 else if (calldata->arg.bitmask == NULL)
3355 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003356 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003357 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003358 &calldata->arg.seq_args,
3359 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003360 task) != 0)
3361 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003362 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003363 return;
3364out_no_action:
3365 task->tk_action = NULL;
3366out_wait:
3367 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003370static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003371 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003372 .rpc_call_done = nfs4_close_done,
3373 .rpc_release = nfs4_free_closedata,
3374};
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376/*
3377 * It is possible for data to be read/written from a mem-mapped file
3378 * after the sys_close call (which hits the vfs layer as a flush).
3379 * This means that we can't safely call nfsv4 close on a file until
3380 * the inode is cleared. This in turn means that we are not good
3381 * NFSv4 citizens - we do not indicate to the server to update the file's
3382 * share state even when we are done with one of the three share
3383 * stateid's in the inode.
3384 *
3385 * NOTE: Caller must be holding the sp->so_owner semaphore!
3386 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003387int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003389 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003390 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003392 struct nfs4_state_owner *sp = state->owner;
3393 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003394 struct rpc_message msg = {
3395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3396 .rpc_cred = state->owner->so_cred,
3397 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003398 struct rpc_task_setup task_setup_data = {
3399 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003400 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003401 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003402 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003403 .flags = RPC_TASK_ASYNC,
3404 };
Trond Myklebust95121352005-10-18 14:20:12 -07003405 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003407 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3408 &task_setup_data.rpc_client, &msg);
3409
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003410 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003412 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003413 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003414 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003416 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003417 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3418 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003420 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3421 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003422 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003423 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003424 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003425 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003426 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003427 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003428 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003429 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003430 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003431 calldata->lr.roc = pnfs_roc(state->inode,
3432 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3433 if (calldata->lr.roc) {
3434 calldata->arg.lr_args = &calldata->lr.arg;
3435 calldata->res.lr_res = &calldata->lr.res;
3436 }
Al Viro643168c2011-06-22 18:20:23 -04003437 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003438
Trond Myklebust1174dd12010-12-21 10:52:24 -05003439 msg.rpc_argp = &calldata->arg;
3440 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003441 task_setup_data.callback_data = calldata;
3442 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003443 if (IS_ERR(task))
3444 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003445 status = 0;
3446 if (wait)
3447 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003448 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003449 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003450out_free_calldata:
3451 kfree(calldata);
3452out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003453 nfs4_put_open_state(state);
3454 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003455 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
Trond Myklebust2b484292010-09-17 10:56:51 -04003458static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003459nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3460 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003463 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3464
3465 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Trond Myklebust565277f2007-10-15 18:17:53 -04003467 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003468 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003469
3470 nfs4_label_release_security(label);
3471
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003472 if (IS_ERR(state))
3473 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003474 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
Trond Myklebust1185a552009-12-03 15:54:02 -05003477static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003478{
3479 if (ctx->state == NULL)
3480 return;
3481 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003482 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003483 else
Al Viro643168c2011-06-22 18:20:23 -04003484 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Trond Myklebustb944dba2013-11-04 15:20:20 -05003487#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3488#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003489#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3492{
Kinglong Mee8c612822015-08-26 21:12:58 +08003493 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003494 struct nfs4_server_caps_arg args = {
3495 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003496 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003497 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 struct nfs4_server_caps_res res = {};
3499 struct rpc_message msg = {
3500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003501 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 .rpc_resp = &res,
3503 };
3504 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003505 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Kinglong Mee8c612822015-08-26 21:12:58 +08003507 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3508 FATTR4_WORD0_FH_EXPIRE_TYPE |
3509 FATTR4_WORD0_LINK_SUPPORT |
3510 FATTR4_WORD0_SYMLINK_SUPPORT |
3511 FATTR4_WORD0_ACLSUPPORT;
3512 if (minorversion)
3513 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3514
Bryan Schumaker7c513052011-03-24 17:12:24 +00003515 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003517 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003518 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003519 case 0:
3520 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3521 res.attr_bitmask[2] = 0;
3522 break;
3523 case 1:
3524 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3525 break;
3526 case 2:
3527 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003530 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3531 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3532 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3533 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003534 NFS_CAP_CTIME|NFS_CAP_MTIME|
3535 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003536 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3537 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 server->caps |= NFS_CAP_ACLS;
3539 if (res.has_links != 0)
3540 server->caps |= NFS_CAP_HARDLINKS;
3541 if (res.has_symlinks != 0)
3542 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003543 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3544 server->caps |= NFS_CAP_FILEID;
3545 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3546 server->caps |= NFS_CAP_MODE;
3547 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3548 server->caps |= NFS_CAP_NLINK;
3549 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3550 server->caps |= NFS_CAP_OWNER;
3551 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3552 server->caps |= NFS_CAP_OWNER_GROUP;
3553 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3554 server->caps |= NFS_CAP_ATIME;
3555 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3556 server->caps |= NFS_CAP_CTIME;
3557 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3558 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003559#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3560 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3561 server->caps |= NFS_CAP_SECURITY_LABEL;
3562#endif
3563 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3564 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003565 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003566
Trond Myklebusta65318b2009-03-11 14:10:28 -04003567 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3568 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3569 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003570 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003571
3572 /* Avoid a regression due to buggy server */
3573 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3574 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003575 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3576 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003579 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return status;
3583}
3584
Trond Myklebust55a97592006-06-09 09:34:19 -04003585int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 struct nfs4_exception exception = { };
3588 int err;
3589 do {
3590 err = nfs4_handle_exception(server,
3591 _nfs4_server_capabilities(server, fhandle),
3592 &exception);
3593 } while (exception.retry);
3594 return err;
3595}
3596
3597static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3598 struct nfs_fsinfo *info)
3599{
David Quigleyaa9c2662013-05-22 12:50:44 -04003600 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003602 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 };
3604 struct nfs4_lookup_res res = {
3605 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003606 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 .fh = fhandle,
3608 };
3609 struct rpc_message msg = {
3610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3611 .rpc_argp = &args,
3612 .rpc_resp = &res,
3613 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003614
David Quigleyaa9c2662013-05-22 12:50:44 -04003615 bitmask[0] = nfs4_fattr_bitmap[0];
3616 bitmask[1] = nfs4_fattr_bitmap[1];
3617 /*
3618 * Process the label in the upcoming getfattr
3619 */
3620 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3621
Trond Myklebust0e574af2005-10-27 22:12:38 -04003622 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003623 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
3626static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3627 struct nfs_fsinfo *info)
3628{
3629 struct nfs4_exception exception = { };
3630 int err;
3631 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003632 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003633 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003634 switch (err) {
3635 case 0:
3636 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003637 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003638 default:
3639 err = nfs4_handle_exception(server, err, &exception);
3640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003642out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return err;
3644}
3645
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003646static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3647 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3648{
Trond Myklebustc2190662013-08-26 19:23:04 -04003649 struct rpc_auth_create_args auth_args = {
3650 .pseudoflavor = flavor,
3651 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003652 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003653
Trond Myklebustc2190662013-08-26 19:23:04 -04003654 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003655 if (IS_ERR(auth))
3656 return -EACCES;
3657 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003658}
3659
Chuck Lever9a744ba2013-03-16 15:56:02 -04003660/*
3661 * Retry pseudoroot lookup with various security flavors. We do this when:
3662 *
3663 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3664 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3665 *
3666 * Returns zero on success, or a negative NFS4ERR value, or a
3667 * negative errno value.
3668 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003669static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003670 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003672 /* Per 3530bis 15.33.5 */
3673 static const rpc_authflavor_t flav_array[] = {
3674 RPC_AUTH_GSS_KRB5P,
3675 RPC_AUTH_GSS_KRB5I,
3676 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003677 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003678 RPC_AUTH_NULL,
3679 };
3680 int status = -EPERM;
3681 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003683 if (server->auth_info.flavor_len > 0) {
3684 /* try each flavor specified by user */
3685 for (i = 0; i < server->auth_info.flavor_len; i++) {
3686 status = nfs4_lookup_root_sec(server, fhandle, info,
3687 server->auth_info.flavors[i]);
3688 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3689 continue;
3690 break;
3691 }
3692 } else {
3693 /* no flavors specified by user, try default list */
3694 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3695 status = nfs4_lookup_root_sec(server, fhandle, info,
3696 flav_array[i]);
3697 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3698 continue;
3699 break;
3700 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003701 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003702
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003703 /*
3704 * -EACCESS could mean that the user doesn't have correct permissions
3705 * to access the mount. It could also mean that we tried to mount
3706 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3707 * existing mount programs don't handle -EACCES very well so it should
3708 * be mapped to -EPERM instead.
3709 */
3710 if (status == -EACCES)
3711 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003712 return status;
3713}
3714
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003715/**
3716 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3717 * @server: initialized nfs_server handle
3718 * @fhandle: we fill in the pseudo-fs root file handle
3719 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003720 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003721 *
3722 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003723 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003724int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003725 struct nfs_fsinfo *info,
3726 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003727{
Andre Przywarac7757072015-04-23 17:17:40 +01003728 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003729
Andre Przywarac7757072015-04-23 17:17:40 +01003730 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003731 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003732
3733 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003734 status = server->nfs_client->cl_mvops->find_root_sec(server,
3735 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003736
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 if (status == 0)
3738 status = nfs4_server_capabilities(server, fhandle);
3739 if (status == 0)
3740 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003741
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003742 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
Bryan Schumakerbae36242012-05-10 15:07:31 -04003745static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3746 struct nfs_fsinfo *info)
3747{
3748 int error;
3749 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003750 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003751
3752 error = nfs4_server_capabilities(server, mntfh);
3753 if (error < 0) {
3754 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3755 return error;
3756 }
3757
David Quigley14c43f72013-05-22 12:50:43 -04003758 label = nfs4_label_alloc(server, GFP_KERNEL);
3759 if (IS_ERR(label))
3760 return PTR_ERR(label);
3761
Trond Myklebusta841b542018-04-07 13:50:59 -04003762 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003763 if (error < 0) {
3764 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003765 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003766 }
3767
3768 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3769 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3770 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3771
David Quigley14c43f72013-05-22 12:50:43 -04003772err_free_label:
3773 nfs4_label_free(label);
3774
Bryan Schumakerbae36242012-05-10 15:07:31 -04003775 return error;
3776}
3777
Manoj Naik6b97fd32006-06-09 09:34:29 -04003778/*
3779 * Get locations and (maybe) other attributes of a referral.
3780 * Note that we'll actually follow the referral later when
3781 * we detect fsid mismatch in inode revalidation
3782 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003783static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3784 const struct qstr *name, struct nfs_fattr *fattr,
3785 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003786{
3787 int status = -ENOMEM;
3788 struct page *page = NULL;
3789 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003790
3791 page = alloc_page(GFP_KERNEL);
3792 if (page == NULL)
3793 goto out;
3794 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3795 if (locations == NULL)
3796 goto out;
3797
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003798 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003799 if (status != 0)
3800 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003801
3802 /*
3803 * If the fsid didn't change, this is a migration event, not a
3804 * referral. Cause us to drop into the exception handler, which
3805 * will kick off migration recovery.
3806 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003807 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003808 dprintk("%s: server did not return a different fsid for"
3809 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003810 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003811 goto out;
3812 }
Andy Adamson533eb462011-06-13 18:25:56 -04003813 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3814 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003815
Andy Adamson533eb462011-06-13 18:25:56 -04003816 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003817 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003818 memset(fhandle, 0, sizeof(struct nfs_fh));
3819out:
3820 if (page)
3821 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003822 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003823 return status;
3824}
3825
David Quigley1775fd32013-05-22 12:50:42 -04003826static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003827 struct nfs_fattr *fattr, struct nfs4_label *label,
3828 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Trond Myklebust771734f2018-04-07 13:54:23 -04003830 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 struct nfs4_getattr_arg args = {
3832 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003833 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 };
3835 struct nfs4_getattr_res res = {
3836 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003837 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 .server = server,
3839 };
3840 struct rpc_message msg = {
3841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3842 .rpc_argp = &args,
3843 .rpc_resp = &res,
3844 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003845
Trond Myklebust771734f2018-04-07 13:54:23 -04003846 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003847
Trond Myklebust0e574af2005-10-27 22:12:38 -04003848 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003849 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850}
3851
David Quigley1775fd32013-05-22 12:50:42 -04003852static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003853 struct nfs_fattr *fattr, struct nfs4_label *label,
3854 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 struct nfs4_exception exception = { };
3857 int err;
3858 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003859 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003860 trace_nfs4_getattr(server, fhandle, fattr, err);
3861 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 &exception);
3863 } while (exception.retry);
3864 return err;
3865}
3866
3867/*
3868 * The file is not closed if it is opened due to the a request to change
3869 * the size of the file. The open call will not be needed once the
3870 * VFS layer lookup-intents are implemented.
3871 *
3872 * Close is called when the inode is destroyed.
3873 * If we haven't opened the file for O_WRONLY, we
3874 * need to in the size_change case to obtain a stateid.
3875 *
3876 * Got race?
3877 * Because OPEN is always done by name in nfsv4, it is
3878 * possible that we opened a different file by the same
3879 * name. We can recognize this race condition, but we
3880 * can't do anything about it besides returning an error.
3881 *
3882 * This will be fixed with VFS changes (lookup-intent).
3883 */
3884static int
3885nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3886 struct iattr *sattr)
3887{
David Howells2b0143b2015-03-17 22:25:59 +00003888 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003889 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003890 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003891 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 int status;
3893
Peng Tao88ac8152014-09-12 11:04:10 +08003894 if (pnfs_ld_layoutret_on_setattr(inode) &&
3895 sattr->ia_valid & ATTR_SIZE &&
3896 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003897 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003898
Trond Myklebust0e574af2005-10-27 22:12:38 -04003899 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Andy Adamson26699402012-05-30 16:12:24 -04003901 /* Deal with open(O_TRUNC) */
3902 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003903 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003904
3905 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003906 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003907 return 0;
3908
Trond Myklebustd5308382005-11-04 15:33:38 -05003909 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003910 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003911
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003912 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003913 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003914 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003915 }
3916
David Quigley14c43f72013-05-22 12:50:43 -04003917 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3918 if (IS_ERR(label))
3919 return PTR_ERR(label);
3920
Trond Myklebust199366f2018-03-20 16:43:18 -04003921 /* Return any delegations if we're going to change ACLs */
3922 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003923 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003924
NeilBrown29b59f92016-10-13 15:26:47 +11003925 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003926 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003927 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 nfs_setsecurity(inode, fattr, label);
3929 }
David Quigley14c43f72013-05-22 12:50:43 -04003930 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return status;
3932}
3933
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003934static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3935 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003936 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003937{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003938 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003939 int status;
3940 struct nfs4_lookup_arg args = {
3941 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003942 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003943 .name = name,
3944 };
3945 struct nfs4_lookup_res res = {
3946 .server = server,
3947 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003948 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003949 .fh = fhandle,
3950 };
3951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3953 .rpc_argp = &args,
3954 .rpc_resp = &res,
3955 };
3956
David Quigleyaa9c2662013-05-22 12:50:44 -04003957 args.bitmask = nfs4_bitmask(server, label);
3958
David Howells2b3de442006-08-22 20:06:09 -04003959 nfs_fattr_init(fattr);
3960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003962 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 dprintk("NFS reply lookup: %d\n", status);
3964 return status;
3965}
3966
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003967static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003968{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003969 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003970 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003971 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3972 fattr->nlink = 2;
3973}
3974
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003975static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003976 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003977 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003978{
3979 struct nfs4_exception exception = { };
3980 struct rpc_clnt *client = *clnt;
3981 int err;
3982 do {
David Quigley1775fd32013-05-22 12:50:42 -04003983 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003984 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003985 switch (err) {
3986 case -NFS4ERR_BADNAME:
3987 err = -ENOENT;
3988 goto out;
3989 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003990 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003991 if (err == -NFS4ERR_MOVED)
3992 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003993 goto out;
3994 case -NFS4ERR_WRONGSEC:
3995 err = -EPERM;
3996 if (client != *clnt)
3997 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003998 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003999 if (IS_ERR(client))
4000 return PTR_ERR(client);
4001
4002 exception.retry = 1;
4003 break;
4004 default:
4005 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4006 }
4007 } while (exception.retry);
4008
4009out:
4010 if (err == 0)
4011 *clnt = client;
4012 else if (client != *clnt)
4013 rpc_shutdown_client(client);
4014
4015 return err;
4016}
4017
Al Virobeffb8f2016-07-20 16:34:42 -04004018static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004019 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4020 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004022 int status;
4023 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004024
David Quigley1775fd32013-05-22 12:50:42 -04004025 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004026 if (client != NFS_CLIENT(dir)) {
4027 rpc_shutdown_client(client);
4028 nfs_fixup_secinfo_attributes(fattr);
4029 }
4030 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004033struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004034nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004035 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4036{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004037 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004038 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004039
David Quigley1775fd32013-05-22 12:50:42 -04004040 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004041 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004042 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004043 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004044}
4045
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004046static int _nfs4_proc_lookupp(struct inode *inode,
4047 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4048 struct nfs4_label *label)
4049{
4050 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4051 struct nfs_server *server = NFS_SERVER(inode);
4052 int status;
4053 struct nfs4_lookupp_arg args = {
4054 .bitmask = server->attr_bitmask,
4055 .fh = NFS_FH(inode),
4056 };
4057 struct nfs4_lookupp_res res = {
4058 .server = server,
4059 .fattr = fattr,
4060 .label = label,
4061 .fh = fhandle,
4062 };
4063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4065 .rpc_argp = &args,
4066 .rpc_resp = &res,
4067 };
4068
4069 args.bitmask = nfs4_bitmask(server, label);
4070
4071 nfs_fattr_init(fattr);
4072
4073 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4074 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4075 &res.seq_res, 0);
4076 dprintk("NFS reply lookupp: %d\n", status);
4077 return status;
4078}
4079
4080static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4081 struct nfs_fattr *fattr, struct nfs4_label *label)
4082{
4083 struct nfs4_exception exception = { };
4084 int err;
4085 do {
4086 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4087 trace_nfs4_lookupp(inode, err);
4088 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4089 &exception);
4090 } while (exception.retry);
4091 return err;
4092}
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4095{
Trond Myklebust76b32992007-08-10 17:45:11 -04004096 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 struct nfs4_accessargs args = {
4098 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004099 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004101 struct nfs4_accessres res = {
4102 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 struct rpc_message msg = {
4105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4106 .rpc_argp = &args,
4107 .rpc_resp = &res,
4108 .rpc_cred = entry->cred,
4109 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004110 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Trond Myklebust7c672652018-06-04 15:00:53 -04004112 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004113 res.fattr = nfs_alloc_fattr();
4114 if (res.fattr == NULL)
4115 return -ENOMEM;
4116 args.bitmask = server->cache_consistency_bitmask;
4117 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004118
Bryan Schumaker7c513052011-03-24 17:12:24 +00004119 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004121 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004122 if (res.fattr)
4123 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004125 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return status;
4127}
4128
4129static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4130{
4131 struct nfs4_exception exception = { };
4132 int err;
4133 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004134 err = _nfs4_proc_access(inode, entry);
4135 trace_nfs4_access(inode, err);
4136 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 &exception);
4138 } while (exception.retry);
4139 return err;
4140}
4141
4142/*
4143 * TODO: For the time being, we don't try to get any attributes
4144 * along with any of the zero-copy operations READ, READDIR,
4145 * READLINK, WRITE.
4146 *
4147 * In the case of the first three, we want to put the GETATTR
4148 * after the read-type operation -- this is because it is hard
4149 * to predict the length of a GETATTR response in v4, and thus
4150 * align the READ data correctly. This means that the GETATTR
4151 * may end up partially falling into the page cache, and we should
4152 * shift it into the 'tail' of the xdr_buf before processing.
4153 * To do this efficiently, we need to know the total length
4154 * of data received, which doesn't seem to be available outside
4155 * of the RPC layer.
4156 *
4157 * In the case of WRITE, we also want to put the GETATTR after
4158 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004159 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 *
4161 * Both of these changes to the XDR layer would in fact be quite
4162 * minor, but I decided to leave them for a subsequent patch.
4163 */
4164static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4165 unsigned int pgbase, unsigned int pglen)
4166{
4167 struct nfs4_readlink args = {
4168 .fh = NFS_FH(inode),
4169 .pgbase = pgbase,
4170 .pglen = pglen,
4171 .pages = &page,
4172 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004173 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 struct rpc_message msg = {
4175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4176 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004177 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
4179
Bryan Schumaker7c513052011-03-24 17:12:24 +00004180 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 -07004181}
4182
4183static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4184 unsigned int pgbase, unsigned int pglen)
4185{
4186 struct nfs4_exception exception = { };
4187 int err;
4188 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004189 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4190 trace_nfs4_readlink(inode, err);
4191 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 &exception);
4193 } while (exception.retry);
4194 return err;
4195}
4196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004198 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200static int
4201nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004202 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004204 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004205 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004206 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 int status = 0;
4209
NeilBrown532d4de2016-10-13 15:26:47 +11004210 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004211 if (IS_ERR(ctx))
4212 return PTR_ERR(ctx);
4213
David Quigleyaa9c2662013-05-22 12:50:44 -04004214 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4215
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004216 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4217 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004218 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 if (IS_ERR(state)) {
4220 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004224 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004225 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return status;
4227}
4228
Al Virobeffb8f2016-07-20 16:34:42 -04004229static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
Trond Myklebust16e42952005-10-27 22:12:44 -04004231 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004232 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004234 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004236 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004237 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004238 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4241 .rpc_argp = &args,
4242 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004244 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004245 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004246
Bryan Schumaker7c513052011-03-24 17:12:24 +00004247 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004248 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004249 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return status;
4251}
4252
Trond Myklebust912678d2018-03-20 16:43:15 -04004253static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4254{
4255 struct nfs4_exception exception = { };
4256 struct inode *inode = d_inode(dentry);
4257 int err;
4258
Trond Myklebustc01d3642018-03-20 16:43:20 -04004259 if (inode) {
4260 if (inode->i_nlink == 1)
4261 nfs4_inode_return_delegation(inode);
4262 else
4263 nfs4_inode_make_writeable(inode);
4264 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004265 do {
4266 err = _nfs4_proc_remove(dir, &dentry->d_name);
4267 trace_nfs4_remove(dir, &dentry->d_name, err);
4268 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4269 &exception);
4270 } while (exception.retry);
4271 return err;
4272}
4273
4274static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
4276 struct nfs4_exception exception = { };
4277 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004280 err = _nfs4_proc_remove(dir, name);
4281 trace_nfs4_remove(dir, name, err);
4282 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 &exception);
4284 } while (exception.retry);
4285 return err;
4286}
4287
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004288static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4289 struct dentry *dentry,
4290 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004292 struct nfs_removeargs *args = msg->rpc_argp;
4293 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Trond Myklebust977fcc22018-03-20 16:43:17 -04004295 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004297 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004298
4299 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004300
4301 if (inode)
4302 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004305static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4306{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004307 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004308 &data->args.seq_args,
4309 &data->res.seq_res,
4310 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311}
4312
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004313static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004315 struct nfs_unlinkdata *data = task->tk_calldata;
4316 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004317
Trond Myklebust14516c32010-07-31 14:29:06 -04004318 if (!nfs4_sequence_done(task, &res->seq_res))
4319 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004320 if (nfs4_async_handle_error(task, res->server, NULL,
4321 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004322 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004323 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004324 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004325 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326}
4327
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004328static void nfs4_proc_rename_setup(struct rpc_message *msg,
4329 struct dentry *old_dentry,
4330 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004331{
Jeff Laytond3d41522010-09-17 17:31:57 -04004332 struct nfs_renameargs *arg = msg->rpc_argp;
4333 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004334 struct inode *old_inode = d_inode(old_dentry);
4335 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004336
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004337 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004338 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004339 if (new_inode)
4340 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004341 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004342 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004343 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004344}
4345
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004346static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4347{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004348 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004349 &data->args.seq_args,
4350 &data->res.seq_res,
4351 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004352}
4353
4354static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4355 struct inode *new_dir)
4356{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004357 struct nfs_renamedata *data = task->tk_calldata;
4358 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004359
4360 if (!nfs4_sequence_done(task, &res->seq_res))
4361 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004362 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004363 return 0;
4364
Trond Myklebustc733c492017-01-11 12:32:26 -05004365 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004366 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004367 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004368 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004369 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004370 return 1;
4371}
4372
Al Virobeffb8f2016-07-20 16:34:42 -04004373static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004375 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004376 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 struct nfs4_link_arg arg = {
4378 .fh = NFS_FH(inode),
4379 .dir_fh = NFS_FH(dir),
4380 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004381 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004383 struct nfs4_link_res res = {
4384 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004385 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4389 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004390 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004392 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Trond Myklebust136f2622010-04-16 16:22:49 -04004394 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004395 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004396 goto out;
4397
David Quigley14c43f72013-05-22 12:50:43 -04004398 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4399 if (IS_ERR(res.label)) {
4400 status = PTR_ERR(res.label);
4401 goto out;
4402 }
4403
Trond Myklebustc01d3642018-03-20 16:43:20 -04004404 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004405 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004406
Bryan Schumaker7c513052011-03-24 17:12:24 +00004407 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004408 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004409 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004410 status = nfs_post_op_update_inode(inode, res.fattr);
4411 if (!status)
4412 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004413 }
David Quigley14c43f72013-05-22 12:50:43 -04004414
4415
4416 nfs4_label_free(res.label);
4417
Trond Myklebust136f2622010-04-16 16:22:49 -04004418out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004419 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 return status;
4421}
4422
Al Virobeffb8f2016-07-20 16:34:42 -04004423static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
4425 struct nfs4_exception exception = { };
4426 int err;
4427 do {
4428 err = nfs4_handle_exception(NFS_SERVER(inode),
4429 _nfs4_proc_link(inode, dir, name),
4430 &exception);
4431 } while (exception.retry);
4432 return err;
4433}
4434
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004435struct nfs4_createdata {
4436 struct rpc_message msg;
4437 struct nfs4_create_arg arg;
4438 struct nfs4_create_res res;
4439 struct nfs_fh fh;
4440 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004441 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442};
4443
4444static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004445 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004446{
4447 struct nfs4_createdata *data;
4448
4449 data = kzalloc(sizeof(*data), GFP_KERNEL);
4450 if (data != NULL) {
4451 struct nfs_server *server = NFS_SERVER(dir);
4452
David Quigley14c43f72013-05-22 12:50:43 -04004453 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4454 if (IS_ERR(data->label))
4455 goto out_free;
4456
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4458 data->msg.rpc_argp = &data->arg;
4459 data->msg.rpc_resp = &data->res;
4460 data->arg.dir_fh = NFS_FH(dir);
4461 data->arg.server = server;
4462 data->arg.name = name;
4463 data->arg.attrs = sattr;
4464 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004465 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004466 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004467 data->res.server = server;
4468 data->res.fh = &data->fh;
4469 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004470 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004471 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004472 }
4473 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004474out_free:
4475 kfree(data);
4476 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004477}
4478
4479static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4480{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004481 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004482 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004483 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004484 update_changeattr(dir, &data->res.dir_cinfo,
4485 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004486 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004487 }
4488 return status;
4489}
4490
4491static void nfs4_free_createdata(struct nfs4_createdata *data)
4492{
David Quigley14c43f72013-05-22 12:50:43 -04004493 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004494 kfree(data);
4495}
4496
Chuck Lever4f390c12006-08-22 20:06:22 -04004497static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004498 struct page *page, unsigned int len, struct iattr *sattr,
4499 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004501 struct nfs4_createdata *data;
4502 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Chuck Lever94a6d752006-08-22 20:06:23 -04004504 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004505 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004506
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004507 status = -ENOMEM;
4508 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4509 if (data == NULL)
4510 goto out;
4511
4512 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4513 data->arg.u.symlink.pages = &page;
4514 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004515 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004517 status = nfs4_do_create(dir, dentry, data);
4518
4519 nfs4_free_createdata(data);
4520out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return status;
4522}
4523
Chuck Lever4f390c12006-08-22 20:06:22 -04004524static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004525 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004528 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004530
4531 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004534 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4535 trace_nfs4_symlink(dir, &dentry->d_name, err);
4536 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 &exception);
4538 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004539
4540 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 return err;
4542}
4543
4544static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004545 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004547 struct nfs4_createdata *data;
4548 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004550 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4551 if (data == NULL)
4552 goto out;
4553
David Quigley1775fd32013-05-22 12:50:42 -04004554 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004555 status = nfs4_do_create(dir, dentry, data);
4556
4557 nfs4_free_createdata(data);
4558out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 return status;
4560}
4561
4562static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4563 struct iattr *sattr)
4564{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004565 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004567 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004569
David Quigleyaa9c2662013-05-22 12:50:44 -04004570 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4571
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004572 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4573 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004575 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4576 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4577 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 &exception);
4579 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004580 nfs4_label_release_security(label);
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 return err;
4583}
4584
4585static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004586 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587{
David Howells2b0143b2015-03-17 22:25:59 +00004588 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 struct nfs4_readdir_arg args = {
4590 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004591 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 .pgbase = 0,
4593 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004594 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004595 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 };
4597 struct nfs4_readdir_res res;
4598 struct rpc_message msg = {
4599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4600 .rpc_argp = &args,
4601 .rpc_resp = &res,
4602 .rpc_cred = cred,
4603 };
4604 int status;
4605
Al Viro6de14722013-09-16 10:53:17 -04004606 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4607 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004608 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004609 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004611 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 -05004612 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004613 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004614 status += args.pgbase;
4615 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004616
4617 nfs_invalidate_atime(dir);
4618
Harvey Harrison3110ff82008-05-02 13:42:44 -07004619 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 return status;
4621}
4622
4623static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004624 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
4626 struct nfs4_exception exception = { };
4627 int err;
4628 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004629 err = _nfs4_proc_readdir(dentry, cred, cookie,
4630 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004631 trace_nfs4_readdir(d_inode(dentry), err);
4632 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 &exception);
4634 } while (exception.retry);
4635 return err;
4636}
4637
4638static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004639 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004641 struct nfs4_createdata *data;
4642 int mode = sattr->ia_mode;
4643 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004645 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4646 if (data == NULL)
4647 goto out;
4648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004650 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004652 data->arg.ftype = NF4BLK;
4653 data->arg.u.device.specdata1 = MAJOR(rdev);
4654 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
4656 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004657 data->arg.ftype = NF4CHR;
4658 data->arg.u.device.specdata1 = MAJOR(rdev);
4659 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004660 } else if (!S_ISSOCK(mode)) {
4661 status = -EINVAL;
4662 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
David Quigley1775fd32013-05-22 12:50:42 -04004664
David Quigleyaa9c2662013-05-22 12:50:44 -04004665 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004666 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004667out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004668 nfs4_free_createdata(data);
4669out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 return status;
4671}
4672
4673static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4674 struct iattr *sattr, dev_t rdev)
4675{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004676 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004678 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004680
David Quigleyaa9c2662013-05-22 12:50:44 -04004681 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4682
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004683 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4684 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004686 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4687 trace_nfs4_mknod(dir, &dentry->d_name, err);
4688 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 &exception);
4690 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004691
4692 nfs4_label_release_security(label);
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return err;
4695}
4696
4697static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4698 struct nfs_fsstat *fsstat)
4699{
4700 struct nfs4_statfs_arg args = {
4701 .fh = fhandle,
4702 .bitmask = server->attr_bitmask,
4703 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004704 struct nfs4_statfs_res res = {
4705 .fsstat = fsstat,
4706 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 struct rpc_message msg = {
4708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4709 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004710 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 };
4712
Trond Myklebust0e574af2005-10-27 22:12:38 -04004713 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004714 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
4717static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4718{
4719 struct nfs4_exception exception = { };
4720 int err;
4721 do {
4722 err = nfs4_handle_exception(server,
4723 _nfs4_proc_statfs(server, fhandle, fsstat),
4724 &exception);
4725 } while (exception.retry);
4726 return err;
4727}
4728
4729static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4730 struct nfs_fsinfo *fsinfo)
4731{
4732 struct nfs4_fsinfo_arg args = {
4733 .fh = fhandle,
4734 .bitmask = server->attr_bitmask,
4735 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004736 struct nfs4_fsinfo_res res = {
4737 .fsinfo = fsinfo,
4738 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 struct rpc_message msg = {
4740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4741 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004742 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 };
4744
Bryan Schumaker7c513052011-03-24 17:12:24 +00004745 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
4748static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4749{
4750 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004751 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 int err;
4753
4754 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004755 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004756 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004757 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004758 nfs4_set_lease_period(server->nfs_client,
4759 fsinfo->lease_time * HZ,
4760 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004761 break;
4762 }
4763 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 } while (exception.retry);
4765 return err;
4766}
4767
4768static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4769{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004770 int error;
4771
Trond Myklebust0e574af2005-10-27 22:12:38 -04004772 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004773 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004774 if (error == 0) {
4775 /* block layout checks this! */
4776 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004777 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004778 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004779
4780 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
4782
4783static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4784 struct nfs_pathconf *pathconf)
4785{
4786 struct nfs4_pathconf_arg args = {
4787 .fh = fhandle,
4788 .bitmask = server->attr_bitmask,
4789 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004790 struct nfs4_pathconf_res res = {
4791 .pathconf = pathconf,
4792 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 struct rpc_message msg = {
4794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4795 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004796 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 };
4798
4799 /* None of the pathconf attributes are mandatory to implement */
4800 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4801 memset(pathconf, 0, sizeof(*pathconf));
4802 return 0;
4803 }
4804
Trond Myklebust0e574af2005-10-27 22:12:38 -04004805 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004806 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807}
4808
4809static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4810 struct nfs_pathconf *pathconf)
4811{
4812 struct nfs4_exception exception = { };
4813 int err;
4814
4815 do {
4816 err = nfs4_handle_exception(server,
4817 _nfs4_proc_pathconf(server, fhandle, pathconf),
4818 &exception);
4819 } while (exception.retry);
4820 return err;
4821}
4822
Trond Myklebust5521abf2013-03-16 20:54:34 -04004823int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004824 const struct nfs_open_context *ctx,
4825 const struct nfs_lock_context *l_ctx,
4826 fmode_t fmode)
4827{
NeilBrown17393472016-10-13 15:26:47 +11004828 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004829}
4830EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4831
Trond Myklebust5521abf2013-03-16 20:54:34 -04004832static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4833 const struct nfs_open_context *ctx,
4834 const struct nfs_lock_context *l_ctx,
4835 fmode_t fmode)
4836{
4837 nfs4_stateid current_stateid;
4838
Trond Myklebuste1253be2014-03-05 08:44:23 -05004839 /* If the current stateid represents a lost lock, then exit */
4840 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4841 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004842 return nfs4_stateid_match(stateid, &current_stateid);
4843}
4844
4845static bool nfs4_error_stateid_expired(int err)
4846{
4847 switch (err) {
4848 case -NFS4ERR_DELEG_REVOKED:
4849 case -NFS4ERR_ADMIN_REVOKED:
4850 case -NFS4ERR_BAD_STATEID:
4851 case -NFS4ERR_STALE_STATEID:
4852 case -NFS4ERR_OLD_STATEID:
4853 case -NFS4ERR_OPENMODE:
4854 case -NFS4ERR_EXPIRED:
4855 return true;
4856 }
4857 return false;
4858}
4859
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004860static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004862 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004864 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004865 if (task->tk_status < 0) {
4866 struct nfs4_exception exception = {
4867 .inode = hdr->inode,
4868 .state = hdr->args.context->state,
4869 .stateid = &hdr->args.stateid,
4870 };
4871 task->tk_status = nfs4_async_handle_exception(task,
4872 server, task->tk_status, &exception);
4873 if (exception.retry) {
4874 rpc_restart_call_prepare(task);
4875 return -EAGAIN;
4876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004878
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004880 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
4883
Trond Myklebust5521abf2013-03-16 20:54:34 -04004884static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004885 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004886{
4887
4888 if (!nfs4_error_stateid_expired(task->tk_status) ||
4889 nfs4_stateid_is_current(&args->stateid,
4890 args->context,
4891 args->lock_context,
4892 FMODE_READ))
4893 return false;
4894 rpc_restart_call_prepare(task);
4895 return true;
4896}
4897
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004898static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004899{
4900
4901 dprintk("--> %s\n", __func__);
4902
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004903 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004904 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004906 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004907 if (task->tk_status > 0)
4908 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004909 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4910 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004911}
4912
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004913static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4914 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004917 if (!hdr->pgio_done_cb)
4918 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004919 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004920 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004923static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4924 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004925{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004926 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004927 &hdr->args.seq_args,
4928 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004929 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004930 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004931 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4932 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004933 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004934 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004935 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004936 return -EIO;
4937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004940static int nfs4_write_done_cb(struct rpc_task *task,
4941 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004943 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004944
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004945 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004946 if (task->tk_status < 0) {
4947 struct nfs4_exception exception = {
4948 .inode = hdr->inode,
4949 .state = hdr->args.context->state,
4950 .stateid = &hdr->args.stateid,
4951 };
4952 task->tk_status = nfs4_async_handle_exception(task,
4953 NFS_SERVER(inode), task->tk_status,
4954 &exception);
4955 if (exception.retry) {
4956 rpc_restart_call_prepare(task);
4957 return -EAGAIN;
4958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004960 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004961 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004962 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004963 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
Trond Myklebust5521abf2013-03-16 20:54:34 -04004967static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004968 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004969{
4970
4971 if (!nfs4_error_stateid_expired(task->tk_status) ||
4972 nfs4_stateid_is_current(&args->stateid,
4973 args->context,
4974 args->lock_context,
4975 FMODE_WRITE))
4976 return false;
4977 rpc_restart_call_prepare(task);
4978 return true;
4979}
4980
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004981static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004982{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004983 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004984 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004985 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004986 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004987 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4988 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004989}
4990
Trond Myklebust5a37f852012-04-28 14:55:16 -04004991static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004992bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004993{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004994 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004995 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004996 return false;
4997 /* Otherwise, request attributes if and only if we don't hold
4998 * a delegation
4999 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005000 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005001}
Fred Isamana69aef12011-03-03 15:13:47 +00005002
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005003static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005004 struct rpc_message *msg,
5005 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005007 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005008
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005009 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5010 hdr->args.bitmask = NULL;
5011 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005012 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005013 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005014
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005015 if (!hdr->pgio_done_cb)
5016 hdr->pgio_done_cb = nfs4_write_done_cb;
5017 hdr->res.server = server;
5018 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005020 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005021 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005022 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
Fred Isaman0b7c0152012-04-20 14:47:39 -04005025static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5026{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005027 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005028 &data->args.seq_args,
5029 &data->res.seq_res,
5030 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031}
5032
Fred Isaman0b7c0152012-04-20 14:47:39 -04005033static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005036
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005037 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005038 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5039 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005040 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005041 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044}
5045
Fred Isaman0b7c0152012-04-20 14:47:39 -04005046static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005047{
5048 if (!nfs4_sequence_done(task, &data->res.seq_res))
5049 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005050 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005051}
5052
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005053static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5054 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005056 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005057
Fred Isaman0b7c0152012-04-20 14:47:39 -04005058 if (data->commit_done_cb == NULL)
5059 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005060 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005061 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005062 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005063 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005066struct nfs4_renewdata {
5067 struct nfs_client *client;
5068 unsigned long timestamp;
5069};
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071/*
5072 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5073 * standalone procedure for queueing an asynchronous RENEW.
5074 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005075static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005076{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005077 struct nfs4_renewdata *data = calldata;
5078 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005079
Elena Reshetova212bf412017-10-20 12:53:38 +03005080 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005081 nfs4_schedule_state_renewal(clp);
5082 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005083 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005084}
5085
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005086static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005088 struct nfs4_renewdata *data = calldata;
5089 struct nfs_client *clp = data->client;
5090 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005092 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005093 switch (task->tk_status) {
5094 case 0:
5095 break;
5096 case -NFS4ERR_LEASE_MOVED:
5097 nfs4_schedule_lease_moved_recovery(clp);
5098 break;
5099 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005100 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005101 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5102 return;
5103 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005104 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005105 return;
5106 }
5107 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005109 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005112static const struct rpc_call_ops nfs4_renew_ops = {
5113 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005114 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005115};
5116
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005117static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118{
5119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5121 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005122 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005124 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005126 if (renew_flags == 0)
5127 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005128 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005129 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005130 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005131 if (data == NULL) {
5132 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005133 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005134 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005135 data->client = clp;
5136 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005137 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005138 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139}
5140
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005141static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
5143 struct rpc_message msg = {
5144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5145 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005146 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 };
5148 unsigned long now = jiffies;
5149 int status;
5150
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005151 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if (status < 0)
5153 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005154 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return 0;
5156}
5157
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005158static inline int nfs4_server_supports_acls(struct nfs_server *server)
5159{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005160 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005161}
5162
Trond Myklebust21f498c2012-08-24 10:59:25 -04005163/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5164 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005165 * the stack.
5166 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005167#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005168
Neil Hormane9e3d722011-03-04 19:26:03 -05005169static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005170 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005171{
5172 struct page *newpage, **spages;
5173 int rc = 0;
5174 size_t len;
5175 spages = pages;
5176
5177 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005178 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005179 newpage = alloc_page(GFP_KERNEL);
5180
5181 if (newpage == NULL)
5182 goto unwind;
5183 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005184 buf += len;
5185 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005186 *pages++ = newpage;
5187 rc++;
5188 } while (buflen != 0);
5189
5190 return rc;
5191
5192unwind:
5193 for(; rc > 0; rc--)
5194 __free_page(spages[rc-1]);
5195 return -ENOMEM;
5196}
5197
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005198struct nfs4_cached_acl {
5199 int cached;
5200 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005201 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005202};
5203
5204static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005205{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005206 struct nfs_inode *nfsi = NFS_I(inode);
5207
5208 spin_lock(&inode->i_lock);
5209 kfree(nfsi->nfs4_acl);
5210 nfsi->nfs4_acl = acl;
5211 spin_unlock(&inode->i_lock);
5212}
5213
5214static void nfs4_zap_acl_attr(struct inode *inode)
5215{
5216 nfs4_set_cached_acl(inode, NULL);
5217}
5218
5219static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5220{
5221 struct nfs_inode *nfsi = NFS_I(inode);
5222 struct nfs4_cached_acl *acl;
5223 int ret = -ENOENT;
5224
5225 spin_lock(&inode->i_lock);
5226 acl = nfsi->nfs4_acl;
5227 if (acl == NULL)
5228 goto out;
5229 if (buf == NULL) /* user is just asking for length */
5230 goto out_len;
5231 if (acl->cached == 0)
5232 goto out;
5233 ret = -ERANGE; /* see getxattr(2) man page */
5234 if (acl->len > buflen)
5235 goto out;
5236 memcpy(buf, acl->data, acl->len);
5237out_len:
5238 ret = acl->len;
5239out:
5240 spin_unlock(&inode->i_lock);
5241 return ret;
5242}
5243
Sachin Prabhu5794d212012-04-17 14:36:40 +01005244static 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 +00005245{
5246 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005247 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005248
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005249 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005250 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005251 if (acl == NULL)
5252 goto out;
5253 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005254 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005255 } else {
5256 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5257 if (acl == NULL)
5258 goto out;
5259 acl->cached = 0;
5260 }
5261 acl->len = acl_len;
5262out:
5263 nfs4_set_cached_acl(inode, acl);
5264}
5265
Andy Adamsonbf118a32011-12-07 11:55:27 -05005266/*
5267 * The getxattr API returns the required buffer length when called with a
5268 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5269 * the required buf. On a NULL buf, we send a page of data to the server
5270 * guessing that the ACL request can be serviced by a page. If so, we cache
5271 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5272 * the cache. If not so, we throw away the page, and cache the required
5273 * length. The next getxattr call will then produce another round trip to
5274 * the server, this time with the input buf of the required size.
5275 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005276static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005277{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005278 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005279 struct nfs_getaclargs args = {
5280 .fh = NFS_FH(inode),
5281 .acl_pages = pages,
5282 .acl_len = buflen,
5283 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005284 struct nfs_getaclres res = {
5285 .acl_len = buflen,
5286 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5289 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005290 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005291 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005292 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005293 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005294
Trond Myklebust21f498c2012-08-24 10:59:25 -04005295 if (npages > ARRAY_SIZE(pages))
5296 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005297
Andy Adamsonbf118a32011-12-07 11:55:27 -05005298 for (i = 0; i < npages; i++) {
5299 pages[i] = alloc_page(GFP_KERNEL);
5300 if (!pages[i])
5301 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005302 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005303
5304 /* for decoding across pages */
5305 res.acl_scratch = alloc_page(GFP_KERNEL);
5306 if (!res.acl_scratch)
5307 goto out_free;
5308
Andy Adamsonbf118a32011-12-07 11:55:27 -05005309 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005310
Peng Taode040be2012-01-10 22:42:47 +08005311 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005312 __func__, buf, buflen, npages, args.acl_len);
5313 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5314 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005315 if (ret)
5316 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005317
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005318 /* Handle the case where the passed-in buffer is too short */
5319 if (res.acl_flags & NFS4_ACL_TRUNC) {
5320 /* Did the user only issue a request for the acl length? */
5321 if (buf == NULL)
5322 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005323 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005324 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005325 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005326 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005327 if (buf) {
5328 if (res.acl_len > buflen) {
5329 ret = -ERANGE;
5330 goto out_free;
5331 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005332 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005333 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005334out_ok:
5335 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005336out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005337 for (i = 0; i < npages; i++)
5338 if (pages[i])
5339 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005340 if (res.acl_scratch)
5341 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005342 return ret;
5343}
5344
Trond Myklebust16b42892006-08-24 12:27:15 -04005345static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5346{
5347 struct nfs4_exception exception = { };
5348 ssize_t ret;
5349 do {
5350 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005351 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005352 if (ret >= 0)
5353 break;
5354 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5355 } while (exception.retry);
5356 return ret;
5357}
5358
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005359static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5360{
5361 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005362 int ret;
5363
5364 if (!nfs4_server_supports_acls(server))
5365 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005366 ret = nfs_revalidate_inode(server, inode);
5367 if (ret < 0)
5368 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005369 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5370 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005371 ret = nfs4_read_cached_acl(inode, buf, buflen);
5372 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005373 /* -ENOENT is returned if there is no ACL or if there is an ACL
5374 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005375 return ret;
5376 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005377}
5378
Trond Myklebust16b42892006-08-24 12:27:15 -04005379static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005380{
5381 struct nfs_server *server = NFS_SERVER(inode);
5382 struct page *pages[NFS4ACL_MAXPAGES];
5383 struct nfs_setaclargs arg = {
5384 .fh = NFS_FH(inode),
5385 .acl_pages = pages,
5386 .acl_len = buflen,
5387 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005388 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005389 struct rpc_message msg = {
5390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5391 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005392 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005393 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005394 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005395 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005396
5397 if (!nfs4_server_supports_acls(server))
5398 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005399 if (npages > ARRAY_SIZE(pages))
5400 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005401 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005402 if (i < 0)
5403 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005404 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005405 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005406
5407 /*
5408 * Free each page after tx, so the only ref left is
5409 * held by the network stack
5410 */
5411 for (; i > 0; i--)
5412 put_page(pages[i-1]);
5413
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005414 /*
5415 * Acl update can result in inode attribute update.
5416 * so mark the attribute cache invalid.
5417 */
5418 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005419 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005420 | NFS_INO_INVALID_CTIME
5421 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005422 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005423 nfs_access_zap_cache(inode);
5424 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005425 return ret;
5426}
5427
Trond Myklebust16b42892006-08-24 12:27:15 -04005428static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5429{
5430 struct nfs4_exception exception = { };
5431 int err;
5432 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005433 err = __nfs4_proc_set_acl(inode, buf, buflen);
5434 trace_nfs4_set_acl(inode, err);
5435 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005436 &exception);
5437 } while (exception.retry);
5438 return err;
5439}
5440
David Quigleyaa9c2662013-05-22 12:50:44 -04005441#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5442static int _nfs4_get_security_label(struct inode *inode, void *buf,
5443 size_t buflen)
5444{
5445 struct nfs_server *server = NFS_SERVER(inode);
5446 struct nfs_fattr fattr;
5447 struct nfs4_label label = {0, 0, buflen, buf};
5448
5449 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005450 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005451 .fh = NFS_FH(inode),
5452 .bitmask = bitmask,
5453 };
5454 struct nfs4_getattr_res res = {
5455 .fattr = &fattr,
5456 .label = &label,
5457 .server = server,
5458 };
5459 struct rpc_message msg = {
5460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005461 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005462 .rpc_resp = &res,
5463 };
5464 int ret;
5465
5466 nfs_fattr_init(&fattr);
5467
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005468 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005469 if (ret)
5470 return ret;
5471 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5472 return -ENOENT;
5473 if (buflen < label.len)
5474 return -ERANGE;
5475 return 0;
5476}
5477
5478static int nfs4_get_security_label(struct inode *inode, void *buf,
5479 size_t buflen)
5480{
5481 struct nfs4_exception exception = { };
5482 int err;
5483
5484 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5485 return -EOPNOTSUPP;
5486
5487 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005488 err = _nfs4_get_security_label(inode, buf, buflen);
5489 trace_nfs4_get_security_label(inode, err);
5490 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005491 &exception);
5492 } while (exception.retry);
5493 return err;
5494}
5495
5496static int _nfs4_do_set_security_label(struct inode *inode,
5497 struct nfs4_label *ilabel,
5498 struct nfs_fattr *fattr,
5499 struct nfs4_label *olabel)
5500{
5501
5502 struct iattr sattr = {0};
5503 struct nfs_server *server = NFS_SERVER(inode);
5504 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005505 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005506 .fh = NFS_FH(inode),
5507 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005508 .server = server,
5509 .bitmask = bitmask,
5510 .label = ilabel,
5511 };
5512 struct nfs_setattrres res = {
5513 .fattr = fattr,
5514 .label = olabel,
5515 .server = server,
5516 };
5517 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5519 .rpc_argp = &arg,
5520 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005521 };
5522 int status;
5523
Jeff Layton12207f62013-11-01 10:49:32 -04005524 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005525
Jeff Layton12207f62013-11-01 10:49:32 -04005526 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005527 if (status)
5528 dprintk("%s failed: %d\n", __func__, status);
5529
5530 return status;
5531}
5532
5533static int nfs4_do_set_security_label(struct inode *inode,
5534 struct nfs4_label *ilabel,
5535 struct nfs_fattr *fattr,
5536 struct nfs4_label *olabel)
5537{
5538 struct nfs4_exception exception = { };
5539 int err;
5540
5541 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005542 err = _nfs4_do_set_security_label(inode, ilabel,
5543 fattr, olabel);
5544 trace_nfs4_set_security_label(inode, err);
5545 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005546 &exception);
5547 } while (exception.retry);
5548 return err;
5549}
5550
5551static int
Al Viro59301222016-05-27 10:19:30 -04005552nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005553{
5554 struct nfs4_label ilabel, *olabel = NULL;
5555 struct nfs_fattr fattr;
5556 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005557 int status;
5558
5559 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5560 return -EOPNOTSUPP;
5561
5562 nfs_fattr_init(&fattr);
5563
5564 ilabel.pi = 0;
5565 ilabel.lfs = 0;
5566 ilabel.label = (char *)buf;
5567 ilabel.len = buflen;
5568
5569 cred = rpc_lookup_cred();
5570 if (IS_ERR(cred))
5571 return PTR_ERR(cred);
5572
5573 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5574 if (IS_ERR(olabel)) {
5575 status = -PTR_ERR(olabel);
5576 goto out;
5577 }
5578
5579 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5580 if (status == 0)
5581 nfs_setsecurity(inode, &fattr, olabel);
5582
5583 nfs4_label_free(olabel);
5584out:
5585 put_rpccred(cred);
5586 return status;
5587}
5588#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5589
5590
Chuck Leverf0920752012-05-21 22:45:41 -04005591static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5592 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005593{
5594 __be32 verf[2];
5595
Chuck Lever2c820d92012-05-21 22:45:33 -04005596 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5597 /* An impossible timestamp guarantees this value
5598 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005599 verf[0] = cpu_to_be32(U32_MAX);
5600 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005601 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005602 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005603 u64 ns = ktime_to_ns(nn->boot_time);
5604
5605 verf[0] = cpu_to_be32(ns >> 32);
5606 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005607 }
Chuck Levercd937102012-03-02 17:14:31 -05005608 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5609}
5610
Jeff Laytona3192682015-06-09 19:43:59 -04005611static int
5612nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005613{
Jeff Laytona3192682015-06-09 19:43:59 -04005614 size_t len;
5615 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005616
Trond Myklebustceb3a162015-01-03 15:16:04 -05005617 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005618 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005619
Jeff Laytona3192682015-06-09 19:43:59 -04005620 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005621 len = 14 +
5622 strlen(clp->cl_rpcclient->cl_nodename) +
5623 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005624 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005625 1;
5626 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005627 if (nfs4_client_id_uniquifier[0] != '\0')
5628 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005629 if (len > NFS4_OPAQUE_LIMIT + 1)
5630 return -EINVAL;
5631
5632 /*
5633 * Since this string is allocated at mount time, and held until the
5634 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5635 * about a memory-reclaim deadlock.
5636 */
5637 str = kmalloc(len, GFP_KERNEL);
5638 if (!str)
5639 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005640
Chuck Levere984a552012-09-14 17:24:21 -04005641 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005642 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005643 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005644 clp->cl_rpcclient->cl_nodename,
5645 nfs4_client_id_uniquifier,
5646 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005647 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005648 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005649 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005650 clp->cl_rpcclient->cl_nodename,
5651 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005652 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005653 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005654
Jeff Laytona3192682015-06-09 19:43:59 -04005655 clp->cl_owner_id = str;
5656 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005657}
5658
Jeff Layton873e3852015-06-09 19:44:00 -04005659static int
5660nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005661{
Jeff Layton873e3852015-06-09 19:44:00 -04005662 size_t len;
5663 char *str;
5664
5665 len = 10 + 10 + 1 + 10 + 1 +
5666 strlen(nfs4_client_id_uniquifier) + 1 +
5667 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5668
5669 if (len > NFS4_OPAQUE_LIMIT + 1)
5670 return -EINVAL;
5671
5672 /*
5673 * Since this string is allocated at mount time, and held until the
5674 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5675 * about a memory-reclaim deadlock.
5676 */
5677 str = kmalloc(len, GFP_KERNEL);
5678 if (!str)
5679 return -ENOMEM;
5680
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005681 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005682 clp->rpc_ops->version, clp->cl_minorversion,
5683 nfs4_client_id_uniquifier,
5684 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005685 clp->cl_owner_id = str;
5686 return 0;
5687}
5688
5689static int
5690nfs4_init_uniform_client_string(struct nfs_client *clp)
5691{
Jeff Layton873e3852015-06-09 19:44:00 -04005692 size_t len;
5693 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005694
5695 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005696 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005697
5698 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005699 return nfs4_init_uniquifier_client_string(clp);
5700
5701 len = 10 + 10 + 1 + 10 + 1 +
5702 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5703
5704 if (len > NFS4_OPAQUE_LIMIT + 1)
5705 return -EINVAL;
5706
5707 /*
5708 * Since this string is allocated at mount time, and held until the
5709 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5710 * about a memory-reclaim deadlock.
5711 */
5712 str = kmalloc(len, GFP_KERNEL);
5713 if (!str)
5714 return -ENOMEM;
5715
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005716 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005717 clp->rpc_ops->version, clp->cl_minorversion,
5718 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005719 clp->cl_owner_id = str;
5720 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005721}
5722
Chuck Lever706cb8d2014-03-12 12:51:47 -04005723/*
5724 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5725 * services. Advertise one based on the address family of the
5726 * clientaddr.
5727 */
5728static unsigned int
5729nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5730{
5731 if (strchr(clp->cl_ipaddr, ':') != NULL)
5732 return scnprintf(buf, len, "tcp6");
5733 else
5734 return scnprintf(buf, len, "tcp");
5735}
5736
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005737static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5738{
5739 struct nfs4_setclientid *sc = calldata;
5740
5741 if (task->tk_status == 0)
5742 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5743}
5744
5745static const struct rpc_call_ops nfs4_setclientid_ops = {
5746 .rpc_call_done = nfs4_setclientid_done,
5747};
5748
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005749/**
5750 * nfs4_proc_setclientid - Negotiate client ID
5751 * @clp: state data structure
5752 * @program: RPC program for NFSv4 callback service
5753 * @port: IP port number for NFS4 callback service
5754 * @cred: RPC credential to use for this call
5755 * @res: where to place the result
5756 *
5757 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5758 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005759int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5760 unsigned short port, struct rpc_cred *cred,
5761 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762{
5763 nfs4_verifier sc_verifier;
5764 struct nfs4_setclientid setclientid = {
5765 .sc_verifier = &sc_verifier,
5766 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005767 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 };
5769 struct rpc_message msg = {
5770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5771 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005772 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005773 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005775 struct rpc_task *task;
5776 struct rpc_task_setup task_setup_data = {
5777 .rpc_client = clp->cl_rpcclient,
5778 .rpc_message = &msg,
5779 .callback_ops = &nfs4_setclientid_ops,
5780 .callback_data = &setclientid,
5781 .flags = RPC_TASK_TIMEOUT,
5782 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005783 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Chuck Leverde734832012-07-11 16:30:50 -04005785 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005786 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005787
5788 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5789 status = nfs4_init_uniform_client_string(clp);
5790 else
Jeff Laytona3192682015-06-09 19:43:59 -04005791 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005792
5793 if (status)
5794 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005795
Chuck Leverde734832012-07-11 16:30:50 -04005796 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005797 setclientid.sc_netid_len =
5798 nfs4_init_callback_netid(clp,
5799 setclientid.sc_netid,
5800 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005801 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005802 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 clp->cl_ipaddr, port >> 8, port & 255);
5804
Jeff Layton3a6bb732015-06-09 19:43:57 -04005805 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005806 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005807 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005808 task = rpc_run_task(&task_setup_data);
5809 if (IS_ERR(task)) {
5810 status = PTR_ERR(task);
5811 goto out;
5812 }
5813 status = task->tk_status;
5814 if (setclientid.sc_cred) {
5815 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5816 put_rpccred(setclientid.sc_cred);
5817 }
5818 rpc_put_task(task);
5819out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005820 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005821 dprintk("NFS reply setclientid: %d\n", status);
5822 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
5824
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005825/**
5826 * nfs4_proc_setclientid_confirm - Confirm client ID
5827 * @clp: state data structure
5828 * @res: result of a previous SETCLIENTID
5829 * @cred: RPC credential to use for this call
5830 *
5831 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5832 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005833int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005834 struct nfs4_setclientid_res *arg,
5835 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 struct rpc_message msg = {
5838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005839 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005840 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 int status;
5843
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005844 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5845 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5846 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005847 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005848 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005849 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 return status;
5851}
5852
Trond Myklebustfe650402006-01-03 09:55:18 +01005853struct nfs4_delegreturndata {
5854 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005855 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005856 struct nfs_fh fh;
5857 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005858 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005859 struct {
5860 struct nfs4_layoutreturn_args arg;
5861 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005862 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005863 u32 roc_barrier;
5864 bool roc;
5865 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005866 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005867 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005868 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005869};
5870
Trond Myklebustfe650402006-01-03 09:55:18 +01005871static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5872{
5873 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005874 struct nfs4_exception exception = {
5875 .inode = data->inode,
5876 .stateid = &data->stateid,
5877 };
Andy Adamson938e1012009-04-01 09:22:28 -04005878
Trond Myklebust14516c32010-07-31 14:29:06 -04005879 if (!nfs4_sequence_done(task, &data->res.seq_res))
5880 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005881
Trond Myklebustca8acf82013-08-13 10:36:56 -04005882 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005883
5884 /* Handle Layoutreturn errors */
5885 if (data->args.lr_args && task->tk_status != 0) {
5886 switch(data->res.lr_ret) {
5887 default:
5888 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5889 break;
5890 case 0:
5891 data->args.lr_args = NULL;
5892 data->res.lr_res = NULL;
5893 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005894 case -NFS4ERR_OLD_STATEID:
5895 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005896 data->inode))
5897 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005898 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005899 case -NFS4ERR_ADMIN_REVOKED:
5900 case -NFS4ERR_DELEG_REVOKED:
5901 case -NFS4ERR_EXPIRED:
5902 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005903 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5904 case -NFS4ERR_WRONG_CRED:
5905 data->args.lr_args = NULL;
5906 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005907 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005908 }
5909 }
5910
Ricardo Labiaga79708862009-12-07 09:23:21 -05005911 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005912 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005913 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005914 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005915 case -NFS4ERR_ADMIN_REVOKED:
5916 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005917 case -NFS4ERR_EXPIRED:
5918 nfs4_free_revoked_stateid(data->res.server,
5919 data->args.stateid,
5920 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005921 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005922 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005923 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005924 task->tk_status = 0;
5925 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005926 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005927 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5928 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005929 task->tk_status = 0;
5930 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005931 case -NFS4ERR_ACCESS:
5932 if (data->args.bitmask) {
5933 data->args.bitmask = NULL;
5934 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005935 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005936 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005937 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005938 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005939 task->tk_status = nfs4_async_handle_exception(task,
5940 data->res.server, task->tk_status,
5941 &exception);
5942 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005943 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005944 }
5945 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005946 return;
5947lr_restart:
5948 data->res.lr_ret = 0;
5949out_restart:
5950 task->tk_status = 0;
5951 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005952}
5953
5954static void nfs4_delegreturn_release(void *calldata)
5955{
Peng Tao039b7562014-07-03 13:05:02 +08005956 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005957 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005958
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005959 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005960 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005961 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5962 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005963 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005964 nfs_iput_and_deactive(inode);
5965 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005966 kfree(calldata);
5967}
5968
Andy Adamson938e1012009-04-01 09:22:28 -04005969static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5970{
5971 struct nfs4_delegreturndata *d_data;
5972
5973 d_data = (struct nfs4_delegreturndata *)data;
5974
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005975 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005976 return;
5977
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005978 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005979 &d_data->args.seq_args,
5980 &d_data->res.seq_res,
5981 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005982}
Andy Adamson938e1012009-04-01 09:22:28 -04005983
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005984static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005985 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005986 .rpc_call_done = nfs4_delegreturn_done,
5987 .rpc_release = nfs4_delegreturn_release,
5988};
5989
Trond Myklebuste6f81072008-01-24 18:14:34 -05005990static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005991{
5992 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005993 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005994 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005995 struct rpc_message msg = {
5996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5997 .rpc_cred = cred,
5998 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005999 struct rpc_task_setup task_setup_data = {
6000 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006001 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006002 .callback_ops = &nfs4_delegreturn_ops,
6003 .flags = RPC_TASK_ASYNC,
6004 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006005 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006006
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006007 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006008 if (data == NULL)
6009 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006010 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006011
6012 nfs4_state_protect(server->nfs_client,
6013 NFS_SP4_MACH_CRED_CLEANUP,
6014 &task_setup_data.rpc_client, &msg);
6015
Trond Myklebustfe650402006-01-03 09:55:18 +01006016 data->args.fhandle = &data->fh;
6017 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006018 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006019 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006020 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006021 data->res.fattr = &data->fattr;
6022 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006023 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006024 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006025 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006026 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006027 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006028 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006029 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006030 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006031 if (data->lr.roc) {
6032 data->args.lr_args = &data->lr.arg;
6033 data->res.lr_res = &data->lr.res;
6034 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006035 } else if (data->lr.roc) {
6036 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6037 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006038 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006039
Trond Myklebustc970aa82007-07-14 15:39:59 -04006040 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006041 msg.rpc_argp = &data->args;
6042 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006043 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006044 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006045 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006046 if (!issync)
6047 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006048 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006049 if (status != 0)
6050 goto out;
6051 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006052out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006053 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006054 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055}
6056
Trond Myklebuste6f81072008-01-24 18:14:34 -05006057int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058{
6059 struct nfs_server *server = NFS_SERVER(inode);
6060 struct nfs4_exception exception = { };
6061 int err;
6062 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006063 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006064 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 switch (err) {
6066 case -NFS4ERR_STALE_STATEID:
6067 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 case 0:
6069 return 0;
6070 }
6071 err = nfs4_handle_exception(server, err, &exception);
6072 } while (exception.retry);
6073 return err;
6074}
6075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6077{
6078 struct inode *inode = state->inode;
6079 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006080 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006081 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006083 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006085 struct nfs_lockt_res res = {
6086 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 };
6088 struct rpc_message msg = {
6089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006090 .rpc_argp = &arg,
6091 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 .rpc_cred = state->owner->so_cred,
6093 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 struct nfs4_lock_state *lsp;
6095 int status;
6096
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006097 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006098 status = nfs4_set_lock_state(state, request);
6099 if (status != 0)
6100 goto out;
6101 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006102 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006103 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006104 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006105 switch (status) {
6106 case 0:
6107 request->fl_type = F_UNLCK;
6108 break;
6109 case -NFS4ERR_DENIED:
6110 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006112 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006113 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006114out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return status;
6116}
6117
6118static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6119{
6120 struct nfs4_exception exception = { };
6121 int err;
6122
6123 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006124 err = _nfs4_proc_getlk(state, cmd, request);
6125 trace_nfs4_get_lock(request, state, cmd, err);
6126 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 &exception);
6128 } while (exception.retry);
6129 return err;
6130}
6131
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006132struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006133 struct nfs_locku_args arg;
6134 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006135 struct nfs4_lock_state *lsp;
6136 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006137 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006138 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006139 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006140 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006141};
6142
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006143static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6144 struct nfs_open_context *ctx,
6145 struct nfs4_lock_state *lsp,
6146 struct nfs_seqid *seqid)
6147{
6148 struct nfs4_unlockdata *p;
6149 struct inode *inode = lsp->ls_state->inode;
6150
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006151 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006152 if (p == NULL)
6153 return NULL;
6154 p->arg.fh = NFS_FH(inode);
6155 p->arg.fl = &p->fl;
6156 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006157 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006158 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006159 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006160 /* Ensure we don't close file until we're done freeing locks! */
6161 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006162 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006163 memcpy(&p->fl, fl, sizeof(p->fl));
6164 p->server = NFS_SERVER(inode);
6165 return p;
6166}
6167
Trond Myklebust06f814a2006-01-03 09:55:07 +01006168static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006169{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006170 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006171 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006172 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006173 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006174 put_nfs_open_context(calldata->ctx);
6175 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006176}
6177
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006178static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006179{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006180 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006181 struct nfs4_exception exception = {
6182 .inode = calldata->lsp->ls_state->inode,
6183 .stateid = &calldata->arg.stateid,
6184 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006185
Trond Myklebust14516c32010-07-31 14:29:06 -04006186 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6187 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006188 switch (task->tk_status) {
6189 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006190 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006191 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006192 if (nfs4_update_lock_stateid(calldata->lsp,
6193 &calldata->res.stateid))
6194 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006195 case -NFS4ERR_ADMIN_REVOKED:
6196 case -NFS4ERR_EXPIRED:
6197 nfs4_free_revoked_stateid(calldata->server,
6198 &calldata->arg.stateid,
6199 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006200 case -NFS4ERR_BAD_STATEID:
6201 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006202 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006203 if (!nfs4_stateid_match(&calldata->arg.stateid,
6204 &calldata->lsp->ls_stateid))
6205 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006206 break;
6207 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006208 task->tk_status = nfs4_async_handle_exception(task,
6209 calldata->server, task->tk_status,
6210 &exception);
6211 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006212 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006213 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006214 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006215}
6216
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006217static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006218{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006219 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006221 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6222 nfs_async_iocounter_wait(task, calldata->l_ctx))
6223 return;
6224
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006225 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006226 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006227 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006228 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006229 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006230 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006231 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006232 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006233 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006234 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006235 &calldata->res.seq_res,
6236 task) != 0)
6237 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006238 return;
6239out_no_action:
6240 task->tk_action = NULL;
6241out_wait:
6242 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243}
6244
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006245static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006246 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006247 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006248 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006249};
6250
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006251static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6252 struct nfs_open_context *ctx,
6253 struct nfs4_lock_state *lsp,
6254 struct nfs_seqid *seqid)
6255{
6256 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006257 struct rpc_message msg = {
6258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6259 .rpc_cred = ctx->cred,
6260 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006261 struct rpc_task_setup task_setup_data = {
6262 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006263 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006264 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006266 .flags = RPC_TASK_ASYNC,
6267 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006268
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006269 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6270 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6271
Frank Filz137d6ac2007-07-09 15:32:29 -07006272 /* Ensure this is an unlock - when canceling a lock, the
6273 * canceled lock is passed in, and it won't be an unlock.
6274 */
6275 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006276 if (fl->fl_flags & FL_CLOSE)
6277 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006278
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006279 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6280 if (data == NULL) {
6281 nfs_free_seqid(seqid);
6282 return ERR_PTR(-ENOMEM);
6283 }
6284
Anna Schumakerfba83f32018-05-04 16:22:50 -04006285 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006286 msg.rpc_argp = &data->arg;
6287 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006288 task_setup_data.callback_data = data;
6289 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006290}
6291
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6293{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006294 struct inode *inode = state->inode;
6295 struct nfs4_state_owner *sp = state->owner;
6296 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006297 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006298 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006299 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006300 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006301 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006302 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
Trond Myklebust9b073572006-06-29 16:38:34 -04006304 status = nfs4_set_lock_state(state, request);
6305 /* Unlock _before_ we do the RPC call */
6306 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006307 /* Exclude nfs_delegation_claim_locks() */
6308 mutex_lock(&sp->so_delegreturn_mutex);
6309 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006310 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006311 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006312 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006313 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006314 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006315 }
6316 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006317 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006318 if (status != 0)
6319 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006320 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006321 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006322 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6323 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006324 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6325 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006326 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006327 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006328 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006329 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006330 status = PTR_ERR(task);
6331 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006332 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006333 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006334 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006335out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006336 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006337 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339}
6340
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006341struct nfs4_lockdata {
6342 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006343 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006344 struct nfs4_lock_state *lsp;
6345 struct nfs_open_context *ctx;
6346 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006347 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006348 int rpc_status;
6349 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006350 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351};
6352
6353static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006354 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6355 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006356{
6357 struct nfs4_lockdata *p;
6358 struct inode *inode = lsp->ls_state->inode;
6359 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006360 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006361
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006362 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006363 if (p == NULL)
6364 return NULL;
6365
6366 p->arg.fh = NFS_FH(inode);
6367 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006368 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006369 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006370 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006371 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6372 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006373 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006374 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006375 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006376 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006377 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006378 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006379 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006380 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006381 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006382 p->ctx = get_nfs_open_context(ctx);
6383 memcpy(&p->fl, fl, sizeof(p->fl));
6384 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006385out_free_seqid:
6386 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006387out_free:
6388 kfree(p);
6389 return NULL;
6390}
6391
6392static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6393{
6394 struct nfs4_lockdata *data = calldata;
6395 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Harvey Harrison3110ff82008-05-02 13:42:44 -07006397 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006398 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006399 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006400 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006401 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006402 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006403 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006404 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006405 nfs4_stateid_copy(&data->arg.open_stateid,
6406 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006407 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006408 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006409 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006410 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006411 nfs4_stateid_copy(&data->arg.lock_stateid,
6412 &data->lsp->ls_stateid);
6413 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006414 if (!nfs4_valid_open_stateid(state)) {
6415 data->rpc_status = -EBADF;
6416 task->tk_action = NULL;
6417 goto out_release_open_seqid;
6418 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006419 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006420 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006421 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006422 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006423 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006424 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006425out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006426 nfs_release_seqid(data->arg.open_seqid);
6427out_release_lock_seqid:
6428 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006429out_wait:
6430 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006431 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006432}
6433
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006434static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6435{
6436 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006437 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438
Harvey Harrison3110ff82008-05-02 13:42:44 -07006439 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006440
Trond Myklebust14516c32010-07-31 14:29:06 -04006441 if (!nfs4_sequence_done(task, &data->res.seq_res))
6442 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006443
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006444 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006445 switch (task->tk_status) {
6446 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006447 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006448 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006449 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006450 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006451 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebustc69899a2015-01-24 16:03:52 -05006452 break;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006453 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006454
Trond Myklebust39071e62015-01-24 15:07:56 -05006455 if (data->arg.new_lock_owner != 0) {
6456 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6457 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6458 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006459 goto out_done;
6460 } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
6461 goto out_done;
6462
Trond Myklebust425c1d42015-01-24 14:57:53 -05006463 break;
6464 case -NFS4ERR_BAD_STATEID:
6465 case -NFS4ERR_OLD_STATEID:
6466 case -NFS4ERR_STALE_STATEID:
6467 case -NFS4ERR_EXPIRED:
6468 if (data->arg.new_lock_owner != 0) {
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006469 if (nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006470 &lsp->ls_state->open_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006471 goto out_done;
6472 } else if (nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006473 &lsp->ls_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006474 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006475 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006476 if (!data->cancelled)
6477 rpc_restart_call_prepare(task);
6478out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006479 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006480}
6481
6482static void nfs4_lock_release(void *calldata)
6483{
6484 struct nfs4_lockdata *data = calldata;
6485
Harvey Harrison3110ff82008-05-02 13:42:44 -07006486 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006487 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006488 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006489 struct rpc_task *task;
6490 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6491 data->arg.lock_seqid);
6492 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006493 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006494 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006495 } else
6496 nfs_free_seqid(data->arg.lock_seqid);
6497 nfs4_put_lock_state(data->lsp);
6498 put_nfs_open_context(data->ctx);
6499 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006500 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006501}
6502
6503static const struct rpc_call_ops nfs4_lock_ops = {
6504 .rpc_call_prepare = nfs4_lock_prepare,
6505 .rpc_call_done = nfs4_lock_done,
6506 .rpc_release = nfs4_lock_release,
6507};
6508
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006509static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6510{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006511 switch (error) {
6512 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006513 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006514 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006515 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006516 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006517 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006518 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006519 break;
6520 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006521 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006522 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006523 };
6524}
6525
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006526static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006527{
6528 struct nfs4_lockdata *data;
6529 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006530 struct rpc_message msg = {
6531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6532 .rpc_cred = state->owner->so_cred,
6533 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006534 struct rpc_task_setup task_setup_data = {
6535 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006536 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006537 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006538 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006539 .flags = RPC_TASK_ASYNC,
6540 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006541 int ret;
6542
Harvey Harrison3110ff82008-05-02 13:42:44 -07006543 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006544 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006545 fl->fl_u.nfs4_fl.owner,
6546 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006547 if (data == NULL)
6548 return -ENOMEM;
6549 if (IS_SETLKW(cmd))
6550 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006551 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6552 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006553 msg.rpc_argp = &data->arg;
6554 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006555 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006556 if (recovery_type > NFS_LOCK_NEW) {
6557 if (recovery_type == NFS_LOCK_RECLAIM)
6558 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006559 } else
6560 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006561 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006562 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006563 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006564 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006565 if (ret == 0) {
6566 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006567 if (ret)
6568 nfs4_handle_setlk_error(data->server, data->lsp,
6569 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006570 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006571 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006572 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006573 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006574 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576}
6577
6578static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6579{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006580 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006581 struct nfs4_exception exception = {
6582 .inode = state->inode,
6583 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006584 int err;
6585
6586 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006587 /* Cache the lock if possible... */
6588 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6589 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006590 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006591 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006592 break;
6593 nfs4_handle_exception(server, err, &exception);
6594 } while (exception.retry);
6595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596}
6597
6598static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6599{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006600 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006601 struct nfs4_exception exception = {
6602 .inode = state->inode,
6603 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006604 int err;
6605
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006606 err = nfs4_set_lock_state(state, request);
6607 if (err != 0)
6608 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006609 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006610 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6611 return 0;
6612 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006613 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006614 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6615 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006616 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006617 switch (err) {
6618 default:
6619 goto out;
6620 case -NFS4ERR_GRACE:
6621 case -NFS4ERR_DELAY:
6622 nfs4_handle_exception(server, err, &exception);
6623 err = 0;
6624 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006625 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006626out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628}
6629
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006630#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006631static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6632{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006633 struct nfs4_lock_state *lsp;
6634 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006635
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006636 status = nfs4_set_lock_state(state, request);
6637 if (status != 0)
6638 return status;
6639 lsp = request->fl_u.nfs4_fl.owner;
6640 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6641 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6642 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006643 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006644}
6645#endif
6646
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6648{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006649 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006650 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006651 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006652 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
Trond Myklebust01c3b862006-06-29 16:38:39 -04006654 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006655 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006656 if (status < 0)
6657 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006658 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006659 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006660 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006661 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006662 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006663 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006664 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006665 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006666 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006667 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006668 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006669 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006670 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006671 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006672out:
6673 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 return status;
6675}
6676
6677static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6678{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006679 struct nfs4_exception exception = {
6680 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006681 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006682 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 int err;
6684
6685 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006686 err = _nfs4_proc_setlk(state, cmd, request);
6687 if (err == -NFS4ERR_DENIED)
6688 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006690 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 } while (exception.retry);
6692 return err;
6693}
6694
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006695#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6696#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6697
6698static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006699nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6700 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006701{
6702 int status = -ERESTARTSYS;
6703 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6704
6705 while(!signalled()) {
6706 status = nfs4_proc_setlk(state, cmd, request);
6707 if ((status != -EAGAIN) || IS_SETLK(cmd))
6708 break;
6709 freezable_schedule_timeout_interruptible(timeout);
6710 timeout *= 2;
6711 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6712 status = -ERESTARTSYS;
6713 }
6714 return status;
6715}
6716
Jeff Laytona1d617d82016-09-17 18:17:39 -04006717#ifdef CONFIG_NFS_V4_1
6718struct nfs4_lock_waiter {
6719 struct task_struct *task;
6720 struct inode *inode;
6721 struct nfs_lowner *owner;
6722 bool notified;
6723};
6724
6725static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006726nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006727{
6728 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006729 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006730
Jeff Layton57174592018-03-18 08:37:03 -04006731 /* NULL key means to wake up everyone */
6732 if (key) {
6733 struct cb_notify_lock_args *cbnl = key;
6734 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6735 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006736
Jeff Layton57174592018-03-18 08:37:03 -04006737 /* Only wake if the callback was for the same owner. */
6738 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6739 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006740
Jeff Layton57174592018-03-18 08:37:03 -04006741 /* Make sure it's for the right inode */
6742 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6743 return 0;
6744
6745 waiter->notified = true;
6746 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006747
6748 /* override "private" so we can use default_wake_function */
6749 wait->private = waiter->task;
6750 ret = autoremove_wake_function(wait, mode, flags, key);
6751 wait->private = waiter;
6752 return ret;
6753}
6754
6755static int
6756nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6757{
6758 int status = -ERESTARTSYS;
6759 unsigned long flags;
6760 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6761 struct nfs_server *server = NFS_SERVER(state->inode);
6762 struct nfs_client *clp = server->nfs_client;
6763 wait_queue_head_t *q = &clp->cl_lock_waitq;
6764 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6765 .id = lsp->ls_seqid.owner_id,
6766 .s_dev = server->s_dev };
6767 struct nfs4_lock_waiter waiter = { .task = current,
6768 .inode = state->inode,
6769 .owner = &owner,
6770 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006771 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006772
6773 /* Don't bother with waitqueue if we don't expect a callback */
6774 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6775 return nfs4_retry_setlk_simple(state, cmd, request);
6776
6777 init_wait(&wait);
6778 wait.private = &waiter;
6779 wait.func = nfs4_wake_lock_waiter;
6780 add_wait_queue(q, &wait);
6781
6782 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006783 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006784 status = nfs4_proc_setlk(state, cmd, request);
6785 if ((status != -EAGAIN) || IS_SETLK(cmd))
6786 break;
6787
6788 status = -ERESTARTSYS;
6789 spin_lock_irqsave(&q->lock, flags);
6790 if (waiter.notified) {
6791 spin_unlock_irqrestore(&q->lock, flags);
6792 continue;
6793 }
6794 set_current_state(TASK_INTERRUPTIBLE);
6795 spin_unlock_irqrestore(&q->lock, flags);
6796
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006797 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006798 }
6799
6800 finish_wait(q, &wait);
6801 return status;
6802}
6803#else /* !CONFIG_NFS_V4_1 */
6804static inline int
6805nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6806{
6807 return nfs4_retry_setlk_simple(state, cmd, request);
6808}
6809#endif
6810
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811static int
6812nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6813{
6814 struct nfs_open_context *ctx;
6815 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 int status;
6817
6818 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006819 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 state = ctx->state;
6821
Trond Myklebustd9531262009-07-21 19:22:38 -04006822 if (IS_GETLK(cmd)) {
6823 if (state != NULL)
6824 return nfs4_proc_getlk(state, F_GETLK, request);
6825 return 0;
6826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6829 return -EINVAL;
6830
Trond Myklebustd9531262009-07-21 19:22:38 -04006831 if (request->fl_type == F_UNLCK) {
6832 if (state != NULL)
6833 return nfs4_proc_unlck(state, cmd, request);
6834 return 0;
6835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
Trond Myklebustd9531262009-07-21 19:22:38 -04006837 if (state == NULL)
6838 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006839
6840 if ((request->fl_flags & FL_POSIX) &&
6841 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6842 return -ENOLCK;
6843
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006844 /*
6845 * Don't rely on the VFS having checked the file open mode,
6846 * since it won't do this for flock() locks.
6847 */
6848 switch (request->fl_type) {
6849 case F_RDLCK:
6850 if (!(filp->f_mode & FMODE_READ))
6851 return -EBADF;
6852 break;
6853 case F_WRLCK:
6854 if (!(filp->f_mode & FMODE_WRITE))
6855 return -EBADF;
6856 }
6857
Jeff Layton1ea67db2016-09-17 18:17:37 -04006858 status = nfs4_set_lock_state(state, request);
6859 if (status != 0)
6860 return status;
6861
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006862 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863}
6864
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006865int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006866{
6867 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006868 int err;
6869
6870 err = nfs4_set_lock_state(state, fl);
6871 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006872 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006873 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006874 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006875}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006876
Trond Myklebustcf470c32012-03-07 13:49:12 -05006877struct nfs_release_lockowner_data {
6878 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006879 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006880 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006881 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006882 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006883};
6884
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006885static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6886{
6887 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006888 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006889 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6890 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006891 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006892 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006893}
6894
6895static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6896{
6897 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006898 struct nfs_server *server = data->server;
6899
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006900 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006901
6902 switch (task->tk_status) {
6903 case 0:
6904 renew_lease(server, data->timestamp);
6905 break;
6906 case -NFS4ERR_STALE_CLIENTID:
6907 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006908 nfs4_schedule_lease_recovery(server->nfs_client);
6909 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006910 case -NFS4ERR_LEASE_MOVED:
6911 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006912 if (nfs4_async_handle_error(task, server,
6913 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006914 rpc_restart_call_prepare(task);
6915 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006916}
6917
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006918static void nfs4_release_lockowner_release(void *calldata)
6919{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006920 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006921 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006922 kfree(calldata);
6923}
6924
Trond Myklebust17280172012-03-11 13:11:00 -04006925static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006926 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6927 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006928 .rpc_release = nfs4_release_lockowner_release,
6929};
6930
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006931static void
6932nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006933{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006934 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006935 struct rpc_message msg = {
6936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6937 };
6938
6939 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006940 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006941
Trond Myklebustcf470c32012-03-07 13:49:12 -05006942 data = kmalloc(sizeof(*data), GFP_NOFS);
6943 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006944 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006945 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006946 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006947 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6948 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6949 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006950
Trond Myklebustcf470c32012-03-07 13:49:12 -05006951 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006952 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006953 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006954 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006955}
6956
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006957#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6958
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006959static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006960 struct dentry *unused, struct inode *inode,
6961 const char *key, const void *buf,
6962 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006963{
Al Viro59301222016-05-27 10:19:30 -04006964 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006965}
6966
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006967static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006968 struct dentry *unused, struct inode *inode,
6969 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006970{
Al Virob2968212016-04-10 20:48:24 -04006971 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006972}
6973
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006974static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006975{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006976 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006977}
6978
David Quigleyc9bccef2013-05-22 12:50:45 -04006979#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006980
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006981static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006982 struct dentry *unused, struct inode *inode,
6983 const char *key, const void *buf,
6984 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006985{
6986 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006987 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006988
6989 return -EOPNOTSUPP;
6990}
6991
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006992static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006993 struct dentry *unused, struct inode *inode,
6994 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006995{
6996 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006997 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006998 return -EOPNOTSUPP;
6999}
7000
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007001static ssize_t
7002nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007003{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007004 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007005
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007006 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7007 len = security_inode_listsecurity(inode, list, list_len);
7008 if (list_len && len > list_len)
7009 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007010 }
7011 return len;
7012}
7013
7014static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7015 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007016 .get = nfs4_xattr_get_nfs4_label,
7017 .set = nfs4_xattr_set_nfs4_label,
7018};
David Quigleyc9bccef2013-05-22 12:50:45 -04007019
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007020#else
7021
7022static ssize_t
7023nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7024{
7025 return 0;
7026}
7027
7028#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007029
Andy Adamson533eb462011-06-13 18:25:56 -04007030/*
7031 * nfs_fhget will use either the mounted_on_fileid or the fileid
7032 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007033static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7034{
Andy Adamson533eb462011-06-13 18:25:56 -04007035 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7036 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7037 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007038 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007039 return;
7040
7041 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007042 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007043 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7044 fattr->nlink = 2;
7045}
7046
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007047static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7048 const struct qstr *name,
7049 struct nfs4_fs_locations *fs_locations,
7050 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007051{
7052 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007053 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007054 struct nfs4_fs_locations_arg args = {
7055 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007056 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007057 .page = page,
7058 .bitmask = bitmask,
7059 };
Benny Halevy22958462009-04-01 09:22:02 -04007060 struct nfs4_fs_locations_res res = {
7061 .fs_locations = fs_locations,
7062 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007063 struct rpc_message msg = {
7064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7065 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007066 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007067 };
7068 int status;
7069
Harvey Harrison3110ff82008-05-02 13:42:44 -07007070 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007071
Chuck Leverc05cefc2017-11-05 15:45:22 -05007072 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7073 bitmask[1] = nfs4_fattr_bitmap[1];
7074
Andy Adamson533eb462011-06-13 18:25:56 -04007075 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7076 * is not supported */
7077 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007078 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007079 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007080 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007081
Trond Myklebustc228fd32007-01-13 02:28:11 -05007082 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007083 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007084 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007085 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007086 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007087 return status;
7088}
7089
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007090int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7091 const struct qstr *name,
7092 struct nfs4_fs_locations *fs_locations,
7093 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007094{
7095 struct nfs4_exception exception = { };
7096 int err;
7097 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007098 err = _nfs4_proc_fs_locations(client, dir, name,
7099 fs_locations, page);
7100 trace_nfs4_get_fs_locations(dir, name, err);
7101 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007102 &exception);
7103 } while (exception.retry);
7104 return err;
7105}
7106
Chuck Leverb03d7352013-10-17 14:12:50 -04007107/*
7108 * This operation also signals the server that this client is
7109 * performing migration recovery. The server can stop returning
7110 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7111 * appended to this compound to identify the client ID which is
7112 * performing recovery.
7113 */
7114static int _nfs40_proc_get_locations(struct inode *inode,
7115 struct nfs4_fs_locations *locations,
7116 struct page *page, struct rpc_cred *cred)
7117{
7118 struct nfs_server *server = NFS_SERVER(inode);
7119 struct rpc_clnt *clnt = server->client;
7120 u32 bitmask[2] = {
7121 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7122 };
7123 struct nfs4_fs_locations_arg args = {
7124 .clientid = server->nfs_client->cl_clientid,
7125 .fh = NFS_FH(inode),
7126 .page = page,
7127 .bitmask = bitmask,
7128 .migration = 1, /* skip LOOKUP */
7129 .renew = 1, /* append RENEW */
7130 };
7131 struct nfs4_fs_locations_res res = {
7132 .fs_locations = locations,
7133 .migration = 1,
7134 .renew = 1,
7135 };
7136 struct rpc_message msg = {
7137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7138 .rpc_argp = &args,
7139 .rpc_resp = &res,
7140 .rpc_cred = cred,
7141 };
7142 unsigned long now = jiffies;
7143 int status;
7144
7145 nfs_fattr_init(&locations->fattr);
7146 locations->server = server;
7147 locations->nlocations = 0;
7148
Anna Schumakerfba83f32018-05-04 16:22:50 -04007149 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007150 status = nfs4_call_sync_sequence(clnt, server, &msg,
7151 &args.seq_args, &res.seq_res);
7152 if (status)
7153 return status;
7154
7155 renew_lease(server, now);
7156 return 0;
7157}
7158
7159#ifdef CONFIG_NFS_V4_1
7160
7161/*
7162 * This operation also signals the server that this client is
7163 * performing migration recovery. The server can stop asserting
7164 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7165 * performing this operation is identified in the SEQUENCE
7166 * operation in this compound.
7167 *
7168 * When the client supports GETATTR(fs_locations_info), it can
7169 * be plumbed in here.
7170 */
7171static int _nfs41_proc_get_locations(struct inode *inode,
7172 struct nfs4_fs_locations *locations,
7173 struct page *page, struct rpc_cred *cred)
7174{
7175 struct nfs_server *server = NFS_SERVER(inode);
7176 struct rpc_clnt *clnt = server->client;
7177 u32 bitmask[2] = {
7178 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7179 };
7180 struct nfs4_fs_locations_arg args = {
7181 .fh = NFS_FH(inode),
7182 .page = page,
7183 .bitmask = bitmask,
7184 .migration = 1, /* skip LOOKUP */
7185 };
7186 struct nfs4_fs_locations_res res = {
7187 .fs_locations = locations,
7188 .migration = 1,
7189 };
7190 struct rpc_message msg = {
7191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7192 .rpc_argp = &args,
7193 .rpc_resp = &res,
7194 .rpc_cred = cred,
7195 };
7196 int status;
7197
7198 nfs_fattr_init(&locations->fattr);
7199 locations->server = server;
7200 locations->nlocations = 0;
7201
Anna Schumakerfba83f32018-05-04 16:22:50 -04007202 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007203 status = nfs4_call_sync_sequence(clnt, server, &msg,
7204 &args.seq_args, &res.seq_res);
7205 if (status == NFS4_OK &&
7206 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7207 status = -NFS4ERR_LEASE_MOVED;
7208 return status;
7209}
7210
7211#endif /* CONFIG_NFS_V4_1 */
7212
7213/**
7214 * nfs4_proc_get_locations - discover locations for a migrated FSID
7215 * @inode: inode on FSID that is migrating
7216 * @locations: result of query
7217 * @page: buffer
7218 * @cred: credential to use for this operation
7219 *
7220 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7221 * operation failed, or a negative errno if a local error occurred.
7222 *
7223 * On success, "locations" is filled in, but if the server has
7224 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7225 * asserted.
7226 *
7227 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7228 * from this client that require migration recovery.
7229 */
7230int nfs4_proc_get_locations(struct inode *inode,
7231 struct nfs4_fs_locations *locations,
7232 struct page *page, struct rpc_cred *cred)
7233{
7234 struct nfs_server *server = NFS_SERVER(inode);
7235 struct nfs_client *clp = server->nfs_client;
7236 const struct nfs4_mig_recovery_ops *ops =
7237 clp->cl_mvops->mig_recovery_ops;
7238 struct nfs4_exception exception = { };
7239 int status;
7240
7241 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7242 (unsigned long long)server->fsid.major,
7243 (unsigned long long)server->fsid.minor,
7244 clp->cl_hostname);
7245 nfs_display_fhandle(NFS_FH(inode), __func__);
7246
7247 do {
7248 status = ops->get_locations(inode, locations, page, cred);
7249 if (status != -NFS4ERR_DELAY)
7250 break;
7251 nfs4_handle_exception(server, status, &exception);
7252 } while (exception.retry);
7253 return status;
7254}
7255
Chuck Lever44c99932013-10-17 14:13:30 -04007256/*
7257 * This operation also signals the server that this client is
7258 * performing "lease moved" recovery. The server can stop
7259 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7260 * is appended to this compound to identify the client ID which is
7261 * performing recovery.
7262 */
7263static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7264{
7265 struct nfs_server *server = NFS_SERVER(inode);
7266 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7267 struct rpc_clnt *clnt = server->client;
7268 struct nfs4_fsid_present_arg args = {
7269 .fh = NFS_FH(inode),
7270 .clientid = clp->cl_clientid,
7271 .renew = 1, /* append RENEW */
7272 };
7273 struct nfs4_fsid_present_res res = {
7274 .renew = 1,
7275 };
7276 struct rpc_message msg = {
7277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7278 .rpc_argp = &args,
7279 .rpc_resp = &res,
7280 .rpc_cred = cred,
7281 };
7282 unsigned long now = jiffies;
7283 int status;
7284
7285 res.fh = nfs_alloc_fhandle();
7286 if (res.fh == NULL)
7287 return -ENOMEM;
7288
Anna Schumakerfba83f32018-05-04 16:22:50 -04007289 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007290 status = nfs4_call_sync_sequence(clnt, server, &msg,
7291 &args.seq_args, &res.seq_res);
7292 nfs_free_fhandle(res.fh);
7293 if (status)
7294 return status;
7295
7296 do_renew_lease(clp, now);
7297 return 0;
7298}
7299
7300#ifdef CONFIG_NFS_V4_1
7301
7302/*
7303 * This operation also signals the server that this client is
7304 * performing "lease moved" recovery. The server can stop asserting
7305 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7306 * this operation is identified in the SEQUENCE operation in this
7307 * compound.
7308 */
7309static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7310{
7311 struct nfs_server *server = NFS_SERVER(inode);
7312 struct rpc_clnt *clnt = server->client;
7313 struct nfs4_fsid_present_arg args = {
7314 .fh = NFS_FH(inode),
7315 };
7316 struct nfs4_fsid_present_res res = {
7317 };
7318 struct rpc_message msg = {
7319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7320 .rpc_argp = &args,
7321 .rpc_resp = &res,
7322 .rpc_cred = cred,
7323 };
7324 int status;
7325
7326 res.fh = nfs_alloc_fhandle();
7327 if (res.fh == NULL)
7328 return -ENOMEM;
7329
Anna Schumakerfba83f32018-05-04 16:22:50 -04007330 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007331 status = nfs4_call_sync_sequence(clnt, server, &msg,
7332 &args.seq_args, &res.seq_res);
7333 nfs_free_fhandle(res.fh);
7334 if (status == NFS4_OK &&
7335 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7336 status = -NFS4ERR_LEASE_MOVED;
7337 return status;
7338}
7339
7340#endif /* CONFIG_NFS_V4_1 */
7341
7342/**
7343 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7344 * @inode: inode on FSID to check
7345 * @cred: credential to use for this operation
7346 *
7347 * Server indicates whether the FSID is present, moved, or not
7348 * recognized. This operation is necessary to clear a LEASE_MOVED
7349 * condition for this client ID.
7350 *
7351 * Returns NFS4_OK if the FSID is present on this server,
7352 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7353 * NFS4ERR code if some error occurred on the server, or a
7354 * negative errno if a local failure occurred.
7355 */
7356int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7357{
7358 struct nfs_server *server = NFS_SERVER(inode);
7359 struct nfs_client *clp = server->nfs_client;
7360 const struct nfs4_mig_recovery_ops *ops =
7361 clp->cl_mvops->mig_recovery_ops;
7362 struct nfs4_exception exception = { };
7363 int status;
7364
7365 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7366 (unsigned long long)server->fsid.major,
7367 (unsigned long long)server->fsid.minor,
7368 clp->cl_hostname);
7369 nfs_display_fhandle(NFS_FH(inode), __func__);
7370
7371 do {
7372 status = ops->fsid_present(inode, cred);
7373 if (status != -NFS4ERR_DELAY)
7374 break;
7375 nfs4_handle_exception(server, status, &exception);
7376 } while (exception.retry);
7377 return status;
7378}
7379
Andy Adamson5ec16a82013-08-08 10:57:55 -04007380/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007381 * If 'use_integrity' is true and the state managment nfs_client
7382 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7383 * and the machine credential as per RFC3530bis and RFC5661 Security
7384 * Considerations sections. Otherwise, just use the user cred with the
7385 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007386 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007387static 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 +00007388{
7389 int status;
7390 struct nfs4_secinfo_arg args = {
7391 .dir_fh = NFS_FH(dir),
7392 .name = name,
7393 };
7394 struct nfs4_secinfo_res res = {
7395 .flavors = flavors,
7396 };
7397 struct rpc_message msg = {
7398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7399 .rpc_argp = &args,
7400 .rpc_resp = &res,
7401 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007402 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007403 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007404
7405 if (use_integrity) {
7406 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007407 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7408 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007409 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007410
7411 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007412
7413 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7414 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7415
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007416 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7417 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007418 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007419
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007420 if (cred)
7421 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007422
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007423 return status;
7424}
7425
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007426int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7427 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007428{
7429 struct nfs4_exception exception = { };
7430 int err;
7431 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007432 err = -NFS4ERR_WRONGSEC;
7433
7434 /* try to use integrity protection with machine cred */
7435 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7436 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7437
7438 /*
7439 * if unable to use integrity protection, or SECINFO with
7440 * integrity protection returns NFS4ERR_WRONGSEC (which is
7441 * disallowed by spec, but exists in deployed servers) use
7442 * the current filesystem's rpc_client and the user cred.
7443 */
7444 if (err == -NFS4ERR_WRONGSEC)
7445 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7446
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007447 trace_nfs4_secinfo(dir, name, err);
7448 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007449 &exception);
7450 } while (exception.retry);
7451 return err;
7452}
7453
Andy Adamson557134a2009-04-01 09:21:53 -04007454#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007456 * Check the exchange flags returned by the server for invalid flags, having
7457 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7458 * DS flags set.
7459 */
7460static int nfs4_check_cl_exchange_flags(u32 flags)
7461{
7462 if (flags & ~EXCHGID4_FLAG_MASK_R)
7463 goto out_inval;
7464 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7465 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7466 goto out_inval;
7467 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7468 goto out_inval;
7469 return NFS_OK;
7470out_inval:
7471 return -NFS4ERR_INVAL;
7472}
7473
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007474static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007475nfs41_same_server_scope(struct nfs41_server_scope *a,
7476 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007477{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007478 if (a->server_scope_sz != b->server_scope_sz)
7479 return false;
7480 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007481}
7482
Andy Adamson02a95de2016-02-05 16:08:37 -05007483static void
7484nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7485{
7486}
7487
7488static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7489 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7490};
7491
Andy Adamson357f54d2010-12-14 10:11:57 -05007492/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007493 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007494 *
7495 * The 4.1 client currently uses the same TCP connection for the
7496 * fore and backchannel.
7497 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007498static
7499int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7500 struct rpc_xprt *xprt,
7501 struct nfs_client *clp,
7502 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007503{
7504 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007505 struct nfs41_bind_conn_to_session_args args = {
7506 .client = clp,
7507 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7508 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007509 struct nfs41_bind_conn_to_session_res res;
7510 struct rpc_message msg = {
7511 .rpc_proc =
7512 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007513 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007514 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007515 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007516 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007517 struct rpc_task_setup task_setup_data = {
7518 .rpc_client = clnt,
7519 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007520 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007521 .rpc_message = &msg,
7522 .flags = RPC_TASK_TIMEOUT,
7523 };
7524 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007525
Trond Myklebust71a097c2015-02-18 09:27:18 -08007526 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7527 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7528 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007529
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007530 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7531 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7532 args.dir = NFS4_CDFC4_FORE;
7533
7534 task = rpc_run_task(&task_setup_data);
7535 if (!IS_ERR(task)) {
7536 status = task->tk_status;
7537 rpc_put_task(task);
7538 } else
7539 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007540 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007541 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007542 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007543 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7544 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007545 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007546 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007547 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007548 dprintk("NFS: %s: Unexpected direction from server\n",
7549 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007550 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007551 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007552 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007553 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7554 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007555 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007556 }
7557 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007558
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007559 return status;
7560}
7561
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007562struct rpc_bind_conn_calldata {
7563 struct nfs_client *clp;
7564 struct rpc_cred *cred;
7565};
7566
7567static int
7568nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7569 struct rpc_xprt *xprt,
7570 void *calldata)
7571{
7572 struct rpc_bind_conn_calldata *p = calldata;
7573
7574 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7575}
7576
7577int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7578{
7579 struct rpc_bind_conn_calldata data = {
7580 .clp = clp,
7581 .cred = cred,
7582 };
7583 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7584 nfs4_proc_bind_conn_to_session_callback, &data);
7585}
7586
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007587/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007588 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7589 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007590 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007591static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7592 .how = SP4_MACH_CRED,
7593 .enforce.u.words = {
7594 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7595 1 << (OP_EXCHANGE_ID - 32) |
7596 1 << (OP_CREATE_SESSION - 32) |
7597 1 << (OP_DESTROY_SESSION - 32) |
7598 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007599 },
7600 .allow.u.words = {
7601 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007602 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007603 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007604 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007605 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007606 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007607 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007608 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007609 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007610 1 << (OP_FREE_STATEID - 32) |
7611 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007612 }
7613};
7614
7615/*
7616 * Select the state protection mode for client `clp' given the server results
7617 * from exchange_id in `sp'.
7618 *
7619 * Returns 0 on success, negative errno otherwise.
7620 */
7621static int nfs4_sp4_select_mode(struct nfs_client *clp,
7622 struct nfs41_state_protection *sp)
7623{
7624 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7625 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7626 1 << (OP_EXCHANGE_ID - 32) |
7627 1 << (OP_CREATE_SESSION - 32) |
7628 1 << (OP_DESTROY_SESSION - 32) |
7629 1 << (OP_DESTROY_CLIENTID - 32)
7630 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007631 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007632 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007633 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007634
7635 if (sp->how == SP4_MACH_CRED) {
7636 /* Print state protect result */
7637 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7638 for (i = 0; i <= LAST_NFS4_OP; i++) {
7639 if (test_bit(i, sp->enforce.u.longs))
7640 dfprintk(MOUNT, " enforce op %d\n", i);
7641 if (test_bit(i, sp->allow.u.longs))
7642 dfprintk(MOUNT, " allow op %d\n", i);
7643 }
7644
7645 /* make sure nothing is on enforce list that isn't supported */
7646 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7647 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7648 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007649 ret = -EINVAL;
7650 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007651 }
7652 }
7653
7654 /*
7655 * Minimal mode - state operations are allowed to use machine
7656 * credential. Note this already happens by default, so the
7657 * client doesn't have to do anything more than the negotiation.
7658 *
7659 * NOTE: we don't care if EXCHANGE_ID is in the list -
7660 * we're already using the machine cred for exchange_id
7661 * and will never use a different cred.
7662 */
7663 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7664 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7665 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7666 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7667 dfprintk(MOUNT, "sp4_mach_cred:\n");
7668 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007669 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007670 } else {
7671 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007672 ret = -EINVAL;
7673 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007674 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007675
7676 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007677 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7678 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007679 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7680 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007681 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007682 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007683
Andrew Elble99ade3c2015-12-02 09:39:51 -05007684 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7685 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007686 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007687 }
7688
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007689 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7690 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7691 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007692 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007693 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007694
7695 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7696 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7697 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007698 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007699 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007700
7701 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7702 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007703 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007704 }
7705
7706 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7707 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007708 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007709 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007710 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007711out:
7712 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007713 return 0;
7714}
7715
Andy Adamson8d89bd72016-09-09 09:22:18 -04007716struct nfs41_exchange_id_data {
7717 struct nfs41_exchange_id_res res;
7718 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007719};
7720
Andy Adamson8d89bd72016-09-09 09:22:18 -04007721static void nfs4_exchange_id_release(void *data)
7722{
7723 struct nfs41_exchange_id_data *cdata =
7724 (struct nfs41_exchange_id_data *)data;
7725
Olga Kornievskaia63513232017-03-13 10:36:19 -04007726 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007727 kfree(cdata->res.impl_id);
7728 kfree(cdata->res.server_scope);
7729 kfree(cdata->res.server_owner);
7730 kfree(cdata);
7731}
7732
7733static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007734 .rpc_release = nfs4_exchange_id_release,
7735};
7736
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007737/*
7738 * _nfs4_proc_exchange_id()
7739 *
7740 * Wrapper for EXCHANGE_ID operation.
7741 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007742static struct rpc_task *
7743nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007744 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007745{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007746 struct rpc_message msg = {
7747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007748 .rpc_cred = cred,
7749 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007750 struct rpc_task_setup task_setup_data = {
7751 .rpc_client = clp->cl_rpcclient,
7752 .callback_ops = &nfs4_exchange_id_call_ops,
7753 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007754 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007755 };
7756 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007757 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007758
Elena Reshetova212bf412017-10-20 12:53:38 +03007759 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007760 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007761
Trond Myklebust9c760d12017-07-31 18:38:50 -04007762 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007763 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007764 if (!calldata)
7765 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007766
Trond Myklebustfd405592017-08-01 16:02:47 -04007767 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007768
7769 status = nfs4_init_uniform_client_string(clp);
7770 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007771 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007772
Andy Adamson8d89bd72016-09-09 09:22:18 -04007773 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7774 GFP_NOFS);
7775 status = -ENOMEM;
7776 if (unlikely(calldata->res.server_owner == NULL))
7777 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007778
Andy Adamson8d89bd72016-09-09 09:22:18 -04007779 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007780 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007781 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007782 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007783
Andy Adamson8d89bd72016-09-09 09:22:18 -04007784 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7785 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007786 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007787
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007788 switch (sp4_how) {
7789 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007790 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007791 break;
7792
7793 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007794 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007795 break;
7796
7797 default:
7798 /* unsupported! */
7799 WARN_ON_ONCE(1);
7800 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007801 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007802 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007803 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007804 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007805 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007806 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7807 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007808 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007809 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007810 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7811 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007812#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007813 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007814#endif
7815 msg.rpc_argp = &calldata->args;
7816 msg.rpc_resp = &calldata->res;
7817 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007818
Trond Myklebust9c760d12017-07-31 18:38:50 -04007819 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007820
7821out_impl_id:
7822 kfree(calldata->res.impl_id);
7823out_server_scope:
7824 kfree(calldata->res.server_scope);
7825out_server_owner:
7826 kfree(calldata->res.server_owner);
7827out_calldata:
7828 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007829out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007830 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007831 return ERR_PTR(status);
7832}
7833
7834/*
7835 * _nfs4_proc_exchange_id()
7836 *
7837 * Wrapper for EXCHANGE_ID operation.
7838 */
7839static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7840 u32 sp4_how)
7841{
7842 struct rpc_task *task;
7843 struct nfs41_exchange_id_args *argp;
7844 struct nfs41_exchange_id_res *resp;
7845 int status;
7846
7847 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7848 if (IS_ERR(task))
7849 return PTR_ERR(task);
7850
7851 argp = task->tk_msg.rpc_argp;
7852 resp = task->tk_msg.rpc_resp;
7853 status = task->tk_status;
7854 if (status != 0)
7855 goto out;
7856
7857 status = nfs4_check_cl_exchange_flags(resp->flags);
7858 if (status != 0)
7859 goto out;
7860
7861 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7862 if (status != 0)
7863 goto out;
7864
7865 clp->cl_clientid = resp->clientid;
7866 clp->cl_exchange_flags = resp->flags;
7867 clp->cl_seqid = resp->seqid;
7868 /* Client ID is not confirmed */
7869 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7870 clear_bit(NFS4_SESSION_ESTABLISHED,
7871 &clp->cl_session->session_state);
7872
7873 if (clp->cl_serverscope != NULL &&
7874 !nfs41_same_server_scope(clp->cl_serverscope,
7875 resp->server_scope)) {
7876 dprintk("%s: server_scope mismatch detected\n",
7877 __func__);
7878 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7879 }
7880
7881 swap(clp->cl_serverowner, resp->server_owner);
7882 swap(clp->cl_serverscope, resp->server_scope);
7883 swap(clp->cl_implid, resp->impl_id);
7884
7885 /* Save the EXCHANGE_ID verifier session trunk tests */
7886 memcpy(clp->cl_confirm.data, argp->verifier.data,
7887 sizeof(clp->cl_confirm.data));
7888out:
7889 trace_nfs4_exchange_id(clp, status);
7890 rpc_put_task(task);
7891 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007892}
7893
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007894/*
7895 * nfs4_proc_exchange_id()
7896 *
7897 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7898 *
7899 * Since the clientid has expired, all compounds using sessions
7900 * associated with the stale clientid will be returning
7901 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7902 * be in some phase of session reset.
7903 *
7904 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7905 */
7906int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7907{
7908 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7909 int status;
7910
7911 /* try SP4_MACH_CRED if krb5i/p */
7912 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7913 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007914 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007915 if (!status)
7916 return 0;
7917 }
7918
7919 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007920 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007921}
7922
Andy Adamson04fa2c62016-09-09 09:22:29 -04007923/**
7924 * nfs4_test_session_trunk
7925 *
7926 * This is an add_xprt_test() test function called from
7927 * rpc_clnt_setup_test_and_add_xprt.
7928 *
7929 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7930 * and is dereferrenced in nfs4_exchange_id_release
7931 *
7932 * Upon success, add the new transport to the rpc_clnt
7933 *
7934 * @clnt: struct rpc_clnt to get new transport
7935 * @xprt: the rpc_xprt to test
7936 * @data: call data for _nfs4_proc_exchange_id.
7937 */
7938int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7939 void *data)
7940{
7941 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007942 struct rpc_task *task;
7943 int status;
7944
Andy Adamson04fa2c62016-09-09 09:22:29 -04007945 u32 sp4_how;
7946
7947 dprintk("--> %s try %s\n", __func__,
7948 xprt->address_strings[RPC_DISPLAY_ADDR]);
7949
7950 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7951
7952 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007953 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7954 if (IS_ERR(task))
7955 return PTR_ERR(task);
7956
7957 status = task->tk_status;
7958 if (status == 0)
7959 status = nfs4_detect_session_trunking(adata->clp,
7960 task->tk_msg.rpc_resp, xprt);
7961
7962 rpc_put_task(task);
7963 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007964}
7965EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7966
Trond Myklebust66245532012-05-25 17:18:09 -04007967static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7968 struct rpc_cred *cred)
7969{
7970 struct rpc_message msg = {
7971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7972 .rpc_argp = clp,
7973 .rpc_cred = cred,
7974 };
7975 int status;
7976
7977 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007978 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007979 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007980 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007981 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7982 return status;
7983}
7984
7985static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7986 struct rpc_cred *cred)
7987{
7988 unsigned int loop;
7989 int ret;
7990
7991 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7992 ret = _nfs4_proc_destroy_clientid(clp, cred);
7993 switch (ret) {
7994 case -NFS4ERR_DELAY:
7995 case -NFS4ERR_CLIENTID_BUSY:
7996 ssleep(1);
7997 break;
7998 default:
7999 return ret;
8000 }
8001 }
8002 return 0;
8003}
8004
8005int nfs4_destroy_clientid(struct nfs_client *clp)
8006{
8007 struct rpc_cred *cred;
8008 int ret = 0;
8009
8010 if (clp->cl_mvops->minor_version < 1)
8011 goto out;
8012 if (clp->cl_exchange_flags == 0)
8013 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008014 if (clp->cl_preserve_clid)
8015 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008016 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008017 ret = nfs4_proc_destroy_clientid(clp, cred);
8018 if (cred)
8019 put_rpccred(cred);
8020 switch (ret) {
8021 case 0:
8022 case -NFS4ERR_STALE_CLIENTID:
8023 clp->cl_exchange_flags = 0;
8024 }
8025out:
8026 return ret;
8027}
8028
Andy Adamson2050f0c2009-04-01 09:22:30 -04008029struct nfs4_get_lease_time_data {
8030 struct nfs4_get_lease_time_args *args;
8031 struct nfs4_get_lease_time_res *res;
8032 struct nfs_client *clp;
8033};
8034
8035static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8036 void *calldata)
8037{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008038 struct nfs4_get_lease_time_data *data =
8039 (struct nfs4_get_lease_time_data *)calldata;
8040
8041 dprintk("--> %s\n", __func__);
8042 /* just setup sequence, do not trigger session recovery
8043 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008044 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008045 &data->args->la_seq_args,
8046 &data->res->lr_seq_res,
8047 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008048 dprintk("<-- %s\n", __func__);
8049}
8050
8051/*
8052 * Called from nfs4_state_manager thread for session setup, so don't recover
8053 * from sequence operation or clientid errors.
8054 */
8055static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8056{
8057 struct nfs4_get_lease_time_data *data =
8058 (struct nfs4_get_lease_time_data *)calldata;
8059
8060 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008061 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8062 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008063 switch (task->tk_status) {
8064 case -NFS4ERR_DELAY:
8065 case -NFS4ERR_GRACE:
8066 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8067 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8068 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008069 /* fall through */
8070 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008071 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008072 return;
8073 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008074 dprintk("<-- %s\n", __func__);
8075}
8076
Trond Myklebust17280172012-03-11 13:11:00 -04008077static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008078 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8079 .rpc_call_done = nfs4_get_lease_time_done,
8080};
8081
8082int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8083{
8084 struct rpc_task *task;
8085 struct nfs4_get_lease_time_args args;
8086 struct nfs4_get_lease_time_res res = {
8087 .lr_fsinfo = fsinfo,
8088 };
8089 struct nfs4_get_lease_time_data data = {
8090 .args = &args,
8091 .res = &res,
8092 .clp = clp,
8093 };
8094 struct rpc_message msg = {
8095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8096 .rpc_argp = &args,
8097 .rpc_resp = &res,
8098 };
8099 struct rpc_task_setup task_setup = {
8100 .rpc_client = clp->cl_rpcclient,
8101 .rpc_message = &msg,
8102 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008103 .callback_data = &data,
8104 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008105 };
8106 int status;
8107
Anna Schumakerfba83f32018-05-04 16:22:50 -04008108 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008109 task = rpc_run_task(&task_setup);
8110
8111 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008112 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008113
Anna Schumakerf6148712017-04-07 14:15:23 -04008114 status = task->tk_status;
8115 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008116 return status;
8117}
8118
Andy Adamsonfc931582009-04-01 09:22:31 -04008119/*
8120 * Initialize the values to be used by the client in CREATE_SESSION
8121 * If nfs4_init_session set the fore channel request and response sizes,
8122 * use them.
8123 *
8124 * Set the back channel max_resp_sz_cached to zero to force the client to
8125 * always set csa_cachethis to FALSE because the current implementation
8126 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8127 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008128static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8129 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008130{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008131 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008132 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008133
Andy Adamson18aad3d2013-06-26 12:21:49 -04008134 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8135 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8136
Andy Adamsonfc931582009-04-01 09:22:31 -04008137 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008138 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8139 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008140 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008141 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008142
8143 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008144 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008145 __func__,
8146 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008147 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008148
8149 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008150 args->bc_attrs.max_rqst_sz = max_bc_payload;
8151 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008152 args->bc_attrs.max_resp_sz_cached = 0;
8153 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008154 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008155
8156 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8157 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8158 __func__,
8159 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8160 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8161 args->bc_attrs.max_reqs);
8162}
8163
Trond Myklebust79969dd2015-02-18 11:30:18 -08008164static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8165 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008166{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008167 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008168 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008169
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008170 if (rcvd->max_resp_sz > sent->max_resp_sz)
8171 return -EINVAL;
8172 /*
8173 * Our requested max_ops is the minimum we need; we're not
8174 * prepared to break up compounds into smaller pieces than that.
8175 * So, no point even trying to continue if the server won't
8176 * cooperate:
8177 */
8178 if (rcvd->max_ops < sent->max_ops)
8179 return -EINVAL;
8180 if (rcvd->max_reqs == 0)
8181 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008182 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8183 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008184 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008185}
8186
Trond Myklebust79969dd2015-02-18 11:30:18 -08008187static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8188 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008189{
8190 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008191 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008192
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008193 if (!(res->flags & SESSION4_BACK_CHAN))
8194 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008195 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8196 return -EINVAL;
8197 if (rcvd->max_resp_sz < sent->max_resp_sz)
8198 return -EINVAL;
8199 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8200 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008201 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008202 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008203 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008204 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008205out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008206 return 0;
8207}
Andy Adamson8d353012009-04-01 09:22:32 -04008208
Andy Adamson8d353012009-04-01 09:22:32 -04008209static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008210 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008211{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008212 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008213
Trond Myklebust79969dd2015-02-18 11:30:18 -08008214 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008215 if (ret)
8216 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008217 return nfs4_verify_back_channel_attrs(args, res);
8218}
8219
8220static void nfs4_update_session(struct nfs4_session *session,
8221 struct nfs41_create_session_res *res)
8222{
8223 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008224 /* Mark client id and session as being confirmed */
8225 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8226 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008227 session->flags = res->flags;
8228 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008229 if (res->flags & SESSION4_BACK_CHAN)
8230 memcpy(&session->bc_attrs, &res->bc_attrs,
8231 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008232}
8233
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008234static int _nfs4_proc_create_session(struct nfs_client *clp,
8235 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008236{
8237 struct nfs4_session *session = clp->cl_session;
8238 struct nfs41_create_session_args args = {
8239 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008240 .clientid = clp->cl_clientid,
8241 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008242 .cb_program = NFS4_CALLBACK,
8243 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008244 struct nfs41_create_session_res res;
8245
Andy Adamsonfc931582009-04-01 09:22:31 -04008246 struct rpc_message msg = {
8247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8248 .rpc_argp = &args,
8249 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008250 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008251 };
8252 int status;
8253
Chuck Lever6b26cc82016-05-02 14:40:40 -04008254 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008255 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008256
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008257 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008258 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008259
Trond Myklebustb519d402016-09-11 14:50:01 -04008260 switch (status) {
8261 case -NFS4ERR_STALE_CLIENTID:
8262 case -NFS4ERR_DELAY:
8263 case -ETIMEDOUT:
8264 case -EACCES:
8265 case -EAGAIN:
8266 goto out;
8267 };
8268
8269 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008270 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008271 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008272 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008273 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008274 if (status)
8275 goto out;
8276 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008277 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008278out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008279 return status;
8280}
8281
8282/*
8283 * Issues a CREATE_SESSION operation to the server.
8284 * It is the responsibility of the caller to verify the session is
8285 * expired before calling this routine.
8286 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008287int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008288{
8289 int status;
8290 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008291 struct nfs4_session *session = clp->cl_session;
8292
8293 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8294
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008295 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008296 if (status)
8297 goto out;
8298
Andy Adamsonaacd5532011-11-09 13:58:21 -05008299 /* Init or reset the session slot tables */
8300 status = nfs4_setup_session_slot_tables(session);
8301 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008302 if (status)
8303 goto out;
8304
8305 ptr = (unsigned *)&session->sess_id.data[0];
8306 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8307 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008308out:
8309 dprintk("<-- %s\n", __func__);
8310 return status;
8311}
8312
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008313/*
8314 * Issue the over-the-wire RPC DESTROY_SESSION.
8315 * The caller must serialize access to this routine.
8316 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008317int nfs4_proc_destroy_session(struct nfs4_session *session,
8318 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008319{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008320 struct rpc_message msg = {
8321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8322 .rpc_argp = session,
8323 .rpc_cred = cred,
8324 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008325 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008326
8327 dprintk("--> nfs4_proc_destroy_session\n");
8328
8329 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008330 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8331 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008332
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008333 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008334 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008335
8336 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008337 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008338 "Session has been destroyed regardless...\n", status);
8339
8340 dprintk("<-- nfs4_proc_destroy_session\n");
8341 return status;
8342}
8343
Trond Myklebust7b38c362012-05-23 13:23:31 -04008344/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008345 * Renew the cl_session lease.
8346 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008347struct nfs4_sequence_data {
8348 struct nfs_client *clp;
8349 struct nfs4_sequence_args args;
8350 struct nfs4_sequence_res res;
8351};
8352
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008353static void nfs41_sequence_release(void *data)
8354{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008355 struct nfs4_sequence_data *calldata = data;
8356 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008357
Elena Reshetova212bf412017-10-20 12:53:38 +03008358 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008359 nfs4_schedule_state_renewal(clp);
8360 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008361 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008362}
8363
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008364static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8365{
8366 switch(task->tk_status) {
8367 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008368 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8369 return -EAGAIN;
8370 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008371 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008372 }
8373 return 0;
8374}
8375
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008376static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008377{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008378 struct nfs4_sequence_data *calldata = data;
8379 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008380
Trond Myklebust14516c32010-07-31 14:29:06 -04008381 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8382 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008383
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008384 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008385 if (task->tk_status < 0) {
8386 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008387 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008388 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008389
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008390 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8391 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008392 return;
8393 }
8394 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008395 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008396out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008397 dprintk("<-- %s\n", __func__);
8398}
8399
8400static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8401{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008402 struct nfs4_sequence_data *calldata = data;
8403 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008404 struct nfs4_sequence_args *args;
8405 struct nfs4_sequence_res *res;
8406
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008407 args = task->tk_msg.rpc_argp;
8408 res = task->tk_msg.rpc_resp;
8409
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008410 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008411}
8412
8413static const struct rpc_call_ops nfs41_sequence_ops = {
8414 .rpc_call_done = nfs41_sequence_call_done,
8415 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008416 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008417};
8418
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008419static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8420 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008421 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008422 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008423{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008424 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008425 struct rpc_message msg = {
8426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8427 .rpc_cred = cred,
8428 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008429 struct rpc_task_setup task_setup_data = {
8430 .rpc_client = clp->cl_rpcclient,
8431 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008432 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008433 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008434 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008435 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008436
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008437 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008438 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008439 goto out_err;
8440
8441 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008442 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008443 if (calldata == NULL)
8444 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008445 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008446 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008447 msg.rpc_argp = &calldata->args;
8448 msg.rpc_resp = &calldata->res;
8449 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008450 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008451
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008452 ret = rpc_run_task(&task_setup_data);
8453 if (IS_ERR(ret))
8454 goto out_err;
8455 return ret;
8456out_put_clp:
8457 nfs_put_client(clp);
8458out_err:
8459 nfs41_release_slot(slot);
8460 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008461}
8462
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008463static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008464{
8465 struct rpc_task *task;
8466 int ret = 0;
8467
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008468 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008469 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008470 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008471 if (IS_ERR(task))
8472 ret = PTR_ERR(task);
8473 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008474 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008475 dprintk("<-- %s status=%d\n", __func__, ret);
8476 return ret;
8477}
8478
8479static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8480{
8481 struct rpc_task *task;
8482 int ret;
8483
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008484 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008485 if (IS_ERR(task)) {
8486 ret = PTR_ERR(task);
8487 goto out;
8488 }
8489 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008490 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008491 ret = task->tk_status;
8492 rpc_put_task(task);
8493out:
8494 dprintk("<-- %s status=%d\n", __func__, ret);
8495 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008496}
8497
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008498struct nfs4_reclaim_complete_data {
8499 struct nfs_client *clp;
8500 struct nfs41_reclaim_complete_args arg;
8501 struct nfs41_reclaim_complete_res res;
8502};
8503
8504static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8505{
8506 struct nfs4_reclaim_complete_data *calldata = data;
8507
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008508 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008509 &calldata->arg.seq_args,
8510 &calldata->res.seq_res,
8511 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008512}
8513
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008514static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8515{
8516 switch(task->tk_status) {
8517 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008518 wake_up_all(&clp->cl_lock_waitq);
8519 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008520 case -NFS4ERR_COMPLETE_ALREADY:
8521 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8522 break;
8523 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008524 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008525 /* fall through */
8526 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008527 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008528 case -NFS4ERR_BADSESSION:
8529 case -NFS4ERR_DEADSESSION:
8530 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8531 nfs4_schedule_session_recovery(clp->cl_session,
8532 task->tk_status);
8533 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008534 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008535 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008536 }
8537 return 0;
8538}
8539
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008540static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8541{
8542 struct nfs4_reclaim_complete_data *calldata = data;
8543 struct nfs_client *clp = calldata->clp;
8544 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8545
8546 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008547 if (!nfs41_sequence_done(task, res))
8548 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008549
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008550 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008551 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8552 rpc_restart_call_prepare(task);
8553 return;
8554 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008555 dprintk("<-- %s\n", __func__);
8556}
8557
8558static void nfs4_free_reclaim_complete_data(void *data)
8559{
8560 struct nfs4_reclaim_complete_data *calldata = data;
8561
8562 kfree(calldata);
8563}
8564
8565static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8566 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8567 .rpc_call_done = nfs4_reclaim_complete_done,
8568 .rpc_release = nfs4_free_reclaim_complete_data,
8569};
8570
8571/*
8572 * Issue a global reclaim complete.
8573 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008574static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8575 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008576{
8577 struct nfs4_reclaim_complete_data *calldata;
8578 struct rpc_task *task;
8579 struct rpc_message msg = {
8580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008581 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008582 };
8583 struct rpc_task_setup task_setup_data = {
8584 .rpc_client = clp->cl_rpcclient,
8585 .rpc_message = &msg,
8586 .callback_ops = &nfs4_reclaim_complete_call_ops,
8587 .flags = RPC_TASK_ASYNC,
8588 };
8589 int status = -ENOMEM;
8590
8591 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008592 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008593 if (calldata == NULL)
8594 goto out;
8595 calldata->clp = clp;
8596 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008597
Anna Schumakerfba83f32018-05-04 16:22:50 -04008598 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008599 msg.rpc_argp = &calldata->arg;
8600 msg.rpc_resp = &calldata->res;
8601 task_setup_data.callback_data = calldata;
8602 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008603 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008604 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008605 goto out;
8606 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008607 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008608 if (status == 0)
8609 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008610 rpc_put_task(task);
8611out:
8612 dprintk("<-- %s status=%d\n", __func__, status);
8613 return status;
8614}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615
8616static void
8617nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8618{
8619 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008620 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008621
8622 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008623 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008624 &lgp->res.seq_res, task);
8625 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008626}
8627
8628static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8629{
8630 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008631
8632 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008633 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008634 dprintk("<-- %s\n", __func__);
8635}
8636
8637static int
8638nfs4_layoutget_handle_exception(struct rpc_task *task,
8639 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8640{
Trond Myklebustee314c22012-10-01 17:25:48 -07008641 struct inode *inode = lgp->args.inode;
8642 struct nfs_server *server = NFS_SERVER(inode);
8643 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008644 int nfs4err = task->tk_status;
8645 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008646 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008647
Boaz Harroshed7e5422014-01-22 20:34:54 +02008648 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008649
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008650 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008651 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008652 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008653
8654 /*
8655 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8656 * on the file. set tk_status to -ENODATA to tell upper layer to
8657 * retry go inband.
8658 */
8659 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008660 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008661 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008662 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008663 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8664 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8665 */
8666 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008667 status = -EOVERFLOW;
8668 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008669 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008670 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008671 * (or clients) writing to the same RAID stripe except when
8672 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008673 *
8674 * Treat it like we would RECALLCONFLICT -- we retry for a little
8675 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008676 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008677 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008678 if (lgp->args.minlength == 0) {
8679 status = -EOVERFLOW;
8680 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008681 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008682 status = -EBUSY;
8683 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008684 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008685 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008686 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008687 case -NFS4ERR_DELEG_REVOKED:
8688 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008689 case -NFS4ERR_EXPIRED:
8690 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008691 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008692 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008693 lo = NFS_I(inode)->layout;
8694 /* If the open stateid was bad, then recover it. */
8695 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008696 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008697 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008698 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008699 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008700 break;
8701 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008702
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008703 /*
8704 * Mark the bad layout state as invalid, then retry
8705 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008706 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008707 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008708 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008709 pnfs_free_lseg_list(&head);
8710 status = -EAGAIN;
8711 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008712 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008713
Trond Myklebust8ac09252017-01-23 22:44:12 -05008714 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008715 err = nfs4_handle_exception(server, nfs4err, exception);
8716 if (!status) {
8717 if (exception->retry)
8718 status = -EAGAIN;
8719 else
8720 status = err;
8721 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008722out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008723 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008724 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008725}
8726
Fred Isamandacb4522016-09-19 17:47:09 -04008727size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008728{
8729 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8730 return nfs_page_array_len(0, max_resp_sz);
8731}
8732
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008733static void nfs4_layoutget_release(void *calldata)
8734{
8735 struct nfs4_layoutget *lgp = calldata;
8736
8737 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008738 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008739 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740 dprintk("<-- %s\n", __func__);
8741}
8742
8743static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8744 .rpc_call_prepare = nfs4_layoutget_prepare,
8745 .rpc_call_done = nfs4_layoutget_done,
8746 .rpc_release = nfs4_layoutget_release,
8747};
8748
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008749struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008750nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008751{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008752 struct inode *inode = lgp->args.inode;
8753 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008754 struct rpc_task *task;
8755 struct rpc_message msg = {
8756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8757 .rpc_argp = &lgp->args,
8758 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008759 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008760 };
8761 struct rpc_task_setup task_setup_data = {
8762 .rpc_client = server->client,
8763 .rpc_message = &msg,
8764 .callback_ops = &nfs4_layoutget_call_ops,
8765 .callback_data = lgp,
8766 .flags = RPC_TASK_ASYNC,
8767 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008768 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008769 struct nfs4_exception exception = {
8770 .inode = inode,
8771 .timeout = *timeout,
8772 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008773 int status = 0;
8774
8775 dprintk("--> %s\n", __func__);
8776
Peng Tao4bd5a982014-11-17 11:05:17 +08008777 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8778 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8779
Anna Schumakerfba83f32018-05-04 16:22:50 -04008780 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008781
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008782 task = rpc_run_task(&task_setup_data);
8783 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008784 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008785 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008786 if (status == 0) {
8787 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8788 *timeout = exception.timeout;
8789 }
8790
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008791 trace_nfs4_layoutget(lgp->args.ctx,
8792 &lgp->args.range,
8793 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008794 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008795 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008796
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008797 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8798 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008799 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008800 rpc_put_task(task);
8801 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008802 if (status)
8803 return ERR_PTR(status);
8804 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008805}
8806
Benny Halevycbe82602011-05-22 19:52:37 +03008807static void
8808nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8809{
8810 struct nfs4_layoutreturn *lrp = calldata;
8811
8812 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008813 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008814 &lrp->args.seq_args,
8815 &lrp->res.seq_res,
8816 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008817}
8818
8819static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8820{
8821 struct nfs4_layoutreturn *lrp = calldata;
8822 struct nfs_server *server;
8823
8824 dprintk("--> %s\n", __func__);
8825
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008826 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008827 return;
8828
8829 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008830 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008831 case -NFS4ERR_OLD_STATEID:
8832 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8833 lrp->args.inode))
8834 goto out_restart;
8835 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008836 default:
8837 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008838 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008839 case 0:
8840 break;
8841 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008842 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008843 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008844 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008845 }
Benny Halevycbe82602011-05-22 19:52:37 +03008846 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008847 return;
8848out_restart:
8849 task->tk_status = 0;
8850 nfs4_sequence_free_slot(&lrp->res.seq_res);
8851 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008852}
8853
8854static void nfs4_layoutreturn_release(void *calldata)
8855{
8856 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008857 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008858
8859 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008860 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008861 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008862 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008863 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8864 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008865 pnfs_put_layout_hdr(lrp->args.layout);
8866 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008867 kfree(calldata);
8868 dprintk("<-- %s\n", __func__);
8869}
8870
8871static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8872 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8873 .rpc_call_done = nfs4_layoutreturn_done,
8874 .rpc_release = nfs4_layoutreturn_release,
8875};
8876
Peng Tao6c166052014-11-17 09:30:40 +08008877int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008878{
8879 struct rpc_task *task;
8880 struct rpc_message msg = {
8881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8882 .rpc_argp = &lrp->args,
8883 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008884 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008885 };
8886 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008887 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008888 .rpc_message = &msg,
8889 .callback_ops = &nfs4_layoutreturn_call_ops,
8890 .callback_data = lrp,
8891 };
Peng Tao6c166052014-11-17 09:30:40 +08008892 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008893
Andrew Elble99ade3c2015-12-02 09:39:51 -05008894 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8895 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8896 &task_setup_data.rpc_client, &msg);
8897
Benny Halevycbe82602011-05-22 19:52:37 +03008898 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008899 if (!sync) {
8900 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8901 if (!lrp->inode) {
8902 nfs4_layoutreturn_release(lrp);
8903 return -EAGAIN;
8904 }
8905 task_setup_data.flags |= RPC_TASK_ASYNC;
8906 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008907 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008908 task = rpc_run_task(&task_setup_data);
8909 if (IS_ERR(task))
8910 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008911 if (sync)
8912 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008913 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008914 dprintk("<-- %s status=%d\n", __func__, status);
8915 rpc_put_task(task);
8916 return status;
8917}
8918
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008919static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008920_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8921 struct pnfs_device *pdev,
8922 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008923{
8924 struct nfs4_getdeviceinfo_args args = {
8925 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008926 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8927 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008928 };
8929 struct nfs4_getdeviceinfo_res res = {
8930 .pdev = pdev,
8931 };
8932 struct rpc_message msg = {
8933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8934 .rpc_argp = &args,
8935 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008936 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008937 };
8938 int status;
8939
8940 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008941 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008942 if (res.notification & ~args.notify_types)
8943 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008944 if (res.notification != args.notify_types)
8945 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008946
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008947 dprintk("<-- %s status=%d\n", __func__, status);
8948
8949 return status;
8950}
8951
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008952int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8953 struct pnfs_device *pdev,
8954 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008955{
8956 struct nfs4_exception exception = { };
8957 int err;
8958
8959 do {
8960 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008961 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008962 &exception);
8963 } while (exception.retry);
8964 return err;
8965}
8966EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8967
Andy Adamson863a3c62011-03-23 13:27:54 +00008968static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8969{
8970 struct nfs4_layoutcommit_data *data = calldata;
8971 struct nfs_server *server = NFS_SERVER(data->args.inode);
8972
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008973 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008974 &data->args.seq_args,
8975 &data->res.seq_res,
8976 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008977}
8978
8979static void
8980nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8981{
8982 struct nfs4_layoutcommit_data *data = calldata;
8983 struct nfs_server *server = NFS_SERVER(data->args.inode);
8984
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008985 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008986 return;
8987
8988 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008989 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8990 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8991 case -NFS4ERR_BADLAYOUT: /* no layout */
8992 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008993 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008994 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008995 break;
8996 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008997 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008998 rpc_restart_call_prepare(task);
8999 return;
9000 }
9001 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009002}
9003
9004static void nfs4_layoutcommit_release(void *calldata)
9005{
9006 struct nfs4_layoutcommit_data *data = calldata;
9007
Andy Adamsondb29c082011-07-30 20:52:38 -04009008 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009009 nfs_post_op_update_inode_force_wcc(data->args.inode,
9010 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009011 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009012 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009013 kfree(data);
9014}
9015
9016static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9017 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9018 .rpc_call_done = nfs4_layoutcommit_done,
9019 .rpc_release = nfs4_layoutcommit_release,
9020};
9021
9022int
Andy Adamsonef311532011-03-12 02:58:10 -05009023nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009024{
9025 struct rpc_message msg = {
9026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9027 .rpc_argp = &data->args,
9028 .rpc_resp = &data->res,
9029 .rpc_cred = data->cred,
9030 };
9031 struct rpc_task_setup task_setup_data = {
9032 .task = &data->task,
9033 .rpc_client = NFS_CLIENT(data->args.inode),
9034 .rpc_message = &msg,
9035 .callback_ops = &nfs4_layoutcommit_ops,
9036 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009037 };
9038 struct rpc_task *task;
9039 int status = 0;
9040
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009041 dprintk("NFS: initiating layoutcommit call. sync %d "
9042 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009043 data->args.lastbytewritten,
9044 data->args.inode->i_ino);
9045
Trond Myklebust472e2592015-02-05 16:50:30 -05009046 if (!sync) {
9047 data->inode = nfs_igrab_and_active(data->args.inode);
9048 if (data->inode == NULL) {
9049 nfs4_layoutcommit_release(data);
9050 return -EAGAIN;
9051 }
9052 task_setup_data.flags = RPC_TASK_ASYNC;
9053 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009054 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009055 task = rpc_run_task(&task_setup_data);
9056 if (IS_ERR(task))
9057 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009058 if (sync)
9059 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009060 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009061 dprintk("%s: status %d\n", __func__, status);
9062 rpc_put_task(task);
9063 return status;
9064}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009065
Andy Adamson97431202013-08-08 10:57:56 -04009066/**
9067 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9068 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9069 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009070static int
9071_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009072 struct nfs_fsinfo *info,
9073 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009074{
9075 struct nfs41_secinfo_no_name_args args = {
9076 .style = SECINFO_STYLE_CURRENT_FH,
9077 };
9078 struct nfs4_secinfo_res res = {
9079 .flavors = flavors,
9080 };
9081 struct rpc_message msg = {
9082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9083 .rpc_argp = &args,
9084 .rpc_resp = &res,
9085 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009086 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009087 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009088 int status;
9089
9090 if (use_integrity) {
9091 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009092 cred = nfs4_get_clid_cred(server->nfs_client);
9093 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009094 }
9095
9096 dprintk("--> %s\n", __func__);
9097 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9098 &res.seq_res, 0);
9099 dprintk("<-- %s status=%d\n", __func__, status);
9100
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009101 if (cred)
9102 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009103
9104 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009105}
9106
9107static int
9108nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9109 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9110{
9111 struct nfs4_exception exception = { };
9112 int err;
9113 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009114 /* first try using integrity protection */
9115 err = -NFS4ERR_WRONGSEC;
9116
9117 /* try to use integrity protection with machine cred */
9118 if (_nfs4_is_integrity_protected(server->nfs_client))
9119 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9120 flavors, true);
9121
9122 /*
9123 * if unable to use integrity protection, or SECINFO with
9124 * integrity protection returns NFS4ERR_WRONGSEC (which is
9125 * disallowed by spec, but exists in deployed servers) use
9126 * the current filesystem's rpc_client and the user cred.
9127 */
9128 if (err == -NFS4ERR_WRONGSEC)
9129 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9130 flavors, false);
9131
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009132 switch (err) {
9133 case 0:
9134 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009135 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009136 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009137 default:
9138 err = nfs4_handle_exception(server, err, &exception);
9139 }
9140 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009141out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009142 return err;
9143}
9144
9145static int
9146nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9147 struct nfs_fsinfo *info)
9148{
9149 int err;
9150 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009151 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009152 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009153 struct nfs4_secinfo4 *secinfo;
9154 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009155
9156 page = alloc_page(GFP_KERNEL);
9157 if (!page) {
9158 err = -ENOMEM;
9159 goto out;
9160 }
9161
9162 flavors = page_address(page);
9163 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9164
9165 /*
9166 * Fall back on "guess and check" method if
9167 * the server doesn't support SECINFO_NO_NAME
9168 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009169 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009170 err = nfs4_find_root_sec(server, fhandle, info);
9171 goto out_freepage;
9172 }
9173 if (err)
9174 goto out_freepage;
9175
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009176 for (i = 0; i < flavors->num_flavors; i++) {
9177 secinfo = &flavors->flavors[i];
9178
9179 switch (secinfo->flavor) {
9180 case RPC_AUTH_NULL:
9181 case RPC_AUTH_UNIX:
9182 case RPC_AUTH_GSS:
9183 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9184 &secinfo->flavor_info);
9185 break;
9186 default:
9187 flavor = RPC_AUTH_MAXFLAVOR;
9188 break;
9189 }
9190
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009191 if (!nfs_auth_info_match(&server->auth_info, flavor))
9192 flavor = RPC_AUTH_MAXFLAVOR;
9193
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009194 if (flavor != RPC_AUTH_MAXFLAVOR) {
9195 err = nfs4_lookup_root_sec(server, fhandle,
9196 info, flavor);
9197 if (!err)
9198 break;
9199 }
9200 }
9201
9202 if (flavor == RPC_AUTH_MAXFLAVOR)
9203 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009204
9205out_freepage:
9206 put_page(page);
9207 if (err == -EACCES)
9208 return -EPERM;
9209out:
9210 return err;
9211}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009212
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009213static int _nfs41_test_stateid(struct nfs_server *server,
9214 nfs4_stateid *stateid,
9215 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009216{
9217 int status;
9218 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009219 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009220 };
9221 struct nfs41_test_stateid_res res;
9222 struct rpc_message msg = {
9223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9224 .rpc_argp = &args,
9225 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009226 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009227 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009228 struct rpc_clnt *rpc_client = server->client;
9229
9230 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9231 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009232
Chuck Lever38527b12012-07-11 16:30:23 -04009233 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009234 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009235 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009236 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009237 if (status != NFS_OK) {
9238 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009239 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009240 }
9241 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009242 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009243}
9244
Trond Myklebust43912bb2016-09-22 13:38:56 -04009245static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9246 int err, struct nfs4_exception *exception)
9247{
9248 exception->retry = 0;
9249 switch(err) {
9250 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009251 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009252 nfs4_handle_exception(server, err, exception);
9253 break;
9254 case -NFS4ERR_BADSESSION:
9255 case -NFS4ERR_BADSLOT:
9256 case -NFS4ERR_BAD_HIGH_SLOT:
9257 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9258 case -NFS4ERR_DEADSESSION:
9259 nfs4_do_handle_exception(server, err, exception);
9260 }
9261}
9262
Chuck Lever38527b12012-07-11 16:30:23 -04009263/**
9264 * nfs41_test_stateid - perform a TEST_STATEID operation
9265 *
9266 * @server: server / transport on which to perform the operation
9267 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009268 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009269 *
9270 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9271 * Otherwise a negative NFS4ERR value is returned if the operation
9272 * failed or the state ID is not currently valid.
9273 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009274static int nfs41_test_stateid(struct nfs_server *server,
9275 nfs4_stateid *stateid,
9276 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009277{
9278 struct nfs4_exception exception = { };
9279 int err;
9280 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009281 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009282 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009283 } while (exception.retry);
9284 return err;
9285}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009286
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009287struct nfs_free_stateid_data {
9288 struct nfs_server *server;
9289 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009290 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009291};
9292
9293static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9294{
9295 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009296 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009297 &data->args.seq_args,
9298 &data->res.seq_res,
9299 task);
9300}
9301
9302static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9303{
9304 struct nfs_free_stateid_data *data = calldata;
9305
9306 nfs41_sequence_done(task, &data->res.seq_res);
9307
9308 switch (task->tk_status) {
9309 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009310 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009311 rpc_restart_call_prepare(task);
9312 }
9313}
9314
9315static void nfs41_free_stateid_release(void *calldata)
9316{
9317 kfree(calldata);
9318}
9319
Trond Myklebust17f26b12013-08-21 15:48:42 -04009320static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009321 .rpc_call_prepare = nfs41_free_stateid_prepare,
9322 .rpc_call_done = nfs41_free_stateid_done,
9323 .rpc_release = nfs41_free_stateid_release,
9324};
9325
Anna Schumaker2f261022018-05-15 13:03:39 -04009326/**
9327 * nfs41_free_stateid - perform a FREE_STATEID operation
9328 *
9329 * @server: server / transport on which to perform the operation
9330 * @stateid: state ID to release
9331 * @cred: credential
9332 * @is_recovery: set to true if this call needs to be privileged
9333 *
9334 * Note: this function is always asynchronous.
9335 */
9336static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009337 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009338 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009339 bool privileged)
9340{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009341 struct rpc_message msg = {
9342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009343 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009344 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009345 struct rpc_task_setup task_setup = {
9346 .rpc_client = server->client,
9347 .rpc_message = &msg,
9348 .callback_ops = &nfs41_free_stateid_ops,
9349 .flags = RPC_TASK_ASYNC,
9350 };
9351 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009352 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009353
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009354 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9355 &task_setup.rpc_client, &msg);
9356
Chuck Lever38527b12012-07-11 16:30:23 -04009357 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009358 data = kmalloc(sizeof(*data), GFP_NOFS);
9359 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009360 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009361 data->server = server;
9362 nfs4_stateid_copy(&data->args.stateid, stateid);
9363
9364 task_setup.callback_data = data;
9365
9366 msg.rpc_argp = &data->args;
9367 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009368 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009369 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009370 if (IS_ERR(task))
9371 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009372 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009373 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009374}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009375
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009376static void
9377nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009378{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009379 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009380
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009381 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009382 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009383}
9384
Trond Myklebust36281ca2012-03-04 18:13:56 -05009385static bool nfs41_match_stateid(const nfs4_stateid *s1,
9386 const nfs4_stateid *s2)
9387{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009388 if (s1->type != s2->type)
9389 return false;
9390
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009391 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009392 return false;
9393
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009394 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009395 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009396
Anna Schumaker045c5512017-01-11 16:59:48 -05009397 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009398}
9399
Andy Adamson557134a2009-04-01 09:21:53 -04009400#endif /* CONFIG_NFS_V4_1 */
9401
Trond Myklebust36281ca2012-03-04 18:13:56 -05009402static bool nfs4_match_stateid(const nfs4_stateid *s1,
9403 const nfs4_stateid *s2)
9404{
Trond Myklebustf597c532012-03-04 18:13:56 -05009405 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009406}
9407
9408
Trond Myklebust17280172012-03-11 13:11:00 -04009409static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009410 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009411 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412 .recover_open = nfs4_open_reclaim,
9413 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009414 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009415 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416};
9417
Andy Adamson591d71c2009-04-01 09:22:47 -04009418#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009419static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009420 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9421 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9422 .recover_open = nfs4_open_reclaim,
9423 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009424 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009425 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009426 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009427};
9428#endif /* CONFIG_NFS_V4_1 */
9429
Trond Myklebust17280172012-03-11 13:11:00 -04009430static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009431 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009432 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009433 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009435 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436};
9437
Andy Adamson591d71c2009-04-01 09:22:47 -04009438#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009439static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009440 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9441 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009442 .recover_open = nfs41_open_expired,
9443 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009444 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009445};
9446#endif /* CONFIG_NFS_V4_1 */
9447
Trond Myklebust17280172012-03-11 13:11:00 -04009448static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009449 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009450 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009451 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009452};
9453
9454#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009455static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009456 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009457 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009458 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009459};
9460#endif
9461
Chuck Leverec011fe2013-10-17 14:12:39 -04009462static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009463 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009464 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009465};
9466
9467#if defined(CONFIG_NFS_V4_1)
9468static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009469 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009470 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009471};
9472#endif /* CONFIG_NFS_V4_1 */
9473
Trond Myklebust97dc1352010-06-16 09:52:26 -04009474static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9475 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009476 .init_caps = NFS_CAP_READDIRPLUS
9477 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009478 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009479 .init_client = nfs40_init_client,
9480 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009481 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009482 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009483 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009484 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009485 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009486 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009487 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9488 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9489 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009490 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009491};
9492
9493#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009494static struct nfs_seqid *
9495nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9496{
9497 return NULL;
9498}
9499
Trond Myklebust97dc1352010-06-16 09:52:26 -04009500static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9501 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009502 .init_caps = NFS_CAP_READDIRPLUS
9503 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009504 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009505 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009506 | NFS_CAP_ATOMIC_OPEN_V1
9507 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009508 .init_client = nfs41_init_client,
9509 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009510 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009511 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009512 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009513 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009514 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009515 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009516 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009517 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9518 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9519 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009520 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009521};
9522#endif
9523
Steve Dickson42c2c422013-05-22 12:50:38 -04009524#if defined(CONFIG_NFS_V4_2)
9525static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9526 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009527 .init_caps = NFS_CAP_READDIRPLUS
9528 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009529 | NFS_CAP_POSIX_LOCK
9530 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009531 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009532 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009533 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009534 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009535 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009536 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009537 | NFS_CAP_LAYOUTSTATS
9538 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009539 .init_client = nfs41_init_client,
9540 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009541 .match_stateid = nfs41_match_stateid,
9542 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009543 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009544 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009545 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009546 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009547 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009548 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9549 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9550 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009551 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009552};
9553#endif
9554
Trond Myklebust97dc1352010-06-16 09:52:26 -04009555const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9556 [0] = &nfs_v4_0_minor_ops,
9557#if defined(CONFIG_NFS_V4_1)
9558 [1] = &nfs_v4_1_minor_ops,
9559#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009560#if defined(CONFIG_NFS_V4_2)
9561 [2] = &nfs_v4_2_minor_ops,
9562#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009563};
9564
Trond Myklebust13997822016-07-24 17:10:52 -04009565static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009566{
9567 ssize_t error, error2;
9568
9569 error = generic_listxattr(dentry, list, size);
9570 if (error < 0)
9571 return error;
9572 if (list) {
9573 list += error;
9574 size -= error;
9575 }
9576
9577 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9578 if (error2 < 0)
9579 return error2;
9580 return error + error2;
9581}
9582
Trond Myklebust17f26b12013-08-21 15:48:42 -04009583static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009584 .create = nfs_create,
9585 .lookup = nfs_lookup,
9586 .atomic_open = nfs_atomic_open,
9587 .link = nfs_link,
9588 .unlink = nfs_unlink,
9589 .symlink = nfs_symlink,
9590 .mkdir = nfs_mkdir,
9591 .rmdir = nfs_rmdir,
9592 .mknod = nfs_mknod,
9593 .rename = nfs_rename,
9594 .permission = nfs_permission,
9595 .getattr = nfs_getattr,
9596 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009597 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009598};
9599
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009600static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009601 .permission = nfs_permission,
9602 .getattr = nfs_getattr,
9603 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009604 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009605};
9606
David Howells509de812006-08-22 20:06:11 -04009607const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608 .version = 4, /* protocol version */
9609 .dentry_ops = &nfs4_dentry_operations,
9610 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009611 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009612 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009613 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009614 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009615 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616 .getattr = nfs4_proc_getattr,
9617 .setattr = nfs4_proc_setattr,
9618 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009619 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620 .access = nfs4_proc_access,
9621 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622 .create = nfs4_proc_create,
9623 .remove = nfs4_proc_remove,
9624 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009625 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009627 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009628 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009629 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630 .link = nfs4_proc_link,
9631 .symlink = nfs4_proc_symlink,
9632 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009633 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 .readdir = nfs4_proc_readdir,
9635 .mknod = nfs4_proc_mknod,
9636 .statfs = nfs4_proc_statfs,
9637 .fsinfo = nfs4_proc_fsinfo,
9638 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009639 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009641 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009643 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009645 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009647 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009648 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009650 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009651 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009652 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009653 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009654 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009655 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009656 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009657 .create_server = nfs4_create_server,
9658 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659};
9660
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009661static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009662 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009663 .list = nfs4_xattr_list_nfs4_acl,
9664 .get = nfs4_xattr_get_nfs4_acl,
9665 .set = nfs4_xattr_set_nfs4_acl,
9666};
9667
9668const struct xattr_handler *nfs4_xattr_handlers[] = {
9669 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009670#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9671 &nfs4_xattr_nfs4_label_handler,
9672#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009673 NULL
9674};
9675
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676/*
9677 * Local variables:
9678 * c-basic-offset: 8
9679 * End:
9680 */