blob: c458a1e28b9174a5857b0656c5158e232e35024c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400228 | FATTR4_WORD0_FILEID,
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_MAXLINK
242 | FATTR4_WORD0_MAXNAME,
243 0
244};
245
Fred Isamandae100c2011-07-30 20:52:37 -0400246const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700250 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800253 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400257 FATTR4_WORD0_TYPE
258 | FATTR4_WORD0_CHANGE
259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
263 FATTR4_WORD1_MODE
264 | FATTR4_WORD1_NUMLINKS
265 | FATTR4_WORD1_OWNER
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400273};
274
Al Virobc4785c2006-10-19 23:28:51 -0700275static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct nfs4_readdir_arg *readdir)
277{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400278 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700279 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000282 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
284 return;
285 }
286
287 readdir->cookie = 0;
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
289 if (cookie == 2)
290 return;
291
292 /*
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
297 * instead of 1 or 2.
298 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800299 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (cookie == 0) {
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400309 *p++ = htonl(attrs); /* bitmap */
310 *p++ = htonl(12); /* attribute buffer length */
311 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400322 *p++ = htonl(attrs); /* bitmap */
323 *p++ = htonl(12); /* attribute buffer length */
324 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000325 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 readdir->pgbase = (char *)p - (char *)start;
328 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Trond Myklebust26d36302016-09-22 13:39:05 -0400332static void nfs4_test_and_free_stateid(struct nfs_server *server,
333 nfs4_stateid *stateid,
334 struct rpc_cred *cred)
335{
336 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
337
338 ops->test_and_free_expired(server, stateid, cred);
339}
340
341static void __nfs4_free_revoked_stateid(struct nfs_server *server,
342 nfs4_stateid *stateid,
343 struct rpc_cred *cred)
344{
345 stateid->type = NFS4_REVOKED_STATEID_TYPE;
346 nfs4_test_and_free_stateid(server, stateid, cred);
347}
348
349static void nfs4_free_revoked_stateid(struct nfs_server *server,
350 const nfs4_stateid *stateid,
351 struct rpc_cred *cred)
352{
353 nfs4_stateid tmp;
354
355 nfs4_stateid_copy(&tmp, stateid);
356 __nfs4_free_revoked_stateid(server, &tmp, cred);
357}
358
NeilBrown8478eaa2014-09-18 16:09:27 +1000359static long nfs4_update_delay(long *timeout)
360{
361 long ret;
362 if (!timeout)
363 return NFS4_POLL_RETRY_MAX;
364 if (*timeout <= 0)
365 *timeout = NFS4_POLL_RETRY_MIN;
366 if (*timeout > NFS4_POLL_RETRY_MAX)
367 *timeout = NFS4_POLL_RETRY_MAX;
368 ret = *timeout;
369 *timeout <<= 1;
370 return ret;
371}
372
Trond Myklebust65de8722008-12-23 15:21:44 -0500373static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
374{
375 int res = 0;
376
377 might_sleep();
378
NeilBrown8478eaa2014-09-18 16:09:27 +1000379 freezable_schedule_timeout_killable_unsafe(
380 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500381 if (fatal_signal_pending(current))
382 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 return res;
384}
385
386/* This is the error handling routine for processes that are allowed
387 * to sleep.
388 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400389static int nfs4_do_handle_exception(struct nfs_server *server,
390 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500391{
392 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500393 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400394 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500395 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500396 int ret = errorcode;
397
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400398 exception->delay = 0;
399 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400401
402 if (stateid == NULL && state != NULL)
403 stateid = &state->stateid;
404
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 switch(errorcode) {
406 case 0:
407 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_DELEG_REVOKED:
409 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400410 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400411 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400412 if (inode != NULL && stateid != NULL) {
413 nfs_inode_find_state_and_recover(inode,
414 stateid);
415 goto wait_on_recovery;
416 }
417 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400418 if (inode) {
419 int err;
420
421 err = nfs_async_inode_return_delegation(inode,
422 stateid);
423 if (err == 0)
424 goto wait_on_recovery;
425 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
426 exception->retry = 1;
427 break;
428 }
429 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 if (state == NULL)
431 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400432 ret = nfs4_schedule_stateid_recovery(server, state);
433 if (ret < 0)
434 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500435 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500437 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500438 nfs4_schedule_lease_recovery(clp);
439 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400440 case -NFS4ERR_MOVED:
441 ret = nfs4_schedule_migration_recovery(server);
442 if (ret < 0)
443 break;
444 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400445 case -NFS4ERR_LEASE_MOVED:
446 nfs4_schedule_lease_moved_recovery(clp);
447 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500448#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400449 case -NFS4ERR_BADSESSION:
450 case -NFS4ERR_BADSLOT:
451 case -NFS4ERR_BAD_HIGH_SLOT:
452 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
453 case -NFS4ERR_DEADSESSION:
454 case -NFS4ERR_SEQ_FALSE_RETRY:
455 case -NFS4ERR_SEQ_MISORDERED:
456 dprintk("%s ERROR: %d Reset session\n", __func__,
457 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400458 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400459 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500460#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500462 if (exception->timeout > HZ) {
463 /* We have retried a decent amount, time to
464 * fail
465 */
466 ret = -EBUSY;
467 break;
468 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500469 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400470 nfs_inc_server_stats(server, NFSIOS_DELAY);
471 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400472 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400473 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400474 exception->delay = 1;
475 return 0;
476
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400477 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500478 case -NFS4ERR_OLD_STATEID:
479 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800480 break;
481 case -NFS4ERR_BADOWNER:
482 /* The following works around a Linux server bug! */
483 case -NFS4ERR_BADNAME:
484 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
485 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
486 exception->retry = 1;
487 printk(KERN_WARNING "NFS: v4 server %s "
488 "does not accept raw "
489 "uid/gids. "
490 "Reenabling the idmapper.\n",
491 server->nfs_client->cl_hostname);
492 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500493 }
494 /* We failed to handle the error */
495 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500496wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400497 exception->recovering = 1;
498 return 0;
499}
500
501/* This is the error handling routine for processes that are allowed
502 * to sleep.
503 */
504int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
505{
506 struct nfs_client *clp = server->nfs_client;
507 int ret;
508
509 ret = nfs4_do_handle_exception(server, errorcode, exception);
510 if (exception->delay) {
511 ret = nfs4_delay(server->client, &exception->timeout);
512 goto out_retry;
513 }
514 if (exception->recovering) {
515 ret = nfs4_wait_clnt_recover(clp);
516 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
517 return -EIO;
518 goto out_retry;
519 }
520 return ret;
521out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500522 if (ret == 0)
523 exception->retry = 1;
524 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500525}
526
Trond Myklebust037fc982015-09-20 15:51:00 -0400527static int
528nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
529 int errorcode, struct nfs4_exception *exception)
530{
531 struct nfs_client *clp = server->nfs_client;
532 int ret;
533
534 ret = nfs4_do_handle_exception(server, errorcode, exception);
535 if (exception->delay) {
536 rpc_delay(task, nfs4_update_delay(&exception->timeout));
537 goto out_retry;
538 }
539 if (exception->recovering) {
540 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
541 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
542 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
543 goto out_retry;
544 }
545 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
546 ret = -EIO;
547 return ret;
548out_retry:
549 if (ret == 0)
550 exception->retry = 1;
551 return ret;
552}
553
554static int
555nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
556 struct nfs4_state *state, long *timeout)
557{
558 struct nfs4_exception exception = {
559 .state = state,
560 };
561
562 if (task->tk_status >= 0)
563 return 0;
564 if (timeout)
565 exception.timeout = *timeout;
566 task->tk_status = nfs4_async_handle_exception(task, server,
567 task->tk_status,
568 &exception);
569 if (exception.delay && timeout)
570 *timeout = exception.timeout;
571 if (exception.retry)
572 return -EAGAIN;
573 return 0;
574}
575
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400576/*
577 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
578 * or 'false' otherwise.
579 */
580static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
581{
582 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500583 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400584}
Trond Myklebust65de8722008-12-23 15:21:44 -0500585
Trond Myklebust452e9352010-07-31 14:29:06 -0400586static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_lock(&clp->cl_lock);
589 if (time_before(clp->cl_last_renewal,timestamp))
590 clp->cl_last_renewal = timestamp;
591 spin_unlock(&clp->cl_lock);
592}
593
Trond Myklebust452e9352010-07-31 14:29:06 -0400594static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
595{
Trond Myklebustbe824162015-07-05 14:50:46 -0400596 struct nfs_client *clp = server->nfs_client;
597
598 if (!nfs4_has_session(clp))
599 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400600}
601
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400602struct nfs4_call_sync_data {
603 const struct nfs_server *seq_server;
604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
606};
607
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800608void nfs4_init_sequence(struct nfs4_sequence_args *args,
609 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400610{
611 args->sa_slot = NULL;
612 args->sa_cache_this = cache_reply;
613 args->sa_privileged = 0;
614
615 res->sr_slot = NULL;
616}
617
618static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
619{
620 args->sa_privileged = 1;
621}
622
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400623static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400624{
625 struct nfs4_slot *slot = res->sr_slot;
626 struct nfs4_slot_table *tbl;
627
Chuck Lever3bd23842013-08-09 12:49:19 -0400628 tbl = slot->table;
629 spin_lock(&tbl->slot_tbl_lock);
630 if (!nfs41_wake_and_assign_slot(tbl, slot))
631 nfs4_free_slot(tbl, slot);
632 spin_unlock(&tbl->slot_tbl_lock);
633
634 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400635}
636
637static int nfs40_sequence_done(struct rpc_task *task,
638 struct nfs4_sequence_res *res)
639{
640 if (res->sr_slot != NULL)
641 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400642 return 1;
643}
644
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400645#if defined(CONFIG_NFS_V4_1)
646
Trond Myklebustd185a332010-06-16 09:52:25 -0400647static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400648{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500649 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400650 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500652 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400653
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500654 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500655 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500656
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400657 /* Bump the slot sequence number */
658 if (slot->seq_done)
659 slot->seq_nr++;
660 slot->seq_done = 0;
661
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500662 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500663 /* Be nice to the server: try to ensure that the last transmitted
664 * value for highest_user_slotid <= target_highest_slotid
665 */
666 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
667 send_new_highest_used_slotid = true;
668
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500669 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500670 send_new_highest_used_slotid = false;
671 goto out_unlock;
672 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500673 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500674
675 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
676 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500677out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500678 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400679 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500680 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400681 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400682 if (waitqueue_active(&tbl->slot_waitq))
683 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400684}
685
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400686static int nfs41_sequence_process(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400688{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500689 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500690 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400691 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500692 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500693 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500695 if (slot == NULL)
696 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400697 /* don't increment the sequence number if the task wasn't sent */
698 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400699 goto out;
700
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500702
Trond Myklebustac20d162012-12-15 15:36:07 -0500703 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400704 if (res->sr_status != -NFS4ERR_DELAY)
705 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500706 interrupted = true;
707 }
708
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400709 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500710 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400711 switch (res->sr_status) {
712 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400713 /* If previous op on slot was interrupted and we reused
714 * the seq# and got a reply from the cache, then retry
715 */
716 if (task->tk_status == -EREMOTEIO && interrupted) {
717 ++slot->seq_nr;
718 goto retry_nowait;
719 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400720 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400721 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500723 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500724 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
726 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500727 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 case 1:
730 /*
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
733 * operation..
734 * Mark the slot as having hosted an interrupted RPC call.
735 */
736 slot->interrupted = 1;
737 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 case -NFS4ERR_DELAY:
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
741 * of RFC5661.
742 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500746 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400747 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500748 case -NFS4ERR_BADSLOT:
749 /*
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
752 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500753 goto retry_nowait;
754 case -NFS4ERR_SEQ_MISORDERED:
755 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500756 * Was the last operation on this sequence interrupted?
757 * If so, retry after bumping the sequence number.
758 */
759 if (interrupted) {
760 ++slot->seq_nr;
761 goto retry_nowait;
762 }
763 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
766 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 if (slot->seq_nr != 1) {
768 slot->seq_nr = 1;
769 goto retry_nowait;
770 }
771 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500772 case -NFS4ERR_SEQ_FALSE_RETRY:
773 ++slot->seq_nr;
774 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400775 default:
776 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400777 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400778 }
779out:
780 /* The session may be reset by one of the error handlers. */
781 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500782out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500783 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500784retry_nowait:
785 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400786 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500787 task->tk_status = 0;
788 ret = 0;
789 }
790 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400791out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400792 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400793 goto out;
794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
795 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400796}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797
798int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
799{
800 if (!nfs41_sequence_process(task, res))
801 return 0;
802 if (res->sr_slot != NULL)
803 nfs41_sequence_free_slot(res);
804 return 1;
805
806}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500807EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400808
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400809static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
810{
811 if (res->sr_slot == NULL)
812 return 1;
813 if (res->sr_slot->table->session != NULL)
814 return nfs41_sequence_process(task, res);
815 return nfs40_sequence_done(task, res);
816}
817
818static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
819{
820 if (res->sr_slot != NULL) {
821 if (res->sr_slot->table->session != NULL)
822 nfs41_sequence_free_slot(res);
823 else
824 nfs40_sequence_free_slot(res);
825 }
826}
827
Peng Tao2c4b1312014-06-11 05:24:16 +0800828int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400829{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500830 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400832 if (!res->sr_slot->table->session)
833 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400835}
Peng Tao2c4b1312014-06-11 05:24:16 +0800836EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400837
Andy Adamsonce5039c2009-04-01 09:22:13 -0400838static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
839{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400840 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400841
Trond Myklebust035168ab2010-06-16 09:52:26 -0400842 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
843
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500844 nfs4_setup_sequence(data->seq_server->nfs_client,
845 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846}
847
Andy Adamson69ab40c2009-04-01 09:22:19 -0400848static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
849{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400850 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400851
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853}
854
Trond Myklebust17280172012-03-11 13:11:00 -0400855static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400856 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400858};
859
Chuck Lever3bd23842013-08-09 12:49:19 -0400860#else /* !CONFIG_NFS_V4_1 */
861
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400862static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
863{
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL)
870 nfs40_sequence_free_slot(res);
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task,
874 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400875{
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400877}
Peng Tao2c4b1312014-06-11 05:24:16 +0800878EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400879
880#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400881
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500882int nfs4_setup_sequence(const struct nfs_client *client,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 struct rpc_task *task)
886{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500888 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500889 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500890
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500891 /* slot already allocated? */
892 if (res->sr_slot != NULL)
893 goto out_start;
894
Anna Schumaker76ee0352017-01-10 16:49:31 -0500895 if (session) {
896 tbl = &session->fc_slot_table;
897 task->tk_timeout = 0;
898 }
899
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
903 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500904
Anna Schumaker3d358082017-01-11 10:54:04 -0500905 slot = nfs4_alloc_slot(tbl);
906 if (IS_ERR(slot)) {
907 /* Try again in 1/4 second */
908 if (slot == ERR_PTR(-ENOMEM))
909 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500910 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500911 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500912 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500913
Anna Schumaker3d358082017-01-11 10:54:04 -0500914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 if (session) {
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500922 }
923
Anna Schumakerad05cc02017-01-11 13:37:06 -0500924 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500925out_start:
926 rpc_call_start(task);
927 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500928
929out_sleep:
930 if (args->sa_privileged)
931 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
932 NULL, RPC_PRIORITY_PRIVILEGED);
933 else
934 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
935 spin_unlock(&tbl->slot_tbl_lock);
936 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500937}
938EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
939
Chuck Lever9915ea72013-08-09 12:48:27 -0400940static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
941{
942 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400944 data->seq_args, data->seq_res, task);
945}
946
947static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
948{
949 struct nfs4_call_sync_data *data = calldata;
950 nfs4_sequence_done(task, data->seq_res);
951}
952
953static const struct rpc_call_ops nfs40_call_sync_ops = {
954 .rpc_call_prepare = nfs40_call_sync_prepare,
955 .rpc_call_done = nfs40_call_sync_done,
956};
957
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400958static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400959 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 struct nfs4_sequence_args *args,
962 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400963{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 int ret;
965 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400966 struct nfs_client *clp = server->nfs_client;
967 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 .seq_server = server,
969 .seq_args = args,
970 .seq_res = res,
971 };
972 struct rpc_task_setup task_setup = {
973 .rpc_client = clnt,
974 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400975 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 .callback_data = &data
977 };
978
979 task = rpc_run_task(&task_setup);
980 if (IS_ERR(task))
981 ret = PTR_ERR(task);
982 else {
983 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400984 rpc_put_task(task);
985 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 return ret;
987}
988
Bryan Schumaker7c513052011-03-24 17:12:24 +0000989int nfs4_call_sync(struct rpc_clnt *clnt,
990 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000991 struct rpc_message *msg,
992 struct nfs4_sequence_args *args,
993 struct nfs4_sequence_res *res,
994 int cache_reply)
995{
Chuck Levera9c92d62013-08-09 12:48:18 -0400996 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400997 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001000static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1001 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct nfs_inode *nfsi = NFS_I(dir);
1004
1005 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001006 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001007 if (cinfo->atomic && cinfo->before == dir->i_version) {
1008 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1009 nfsi->attrtimeo_timestamp = jiffies;
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001012 if (cinfo->before != dir->i_version)
1013 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1014 NFS_INO_INVALID_ACL;
1015 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001016 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001017 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001018 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001019 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_unlock(&dir->i_lock);
1021}
1022
1023struct nfs4_opendata {
1024 struct kref kref;
1025 struct nfs_openargs o_arg;
1026 struct nfs_openres o_res;
1027 struct nfs_open_confirmargs c_arg;
1028 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001029 struct nfs4_string owner_name;
1030 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001031 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001033 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001035 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfs4_state_owner *owner;
1037 struct nfs4_state *state;
1038 struct iattr attrs;
1039 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001040 bool rpc_done;
1041 bool file_created;
1042 bool is_recover;
1043 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001044 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001046
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1048 int err, struct nfs4_exception *exception)
1049{
1050 if (err != -EINVAL)
1051 return false;
1052 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1053 return false;
1054 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1055 exception->retry = 1;
1056 return true;
1057}
1058
Trond Myklebust6ae37332015-01-30 14:21:14 -05001059static u32
1060nfs4_map_atomic_open_share(struct nfs_server *server,
1061 fmode_t fmode, int openflags)
1062{
1063 u32 res = 0;
1064
1065 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1066 case FMODE_READ:
1067 res = NFS4_SHARE_ACCESS_READ;
1068 break;
1069 case FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_WRITE;
1071 break;
1072 case FMODE_READ|FMODE_WRITE:
1073 res = NFS4_SHARE_ACCESS_BOTH;
1074 }
1075 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1076 goto out;
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags & O_DIRECT)
1079 res |= NFS4_SHARE_WANT_NO_DELEG;
1080out:
1081 return res;
1082}
1083
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001084static enum open_claim_type4
1085nfs4_map_atomic_open_claim(struct nfs_server *server,
1086 enum open_claim_type4 claim)
1087{
1088 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1089 return claim;
1090 switch (claim) {
1091 default:
1092 return claim;
1093 case NFS4_OPEN_CLAIM_FH:
1094 return NFS4_OPEN_CLAIM_NULL;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1099 }
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103{
1104 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001105 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 p->o_res.seqid = p->o_arg.seqid;
1107 p->c_res.seqid = p->c_arg.seqid;
1108 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001109 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001111 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Al Viro82a2c1b2011-06-22 18:30:55 -04001114static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001116 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001118 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001119 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120{
Al Viro82a2c1b2011-06-22 18:30:55 -04001121 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001122 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001123 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001124 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125 struct nfs4_opendata *p;
1126
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001127 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 if (p == NULL)
1129 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001130
1131 p->f_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->f_label))
1133 goto err_free_p;
1134
Kinglong Meea49c2692015-07-27 15:31:38 +08001135 p->a_label = nfs4_label_alloc(server, gfp_mask);
1136 if (IS_ERR(p->a_label))
1137 goto err_free_f;
1138
Trond Myklebust63f5f792015-01-23 19:19:25 -05001139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1140 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001141 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001142 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001143 nfs_sb_active(dentry->d_sb);
1144 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145 p->dir = parent;
1146 p->owner = sp;
1147 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 p->o_arg.open_flags = flags;
1149 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001150 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001152 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1153 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags & O_EXCL)) {
1157 /* ask server to check for all possible rights as results
1158 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001159 switch (p->o_arg.claim) {
1160 default:
1161 break;
1162 case NFS4_OPEN_CLAIM_NULL:
1163 case NFS4_OPEN_CLAIM_FH:
1164 p->o_arg.access = NFS4_ACCESS_READ |
1165 NFS4_ACCESS_MODIFY |
1166 NFS4_ACCESS_EXTEND |
1167 NFS4_ACCESS_EXECUTE;
1168 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001169 }
David Howells7539bba2006-08-22 20:06:09 -04001170 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001171 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1172 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001173 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001175 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001176 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001177 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1182 p->o_arg.fh = NFS_FH(dir);
1183 break;
1184 case NFS4_OPEN_CLAIM_PREVIOUS:
1185 case NFS4_OPEN_CLAIM_FH:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001188 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001189 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001190 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001191 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001192
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->o_arg.u.attrs = &p->attrs;
1194 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001195
1196 verf[0] = jiffies;
1197 verf[1] = current->pid;
1198 memcpy(p->o_arg.u.verifier.data, verf,
1199 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001201 p->c_arg.fh = &p->o_res.fh;
1202 p->c_arg.stateid = &p->o_res.stateid;
1203 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 nfs4_label_free(p->a_label);
1210err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001211 nfs4_label_free(p->f_label);
1212err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 kfree(p);
1214err:
1215 dput(parent);
1216 return NULL;
1217}
1218
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001219static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221 struct nfs4_opendata *p = container_of(kref,
1222 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224
1225 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001226 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 if (p->state != NULL)
1228 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001230
Kinglong Meea49c2692015-07-27 15:31:38 +08001231 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001232 nfs4_label_free(p->f_label);
1233
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 dput(p->dentry);
1236 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001237 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001238 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001239 kfree(p);
1240}
1241
1242static void nfs4_opendata_put(struct nfs4_opendata *p)
1243{
1244 if (p != NULL)
1245 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246}
1247
Trond Myklebust24311f82015-09-20 10:50:17 -04001248static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1249 fmode_t fmode)
1250{
1251 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1252 case FMODE_READ|FMODE_WRITE:
1253 return state->n_rdwr != 0;
1254 case FMODE_WRITE:
1255 return state->n_wronly != 0;
1256 case FMODE_READ:
1257 return state->n_rdonly != 0;
1258 }
1259 WARN_ON_ONCE(1);
1260 return false;
1261}
1262
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264{
1265 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266
Trond Myklebust536e43d2012-01-17 22:04:26 -05001267 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268 goto out;
1269 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001270 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1272 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1276 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 break;
1278 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001279 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1280 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001281 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001283 return ret;
1284}
1285
Trond Myklebust2a606182015-08-19 22:30:00 -05001286static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1287 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001289 if (delegation == NULL)
1290 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001293 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1294 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001295 switch (claim) {
1296 case NFS4_OPEN_CLAIM_NULL:
1297 case NFS4_OPEN_CLAIM_FH:
1298 break;
1299 case NFS4_OPEN_CLAIM_PREVIOUS:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1301 break;
1302 default:
1303 return 0;
1304 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001305 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 return 1;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001310{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001312 case FMODE_WRITE:
1313 state->n_wronly++;
1314 break;
1315 case FMODE_READ:
1316 state->n_rdonly++;
1317 break;
1318 case FMODE_READ|FMODE_WRITE:
1319 state->n_rdwr++;
1320 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001322}
1323
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001324#ifdef CONFIG_NFS_V4_1
1325static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1326{
1327 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1328 return true;
1329 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1330 return true;
1331 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1332 return true;
1333 return false;
1334}
1335#endif /* CONFIG_NFS_V4_1 */
1336
Trond Myklebust4f14c192014-02-12 19:15:06 -05001337static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001338{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001339 struct nfs_client *clp = state->owner->so_server->nfs_client;
1340 bool need_recover = false;
1341
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1343 need_recover = true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1345 need_recover = true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1347 need_recover = true;
1348 if (need_recover)
1349 nfs4_state_mark_reclaim_nograce(clp, state);
1350}
1351
Trond Myklebuste999e802014-02-10 18:20:47 -05001352static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001353 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001354{
1355 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001358 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001360 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001362 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1363 return true;
1364 return false;
1365}
1366
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1368{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001369 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1370 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001371 if (state->n_wronly)
1372 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1373 if (state->n_rdonly)
1374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1375 if (state->n_rdwr)
1376 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001377 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001378}
1379
Trond Myklebust226056c2014-02-11 10:41:07 -05001380static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1381 nfs4_stateid *stateid, fmode_t fmode)
1382{
1383 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1384 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1385 case FMODE_WRITE:
1386 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1387 break;
1388 case FMODE_READ:
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 break;
1391 case 0:
1392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_bit(NFS_OPEN_STATE, &state->flags);
1395 }
1396 if (stateid == NULL)
1397 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1400 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001401 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001402 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001403 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&state->stateid, stateid);
1406 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001407}
1408
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001409static void nfs_clear_open_stateid(struct nfs4_state *state,
1410 nfs4_stateid *arg_stateid,
1411 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001412{
1413 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1416 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1419 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001420}
1421
Trond Myklebust1393d962016-09-22 13:39:13 -04001422static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1423 const nfs4_stateid *stateid, fmode_t fmode,
1424 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001425{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 case FMODE_READ:
1428 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1429 break;
1430 case FMODE_WRITE:
1431 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1432 break;
1433 case FMODE_READ|FMODE_WRITE:
1434 set_bit(NFS_O_RDWR_STATE, &state->flags);
1435 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return;
1438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1439 nfs4_stateid_copy(&state->stateid, stateid);
1440 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001441}
1442
Trond Myklebust1393d962016-09-22 13:39:13 -04001443static void __update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *open_stateid,
1445 const nfs4_stateid *deleg_stateid,
1446 fmode_t fmode,
1447 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001449 /*
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1452 */
Andrew Elble361cad32015-12-02 09:20:57 -05001453 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001454 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001455 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457 set_bit(NFS_DELEGATED_STATE, &state->flags);
1458 }
1459 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001460 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001463 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust1393d962016-09-22 13:39:13 -04001466static int update_open_stateid(struct nfs4_state *state,
1467 const nfs4_stateid *open_stateid,
1468 const nfs4_stateid *delegation,
1469 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001470{
Trond Myklebust1393d962016-09-22 13:39:13 -04001471 struct nfs_server *server = NFS_SERVER(state->inode);
1472 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001473 struct nfs_inode *nfsi = NFS_I(state->inode);
1474 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001475 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001476 int ret = 0;
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001479
1480 rcu_read_lock();
1481 deleg_cur = rcu_dereference(nfsi->delegation);
1482 if (deleg_cur == NULL)
1483 goto no_delegation;
1484
1485 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001486 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001487 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001489 goto no_delegation_unlock;
1490
1491 if (delegation == NULL)
1492 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001493 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001494 goto no_delegation_unlock;
1495
Trond Myklebustb7391f42008-12-23 15:21:52 -05001496 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001497 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1498 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001499 ret = 1;
1500no_delegation_unlock:
1501 spin_unlock(&deleg_cur->lock);
1502no_delegation:
1503 rcu_read_unlock();
1504
1505 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 nfs4_schedule_state_manager(clp);
1511 if (freeme.type != 0)
1512 nfs4_test_and_free_stateid(server, &freeme,
1513 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001514
1515 return ret;
1516}
1517
Trond Myklebust39071e62015-01-24 15:07:56 -05001518static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1519 const nfs4_stateid *stateid)
1520{
1521 struct nfs4_state *state = lsp->ls_state;
1522 bool ret = false;
1523
1524 spin_lock(&state->state_lock);
1525 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1526 goto out_noupdate;
1527 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1530 ret = true;
1531out_noupdate:
1532 spin_unlock(&state->state_lock);
1533 return ret;
1534}
Trond Myklebust34310432008-12-23 15:21:38 -05001535
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001536static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537{
1538 struct nfs_delegation *delegation;
1539
1540 rcu_read_lock();
1541 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001542 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_unlock();
1544 return;
1545 }
1546 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001547 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548}
1549
Trond Myklebust6ee41262007-07-08 14:11:36 -04001550static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551{
1552 struct nfs4_state *state = opendata->state;
1553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001555 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001557 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 nfs4_stateid stateid;
1559 int ret = -EAGAIN;
1560
Trond Myklebustaac00a82007-07-05 19:02:21 -04001561 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001565 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001566 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001568 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 rcu_read_lock();
1570 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001571 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001572 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001573 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001574 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001576 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001578 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001579 if (!opendata->is_recover) {
1580 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1581 if (ret != 0)
1582 goto out;
1583 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001584 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001585
1586 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001587 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001588 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590out:
1591 return ERR_PTR(ret);
1592out_return_state:
1593 atomic_inc(&state->count);
1594 return state;
1595}
1596
Andy Adamsone23008e2012-10-02 21:07:32 -04001597static void
1598nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1599{
1600 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1601 struct nfs_delegation *delegation;
1602 int delegation_flags = 0;
1603
1604 rcu_read_lock();
1605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1606 if (delegation)
1607 delegation_flags = delegation->flags;
1608 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001609 switch (data->o_arg.claim) {
1610 default:
1611 break;
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1617 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001618 return;
1619 }
1620 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001621 nfs_inode_set_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624 else
1625 nfs_inode_reclaim_delegation(state->inode,
1626 data->owner->so_cred,
1627 &data->o_res);
1628}
1629
1630/*
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1633 */
1634static struct nfs4_state *
1635_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1636{
1637 struct inode *inode = data->state->inode;
1638 struct nfs4_state *state = data->state;
1639 int ret;
1640
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001641 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001642 if (data->rpc_status)
1643 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001644 /* cached opens have already been processed */
1645 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001646 }
1647
Andy Adamsone23008e2012-10-02 21:07:32 -04001648 ret = nfs_refresh_inode(inode, &data->f_attr);
1649 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001650 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001651
1652 if (data->o_res.delegation_type != 0)
1653 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001654update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 update_open_stateid(state, &data->o_res.stateid, NULL,
1656 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001657 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001658
1659 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001660}
1661
1662static struct nfs4_state *
1663_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664{
1665 struct inode *inode;
1666 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001670 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001671 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 goto out;
1673 }
1674
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001678 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001679 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001680 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err;
1682 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 state = nfs4_get_open_state(inode, data->owner);
1684 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001686 if (data->o_res.delegation_type != 0)
1687 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001688 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001692 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001694err_put_inode:
1695 iput(inode);
1696err:
1697 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
1699
Andy Adamsone23008e2012-10-02 21:07:32 -04001700static struct nfs4_state *
1701nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1702{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001703 struct nfs4_state *ret;
1704
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001706 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1707 else
1708 ret = _nfs4_opendata_to_nfs4_state(data);
1709 nfs4_sequence_free_slot(&data->o_res.seq_res);
1710 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001711}
1712
Trond Myklebust864472e2006-01-03 09:55:15 +01001713static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1714{
1715 struct nfs_inode *nfsi = NFS_I(state->inode);
1716 struct nfs_open_context *ctx;
1717
1718 spin_lock(&state->inode->i_lock);
1719 list_for_each_entry(ctx, &nfsi->open_files, list) {
1720 if (ctx->state != state)
1721 continue;
1722 get_nfs_open_context(ctx);
1723 spin_unlock(&state->inode->i_lock);
1724 return ctx;
1725 }
1726 spin_unlock(&state->inode->i_lock);
1727 return ERR_PTR(-ENOENT);
1728}
1729
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001730static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1731 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001732{
1733 struct nfs4_opendata *opendata;
1734
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001735 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001736 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 if (opendata == NULL)
1738 return ERR_PTR(-ENOMEM);
1739 opendata->state = state;
1740 atomic_inc(&state->count);
1741 return opendata;
1742}
1743
Trond Myklebust24311f82015-09-20 10:50:17 -04001744static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1745 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001746{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001747 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
1749
Trond Myklebust24311f82015-09-20 10:50:17 -04001750 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001751 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001752 opendata->o_arg.open_flags = 0;
1753 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001754 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1755 NFS_SB(opendata->dentry->d_sb),
1756 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1758 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1759 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001760 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 if (ret != 0)
1762 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001763 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 if (IS_ERR(newstate))
1765 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001766 if (newstate != opendata->state)
1767 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001768 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001769 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001770}
1771
1772static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1773{
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
1775
Trond Myklebust4f14c192014-02-12 19:15:06 -05001776 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001781 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001782 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001783 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1785 if (ret != 0)
1786 return ret;
1787 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1788 if (ret != 0)
1789 return ret;
1790 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1791 if (ret != 0)
1792 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001793 /*
1794 * We may have performed cached opens for all three recoveries.
1795 * Check if we need to update the current stateid.
1796 */
1797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001798 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001799 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001801 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001802 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
1808 * OPEN_RECLAIM:
1809 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001811static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int status;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1819 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (IS_ERR(opendata))
1821 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_lock();
1823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001824 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001825 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001826 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 opendata->o_arg.u.delegation_type = delegation_type;
1828 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return status;
1831}
1832
Trond Myklebust539cd032007-06-05 11:46:42 -04001833static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct nfs_server *server = NFS_SERVER(state->inode);
1836 struct nfs4_exception exception = { };
1837 int err;
1838 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001839 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001840 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001841 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1842 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001843 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001844 break;
1845 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 } while (exception.retry);
1847 return err;
1848}
1849
Trond Myklebust864472e2006-01-03 09:55:15 +01001850static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1851{
1852 struct nfs_open_context *ctx;
1853 int ret;
1854
1855 ctx = nfs4_state_find_open_context(state);
1856 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001857 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001858 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 put_nfs_open_context(ctx);
1860 return ret;
1861}
1862
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001863static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001865 switch (err) {
1866 default:
1867 printk(KERN_ERR "NFS: %s: unhandled error "
1868 "%d.\n", __func__, err);
1869 case 0:
1870 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001871 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 case -ESTALE:
1873 break;
1874 case -NFS4ERR_BADSESSION:
1875 case -NFS4ERR_BADSLOT:
1876 case -NFS4ERR_BAD_HIGH_SLOT:
1877 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1878 case -NFS4ERR_DEADSESSION:
1879 set_bit(NFS_DELEGATED_STATE, &state->flags);
1880 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1881 return -EAGAIN;
1882 case -NFS4ERR_STALE_CLIENTID:
1883 case -NFS4ERR_STALE_STATEID:
1884 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001885 /* Don't recall a delegation if it was lost */
1886 nfs4_schedule_lease_recovery(server->nfs_client);
1887 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001888 case -NFS4ERR_MOVED:
1889 nfs4_schedule_migration_recovery(server);
1890 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001891 case -NFS4ERR_LEASE_MOVED:
1892 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1893 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001894 case -NFS4ERR_DELEG_REVOKED:
1895 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001896 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001897 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001898 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 nfs_inode_find_state_and_recover(state->inode,
1900 stateid);
1901 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001902 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001903 case -NFS4ERR_DELAY:
1904 case -NFS4ERR_GRACE:
1905 set_bit(NFS_DELEGATED_STATE, &state->flags);
1906 ssleep(1);
1907 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001908 case -ENOMEM:
1909 case -NFS4ERR_DENIED:
1910 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1911 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
1914}
1915
Trond Myklebust24311f82015-09-20 10:50:17 -04001916int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1917 struct nfs4_state *state, const nfs4_stateid *stateid,
1918 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919{
1920 struct nfs_server *server = NFS_SERVER(state->inode);
1921 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001922 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001923
1924 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1925 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1926 if (IS_ERR(opendata))
1927 return PTR_ERR(opendata);
1928 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001929 write_seqlock(&state->seqlock);
1930 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1931 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1933 switch (type & (FMODE_READ|FMODE_WRITE)) {
1934 case FMODE_READ|FMODE_WRITE:
1935 case FMODE_WRITE:
1936 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1937 if (err)
1938 break;
1939 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1940 if (err)
1941 break;
1942 case FMODE_READ:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1944 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001945 nfs4_opendata_put(opendata);
1946 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1947}
1948
Chuck Leverbe05c862013-08-09 12:49:47 -04001949static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1950{
1951 struct nfs4_opendata *data = calldata;
1952
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001953 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1954 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001955}
1956
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001957static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1958{
1959 struct nfs4_opendata *data = calldata;
1960
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001961 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001963 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001964 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001965 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001966 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001967 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001968 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970}
1971
1972static void nfs4_open_confirm_release(void *calldata)
1973{
1974 struct nfs4_opendata *data = calldata;
1975 struct nfs4_state *state = NULL;
1976
1977 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001978 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 goto out_free;
1980 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001984 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001985 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001986out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001987 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988}
1989
1990static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001991 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 .rpc_call_done = nfs4_open_confirm_done,
1993 .rpc_release = nfs4_open_confirm_release,
1994};
1995
1996/*
1997 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1998 */
1999static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2000{
David Howells2b0143b2015-03-17 22:25:59 +00002001 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2005 .rpc_argp = &data->c_arg,
2006 .rpc_resp = &data->c_res,
2007 .rpc_cred = data->owner->so_cred,
2008 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002009 struct rpc_task_setup task_setup_data = {
2010 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002011 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .callback_ops = &nfs4_open_confirm_ops,
2013 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002014 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002015 .flags = RPC_TASK_ASYNC,
2016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int status;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002021 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002022 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002024 if (data->is_recover)
2025 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002026 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002027 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002029 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002031 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002032 smp_wmb();
2033 } else
2034 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002035 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return status;
2037}
2038
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002039static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 struct nfs4_opendata *data = calldata;
2042 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002043 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002044 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002047 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002048 /*
2049 * Check if we still need to send an OPEN call, or if we can use
2050 * a delegation instead.
2051 */
2052 if (data->state != NULL) {
2053 struct nfs_delegation *delegation;
2054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002056 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_lock();
2058 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002059 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002060 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002061 rcu_read_unlock();
2062 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002063 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002064 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002065 switch (claim) {
2066 default:
2067 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_PREVIOUS:
2069 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2070 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002071 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002072 case NFS4_OPEN_CLAIM_FH:
2073 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2075 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002077 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002078 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002079 &data->o_res.seq_res,
2080 task) != 0)
2081 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002082
2083 /* Set the create mode (note dependency on the session type) */
2084 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2085 if (data->o_arg.open_flags & O_EXCL) {
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2087 if (nfs4_has_persistent_session(clp))
2088 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2089 else if (clp->cl_mvops->minor_version > 0)
2090 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2091 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002093unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002094 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002095 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002096out_no_action:
2097 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002098out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002099 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002100}
2101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102static void nfs4_open_done(struct rpc_task *task, void *calldata)
2103{
2104 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002107
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002108 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002109 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002112 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2113 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 case S_IFREG:
2115 break;
2116 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002118 break;
2119 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 break;
2122 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002123 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002124 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002127 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2128 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002129 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002130 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133static void nfs4_open_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002139 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 goto out_free;
2144 /* In case we need an open_confirm, no cleanup! */
2145 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2146 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002148 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002151 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002152}
2153
2154static const struct rpc_call_ops nfs4_open_ops = {
2155 .rpc_call_prepare = nfs4_open_prepare,
2156 .rpc_call_done = nfs4_open_done,
2157 .rpc_release = nfs4_open_release,
2158};
2159
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002160static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 struct nfs_server *server = NFS_SERVER(dir);
2164 struct nfs_openargs *o_arg = &data->o_arg;
2165 struct nfs_openres *o_res = &data->o_res;
2166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2169 .rpc_argp = o_arg,
2170 .rpc_resp = o_res,
2171 .rpc_cred = data->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_open_ops,
2177 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .flags = RPC_TASK_ASYNC,
2180 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 int status;
2182
Chuck Levera9c92d62013-08-09 12:48:18 -04002183 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002185 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002186 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002187 data->cancelled = false;
2188 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002189 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002190 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002191 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002192 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002194 if (IS_ERR(task))
2195 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002196 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002197 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002198 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002199 smp_wmb();
2200 } else
2201 status = data->rpc_status;
2202 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002203
2204 return status;
2205}
2206
2207static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2208{
David Howells2b0143b2015-03-17 22:25:59 +00002209 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002210 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002211 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212
2213 status = nfs4_run_open_task(data, 1);
2214 if (status != 0 || !data->rpc_done)
2215 return status;
2216
Trond Myklebust6926afd12012-01-07 13:22:46 -05002217 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2218
Anna Schumakerd7e98252017-01-11 16:13:29 -05002219 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002221
2222 return status;
2223}
2224
Trond Myklebustf3792d62014-07-10 08:54:32 -04002225/*
2226 * Additional permission checks in order to distinguish between an
2227 * open for read, and an open for execute. This works around the
2228 * fact that NFSv4 OPEN treats read and execute permissions as being
2229 * the same.
2230 * Note that in the non-execute case, we want to turn off permission
2231 * checking if we just created a new file (POSIX open() semantics).
2232 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233static int nfs4_opendata_access(struct rpc_cred *cred,
2234 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002235 struct nfs4_state *state, fmode_t fmode,
2236 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002237{
2238 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002239 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002240
2241 /* access call failed or for some reason the server doesn't
2242 * support any access modes -- defer access call until later */
2243 if (opendata->o_res.access_supported == 0)
2244 return 0;
2245
2246 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002247 /*
2248 * Use openflags to check for exec, because fmode won't
2249 * always have FMODE_EXEC set when file open for exec.
2250 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 if (openflags & __FMODE_EXEC) {
2252 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002253 if (S_ISDIR(state->inode->i_mode))
2254 mask = NFS4_ACCESS_LOOKUP;
2255 else
2256 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002257 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002258 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002259
2260 cache.cred = cred;
2261 cache.jiffies = jiffies;
2262 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2263 nfs_access_add_cache(state->inode, &cache);
2264
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002265 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2266 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002267 return 0;
2268
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002269 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002270}
2271
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002272/*
2273 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2274 */
2275static int _nfs4_proc_open(struct nfs4_opendata *data)
2276{
David Howells2b0143b2015-03-17 22:25:59 +00002277 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002278 struct nfs_server *server = NFS_SERVER(dir);
2279 struct nfs_openargs *o_arg = &data->o_arg;
2280 struct nfs_openres *o_res = &data->o_res;
2281 int status;
2282
2283 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002284 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002285 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002286 if (status != 0) {
2287 if (status == -NFS4ERR_BADNAME &&
2288 !(o_arg->open_flags & O_CREAT))
2289 return -ENOENT;
2290 return status;
2291 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002292
Trond Myklebust6926afd12012-01-07 13:22:46 -05002293 nfs_fattr_map_and_free_names(server, &data->f_attr);
2294
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002295 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002296 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002297 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002298 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002299 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002300 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002301 update_changeattr(dir, &o_res->cinfo,
2302 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002303 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002304 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2305 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002307 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002311 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2312 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002313 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002314 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318/*
2319 * OPEN_EXPIRED:
2320 * reclaim state on the server after a network partition.
2321 * Assumes caller holds the appropriate lock
2322 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002323static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002325 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002326 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002328 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002329 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002330 if (IS_ERR(opendata))
2331 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002332 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002333 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002334 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002335 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002339static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002340{
Trond Myklebust539cd032007-06-05 11:46:42 -04002341 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002342 struct nfs4_exception exception = { };
2343 int err;
2344
2345 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002346 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002347 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002348 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2349 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002350 switch (err) {
2351 default:
2352 goto out;
2353 case -NFS4ERR_GRACE:
2354 case -NFS4ERR_DELAY:
2355 nfs4_handle_exception(server, err, &exception);
2356 err = 0;
2357 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002358 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002359out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002360 return err;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002366 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Trond Myklebust864472e2006-01-03 09:55:15 +01002368 ctx = nfs4_state_find_open_context(state);
2369 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002370 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002371 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002372 put_nfs_open_context(ctx);
2373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
Trond Myklebust41020b62016-09-22 13:38:58 -04002376static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2377 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002378{
Trond Myklebust41020b62016-09-22 13:38:58 -04002379 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002380 write_seqlock(&state->seqlock);
2381 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2382 write_sequnlock(&state->seqlock);
2383 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2384}
2385
2386static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2387{
2388 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002389 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002390}
2391
2392static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2393{
2394 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2395 nfs40_clear_delegation_stateid(state);
2396 return nfs4_open_expired(sp, state);
2397}
2398
Trond Myklebust45870d62016-09-22 13:38:59 -04002399static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2400 nfs4_stateid *stateid,
2401 struct rpc_cred *cred)
2402{
2403 return -NFS4ERR_BAD_STATEID;
2404}
2405
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002406#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002407static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2408 nfs4_stateid *stateid,
2409 struct rpc_cred *cred)
2410{
2411 int status;
2412
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002413 switch (stateid->type) {
2414 default:
2415 break;
2416 case NFS4_INVALID_STATEID_TYPE:
2417 case NFS4_SPECIAL_STATEID_TYPE:
2418 return -NFS4ERR_BAD_STATEID;
2419 case NFS4_REVOKED_STATEID_TYPE:
2420 goto out_free;
2421 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002422
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002423 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002424 switch (status) {
2425 case -NFS4ERR_EXPIRED:
2426 case -NFS4ERR_ADMIN_REVOKED:
2427 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002428 break;
2429 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002430 return status;
2431 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002432out_free:
2433 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002434 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002435 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002436}
2437
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002438static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002439{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002440 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002441 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002443 struct rpc_cred *cred;
2444 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002445
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002446 /* Get the delegation credential for use by test/free_stateid */
2447 rcu_read_lock();
2448 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002449 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002450 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002451 return;
2452 }
2453
2454 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002455 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2456 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2457 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002458 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002459 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002460 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002461 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002462
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002463 cred = get_rpccred(delegation->cred);
2464 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002465 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002466 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002467 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002468 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002469
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002470 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002471}
2472
2473/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002474 * nfs41_check_expired_locks - possibly free a lock stateid
2475 *
2476 * @state: NFSv4 state for an inode
2477 *
2478 * Returns NFS_OK if recovery for this stateid is now finished.
2479 * Otherwise a negative NFS4ERR value is returned.
2480 */
2481static int nfs41_check_expired_locks(struct nfs4_state *state)
2482{
2483 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002484 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002485 struct nfs_server *server = NFS_SERVER(state->inode);
2486
2487 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2488 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002489
2490 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002491 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2492 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2493 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2494
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002495 atomic_inc(&lsp->ls_count);
2496 spin_unlock(&state->state_lock);
2497
2498 nfs4_put_lock_state(prev);
2499 prev = lsp;
2500
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002501 status = nfs41_test_and_free_expired_stateid(server,
2502 &lsp->ls_stateid,
2503 cred);
2504 trace_nfs4_test_lock_stateid(state, lsp, status);
2505 if (status == -NFS4ERR_EXPIRED ||
2506 status == -NFS4ERR_BAD_STATEID) {
2507 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002508 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002509 if (!recover_lost_locks)
2510 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2511 } else if (status != NFS_OK) {
2512 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002513 nfs4_put_lock_state(prev);
2514 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002515 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002516 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002517 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002518 }
2519 spin_unlock(&state->state_lock);
2520 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002521out:
2522 return ret;
2523}
2524
2525/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002526 * nfs41_check_open_stateid - possibly free an open stateid
2527 *
2528 * @state: NFSv4 state for an inode
2529 *
2530 * Returns NFS_OK if recovery for this stateid is now finished.
2531 * Otherwise a negative NFS4ERR value is returned.
2532 */
2533static int nfs41_check_open_stateid(struct nfs4_state *state)
2534{
2535 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002536 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002538 int status;
2539
Trond Myklebustb134fc42016-09-22 13:39:16 -04002540 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002541 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2542 if (nfs4_have_delegation(state->inode, state->state))
2543 return NFS_OK;
2544 return -NFS4ERR_OPENMODE;
2545 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002546 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002547 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002548 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002549 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002550 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002551 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2552 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2553 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002554 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002555 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002556 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002557 if (status != NFS_OK)
2558 return status;
2559 if (nfs_open_stateid_recover_openmode(state))
2560 return -NFS4ERR_OPENMODE;
2561 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002562}
2563
2564static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2565{
Chuck Levereb64cf92012-07-11 16:30:05 -04002566 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002567
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002568 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002569 status = nfs41_check_expired_locks(state);
2570 if (status != NFS_OK)
2571 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002572 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002573 if (status != NFS_OK)
2574 status = nfs4_open_expired(sp, state);
2575 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002576}
2577#endif
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002580 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2581 * fields corresponding to attributes that were used to store the verifier.
2582 * Make sure we clobber those fields in the later setattr call
2583 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002584static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2585 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002586{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002587 const u32 *attrset = opendata->o_res.attrset;
2588
2589 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002590 !(sattr->ia_valid & ATTR_ATIME_SET))
2591 sattr->ia_valid |= ATTR_ATIME;
2592
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002593 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002594 !(sattr->ia_valid & ATTR_MTIME_SET))
2595 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002596
2597 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002598 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2599 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002600 sattr->ia_valid &= ~ATTR_MODE;
2601
2602 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2603 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002604}
2605
Trond Myklebustc21443c2013-02-07 14:26:21 -05002606static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2607 fmode_t fmode,
2608 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002609 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002610{
2611 struct nfs4_state_owner *sp = opendata->owner;
2612 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002613 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002614 struct nfs4_state *state;
2615 unsigned int seq;
2616 int ret;
2617
2618 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2619
2620 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002621 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002622 goto out;
2623
2624 state = nfs4_opendata_to_nfs4_state(opendata);
2625 ret = PTR_ERR(state);
2626 if (IS_ERR(state))
2627 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002628 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002629 if (server->caps & NFS_CAP_POSIX_LOCK)
2630 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002631 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2632 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002633
Trond Myklebust275bb302013-05-29 13:11:28 -04002634 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002635 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002636 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002637 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002638 alias = d_exact_alias(dentry, state->inode);
2639 if (!alias)
2640 alias = d_splice_alias(igrab(state->inode), dentry);
2641 /* d_splice_alias() can't fail here - it's a non-directory */
2642 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002643 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002644 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002645 }
2646 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002647 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002648 }
2649
Trond Myklebustc21443c2013-02-07 14:26:21 -05002650 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2651 if (ret != 0)
2652 goto out;
2653
David Howells2b0143b2015-03-17 22:25:59 +00002654 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002655 nfs_inode_attach_open_context(ctx);
2656 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2657 nfs4_schedule_stateid_recovery(server, state);
2658 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002659out:
2660 return ret;
2661}
2662
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002663/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002664 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 */
Andy Adamson82be4172012-05-23 05:02:35 -04002666static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002667 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002668 int flags,
2669 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002670 struct nfs4_label *label,
2671 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 struct nfs4_state_owner *sp;
2674 struct nfs4_state *state = NULL;
2675 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002676 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002677 struct dentry *dentry = ctx->dentry;
2678 struct rpc_cred *cred = ctx->cred;
2679 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2680 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002681 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002682 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002683 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002687 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2688 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2690 goto out_err;
2691 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002692 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002693 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002694 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002695 if (d_really_is_positive(dentry))
2696 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002697 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002698 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002699 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002700 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002701 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002702 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002703 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
David Quigley14c43f72013-05-22 12:50:43 -04002705 if (label) {
2706 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2707 if (IS_ERR(olabel)) {
2708 status = PTR_ERR(olabel);
2709 goto err_opendata_put;
2710 }
2711 }
2712
Trond Myklebuste911b812014-03-26 13:24:37 -07002713 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2714 if (!opendata->f_attr.mdsthreshold) {
2715 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2716 if (!opendata->f_attr.mdsthreshold)
2717 goto err_free_label;
2718 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002719 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002720 }
David Howells2b0143b2015-03-17 22:25:59 +00002721 if (d_really_is_positive(dentry))
2722 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002723
Trond Myklebust3efb9722013-05-29 13:17:04 -04002724 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002725 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002726 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002727 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002728
NeilBrownefcbc042015-07-30 13:00:56 +10002729 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002730 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002731 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002732 /*
2733 * send create attributes which was not set by open
2734 * with an extra setattr.
2735 */
2736 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2737 nfs_fattr_init(opendata->o_res.f_attr);
2738 status = nfs4_do_setattr(state->inode, cred,
2739 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002740 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002741 if (status == 0) {
2742 nfs_setattr_update_inode(state->inode, sattr,
2743 opendata->o_res.f_attr);
2744 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2745 }
David Quigley1775fd32013-05-22 12:50:42 -04002746 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002747 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002748 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002749 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002750
Trond Myklebuste911b812014-03-26 13:24:37 -07002751 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002752 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002753 opendata->f_attr.mdsthreshold = NULL;
2754 }
Andy Adamson82be4172012-05-23 05:02:35 -04002755
David Quigley14c43f72013-05-22 12:50:43 -04002756 nfs4_label_free(olabel);
2757
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002758 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002761err_free_label:
2762 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002763err_opendata_put:
2764 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002765err_put_state_owner:
2766 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return status;
2769}
2770
2771
Andy Adamson82be4172012-05-23 05:02:35 -04002772static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002773 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002774 int flags,
2775 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002776 struct nfs4_label *label,
2777 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002779 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct nfs4_exception exception = { };
2781 struct nfs4_state *res;
2782 int status;
2783
2784 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002785 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002786 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002787 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (status == 0)
2789 break;
2790 /* NOTE: BAD_SEQID means the server and client disagree about the
2791 * book-keeping w.r.t. state-changing operations
2792 * (OPEN/CLOSE/LOCK/LOCKU...)
2793 * It is actually a sign of a bug on the client or on the server.
2794 *
2795 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002796 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 * have unhashed the old state_owner for us, and that we can
2798 * therefore safely retry using a new one. We should still warn
2799 * the user though...
2800 */
2801 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002802 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002803 " returned a bad sequence-id error!\n",
2804 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 exception.retry = 1;
2806 continue;
2807 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002808 /*
2809 * BAD_STATEID on OPEN means that the server cancelled our
2810 * state before it received the OPEN_CONFIRM.
2811 * Recover by retrying the request as per the discussion
2812 * on Page 181 of RFC3530.
2813 */
2814 if (status == -NFS4ERR_BAD_STATEID) {
2815 exception.retry = 1;
2816 continue;
2817 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002818 if (status == -EAGAIN) {
2819 /* We must have found a delegation */
2820 exception.retry = 1;
2821 continue;
2822 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002823 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2824 continue;
2825 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 status, &exception));
2827 } while (exception.retry);
2828 return res;
2829}
2830
Trond Myklebust8487c472016-06-26 08:44:35 -04002831static int _nfs4_do_setattr(struct inode *inode,
2832 struct nfs_setattrargs *arg,
2833 struct nfs_setattrres *res,
2834 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002835 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002836{
2837 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002838 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2840 .rpc_argp = arg,
2841 .rpc_resp = res,
2842 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002843 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002844 struct rpc_cred *delegation_cred = NULL;
2845 unsigned long timestamp = jiffies;
2846 fmode_t fmode;
2847 bool truncate;
2848 int status;
2849
2850 nfs_fattr_init(res->fattr);
2851
2852 /* Servers should only apply open mode checks for file size changes */
2853 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2854 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2855
2856 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2857 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002858 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002859 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002860 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002861 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002862 l_ctx = nfs_get_lock_context(ctx);
2863 if (IS_ERR(l_ctx))
2864 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002865 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2866 &arg->stateid, &delegation_cred);
2867 nfs_put_lock_context(l_ctx);
2868 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002869 return -EBADF;
2870 } else
2871 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2872 if (delegation_cred)
2873 msg.rpc_cred = delegation_cred;
2874
2875 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2876
2877 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002878 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002879 renew_lease(server, timestamp);
2880 trace_nfs4_setattr(inode, &arg->stateid, status);
2881 return status;
2882}
2883
2884static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2885 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002886 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002887 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002889 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002890 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002891 struct nfs_setattrargs arg = {
2892 .fh = NFS_FH(inode),
2893 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .server = server,
2895 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002896 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002897 };
2898 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002900 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002902 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002903 struct nfs4_exception exception = {
2904 .state = state,
2905 .inode = inode,
2906 .stateid = &arg.stateid,
2907 };
2908 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
David Quigleyaa9c2662013-05-22 12:50:44 -04002910 arg.bitmask = nfs4_bitmask(server, ilabel);
2911 if (ilabel)
2912 arg.bitmask = nfs4_bitmask(server, olabel);
2913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002915 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002916 switch (err) {
2917 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002918 if (!(sattr->ia_valid & ATTR_SIZE)) {
2919 pr_warn_once("NFSv4: server %s is incorrectly "
2920 "applying open mode checks to "
2921 "a SETATTR that is not "
2922 "changing file size.\n",
2923 server->nfs_client->cl_hostname);
2924 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002925 if (state && !(state->state & FMODE_WRITE)) {
2926 err = -EBADF;
2927 if (sattr->ia_valid & ATTR_OPEN)
2928 err = -EACCES;
2929 goto out;
2930 }
2931 }
2932 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002934out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return err;
2936}
2937
Peng Tao500d7012015-09-22 11:35:22 +08002938static bool
2939nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2940{
2941 if (inode == NULL || !nfs_have_layout(inode))
2942 return false;
2943
2944 return pnfs_wait_on_layoutreturn(inode, task);
2945}
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947struct nfs4_closedata {
2948 struct inode *inode;
2949 struct nfs4_state *state;
2950 struct nfs_closeargs arg;
2951 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002952 struct {
2953 struct nfs4_layoutreturn_args arg;
2954 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002955 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002956 u32 roc_barrier;
2957 bool roc;
2958 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002959 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002960 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961};
2962
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002963static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002964{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002965 struct nfs4_closedata *calldata = data;
2966 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002967 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002968
Trond Myklebustcf805162016-11-15 14:56:07 -05002969 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002970 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2971 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002972 nfs4_put_open_state(calldata->state);
2973 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002974 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002975 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002976 kfree(calldata);
2977}
2978
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002979static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002981 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002984 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Chuck Levera3ca5652012-03-01 17:00:40 -05002986 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002987 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2988 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002989 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002990
2991 /* Handle Layoutreturn errors */
2992 if (calldata->arg.lr_args && task->tk_status != 0) {
2993 switch (calldata->res.lr_ret) {
2994 default:
2995 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2996 break;
2997 case 0:
2998 calldata->arg.lr_args = NULL;
2999 calldata->res.lr_res = NULL;
3000 break;
3001 case -NFS4ERR_ADMIN_REVOKED:
3002 case -NFS4ERR_DELEG_REVOKED:
3003 case -NFS4ERR_EXPIRED:
3004 case -NFS4ERR_BAD_STATEID:
3005 case -NFS4ERR_OLD_STATEID:
3006 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3007 case -NFS4ERR_WRONG_CRED:
3008 calldata->arg.lr_args = NULL;
3009 calldata->res.lr_res = NULL;
3010 calldata->res.lr_ret = 0;
3011 rpc_restart_call_prepare(task);
3012 return;
3013 }
3014 }
3015
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003016 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 * the state_owner. we keep this around to process errors
3018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 switch (task->tk_status) {
3020 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003021 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003022 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003023 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003024 case -NFS4ERR_ACCESS:
3025 if (calldata->arg.bitmask != NULL) {
3026 calldata->arg.bitmask = NULL;
3027 calldata->res.fattr = NULL;
3028 task->tk_status = 0;
3029 rpc_restart_call_prepare(task);
3030 goto out_release;
3031
3032 }
3033 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003034 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003036 case -NFS4ERR_EXPIRED:
3037 nfs4_free_revoked_stateid(server,
3038 &calldata->arg.stateid,
3039 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003040 case -NFS4ERR_OLD_STATEID:
3041 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003042 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003043 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003044 rpc_restart_call_prepare(task);
3045 goto out_release;
3046 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003047 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003050 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003051 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003052 goto out_release;
3053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003055 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3056 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003057out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003058 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003059 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003060 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003063static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003065 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003066 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003067 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003068 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003069 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003070
Chuck Levera3ca5652012-03-01 17:00:40 -05003071 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003072 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003073 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003074
Trond Myklebust88069f72009-12-08 08:33:16 -05003075 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003076 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003077 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3078 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3079 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003080 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003081 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003082 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003083 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003084 if (state->n_rdonly == 0)
3085 call_close |= is_rdonly;
3086 else if (is_rdonly)
3087 calldata->arg.fmode |= FMODE_READ;
3088 if (state->n_wronly == 0)
3089 call_close |= is_wronly;
3090 else if (is_wronly)
3091 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003092 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3093 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003094 } else if (is_rdwr)
3095 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3096
Trond Myklebust5cc78612016-11-14 11:19:56 -05003097 if (!nfs4_valid_open_stateid(state) ||
3098 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003099 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003100 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003101
3102 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003103 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003104 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003105 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003106
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003107 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003108 nfs_release_seqid(calldata->arg.seqid);
3109 goto out_wait;
3110 }
3111
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003112 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003113 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003114
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003115 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003116 /* Close-to-open cache consistency revalidation */
3117 if (!nfs4_have_delegation(inode, FMODE_READ))
3118 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3119 else
3120 calldata->arg.bitmask = NULL;
3121 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003122
Trond Myklebust6ae37332015-01-30 14:21:14 -05003123 calldata->arg.share_access =
3124 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3125 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003126
Trond Myklebustd8d84982016-12-19 12:14:44 -05003127 if (calldata->res.fattr == NULL)
3128 calldata->arg.bitmask = NULL;
3129 else if (calldata->arg.bitmask == NULL)
3130 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003131 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003132 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003133 &calldata->arg.seq_args,
3134 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003135 task) != 0)
3136 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003137 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003138 return;
3139out_no_action:
3140 task->tk_action = NULL;
3141out_wait:
3142 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003145static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003146 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147 .rpc_call_done = nfs4_close_done,
3148 .rpc_release = nfs4_free_closedata,
3149};
3150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151/*
3152 * It is possible for data to be read/written from a mem-mapped file
3153 * after the sys_close call (which hits the vfs layer as a flush).
3154 * This means that we can't safely call nfsv4 close on a file until
3155 * the inode is cleared. This in turn means that we are not good
3156 * NFSv4 citizens - we do not indicate to the server to update the file's
3157 * share state even when we are done with one of the three share
3158 * stateid's in the inode.
3159 *
3160 * NOTE: Caller must be holding the sp->so_owner semaphore!
3161 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003162int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003164 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003165 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003167 struct nfs4_state_owner *sp = state->owner;
3168 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3171 .rpc_cred = state->owner->so_cred,
3172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003173 struct rpc_task_setup task_setup_data = {
3174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003176 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003177 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003178 .flags = RPC_TASK_ASYNC,
3179 };
Trond Myklebust95121352005-10-18 14:20:12 -07003180 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003182 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3183 &task_setup_data.rpc_client, &msg);
3184
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003185 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003187 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003188 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003189 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003191 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003193 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3194 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003195 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003196 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003197 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003198 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003199 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003200 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003201 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003202 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003203 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003204 calldata->lr.roc = pnfs_roc(state->inode,
3205 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3206 if (calldata->lr.roc) {
3207 calldata->arg.lr_args = &calldata->lr.arg;
3208 calldata->res.lr_res = &calldata->lr.res;
3209 }
Al Viro643168c2011-06-22 18:20:23 -04003210 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003211
Trond Myklebust1174dd12010-12-21 10:52:24 -05003212 msg.rpc_argp = &calldata->arg;
3213 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003214 task_setup_data.callback_data = calldata;
3215 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003216 if (IS_ERR(task))
3217 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003218 status = 0;
3219 if (wait)
3220 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003221 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003222 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003223out_free_calldata:
3224 kfree(calldata);
3225out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003226 nfs4_put_open_state(state);
3227 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003228 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Trond Myklebust2b484292010-09-17 10:56:51 -04003231static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003232nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3233 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003236 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3237
3238 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Trond Myklebust565277f2007-10-15 18:17:53 -04003240 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003241 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003242
3243 nfs4_label_release_security(label);
3244
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003245 if (IS_ERR(state))
3246 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003247 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Trond Myklebust1185a552009-12-03 15:54:02 -05003250static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003251{
3252 if (ctx->state == NULL)
3253 return;
3254 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003255 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003256 else
Al Viro643168c2011-06-22 18:20:23 -04003257 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Trond Myklebustb944dba2013-11-04 15:20:20 -05003260#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3261#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003262#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3265{
Kinglong Mee8c612822015-08-26 21:12:58 +08003266 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003267 struct nfs4_server_caps_arg args = {
3268 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003269 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 struct nfs4_server_caps_res res = {};
3272 struct rpc_message msg = {
3273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003274 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 .rpc_resp = &res,
3276 };
3277 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003278 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Kinglong Mee8c612822015-08-26 21:12:58 +08003280 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3281 FATTR4_WORD0_FH_EXPIRE_TYPE |
3282 FATTR4_WORD0_LINK_SUPPORT |
3283 FATTR4_WORD0_SYMLINK_SUPPORT |
3284 FATTR4_WORD0_ACLSUPPORT;
3285 if (minorversion)
3286 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3287
Bryan Schumaker7c513052011-03-24 17:12:24 +00003288 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003290 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003291 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003292 case 0:
3293 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3294 res.attr_bitmask[2] = 0;
3295 break;
3296 case 1:
3297 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3298 break;
3299 case 2:
3300 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003303 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3304 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3305 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3306 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003307 NFS_CAP_CTIME|NFS_CAP_MTIME|
3308 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003309 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3310 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 server->caps |= NFS_CAP_ACLS;
3312 if (res.has_links != 0)
3313 server->caps |= NFS_CAP_HARDLINKS;
3314 if (res.has_symlinks != 0)
3315 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003316 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3317 server->caps |= NFS_CAP_FILEID;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3319 server->caps |= NFS_CAP_MODE;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3321 server->caps |= NFS_CAP_NLINK;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3323 server->caps |= NFS_CAP_OWNER;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3325 server->caps |= NFS_CAP_OWNER_GROUP;
3326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3327 server->caps |= NFS_CAP_ATIME;
3328 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3329 server->caps |= NFS_CAP_CTIME;
3330 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3331 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003332#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3333 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3334 server->caps |= NFS_CAP_SECURITY_LABEL;
3335#endif
3336 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3337 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003338 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003339
Trond Myklebusta65318b2009-03-11 14:10:28 -04003340 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3341 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3342 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003343 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003344
3345 /* Avoid a regression due to buggy server */
3346 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3347 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003348 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3349 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003352 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return status;
3356}
3357
Trond Myklebust55a97592006-06-09 09:34:19 -04003358int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 struct nfs4_exception exception = { };
3361 int err;
3362 do {
3363 err = nfs4_handle_exception(server,
3364 _nfs4_server_capabilities(server, fhandle),
3365 &exception);
3366 } while (exception.retry);
3367 return err;
3368}
3369
3370static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3371 struct nfs_fsinfo *info)
3372{
David Quigleyaa9c2662013-05-22 12:50:44 -04003373 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003375 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 };
3377 struct nfs4_lookup_res res = {
3378 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003379 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 .fh = fhandle,
3381 };
3382 struct rpc_message msg = {
3383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3384 .rpc_argp = &args,
3385 .rpc_resp = &res,
3386 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003387
David Quigleyaa9c2662013-05-22 12:50:44 -04003388 bitmask[0] = nfs4_fattr_bitmap[0];
3389 bitmask[1] = nfs4_fattr_bitmap[1];
3390 /*
3391 * Process the label in the upcoming getfattr
3392 */
3393 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3394
Trond Myklebust0e574af2005-10-27 22:12:38 -04003395 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_fsinfo *info)
3401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003405 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003406 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003407 switch (err) {
3408 case 0:
3409 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003410 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003411 default:
3412 err = nfs4_handle_exception(server, err, &exception);
3413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003415out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return err;
3417}
3418
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003419static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3420 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3421{
Trond Myklebustc2190662013-08-26 19:23:04 -04003422 struct rpc_auth_create_args auth_args = {
3423 .pseudoflavor = flavor,
3424 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003425 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003426
Trond Myklebustc2190662013-08-26 19:23:04 -04003427 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003428 if (IS_ERR(auth))
3429 return -EACCES;
3430 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003431}
3432
Chuck Lever9a744ba2013-03-16 15:56:02 -04003433/*
3434 * Retry pseudoroot lookup with various security flavors. We do this when:
3435 *
3436 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3437 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3438 *
3439 * Returns zero on success, or a negative NFS4ERR value, or a
3440 * negative errno value.
3441 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003442static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003443 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003445 /* Per 3530bis 15.33.5 */
3446 static const rpc_authflavor_t flav_array[] = {
3447 RPC_AUTH_GSS_KRB5P,
3448 RPC_AUTH_GSS_KRB5I,
3449 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003450 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003451 RPC_AUTH_NULL,
3452 };
3453 int status = -EPERM;
3454 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003456 if (server->auth_info.flavor_len > 0) {
3457 /* try each flavor specified by user */
3458 for (i = 0; i < server->auth_info.flavor_len; i++) {
3459 status = nfs4_lookup_root_sec(server, fhandle, info,
3460 server->auth_info.flavors[i]);
3461 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3462 continue;
3463 break;
3464 }
3465 } else {
3466 /* no flavors specified by user, try default list */
3467 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3468 status = nfs4_lookup_root_sec(server, fhandle, info,
3469 flav_array[i]);
3470 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3471 continue;
3472 break;
3473 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003474 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003475
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003476 /*
3477 * -EACCESS could mean that the user doesn't have correct permissions
3478 * to access the mount. It could also mean that we tried to mount
3479 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3480 * existing mount programs don't handle -EACCES very well so it should
3481 * be mapped to -EPERM instead.
3482 */
3483 if (status == -EACCES)
3484 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003485 return status;
3486}
3487
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003488/**
3489 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3490 * @server: initialized nfs_server handle
3491 * @fhandle: we fill in the pseudo-fs root file handle
3492 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003493 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003494 *
3495 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003496 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003497int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003498 struct nfs_fsinfo *info,
3499 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003500{
Andre Przywarac7757072015-04-23 17:17:40 +01003501 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003502
Andre Przywarac7757072015-04-23 17:17:40 +01003503 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003504 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003505
3506 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003507 status = server->nfs_client->cl_mvops->find_root_sec(server,
3508 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 if (status == 0)
3511 status = nfs4_server_capabilities(server, fhandle);
3512 if (status == 0)
3513 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003514
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003515 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
Bryan Schumakerbae36242012-05-10 15:07:31 -04003518static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3519 struct nfs_fsinfo *info)
3520{
3521 int error;
3522 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003523 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003524
3525 error = nfs4_server_capabilities(server, mntfh);
3526 if (error < 0) {
3527 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3528 return error;
3529 }
3530
David Quigley14c43f72013-05-22 12:50:43 -04003531 label = nfs4_label_alloc(server, GFP_KERNEL);
3532 if (IS_ERR(label))
3533 return PTR_ERR(label);
3534
David Quigley1775fd32013-05-22 12:50:42 -04003535 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003536 if (error < 0) {
3537 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003538 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003539 }
3540
3541 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3542 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3543 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3544
David Quigley14c43f72013-05-22 12:50:43 -04003545err_free_label:
3546 nfs4_label_free(label);
3547
Bryan Schumakerbae36242012-05-10 15:07:31 -04003548 return error;
3549}
3550
Manoj Naik6b97fd32006-06-09 09:34:29 -04003551/*
3552 * Get locations and (maybe) other attributes of a referral.
3553 * Note that we'll actually follow the referral later when
3554 * we detect fsid mismatch in inode revalidation
3555 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003556static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3557 const struct qstr *name, struct nfs_fattr *fattr,
3558 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003559{
3560 int status = -ENOMEM;
3561 struct page *page = NULL;
3562 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003563
3564 page = alloc_page(GFP_KERNEL);
3565 if (page == NULL)
3566 goto out;
3567 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3568 if (locations == NULL)
3569 goto out;
3570
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003571 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003572 if (status != 0)
3573 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003574
3575 /*
3576 * If the fsid didn't change, this is a migration event, not a
3577 * referral. Cause us to drop into the exception handler, which
3578 * will kick off migration recovery.
3579 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003581 dprintk("%s: server did not return a different fsid for"
3582 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003583 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003584 goto out;
3585 }
Andy Adamson533eb462011-06-13 18:25:56 -04003586 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3587 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003588
Andy Adamson533eb462011-06-13 18:25:56 -04003589 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003590 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003591 memset(fhandle, 0, sizeof(struct nfs_fh));
3592out:
3593 if (page)
3594 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003595 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003596 return status;
3597}
3598
David Quigley1775fd32013-05-22 12:50:42 -04003599static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3600 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
3602 struct nfs4_getattr_arg args = {
3603 .fh = fhandle,
3604 .bitmask = server->attr_bitmask,
3605 };
3606 struct nfs4_getattr_res res = {
3607 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003608 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 .server = server,
3610 };
3611 struct rpc_message msg = {
3612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3613 .rpc_argp = &args,
3614 .rpc_resp = &res,
3615 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003616
3617 args.bitmask = nfs4_bitmask(server, label);
3618
Trond Myklebust0e574af2005-10-27 22:12:38 -04003619 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003620 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
David Quigley1775fd32013-05-22 12:50:42 -04003623static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3624 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 struct nfs4_exception exception = { };
3627 int err;
3628 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003629 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3630 trace_nfs4_getattr(server, fhandle, fattr, err);
3631 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 &exception);
3633 } while (exception.retry);
3634 return err;
3635}
3636
3637/*
3638 * The file is not closed if it is opened due to the a request to change
3639 * the size of the file. The open call will not be needed once the
3640 * VFS layer lookup-intents are implemented.
3641 *
3642 * Close is called when the inode is destroyed.
3643 * If we haven't opened the file for O_WRONLY, we
3644 * need to in the size_change case to obtain a stateid.
3645 *
3646 * Got race?
3647 * Because OPEN is always done by name in nfsv4, it is
3648 * possible that we opened a different file by the same
3649 * name. We can recognize this race condition, but we
3650 * can't do anything about it besides returning an error.
3651 *
3652 * This will be fixed with VFS changes (lookup-intent).
3653 */
3654static int
3655nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3656 struct iattr *sattr)
3657{
David Howells2b0143b2015-03-17 22:25:59 +00003658 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003659 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003660 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003661 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 int status;
3663
Peng Tao88ac8152014-09-12 11:04:10 +08003664 if (pnfs_ld_layoutret_on_setattr(inode) &&
3665 sattr->ia_valid & ATTR_SIZE &&
3666 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003667 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003668
Trond Myklebust0e574af2005-10-27 22:12:38 -04003669 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Andy Adamson26699402012-05-30 16:12:24 -04003671 /* Deal with open(O_TRUNC) */
3672 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003673 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003674
3675 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003676 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003677 return 0;
3678
Trond Myklebustd5308382005-11-04 15:33:38 -05003679 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003680 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003681
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003682 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003683 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003684 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003685 }
3686
David Quigley14c43f72013-05-22 12:50:43 -04003687 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3688 if (IS_ERR(label))
3689 return PTR_ERR(label);
3690
NeilBrown29b59f92016-10-13 15:26:47 +11003691 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003692 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003693 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003694 nfs_setsecurity(inode, fattr, label);
3695 }
David Quigley14c43f72013-05-22 12:50:43 -04003696 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return status;
3698}
3699
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003700static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3701 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003702 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003703{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003704 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003705 int status;
3706 struct nfs4_lookup_arg args = {
3707 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003708 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003709 .name = name,
3710 };
3711 struct nfs4_lookup_res res = {
3712 .server = server,
3713 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003714 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003715 .fh = fhandle,
3716 };
3717 struct rpc_message msg = {
3718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3719 .rpc_argp = &args,
3720 .rpc_resp = &res,
3721 };
3722
David Quigleyaa9c2662013-05-22 12:50:44 -04003723 args.bitmask = nfs4_bitmask(server, label);
3724
David Howells2b3de442006-08-22 20:06:09 -04003725 nfs_fattr_init(fattr);
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003728 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 dprintk("NFS reply lookup: %d\n", status);
3730 return status;
3731}
3732
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003733static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003734{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003735 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003736 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003737 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3738 fattr->nlink = 2;
3739}
3740
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003741static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003742 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003743 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003744{
3745 struct nfs4_exception exception = { };
3746 struct rpc_clnt *client = *clnt;
3747 int err;
3748 do {
David Quigley1775fd32013-05-22 12:50:42 -04003749 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003750 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003751 switch (err) {
3752 case -NFS4ERR_BADNAME:
3753 err = -ENOENT;
3754 goto out;
3755 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003756 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003757 if (err == -NFS4ERR_MOVED)
3758 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003759 goto out;
3760 case -NFS4ERR_WRONGSEC:
3761 err = -EPERM;
3762 if (client != *clnt)
3763 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003764 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003765 if (IS_ERR(client))
3766 return PTR_ERR(client);
3767
3768 exception.retry = 1;
3769 break;
3770 default:
3771 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3772 }
3773 } while (exception.retry);
3774
3775out:
3776 if (err == 0)
3777 *clnt = client;
3778 else if (client != *clnt)
3779 rpc_shutdown_client(client);
3780
3781 return err;
3782}
3783
Al Virobeffb8f2016-07-20 16:34:42 -04003784static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003785 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3786 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003788 int status;
3789 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003790
David Quigley1775fd32013-05-22 12:50:42 -04003791 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003792 if (client != NFS_CLIENT(dir)) {
3793 rpc_shutdown_client(client);
3794 nfs_fixup_secinfo_attributes(fattr);
3795 }
3796 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003799struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003800nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003801 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3802{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003803 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003804 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003805
David Quigley1775fd32013-05-22 12:50:42 -04003806 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003807 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003808 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003809 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003810}
3811
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003812static int _nfs4_proc_lookupp(struct inode *inode,
3813 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3814 struct nfs4_label *label)
3815{
3816 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3817 struct nfs_server *server = NFS_SERVER(inode);
3818 int status;
3819 struct nfs4_lookupp_arg args = {
3820 .bitmask = server->attr_bitmask,
3821 .fh = NFS_FH(inode),
3822 };
3823 struct nfs4_lookupp_res res = {
3824 .server = server,
3825 .fattr = fattr,
3826 .label = label,
3827 .fh = fhandle,
3828 };
3829 struct rpc_message msg = {
3830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3831 .rpc_argp = &args,
3832 .rpc_resp = &res,
3833 };
3834
3835 args.bitmask = nfs4_bitmask(server, label);
3836
3837 nfs_fattr_init(fattr);
3838
3839 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3840 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3841 &res.seq_res, 0);
3842 dprintk("NFS reply lookupp: %d\n", status);
3843 return status;
3844}
3845
3846static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3847 struct nfs_fattr *fattr, struct nfs4_label *label)
3848{
3849 struct nfs4_exception exception = { };
3850 int err;
3851 do {
3852 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3853 trace_nfs4_lookupp(inode, err);
3854 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3855 &exception);
3856 } while (exception.retry);
3857 return err;
3858}
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3861{
Trond Myklebust76b32992007-08-10 17:45:11 -04003862 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 struct nfs4_accessargs args = {
3864 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003865 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003867 struct nfs4_accessres res = {
3868 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 struct rpc_message msg = {
3871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3872 .rpc_argp = &args,
3873 .rpc_resp = &res,
3874 .rpc_cred = entry->cred,
3875 };
3876 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003877 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 /*
3880 * Determine which access bits we want to ask for...
3881 */
3882 if (mode & MAY_READ)
3883 args.access |= NFS4_ACCESS_READ;
3884 if (S_ISDIR(inode->i_mode)) {
3885 if (mode & MAY_WRITE)
3886 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3887 if (mode & MAY_EXEC)
3888 args.access |= NFS4_ACCESS_LOOKUP;
3889 } else {
3890 if (mode & MAY_WRITE)
3891 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3892 if (mode & MAY_EXEC)
3893 args.access |= NFS4_ACCESS_EXECUTE;
3894 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003895
3896 res.fattr = nfs_alloc_fattr();
3897 if (res.fattr == NULL)
3898 return -ENOMEM;
3899
Bryan Schumaker7c513052011-03-24 17:12:24 +00003900 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003902 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003903 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003905 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 return status;
3907}
3908
3909static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3910{
3911 struct nfs4_exception exception = { };
3912 int err;
3913 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003914 err = _nfs4_proc_access(inode, entry);
3915 trace_nfs4_access(inode, err);
3916 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 &exception);
3918 } while (exception.retry);
3919 return err;
3920}
3921
3922/*
3923 * TODO: For the time being, we don't try to get any attributes
3924 * along with any of the zero-copy operations READ, READDIR,
3925 * READLINK, WRITE.
3926 *
3927 * In the case of the first three, we want to put the GETATTR
3928 * after the read-type operation -- this is because it is hard
3929 * to predict the length of a GETATTR response in v4, and thus
3930 * align the READ data correctly. This means that the GETATTR
3931 * may end up partially falling into the page cache, and we should
3932 * shift it into the 'tail' of the xdr_buf before processing.
3933 * To do this efficiently, we need to know the total length
3934 * of data received, which doesn't seem to be available outside
3935 * of the RPC layer.
3936 *
3937 * In the case of WRITE, we also want to put the GETATTR after
3938 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003939 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 *
3941 * Both of these changes to the XDR layer would in fact be quite
3942 * minor, but I decided to leave them for a subsequent patch.
3943 */
3944static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3945 unsigned int pgbase, unsigned int pglen)
3946{
3947 struct nfs4_readlink args = {
3948 .fh = NFS_FH(inode),
3949 .pgbase = pgbase,
3950 .pglen = pglen,
3951 .pages = &page,
3952 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003953 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 struct rpc_message msg = {
3955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3956 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003957 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 };
3959
Bryan Schumaker7c513052011-03-24 17:12:24 +00003960 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 -07003961}
3962
3963static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3964 unsigned int pgbase, unsigned int pglen)
3965{
3966 struct nfs4_exception exception = { };
3967 int err;
3968 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003969 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3970 trace_nfs4_readlink(inode, err);
3971 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 &exception);
3973 } while (exception.retry);
3974 return err;
3975}
3976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003978 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980static int
3981nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003982 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003984 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003985 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003986 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 int status = 0;
3989
NeilBrown532d4de2016-10-13 15:26:47 +11003990 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003991 if (IS_ERR(ctx))
3992 return PTR_ERR(ctx);
3993
David Quigleyaa9c2662013-05-22 12:50:44 -04003994 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3995
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003996 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3997 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003998 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (IS_ERR(state)) {
4000 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004004 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004005 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return status;
4007}
4008
Al Virobeffb8f2016-07-20 16:34:42 -04004009static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Trond Myklebust16e42952005-10-27 22:12:44 -04004011 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004012 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004014 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004016 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004017 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4021 .rpc_argp = &args,
4022 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004024 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004025 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004026
Bryan Schumaker7c513052011-03-24 17:12:24 +00004027 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004028 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004029 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return status;
4031}
4032
Al Virobeffb8f2016-07-20 16:34:42 -04004033static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 struct nfs4_exception exception = { };
4036 int err;
4037 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004038 err = _nfs4_proc_remove(dir, name);
4039 trace_nfs4_remove(dir, name, err);
4040 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 &exception);
4042 } while (exception.retry);
4043 return err;
4044}
4045
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004046static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004048 struct nfs_server *server = NFS_SERVER(dir);
4049 struct nfs_removeargs *args = msg->rpc_argp;
4050 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004052 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004054 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004055
4056 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004059static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4060{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004061 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004062 &data->args.seq_args,
4063 &data->res.seq_res,
4064 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004067static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004069 struct nfs_unlinkdata *data = task->tk_calldata;
4070 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004071
Trond Myklebust14516c32010-07-31 14:29:06 -04004072 if (!nfs4_sequence_done(task, &res->seq_res))
4073 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004074 if (nfs4_async_handle_error(task, res->server, NULL,
4075 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004076 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004077 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004078 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004079 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080}
4081
Jeff Laytond3d41522010-09-17 17:31:57 -04004082static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4083{
4084 struct nfs_server *server = NFS_SERVER(dir);
4085 struct nfs_renameargs *arg = msg->rpc_argp;
4086 struct nfs_renameres *res = msg->rpc_resp;
4087
4088 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004089 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004090 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004091}
4092
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004093static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4094{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004095 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004096 &data->args.seq_args,
4097 &data->res.seq_res,
4098 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004099}
4100
4101static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4102 struct inode *new_dir)
4103{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004104 struct nfs_renamedata *data = task->tk_calldata;
4105 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004106
4107 if (!nfs4_sequence_done(task, &res->seq_res))
4108 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004109 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004110 return 0;
4111
Trond Myklebustc733c492017-01-11 12:32:26 -05004112 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004113 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004114 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004115 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004116 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004117 return 1;
4118}
4119
Al Virobeffb8f2016-07-20 16:34:42 -04004120static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004122 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct nfs4_link_arg arg = {
4124 .fh = NFS_FH(inode),
4125 .dir_fh = NFS_FH(dir),
4126 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004127 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004129 struct nfs4_link_res res = {
4130 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004131 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004132 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 struct rpc_message msg = {
4134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4135 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004136 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004138 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Trond Myklebust136f2622010-04-16 16:22:49 -04004140 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004141 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004142 goto out;
4143
David Quigley14c43f72013-05-22 12:50:43 -04004144 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4145 if (IS_ERR(res.label)) {
4146 status = PTR_ERR(res.label);
4147 goto out;
4148 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004149 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004150
Bryan Schumaker7c513052011-03-24 17:12:24 +00004151 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004153 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004154 status = nfs_post_op_update_inode(inode, res.fattr);
4155 if (!status)
4156 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004157 }
David Quigley14c43f72013-05-22 12:50:43 -04004158
4159
4160 nfs4_label_free(res.label);
4161
Trond Myklebust136f2622010-04-16 16:22:49 -04004162out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004163 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 return status;
4165}
4166
Al Virobeffb8f2016-07-20 16:34:42 -04004167static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
4169 struct nfs4_exception exception = { };
4170 int err;
4171 do {
4172 err = nfs4_handle_exception(NFS_SERVER(inode),
4173 _nfs4_proc_link(inode, dir, name),
4174 &exception);
4175 } while (exception.retry);
4176 return err;
4177}
4178
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179struct nfs4_createdata {
4180 struct rpc_message msg;
4181 struct nfs4_create_arg arg;
4182 struct nfs4_create_res res;
4183 struct nfs_fh fh;
4184 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004185 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004186};
4187
4188static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004189 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004190{
4191 struct nfs4_createdata *data;
4192
4193 data = kzalloc(sizeof(*data), GFP_KERNEL);
4194 if (data != NULL) {
4195 struct nfs_server *server = NFS_SERVER(dir);
4196
David Quigley14c43f72013-05-22 12:50:43 -04004197 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4198 if (IS_ERR(data->label))
4199 goto out_free;
4200
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4202 data->msg.rpc_argp = &data->arg;
4203 data->msg.rpc_resp = &data->res;
4204 data->arg.dir_fh = NFS_FH(dir);
4205 data->arg.server = server;
4206 data->arg.name = name;
4207 data->arg.attrs = sattr;
4208 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004209 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004210 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004211 data->res.server = server;
4212 data->res.fh = &data->fh;
4213 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004214 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004215 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 }
4217 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004218out_free:
4219 kfree(data);
4220 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221}
4222
4223static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4224{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004225 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004226 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004228 update_changeattr(dir, &data->res.dir_cinfo,
4229 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004230 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004231 }
4232 return status;
4233}
4234
4235static void nfs4_free_createdata(struct nfs4_createdata *data)
4236{
David Quigley14c43f72013-05-22 12:50:43 -04004237 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004238 kfree(data);
4239}
4240
Chuck Lever4f390c12006-08-22 20:06:22 -04004241static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004242 struct page *page, unsigned int len, struct iattr *sattr,
4243 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004245 struct nfs4_createdata *data;
4246 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Chuck Lever94a6d752006-08-22 20:06:23 -04004248 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004250
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004251 status = -ENOMEM;
4252 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4253 if (data == NULL)
4254 goto out;
4255
4256 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4257 data->arg.u.symlink.pages = &page;
4258 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004259 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004261 status = nfs4_do_create(dir, dentry, data);
4262
4263 nfs4_free_createdata(data);
4264out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return status;
4266}
4267
Chuck Lever4f390c12006-08-22 20:06:22 -04004268static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004269 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004272 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004274
4275 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004278 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4279 trace_nfs4_symlink(dir, &dentry->d_name, err);
4280 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 &exception);
4282 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004283
4284 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return err;
4286}
4287
4288static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004289 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004291 struct nfs4_createdata *data;
4292 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004294 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4295 if (data == NULL)
4296 goto out;
4297
David Quigley1775fd32013-05-22 12:50:42 -04004298 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004299 status = nfs4_do_create(dir, dentry, data);
4300
4301 nfs4_free_createdata(data);
4302out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return status;
4304}
4305
4306static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4307 struct iattr *sattr)
4308{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004309 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004311 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004313
David Quigleyaa9c2662013-05-22 12:50:44 -04004314 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4315
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004316 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4317 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004319 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4320 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4321 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 &exception);
4323 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004324 nfs4_label_release_security(label);
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return err;
4327}
4328
4329static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004330 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
David Howells2b0143b2015-03-17 22:25:59 +00004332 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 struct nfs4_readdir_arg args = {
4334 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004335 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 .pgbase = 0,
4337 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004338 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004339 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 };
4341 struct nfs4_readdir_res res;
4342 struct rpc_message msg = {
4343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4344 .rpc_argp = &args,
4345 .rpc_resp = &res,
4346 .rpc_cred = cred,
4347 };
4348 int status;
4349
Al Viro6de14722013-09-16 10:53:17 -04004350 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4351 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004352 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004353 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004355 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 -05004356 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004357 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004358 status += args.pgbase;
4359 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004360
4361 nfs_invalidate_atime(dir);
4362
Harvey Harrison3110ff82008-05-02 13:42:44 -07004363 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 return status;
4365}
4366
4367static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004368 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
4370 struct nfs4_exception exception = { };
4371 int err;
4372 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004373 err = _nfs4_proc_readdir(dentry, cred, cookie,
4374 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004375 trace_nfs4_readdir(d_inode(dentry), err);
4376 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 &exception);
4378 } while (exception.retry);
4379 return err;
4380}
4381
4382static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004383 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004385 struct nfs4_createdata *data;
4386 int mode = sattr->ia_mode;
4387 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004389 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4390 if (data == NULL)
4391 goto out;
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004394 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 data->arg.ftype = NF4BLK;
4397 data->arg.u.device.specdata1 = MAJOR(rdev);
4398 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
4400 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 data->arg.ftype = NF4CHR;
4402 data->arg.u.device.specdata1 = MAJOR(rdev);
4403 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004404 } else if (!S_ISSOCK(mode)) {
4405 status = -EINVAL;
4406 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
David Quigley1775fd32013-05-22 12:50:42 -04004408
David Quigleyaa9c2662013-05-22 12:50:44 -04004409 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004410 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004411out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 nfs4_free_createdata(data);
4413out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return status;
4415}
4416
4417static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4418 struct iattr *sattr, dev_t rdev)
4419{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004420 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004422 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004424
David Quigleyaa9c2662013-05-22 12:50:44 -04004425 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4426
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004427 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4428 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004430 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4431 trace_nfs4_mknod(dir, &dentry->d_name, err);
4432 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 &exception);
4434 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004435
4436 nfs4_label_release_security(label);
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return err;
4439}
4440
4441static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4442 struct nfs_fsstat *fsstat)
4443{
4444 struct nfs4_statfs_arg args = {
4445 .fh = fhandle,
4446 .bitmask = server->attr_bitmask,
4447 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004448 struct nfs4_statfs_res res = {
4449 .fsstat = fsstat,
4450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4453 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004454 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 };
4456
Trond Myklebust0e574af2005-10-27 22:12:38 -04004457 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004458 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
4461static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4462{
4463 struct nfs4_exception exception = { };
4464 int err;
4465 do {
4466 err = nfs4_handle_exception(server,
4467 _nfs4_proc_statfs(server, fhandle, fsstat),
4468 &exception);
4469 } while (exception.retry);
4470 return err;
4471}
4472
4473static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4474 struct nfs_fsinfo *fsinfo)
4475{
4476 struct nfs4_fsinfo_arg args = {
4477 .fh = fhandle,
4478 .bitmask = server->attr_bitmask,
4479 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004480 struct nfs4_fsinfo_res res = {
4481 .fsinfo = fsinfo,
4482 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4485 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004486 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 };
4488
Bryan Schumaker7c513052011-03-24 17:12:24 +00004489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490}
4491
4492static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4493{
4494 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004495 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 int err;
4497
4498 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004499 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004500 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004501 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004502 nfs4_set_lease_period(server->nfs_client,
4503 fsinfo->lease_time * HZ,
4504 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004505 break;
4506 }
4507 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 } while (exception.retry);
4509 return err;
4510}
4511
4512static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4513{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004514 int error;
4515
Trond Myklebust0e574af2005-10-27 22:12:38 -04004516 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004517 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004518 if (error == 0) {
4519 /* block layout checks this! */
4520 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004521 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004522 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004523
4524 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525}
4526
4527static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4528 struct nfs_pathconf *pathconf)
4529{
4530 struct nfs4_pathconf_arg args = {
4531 .fh = fhandle,
4532 .bitmask = server->attr_bitmask,
4533 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004534 struct nfs4_pathconf_res res = {
4535 .pathconf = pathconf,
4536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 struct rpc_message msg = {
4538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4539 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004540 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 };
4542
4543 /* None of the pathconf attributes are mandatory to implement */
4544 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4545 memset(pathconf, 0, sizeof(*pathconf));
4546 return 0;
4547 }
4548
Trond Myklebust0e574af2005-10-27 22:12:38 -04004549 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004550 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
4553static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4554 struct nfs_pathconf *pathconf)
4555{
4556 struct nfs4_exception exception = { };
4557 int err;
4558
4559 do {
4560 err = nfs4_handle_exception(server,
4561 _nfs4_proc_pathconf(server, fhandle, pathconf),
4562 &exception);
4563 } while (exception.retry);
4564 return err;
4565}
4566
Trond Myklebust5521abf2013-03-16 20:54:34 -04004567int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004568 const struct nfs_open_context *ctx,
4569 const struct nfs_lock_context *l_ctx,
4570 fmode_t fmode)
4571{
NeilBrown17393472016-10-13 15:26:47 +11004572 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004573}
4574EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4575
Trond Myklebust5521abf2013-03-16 20:54:34 -04004576static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4577 const struct nfs_open_context *ctx,
4578 const struct nfs_lock_context *l_ctx,
4579 fmode_t fmode)
4580{
4581 nfs4_stateid current_stateid;
4582
Trond Myklebuste1253be2014-03-05 08:44:23 -05004583 /* If the current stateid represents a lost lock, then exit */
4584 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4585 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004586 return nfs4_stateid_match(stateid, &current_stateid);
4587}
4588
4589static bool nfs4_error_stateid_expired(int err)
4590{
4591 switch (err) {
4592 case -NFS4ERR_DELEG_REVOKED:
4593 case -NFS4ERR_ADMIN_REVOKED:
4594 case -NFS4ERR_BAD_STATEID:
4595 case -NFS4ERR_STALE_STATEID:
4596 case -NFS4ERR_OLD_STATEID:
4597 case -NFS4ERR_OPENMODE:
4598 case -NFS4ERR_EXPIRED:
4599 return true;
4600 }
4601 return false;
4602}
4603
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004604static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004606 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004609 if (task->tk_status < 0) {
4610 struct nfs4_exception exception = {
4611 .inode = hdr->inode,
4612 .state = hdr->args.context->state,
4613 .stateid = &hdr->args.stateid,
4614 };
4615 task->tk_status = nfs4_async_handle_exception(task,
4616 server, task->tk_status, &exception);
4617 if (exception.retry) {
4618 rpc_restart_call_prepare(task);
4619 return -EAGAIN;
4620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004624 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
Trond Myklebust5521abf2013-03-16 20:54:34 -04004628static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004629 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004630{
4631
4632 if (!nfs4_error_stateid_expired(task->tk_status) ||
4633 nfs4_stateid_is_current(&args->stateid,
4634 args->context,
4635 args->lock_context,
4636 FMODE_READ))
4637 return false;
4638 rpc_restart_call_prepare(task);
4639 return true;
4640}
4641
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004642static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004643{
4644
4645 dprintk("--> %s\n", __func__);
4646
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004647 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004648 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004650 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004651 if (task->tk_status > 0)
4652 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4654 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004655}
4656
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4658 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004661 if (!hdr->pgio_done_cb)
4662 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004663 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4668 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004669{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004670 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671 &hdr->args.seq_args,
4672 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004673 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004674 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4676 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004677 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004678 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004680 return -EIO;
4681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684static int nfs4_write_done_cb(struct rpc_task *task,
4685 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004688
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004690 if (task->tk_status < 0) {
4691 struct nfs4_exception exception = {
4692 .inode = hdr->inode,
4693 .state = hdr->args.context->state,
4694 .stateid = &hdr->args.stateid,
4695 };
4696 task->tk_status = nfs4_async_handle_exception(task,
4697 NFS_SERVER(inode), task->tk_status,
4698 &exception);
4699 if (exception.retry) {
4700 rpc_restart_call_prepare(task);
4701 return -EAGAIN;
4702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004704 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004706 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004707 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
Trond Myklebust5521abf2013-03-16 20:54:34 -04004711static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004712 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004713{
4714
4715 if (!nfs4_error_stateid_expired(task->tk_status) ||
4716 nfs4_stateid_is_current(&args->stateid,
4717 args->context,
4718 args->lock_context,
4719 FMODE_WRITE))
4720 return false;
4721 rpc_restart_call_prepare(task);
4722 return true;
4723}
4724
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004725static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004726{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004727 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004728 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004730 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4732 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004733}
4734
Trond Myklebust5a37f852012-04-28 14:55:16 -04004735static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004736bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004737{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004738 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004739 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004740 return false;
4741 /* Otherwise, request attributes if and only if we don't hold
4742 * a delegation
4743 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004744 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004745}
Fred Isamana69aef12011-03-03 15:13:47 +00004746
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004747static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4748 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004750 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004751
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004752 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4753 hdr->args.bitmask = NULL;
4754 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004755 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004756 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004757
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004758 if (!hdr->pgio_done_cb)
4759 hdr->pgio_done_cb = nfs4_write_done_cb;
4760 hdr->res.server = server;
4761 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004763 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
Fred Isaman0b7c0152012-04-20 14:47:39 -04004767static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4768{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004769 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004770 &data->args.seq_args,
4771 &data->res.seq_res,
4772 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773}
4774
Fred Isaman0b7c0152012-04-20 14:47:39 -04004775static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004778
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004779 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004780 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4781 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004782 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004783 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Fred Isaman0b7c0152012-04-20 14:47:39 -04004788static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004789{
4790 if (!nfs4_sequence_done(task, &data->res.seq_res))
4791 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004792 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004793}
4794
Fred Isaman0b7c0152012-04-20 14:47:39 -04004795static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004797 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004798
Fred Isaman0b7c0152012-04-20 14:47:39 -04004799 if (data->commit_done_cb == NULL)
4800 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004801 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004802 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004803 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804}
4805
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004806struct nfs4_renewdata {
4807 struct nfs_client *client;
4808 unsigned long timestamp;
4809};
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811/*
4812 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4813 * standalone procedure for queueing an asynchronous RENEW.
4814 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004815static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004816{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004817 struct nfs4_renewdata *data = calldata;
4818 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004819
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004820 if (atomic_read(&clp->cl_count) > 1)
4821 nfs4_schedule_state_renewal(clp);
4822 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004823 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004824}
4825
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004826static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004828 struct nfs4_renewdata *data = calldata;
4829 struct nfs_client *clp = data->client;
4830 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004832 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004833 switch (task->tk_status) {
4834 case 0:
4835 break;
4836 case -NFS4ERR_LEASE_MOVED:
4837 nfs4_schedule_lease_moved_recovery(clp);
4838 break;
4839 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004840 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004841 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4842 return;
4843 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004844 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004845 return;
4846 }
4847 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004849 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004852static const struct rpc_call_ops nfs4_renew_ops = {
4853 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004854 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004855};
4856
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004857static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
4859 struct rpc_message msg = {
4860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4861 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004862 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004864 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004866 if (renew_flags == 0)
4867 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004868 if (!atomic_inc_not_zero(&clp->cl_count))
4869 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004870 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004871 if (data == NULL) {
4872 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004873 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004874 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004875 data->client = clp;
4876 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004877 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004878 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
4880
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004881static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
4883 struct rpc_message msg = {
4884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4885 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004886 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 };
4888 unsigned long now = jiffies;
4889 int status;
4890
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004891 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 if (status < 0)
4893 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004894 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 return 0;
4896}
4897
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004898static inline int nfs4_server_supports_acls(struct nfs_server *server)
4899{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004900 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004901}
4902
Trond Myklebust21f498c2012-08-24 10:59:25 -04004903/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4904 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004905 * the stack.
4906 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004907#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004908
Neil Hormane9e3d722011-03-04 19:26:03 -05004909static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004910 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004911{
4912 struct page *newpage, **spages;
4913 int rc = 0;
4914 size_t len;
4915 spages = pages;
4916
4917 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004918 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004919 newpage = alloc_page(GFP_KERNEL);
4920
4921 if (newpage == NULL)
4922 goto unwind;
4923 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004924 buf += len;
4925 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004926 *pages++ = newpage;
4927 rc++;
4928 } while (buflen != 0);
4929
4930 return rc;
4931
4932unwind:
4933 for(; rc > 0; rc--)
4934 __free_page(spages[rc-1]);
4935 return -ENOMEM;
4936}
4937
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004938struct nfs4_cached_acl {
4939 int cached;
4940 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004941 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004942};
4943
4944static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004945{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004946 struct nfs_inode *nfsi = NFS_I(inode);
4947
4948 spin_lock(&inode->i_lock);
4949 kfree(nfsi->nfs4_acl);
4950 nfsi->nfs4_acl = acl;
4951 spin_unlock(&inode->i_lock);
4952}
4953
4954static void nfs4_zap_acl_attr(struct inode *inode)
4955{
4956 nfs4_set_cached_acl(inode, NULL);
4957}
4958
4959static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4960{
4961 struct nfs_inode *nfsi = NFS_I(inode);
4962 struct nfs4_cached_acl *acl;
4963 int ret = -ENOENT;
4964
4965 spin_lock(&inode->i_lock);
4966 acl = nfsi->nfs4_acl;
4967 if (acl == NULL)
4968 goto out;
4969 if (buf == NULL) /* user is just asking for length */
4970 goto out_len;
4971 if (acl->cached == 0)
4972 goto out;
4973 ret = -ERANGE; /* see getxattr(2) man page */
4974 if (acl->len > buflen)
4975 goto out;
4976 memcpy(buf, acl->data, acl->len);
4977out_len:
4978 ret = acl->len;
4979out:
4980 spin_unlock(&inode->i_lock);
4981 return ret;
4982}
4983
Sachin Prabhu5794d212012-04-17 14:36:40 +01004984static 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 +00004985{
4986 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004987 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004988
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004989 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004990 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004991 if (acl == NULL)
4992 goto out;
4993 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004994 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004995 } else {
4996 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4997 if (acl == NULL)
4998 goto out;
4999 acl->cached = 0;
5000 }
5001 acl->len = acl_len;
5002out:
5003 nfs4_set_cached_acl(inode, acl);
5004}
5005
Andy Adamsonbf118a32011-12-07 11:55:27 -05005006/*
5007 * The getxattr API returns the required buffer length when called with a
5008 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5009 * the required buf. On a NULL buf, we send a page of data to the server
5010 * guessing that the ACL request can be serviced by a page. If so, we cache
5011 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5012 * the cache. If not so, we throw away the page, and cache the required
5013 * length. The next getxattr call will then produce another round trip to
5014 * the server, this time with the input buf of the required size.
5015 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005016static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005017{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005018 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005019 struct nfs_getaclargs args = {
5020 .fh = NFS_FH(inode),
5021 .acl_pages = pages,
5022 .acl_len = buflen,
5023 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005024 struct nfs_getaclres res = {
5025 .acl_len = buflen,
5026 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005027 struct rpc_message msg = {
5028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5029 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005030 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005031 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005032 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005033 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005034
Trond Myklebust21f498c2012-08-24 10:59:25 -04005035 if (npages > ARRAY_SIZE(pages))
5036 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005037
Andy Adamsonbf118a32011-12-07 11:55:27 -05005038 for (i = 0; i < npages; i++) {
5039 pages[i] = alloc_page(GFP_KERNEL);
5040 if (!pages[i])
5041 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005042 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005043
5044 /* for decoding across pages */
5045 res.acl_scratch = alloc_page(GFP_KERNEL);
5046 if (!res.acl_scratch)
5047 goto out_free;
5048
Andy Adamsonbf118a32011-12-07 11:55:27 -05005049 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005050
Peng Taode040be2012-01-10 22:42:47 +08005051 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005052 __func__, buf, buflen, npages, args.acl_len);
5053 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5054 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005055 if (ret)
5056 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005057
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005058 /* Handle the case where the passed-in buffer is too short */
5059 if (res.acl_flags & NFS4_ACL_TRUNC) {
5060 /* Did the user only issue a request for the acl length? */
5061 if (buf == NULL)
5062 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005063 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005064 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005065 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005066 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005067 if (buf) {
5068 if (res.acl_len > buflen) {
5069 ret = -ERANGE;
5070 goto out_free;
5071 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005072 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005073 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005074out_ok:
5075 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005076out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005077 for (i = 0; i < npages; i++)
5078 if (pages[i])
5079 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005080 if (res.acl_scratch)
5081 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005082 return ret;
5083}
5084
Trond Myklebust16b42892006-08-24 12:27:15 -04005085static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5086{
5087 struct nfs4_exception exception = { };
5088 ssize_t ret;
5089 do {
5090 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005091 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005092 if (ret >= 0)
5093 break;
5094 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5095 } while (exception.retry);
5096 return ret;
5097}
5098
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005099static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5100{
5101 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005102 int ret;
5103
5104 if (!nfs4_server_supports_acls(server))
5105 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005106 ret = nfs_revalidate_inode(server, inode);
5107 if (ret < 0)
5108 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005109 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5110 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005111 ret = nfs4_read_cached_acl(inode, buf, buflen);
5112 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005113 /* -ENOENT is returned if there is no ACL or if there is an ACL
5114 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005115 return ret;
5116 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005117}
5118
Trond Myklebust16b42892006-08-24 12:27:15 -04005119static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005120{
5121 struct nfs_server *server = NFS_SERVER(inode);
5122 struct page *pages[NFS4ACL_MAXPAGES];
5123 struct nfs_setaclargs arg = {
5124 .fh = NFS_FH(inode),
5125 .acl_pages = pages,
5126 .acl_len = buflen,
5127 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005128 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005129 struct rpc_message msg = {
5130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5131 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005132 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005133 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005134 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005135 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005136
5137 if (!nfs4_server_supports_acls(server))
5138 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005139 if (npages > ARRAY_SIZE(pages))
5140 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005141 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005142 if (i < 0)
5143 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005144 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005145 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005146
5147 /*
5148 * Free each page after tx, so the only ref left is
5149 * held by the network stack
5150 */
5151 for (; i > 0; i--)
5152 put_page(pages[i-1]);
5153
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005154 /*
5155 * Acl update can result in inode attribute update.
5156 * so mark the attribute cache invalid.
5157 */
5158 spin_lock(&inode->i_lock);
5159 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5160 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005161 nfs_access_zap_cache(inode);
5162 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005163 return ret;
5164}
5165
Trond Myklebust16b42892006-08-24 12:27:15 -04005166static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5167{
5168 struct nfs4_exception exception = { };
5169 int err;
5170 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005171 err = __nfs4_proc_set_acl(inode, buf, buflen);
5172 trace_nfs4_set_acl(inode, err);
5173 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005174 &exception);
5175 } while (exception.retry);
5176 return err;
5177}
5178
David Quigleyaa9c2662013-05-22 12:50:44 -04005179#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5180static int _nfs4_get_security_label(struct inode *inode, void *buf,
5181 size_t buflen)
5182{
5183 struct nfs_server *server = NFS_SERVER(inode);
5184 struct nfs_fattr fattr;
5185 struct nfs4_label label = {0, 0, buflen, buf};
5186
5187 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005188 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005189 .fh = NFS_FH(inode),
5190 .bitmask = bitmask,
5191 };
5192 struct nfs4_getattr_res res = {
5193 .fattr = &fattr,
5194 .label = &label,
5195 .server = server,
5196 };
5197 struct rpc_message msg = {
5198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005199 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005200 .rpc_resp = &res,
5201 };
5202 int ret;
5203
5204 nfs_fattr_init(&fattr);
5205
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005206 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005207 if (ret)
5208 return ret;
5209 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5210 return -ENOENT;
5211 if (buflen < label.len)
5212 return -ERANGE;
5213 return 0;
5214}
5215
5216static int nfs4_get_security_label(struct inode *inode, void *buf,
5217 size_t buflen)
5218{
5219 struct nfs4_exception exception = { };
5220 int err;
5221
5222 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5223 return -EOPNOTSUPP;
5224
5225 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005226 err = _nfs4_get_security_label(inode, buf, buflen);
5227 trace_nfs4_get_security_label(inode, err);
5228 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005229 &exception);
5230 } while (exception.retry);
5231 return err;
5232}
5233
5234static int _nfs4_do_set_security_label(struct inode *inode,
5235 struct nfs4_label *ilabel,
5236 struct nfs_fattr *fattr,
5237 struct nfs4_label *olabel)
5238{
5239
5240 struct iattr sattr = {0};
5241 struct nfs_server *server = NFS_SERVER(inode);
5242 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005243 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005244 .fh = NFS_FH(inode),
5245 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005246 .server = server,
5247 .bitmask = bitmask,
5248 .label = ilabel,
5249 };
5250 struct nfs_setattrres res = {
5251 .fattr = fattr,
5252 .label = olabel,
5253 .server = server,
5254 };
5255 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5257 .rpc_argp = &arg,
5258 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005259 };
5260 int status;
5261
Jeff Layton12207f62013-11-01 10:49:32 -04005262 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005263
Jeff Layton12207f62013-11-01 10:49:32 -04005264 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005265 if (status)
5266 dprintk("%s failed: %d\n", __func__, status);
5267
5268 return status;
5269}
5270
5271static int nfs4_do_set_security_label(struct inode *inode,
5272 struct nfs4_label *ilabel,
5273 struct nfs_fattr *fattr,
5274 struct nfs4_label *olabel)
5275{
5276 struct nfs4_exception exception = { };
5277 int err;
5278
5279 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005280 err = _nfs4_do_set_security_label(inode, ilabel,
5281 fattr, olabel);
5282 trace_nfs4_set_security_label(inode, err);
5283 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005284 &exception);
5285 } while (exception.retry);
5286 return err;
5287}
5288
5289static int
Al Viro59301222016-05-27 10:19:30 -04005290nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005291{
5292 struct nfs4_label ilabel, *olabel = NULL;
5293 struct nfs_fattr fattr;
5294 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005295 int status;
5296
5297 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5298 return -EOPNOTSUPP;
5299
5300 nfs_fattr_init(&fattr);
5301
5302 ilabel.pi = 0;
5303 ilabel.lfs = 0;
5304 ilabel.label = (char *)buf;
5305 ilabel.len = buflen;
5306
5307 cred = rpc_lookup_cred();
5308 if (IS_ERR(cred))
5309 return PTR_ERR(cred);
5310
5311 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5312 if (IS_ERR(olabel)) {
5313 status = -PTR_ERR(olabel);
5314 goto out;
5315 }
5316
5317 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5318 if (status == 0)
5319 nfs_setsecurity(inode, &fattr, olabel);
5320
5321 nfs4_label_free(olabel);
5322out:
5323 put_rpccred(cred);
5324 return status;
5325}
5326#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5327
5328
Chuck Leverf0920752012-05-21 22:45:41 -04005329static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5330 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005331{
5332 __be32 verf[2];
5333
Chuck Lever2c820d92012-05-21 22:45:33 -04005334 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5335 /* An impossible timestamp guarantees this value
5336 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005337 verf[0] = cpu_to_be32(U32_MAX);
5338 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005339 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005340 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005341 u64 ns = ktime_to_ns(nn->boot_time);
5342
5343 verf[0] = cpu_to_be32(ns >> 32);
5344 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005345 }
Chuck Levercd937102012-03-02 17:14:31 -05005346 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5347}
5348
Jeff Laytona3192682015-06-09 19:43:59 -04005349static int
5350nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005351{
Jeff Laytona3192682015-06-09 19:43:59 -04005352 size_t len;
5353 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005354
Trond Myklebustceb3a162015-01-03 15:16:04 -05005355 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005356 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005357
Jeff Laytona3192682015-06-09 19:43:59 -04005358 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005359 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005360 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5361 1 +
5362 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5363 1;
5364 rcu_read_unlock();
5365
5366 if (len > NFS4_OPAQUE_LIMIT + 1)
5367 return -EINVAL;
5368
5369 /*
5370 * Since this string is allocated at mount time, and held until the
5371 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5372 * about a memory-reclaim deadlock.
5373 */
5374 str = kmalloc(len, GFP_KERNEL);
5375 if (!str)
5376 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005377
Chuck Levere984a552012-09-14 17:24:21 -04005378 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005379 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005380 clp->cl_ipaddr,
5381 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5382 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005383 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005384
Jeff Laytona3192682015-06-09 19:43:59 -04005385 clp->cl_owner_id = str;
5386 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005387}
5388
Jeff Layton873e3852015-06-09 19:44:00 -04005389static int
5390nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005391{
Jeff Layton873e3852015-06-09 19:44:00 -04005392 size_t len;
5393 char *str;
5394
5395 len = 10 + 10 + 1 + 10 + 1 +
5396 strlen(nfs4_client_id_uniquifier) + 1 +
5397 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5398
5399 if (len > NFS4_OPAQUE_LIMIT + 1)
5400 return -EINVAL;
5401
5402 /*
5403 * Since this string is allocated at mount time, and held until the
5404 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5405 * about a memory-reclaim deadlock.
5406 */
5407 str = kmalloc(len, GFP_KERNEL);
5408 if (!str)
5409 return -ENOMEM;
5410
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005411 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005412 clp->rpc_ops->version, clp->cl_minorversion,
5413 nfs4_client_id_uniquifier,
5414 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005415 clp->cl_owner_id = str;
5416 return 0;
5417}
5418
5419static int
5420nfs4_init_uniform_client_string(struct nfs_client *clp)
5421{
Jeff Layton873e3852015-06-09 19:44:00 -04005422 size_t len;
5423 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005424
5425 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005426 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005427
5428 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005429 return nfs4_init_uniquifier_client_string(clp);
5430
5431 len = 10 + 10 + 1 + 10 + 1 +
5432 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5433
5434 if (len > NFS4_OPAQUE_LIMIT + 1)
5435 return -EINVAL;
5436
5437 /*
5438 * Since this string is allocated at mount time, and held until the
5439 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5440 * about a memory-reclaim deadlock.
5441 */
5442 str = kmalloc(len, GFP_KERNEL);
5443 if (!str)
5444 return -ENOMEM;
5445
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005446 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005447 clp->rpc_ops->version, clp->cl_minorversion,
5448 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005449 clp->cl_owner_id = str;
5450 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005451}
5452
Chuck Lever706cb8d2014-03-12 12:51:47 -04005453/*
5454 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5455 * services. Advertise one based on the address family of the
5456 * clientaddr.
5457 */
5458static unsigned int
5459nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5460{
5461 if (strchr(clp->cl_ipaddr, ':') != NULL)
5462 return scnprintf(buf, len, "tcp6");
5463 else
5464 return scnprintf(buf, len, "tcp");
5465}
5466
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005467static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5468{
5469 struct nfs4_setclientid *sc = calldata;
5470
5471 if (task->tk_status == 0)
5472 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5473}
5474
5475static const struct rpc_call_ops nfs4_setclientid_ops = {
5476 .rpc_call_done = nfs4_setclientid_done,
5477};
5478
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005479/**
5480 * nfs4_proc_setclientid - Negotiate client ID
5481 * @clp: state data structure
5482 * @program: RPC program for NFSv4 callback service
5483 * @port: IP port number for NFS4 callback service
5484 * @cred: RPC credential to use for this call
5485 * @res: where to place the result
5486 *
5487 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5488 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005489int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5490 unsigned short port, struct rpc_cred *cred,
5491 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492{
5493 nfs4_verifier sc_verifier;
5494 struct nfs4_setclientid setclientid = {
5495 .sc_verifier = &sc_verifier,
5496 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005497 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 };
5499 struct rpc_message msg = {
5500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5501 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005502 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005503 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005505 struct rpc_task *task;
5506 struct rpc_task_setup task_setup_data = {
5507 .rpc_client = clp->cl_rpcclient,
5508 .rpc_message = &msg,
5509 .callback_ops = &nfs4_setclientid_ops,
5510 .callback_data = &setclientid,
5511 .flags = RPC_TASK_TIMEOUT,
5512 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005513 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Chuck Leverde734832012-07-11 16:30:50 -04005515 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005516 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005517
5518 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5519 status = nfs4_init_uniform_client_string(clp);
5520 else
Jeff Laytona3192682015-06-09 19:43:59 -04005521 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005522
5523 if (status)
5524 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005525
Chuck Leverde734832012-07-11 16:30:50 -04005526 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005527 setclientid.sc_netid_len =
5528 nfs4_init_callback_netid(clp,
5529 setclientid.sc_netid,
5530 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005531 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005532 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 clp->cl_ipaddr, port >> 8, port & 255);
5534
Jeff Layton3a6bb732015-06-09 19:43:57 -04005535 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005536 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005537 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005538 task = rpc_run_task(&task_setup_data);
5539 if (IS_ERR(task)) {
5540 status = PTR_ERR(task);
5541 goto out;
5542 }
5543 status = task->tk_status;
5544 if (setclientid.sc_cred) {
5545 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5546 put_rpccred(setclientid.sc_cred);
5547 }
5548 rpc_put_task(task);
5549out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005550 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS reply setclientid: %d\n", status);
5552 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553}
5554
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005555/**
5556 * nfs4_proc_setclientid_confirm - Confirm client ID
5557 * @clp: state data structure
5558 * @res: result of a previous SETCLIENTID
5559 * @cred: RPC credential to use for this call
5560 *
5561 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5562 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005563int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005564 struct nfs4_setclientid_res *arg,
5565 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 struct rpc_message msg = {
5568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005569 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005570 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 int status;
5573
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005574 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5575 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5576 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005577 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005578 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005579 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 return status;
5581}
5582
Trond Myklebustfe650402006-01-03 09:55:18 +01005583struct nfs4_delegreturndata {
5584 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005585 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005586 struct nfs_fh fh;
5587 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005588 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005589 struct {
5590 struct nfs4_layoutreturn_args arg;
5591 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005592 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005593 u32 roc_barrier;
5594 bool roc;
5595 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005596 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005597 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005598 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005599};
5600
Trond Myklebustfe650402006-01-03 09:55:18 +01005601static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5602{
5603 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005604
Trond Myklebust14516c32010-07-31 14:29:06 -04005605 if (!nfs4_sequence_done(task, &data->res.seq_res))
5606 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005607
Trond Myklebustca8acf82013-08-13 10:36:56 -04005608 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005609
5610 /* Handle Layoutreturn errors */
5611 if (data->args.lr_args && task->tk_status != 0) {
5612 switch(data->res.lr_ret) {
5613 default:
5614 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5615 break;
5616 case 0:
5617 data->args.lr_args = NULL;
5618 data->res.lr_res = NULL;
5619 break;
5620 case -NFS4ERR_ADMIN_REVOKED:
5621 case -NFS4ERR_DELEG_REVOKED:
5622 case -NFS4ERR_EXPIRED:
5623 case -NFS4ERR_BAD_STATEID:
5624 case -NFS4ERR_OLD_STATEID:
5625 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5626 case -NFS4ERR_WRONG_CRED:
5627 data->args.lr_args = NULL;
5628 data->res.lr_res = NULL;
5629 data->res.lr_ret = 0;
5630 rpc_restart_call_prepare(task);
5631 return;
5632 }
5633 }
5634
Ricardo Labiaga79708862009-12-07 09:23:21 -05005635 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005636 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005637 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005638 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005639 case -NFS4ERR_ADMIN_REVOKED:
5640 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005641 case -NFS4ERR_EXPIRED:
5642 nfs4_free_revoked_stateid(data->res.server,
5643 data->args.stateid,
5644 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005645 case -NFS4ERR_BAD_STATEID:
5646 case -NFS4ERR_OLD_STATEID:
5647 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005648 task->tk_status = 0;
5649 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005650 case -NFS4ERR_ACCESS:
5651 if (data->args.bitmask) {
5652 data->args.bitmask = NULL;
5653 data->res.fattr = NULL;
5654 task->tk_status = 0;
5655 rpc_restart_call_prepare(task);
5656 return;
5657 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005658 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005659 if (nfs4_async_handle_error(task, data->res.server,
5660 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005661 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005662 return;
5663 }
5664 }
5665 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005666}
5667
5668static void nfs4_delegreturn_release(void *calldata)
5669{
Peng Tao039b7562014-07-03 13:05:02 +08005670 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005671 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005672
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005673 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005674 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005675 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5676 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005677 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005678 nfs_iput_and_deactive(inode);
5679 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005680 kfree(calldata);
5681}
5682
Andy Adamson938e1012009-04-01 09:22:28 -04005683static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5684{
5685 struct nfs4_delegreturndata *d_data;
5686
5687 d_data = (struct nfs4_delegreturndata *)data;
5688
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005689 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005690 return;
5691
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005692 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005693 &d_data->args.seq_args,
5694 &d_data->res.seq_res,
5695 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005696}
Andy Adamson938e1012009-04-01 09:22:28 -04005697
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005698static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005699 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005700 .rpc_call_done = nfs4_delegreturn_done,
5701 .rpc_release = nfs4_delegreturn_release,
5702};
5703
Trond Myklebuste6f81072008-01-24 18:14:34 -05005704static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005705{
5706 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005707 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005708 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005709 struct rpc_message msg = {
5710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5711 .rpc_cred = cred,
5712 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005713 struct rpc_task_setup task_setup_data = {
5714 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005715 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005716 .callback_ops = &nfs4_delegreturn_ops,
5717 .flags = RPC_TASK_ASYNC,
5718 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005719 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005720
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005721 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005722 if (data == NULL)
5723 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005724 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005725
5726 nfs4_state_protect(server->nfs_client,
5727 NFS_SP4_MACH_CRED_CLEANUP,
5728 &task_setup_data.rpc_client, &msg);
5729
Trond Myklebustfe650402006-01-03 09:55:18 +01005730 data->args.fhandle = &data->fh;
5731 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005732 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005733 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005734 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005735 data->res.fattr = &data->fattr;
5736 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005737 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005738 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005739 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005740 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005741 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005742 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005743 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005744 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005745 if (data->lr.roc) {
5746 data->args.lr_args = &data->lr.arg;
5747 data->res.lr_res = &data->lr.res;
5748 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005749 } else if (data->lr.roc) {
5750 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5751 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005752 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005753
Trond Myklebustc970aa82007-07-14 15:39:59 -04005754 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005755 msg.rpc_argp = &data->args;
5756 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005757 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005758 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005759 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005760 if (!issync)
5761 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005762 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005763 if (status != 0)
5764 goto out;
5765 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005766out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005767 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005768 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769}
5770
Trond Myklebuste6f81072008-01-24 18:14:34 -05005771int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772{
5773 struct nfs_server *server = NFS_SERVER(inode);
5774 struct nfs4_exception exception = { };
5775 int err;
5776 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005777 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005778 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 switch (err) {
5780 case -NFS4ERR_STALE_STATEID:
5781 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 case 0:
5783 return 0;
5784 }
5785 err = nfs4_handle_exception(server, err, &exception);
5786 } while (exception.retry);
5787 return err;
5788}
5789
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5791{
5792 struct inode *inode = state->inode;
5793 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005794 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005795 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005797 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005799 struct nfs_lockt_res res = {
5800 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 };
5802 struct rpc_message msg = {
5803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005804 .rpc_argp = &arg,
5805 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 .rpc_cred = state->owner->so_cred,
5807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 struct nfs4_lock_state *lsp;
5809 int status;
5810
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005811 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005812 status = nfs4_set_lock_state(state, request);
5813 if (status != 0)
5814 goto out;
5815 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005816 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005817 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005818 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005819 switch (status) {
5820 case 0:
5821 request->fl_type = F_UNLCK;
5822 break;
5823 case -NFS4ERR_DENIED:
5824 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005826 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005827 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005828out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 return status;
5830}
5831
5832static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5833{
5834 struct nfs4_exception exception = { };
5835 int err;
5836
5837 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005838 err = _nfs4_proc_getlk(state, cmd, request);
5839 trace_nfs4_get_lock(request, state, cmd, err);
5840 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 &exception);
5842 } while (exception.retry);
5843 return err;
5844}
5845
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005846struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005847 struct nfs_locku_args arg;
5848 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005849 struct nfs4_lock_state *lsp;
5850 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005851 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005852 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005853 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005854 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005855};
5856
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005857static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5858 struct nfs_open_context *ctx,
5859 struct nfs4_lock_state *lsp,
5860 struct nfs_seqid *seqid)
5861{
5862 struct nfs4_unlockdata *p;
5863 struct inode *inode = lsp->ls_state->inode;
5864
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005865 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005866 if (p == NULL)
5867 return NULL;
5868 p->arg.fh = NFS_FH(inode);
5869 p->arg.fl = &p->fl;
5870 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005871 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005872 p->lsp = lsp;
5873 atomic_inc(&lsp->ls_count);
5874 /* Ensure we don't close file until we're done freeing locks! */
5875 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005876 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005877 memcpy(&p->fl, fl, sizeof(p->fl));
5878 p->server = NFS_SERVER(inode);
5879 return p;
5880}
5881
Trond Myklebust06f814a2006-01-03 09:55:07 +01005882static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005883{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005884 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005885 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005886 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005887 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005888 put_nfs_open_context(calldata->ctx);
5889 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005890}
5891
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005892static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005893{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005894 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005895
Trond Myklebust14516c32010-07-31 14:29:06 -04005896 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5897 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005898 switch (task->tk_status) {
5899 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005900 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005901 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005902 if (nfs4_update_lock_stateid(calldata->lsp,
5903 &calldata->res.stateid))
5904 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005905 case -NFS4ERR_ADMIN_REVOKED:
5906 case -NFS4ERR_EXPIRED:
5907 nfs4_free_revoked_stateid(calldata->server,
5908 &calldata->arg.stateid,
5909 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005910 case -NFS4ERR_BAD_STATEID:
5911 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005912 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005913 if (!nfs4_stateid_match(&calldata->arg.stateid,
5914 &calldata->lsp->ls_stateid))
5915 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005916 break;
5917 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005918 if (nfs4_async_handle_error(task, calldata->server,
5919 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005920 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005921 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005922 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005923}
5924
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005925static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005926{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005927 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005929 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5930 nfs_async_iocounter_wait(task, calldata->l_ctx))
5931 return;
5932
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005933 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005934 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005935 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005936 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005937 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005938 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005940 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005941 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005942 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005943 &calldata->res.seq_res,
5944 task) != 0)
5945 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005946 return;
5947out_no_action:
5948 task->tk_action = NULL;
5949out_wait:
5950 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951}
5952
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005953static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005954 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005955 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005956 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005957};
5958
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005959static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5960 struct nfs_open_context *ctx,
5961 struct nfs4_lock_state *lsp,
5962 struct nfs_seqid *seqid)
5963{
5964 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5967 .rpc_cred = ctx->cred,
5968 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005969 struct rpc_task_setup task_setup_data = {
5970 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005971 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005972 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005973 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005974 .flags = RPC_TASK_ASYNC,
5975 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005977 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5978 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5979
Frank Filz137d6ac2007-07-09 15:32:29 -07005980 /* Ensure this is an unlock - when canceling a lock, the
5981 * canceled lock is passed in, and it won't be an unlock.
5982 */
5983 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005984 if (fl->fl_flags & FL_CLOSE)
5985 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07005986
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005987 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5988 if (data == NULL) {
5989 nfs_free_seqid(seqid);
5990 return ERR_PTR(-ENOMEM);
5991 }
5992
Chuck Levera9c92d62013-08-09 12:48:18 -04005993 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005994 msg.rpc_argp = &data->arg;
5995 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005996 task_setup_data.callback_data = data;
5997 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005998}
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6001{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006002 struct inode *inode = state->inode;
6003 struct nfs4_state_owner *sp = state->owner;
6004 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006005 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006006 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006007 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006008 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006009 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006010 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
Trond Myklebust9b073572006-06-29 16:38:34 -04006012 status = nfs4_set_lock_state(state, request);
6013 /* Unlock _before_ we do the RPC call */
6014 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006015 /* Exclude nfs_delegation_claim_locks() */
6016 mutex_lock(&sp->so_delegreturn_mutex);
6017 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006018 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006019 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006020 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006021 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006022 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006023 }
6024 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006025 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006026 if (status != 0)
6027 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006028 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006029 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006030 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6031 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006032 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6033 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006034 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006035 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006036 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006037 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006038 status = PTR_ERR(task);
6039 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006040 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006041 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006042 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006043out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006044 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006045 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006046 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047}
6048
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006049struct nfs4_lockdata {
6050 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006051 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006052 struct nfs4_lock_state *lsp;
6053 struct nfs_open_context *ctx;
6054 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006055 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056 int rpc_status;
6057 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006058 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006059};
6060
6061static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006062 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6063 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064{
6065 struct nfs4_lockdata *p;
6066 struct inode *inode = lsp->ls_state->inode;
6067 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006068 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006069
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006070 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071 if (p == NULL)
6072 return NULL;
6073
6074 p->arg.fh = NFS_FH(inode);
6075 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006076 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006077 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006078 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006079 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6080 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006081 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006082 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006083 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006084 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006085 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006086 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006088 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089 atomic_inc(&lsp->ls_count);
6090 p->ctx = get_nfs_open_context(ctx);
6091 memcpy(&p->fl, fl, sizeof(p->fl));
6092 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006093out_free_seqid:
6094 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006095out_free:
6096 kfree(p);
6097 return NULL;
6098}
6099
6100static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6101{
6102 struct nfs4_lockdata *data = calldata;
6103 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Harvey Harrison3110ff82008-05-02 13:42:44 -07006105 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006106 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006107 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006108 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006109 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006110 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006111 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006112 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006113 nfs4_stateid_copy(&data->arg.open_stateid,
6114 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006115 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006116 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006117 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006118 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006119 nfs4_stateid_copy(&data->arg.lock_stateid,
6120 &data->lsp->ls_stateid);
6121 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006122 if (!nfs4_valid_open_stateid(state)) {
6123 data->rpc_status = -EBADF;
6124 task->tk_action = NULL;
6125 goto out_release_open_seqid;
6126 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006127 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006128 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006129 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006130 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006131 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006132 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006133out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006134 nfs_release_seqid(data->arg.open_seqid);
6135out_release_lock_seqid:
6136 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006137out_wait:
6138 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006139 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006140}
6141
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006142static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6143{
6144 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006145 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006146
Harvey Harrison3110ff82008-05-02 13:42:44 -07006147 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006148
Trond Myklebust14516c32010-07-31 14:29:06 -04006149 if (!nfs4_sequence_done(task, &data->res.seq_res))
6150 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006151
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006152 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006153 switch (task->tk_status) {
6154 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006155 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006156 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006157 if (data->arg.new_lock) {
6158 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006159 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006160 rpc_restart_call_prepare(task);
6161 break;
6162 }
6163 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006164 if (data->arg.new_lock_owner != 0) {
6165 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6166 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6167 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6168 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6169 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006170 break;
6171 case -NFS4ERR_BAD_STATEID:
6172 case -NFS4ERR_OLD_STATEID:
6173 case -NFS4ERR_STALE_STATEID:
6174 case -NFS4ERR_EXPIRED:
6175 if (data->arg.new_lock_owner != 0) {
6176 if (!nfs4_stateid_match(&data->arg.open_stateid,
6177 &lsp->ls_state->open_stateid))
6178 rpc_restart_call_prepare(task);
6179 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6180 &lsp->ls_stateid))
6181 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006182 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006183 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184}
6185
6186static void nfs4_lock_release(void *calldata)
6187{
6188 struct nfs4_lockdata *data = calldata;
6189
Harvey Harrison3110ff82008-05-02 13:42:44 -07006190 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006191 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006192 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006193 struct rpc_task *task;
6194 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6195 data->arg.lock_seqid);
6196 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006197 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006198 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 } else
6200 nfs_free_seqid(data->arg.lock_seqid);
6201 nfs4_put_lock_state(data->lsp);
6202 put_nfs_open_context(data->ctx);
6203 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006204 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205}
6206
6207static const struct rpc_call_ops nfs4_lock_ops = {
6208 .rpc_call_prepare = nfs4_lock_prepare,
6209 .rpc_call_done = nfs4_lock_done,
6210 .rpc_release = nfs4_lock_release,
6211};
6212
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006213static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6214{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006215 switch (error) {
6216 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006217 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006218 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006219 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006220 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006221 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006222 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006223 break;
6224 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006225 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006226 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006227 };
6228}
6229
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006230static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231{
6232 struct nfs4_lockdata *data;
6233 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006234 struct rpc_message msg = {
6235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6236 .rpc_cred = state->owner->so_cred,
6237 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006238 struct rpc_task_setup task_setup_data = {
6239 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006240 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006241 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006242 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006243 .flags = RPC_TASK_ASYNC,
6244 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006245 int ret;
6246
Harvey Harrison3110ff82008-05-02 13:42:44 -07006247 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006248 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006249 fl->fl_u.nfs4_fl.owner,
6250 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006251 if (data == NULL)
6252 return -ENOMEM;
6253 if (IS_SETLKW(cmd))
6254 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006255 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006256 msg.rpc_argp = &data->arg;
6257 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006258 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006259 if (recovery_type > NFS_LOCK_NEW) {
6260 if (recovery_type == NFS_LOCK_RECLAIM)
6261 data->arg.reclaim = NFS_LOCK_RECLAIM;
6262 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006263 } else
6264 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006265 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006266 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006267 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006268 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006269 if (ret == 0) {
6270 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006271 if (ret)
6272 nfs4_handle_setlk_error(data->server, data->lsp,
6273 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006275 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006276 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006277 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006278 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006279 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
6282static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6283{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006284 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006285 struct nfs4_exception exception = {
6286 .inode = state->inode,
6287 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006288 int err;
6289
6290 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006291 /* Cache the lock if possible... */
6292 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6293 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006294 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006295 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006296 break;
6297 nfs4_handle_exception(server, err, &exception);
6298 } while (exception.retry);
6299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300}
6301
6302static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6303{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006304 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006305 struct nfs4_exception exception = {
6306 .inode = state->inode,
6307 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006308 int err;
6309
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006310 err = nfs4_set_lock_state(state, request);
6311 if (err != 0)
6312 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006313 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006314 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6315 return 0;
6316 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006317 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006318 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6319 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006320 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006321 switch (err) {
6322 default:
6323 goto out;
6324 case -NFS4ERR_GRACE:
6325 case -NFS4ERR_DELAY:
6326 nfs4_handle_exception(server, err, &exception);
6327 err = 0;
6328 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006329 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006330out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006331 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332}
6333
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006334#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006335static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6336{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006337 struct nfs4_lock_state *lsp;
6338 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006339
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006340 status = nfs4_set_lock_state(state, request);
6341 if (status != 0)
6342 return status;
6343 lsp = request->fl_u.nfs4_fl.owner;
6344 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6345 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6346 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006347 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006348}
6349#endif
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6352{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006353 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006354 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006355 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006356 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
Trond Myklebust01c3b862006-06-29 16:38:39 -04006358 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006359 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006360 if (status < 0)
6361 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006362 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006363 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006364 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006365 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006366 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006367 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006368 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006369 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006370 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006371 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006372 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006373 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006374 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006375 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006376out:
6377 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 return status;
6379}
6380
6381static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6382{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006383 struct nfs4_exception exception = {
6384 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006385 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 int err;
6388
6389 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006390 err = _nfs4_proc_setlk(state, cmd, request);
6391 if (err == -NFS4ERR_DENIED)
6392 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006394 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 } while (exception.retry);
6396 return err;
6397}
6398
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006399#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6400#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6401
6402static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006403nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6404 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006405{
6406 int status = -ERESTARTSYS;
6407 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6408
6409 while(!signalled()) {
6410 status = nfs4_proc_setlk(state, cmd, request);
6411 if ((status != -EAGAIN) || IS_SETLK(cmd))
6412 break;
6413 freezable_schedule_timeout_interruptible(timeout);
6414 timeout *= 2;
6415 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6416 status = -ERESTARTSYS;
6417 }
6418 return status;
6419}
6420
Jeff Laytona1d617d82016-09-17 18:17:39 -04006421#ifdef CONFIG_NFS_V4_1
6422struct nfs4_lock_waiter {
6423 struct task_struct *task;
6424 struct inode *inode;
6425 struct nfs_lowner *owner;
6426 bool notified;
6427};
6428
6429static int
6430nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6431{
6432 int ret;
6433 struct cb_notify_lock_args *cbnl = key;
6434 struct nfs4_lock_waiter *waiter = wait->private;
6435 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6436 *wowner = waiter->owner;
6437
6438 /* Only wake if the callback was for the same owner */
6439 if (lowner->clientid != wowner->clientid ||
6440 lowner->id != wowner->id ||
6441 lowner->s_dev != wowner->s_dev)
6442 return 0;
6443
6444 /* Make sure it's for the right inode */
6445 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6446 return 0;
6447
6448 waiter->notified = true;
6449
6450 /* override "private" so we can use default_wake_function */
6451 wait->private = waiter->task;
6452 ret = autoremove_wake_function(wait, mode, flags, key);
6453 wait->private = waiter;
6454 return ret;
6455}
6456
6457static int
6458nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6459{
6460 int status = -ERESTARTSYS;
6461 unsigned long flags;
6462 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6463 struct nfs_server *server = NFS_SERVER(state->inode);
6464 struct nfs_client *clp = server->nfs_client;
6465 wait_queue_head_t *q = &clp->cl_lock_waitq;
6466 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6467 .id = lsp->ls_seqid.owner_id,
6468 .s_dev = server->s_dev };
6469 struct nfs4_lock_waiter waiter = { .task = current,
6470 .inode = state->inode,
6471 .owner = &owner,
6472 .notified = false };
6473 wait_queue_t wait;
6474
6475 /* Don't bother with waitqueue if we don't expect a callback */
6476 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6477 return nfs4_retry_setlk_simple(state, cmd, request);
6478
6479 init_wait(&wait);
6480 wait.private = &waiter;
6481 wait.func = nfs4_wake_lock_waiter;
6482 add_wait_queue(q, &wait);
6483
6484 while(!signalled()) {
6485 status = nfs4_proc_setlk(state, cmd, request);
6486 if ((status != -EAGAIN) || IS_SETLK(cmd))
6487 break;
6488
6489 status = -ERESTARTSYS;
6490 spin_lock_irqsave(&q->lock, flags);
6491 if (waiter.notified) {
6492 spin_unlock_irqrestore(&q->lock, flags);
6493 continue;
6494 }
6495 set_current_state(TASK_INTERRUPTIBLE);
6496 spin_unlock_irqrestore(&q->lock, flags);
6497
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006498 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006499 }
6500
6501 finish_wait(q, &wait);
6502 return status;
6503}
6504#else /* !CONFIG_NFS_V4_1 */
6505static inline int
6506nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6507{
6508 return nfs4_retry_setlk_simple(state, cmd, request);
6509}
6510#endif
6511
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512static int
6513nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6514{
6515 struct nfs_open_context *ctx;
6516 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 int status;
6518
6519 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006520 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 state = ctx->state;
6522
Trond Myklebustd9531262009-07-21 19:22:38 -04006523 if (IS_GETLK(cmd)) {
6524 if (state != NULL)
6525 return nfs4_proc_getlk(state, F_GETLK, request);
6526 return 0;
6527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
6529 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6530 return -EINVAL;
6531
Trond Myklebustd9531262009-07-21 19:22:38 -04006532 if (request->fl_type == F_UNLCK) {
6533 if (state != NULL)
6534 return nfs4_proc_unlck(state, cmd, request);
6535 return 0;
6536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
Trond Myklebustd9531262009-07-21 19:22:38 -04006538 if (state == NULL)
6539 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006540
6541 if ((request->fl_flags & FL_POSIX) &&
6542 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6543 return -ENOLCK;
6544
Jeff Layton1ea67db2016-09-17 18:17:37 -04006545 status = nfs4_set_lock_state(state, request);
6546 if (status != 0)
6547 return status;
6548
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006549 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
6551
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006552int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006553{
6554 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006555 int err;
6556
6557 err = nfs4_set_lock_state(state, fl);
6558 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006559 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006560 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006561 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006562}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006563
Trond Myklebustcf470c32012-03-07 13:49:12 -05006564struct nfs_release_lockowner_data {
6565 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006566 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006567 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006568 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006569 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006570};
6571
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006572static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6573{
6574 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006575 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006576 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6577 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006578 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006579 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006580}
6581
6582static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6583{
6584 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006585 struct nfs_server *server = data->server;
6586
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006587 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006588
6589 switch (task->tk_status) {
6590 case 0:
6591 renew_lease(server, data->timestamp);
6592 break;
6593 case -NFS4ERR_STALE_CLIENTID:
6594 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006595 nfs4_schedule_lease_recovery(server->nfs_client);
6596 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006597 case -NFS4ERR_LEASE_MOVED:
6598 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006599 if (nfs4_async_handle_error(task, server,
6600 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006601 rpc_restart_call_prepare(task);
6602 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006603}
6604
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006605static void nfs4_release_lockowner_release(void *calldata)
6606{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006607 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006608 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006609 kfree(calldata);
6610}
6611
Trond Myklebust17280172012-03-11 13:11:00 -04006612static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006613 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6614 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006615 .rpc_release = nfs4_release_lockowner_release,
6616};
6617
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006618static void
6619nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006620{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006621 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006622 struct rpc_message msg = {
6623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6624 };
6625
6626 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006627 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006628
Trond Myklebustcf470c32012-03-07 13:49:12 -05006629 data = kmalloc(sizeof(*data), GFP_NOFS);
6630 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006631 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006632 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006633 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006634 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6635 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6636 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006637
Trond Myklebustcf470c32012-03-07 13:49:12 -05006638 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006639 msg.rpc_resp = &data->res;
6640 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006641 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006642}
6643
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006644#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6645
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006646static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006647 struct dentry *unused, struct inode *inode,
6648 const char *key, const void *buf,
6649 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006650{
Al Viro59301222016-05-27 10:19:30 -04006651 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006652}
6653
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006654static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006655 struct dentry *unused, struct inode *inode,
6656 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006657{
Al Virob2968212016-04-10 20:48:24 -04006658 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006659}
6660
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006661static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006662{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006663 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006664}
6665
David Quigleyc9bccef2013-05-22 12:50:45 -04006666#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006667
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006668static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006669 struct dentry *unused, struct inode *inode,
6670 const char *key, const void *buf,
6671 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006672{
6673 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006674 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006675
6676 return -EOPNOTSUPP;
6677}
6678
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006679static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006680 struct dentry *unused, struct inode *inode,
6681 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006682{
6683 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006684 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006685 return -EOPNOTSUPP;
6686}
6687
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006688static ssize_t
6689nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006690{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006691 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006692
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006693 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6694 len = security_inode_listsecurity(inode, list, list_len);
6695 if (list_len && len > list_len)
6696 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006697 }
6698 return len;
6699}
6700
6701static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6702 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006703 .get = nfs4_xattr_get_nfs4_label,
6704 .set = nfs4_xattr_set_nfs4_label,
6705};
David Quigleyc9bccef2013-05-22 12:50:45 -04006706
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006707#else
6708
6709static ssize_t
6710nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6711{
6712 return 0;
6713}
6714
6715#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006716
Andy Adamson533eb462011-06-13 18:25:56 -04006717/*
6718 * nfs_fhget will use either the mounted_on_fileid or the fileid
6719 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006720static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6721{
Andy Adamson533eb462011-06-13 18:25:56 -04006722 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6723 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6724 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006725 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006726 return;
6727
6728 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006729 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006730 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6731 fattr->nlink = 2;
6732}
6733
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006734static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6735 const struct qstr *name,
6736 struct nfs4_fs_locations *fs_locations,
6737 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006738{
6739 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006740 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006741 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006742 };
6743 struct nfs4_fs_locations_arg args = {
6744 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006745 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006746 .page = page,
6747 .bitmask = bitmask,
6748 };
Benny Halevy22958462009-04-01 09:22:02 -04006749 struct nfs4_fs_locations_res res = {
6750 .fs_locations = fs_locations,
6751 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006752 struct rpc_message msg = {
6753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6754 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006755 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006756 };
6757 int status;
6758
Harvey Harrison3110ff82008-05-02 13:42:44 -07006759 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006760
6761 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6762 * is not supported */
6763 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6764 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6765 else
6766 bitmask[0] |= FATTR4_WORD0_FILEID;
6767
Trond Myklebustc228fd32007-01-13 02:28:11 -05006768 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006769 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006770 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006771 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006772 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006773 return status;
6774}
6775
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006776int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6777 const struct qstr *name,
6778 struct nfs4_fs_locations *fs_locations,
6779 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006780{
6781 struct nfs4_exception exception = { };
6782 int err;
6783 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006784 err = _nfs4_proc_fs_locations(client, dir, name,
6785 fs_locations, page);
6786 trace_nfs4_get_fs_locations(dir, name, err);
6787 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006788 &exception);
6789 } while (exception.retry);
6790 return err;
6791}
6792
Chuck Leverb03d7352013-10-17 14:12:50 -04006793/*
6794 * This operation also signals the server that this client is
6795 * performing migration recovery. The server can stop returning
6796 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6797 * appended to this compound to identify the client ID which is
6798 * performing recovery.
6799 */
6800static int _nfs40_proc_get_locations(struct inode *inode,
6801 struct nfs4_fs_locations *locations,
6802 struct page *page, struct rpc_cred *cred)
6803{
6804 struct nfs_server *server = NFS_SERVER(inode);
6805 struct rpc_clnt *clnt = server->client;
6806 u32 bitmask[2] = {
6807 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6808 };
6809 struct nfs4_fs_locations_arg args = {
6810 .clientid = server->nfs_client->cl_clientid,
6811 .fh = NFS_FH(inode),
6812 .page = page,
6813 .bitmask = bitmask,
6814 .migration = 1, /* skip LOOKUP */
6815 .renew = 1, /* append RENEW */
6816 };
6817 struct nfs4_fs_locations_res res = {
6818 .fs_locations = locations,
6819 .migration = 1,
6820 .renew = 1,
6821 };
6822 struct rpc_message msg = {
6823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6824 .rpc_argp = &args,
6825 .rpc_resp = &res,
6826 .rpc_cred = cred,
6827 };
6828 unsigned long now = jiffies;
6829 int status;
6830
6831 nfs_fattr_init(&locations->fattr);
6832 locations->server = server;
6833 locations->nlocations = 0;
6834
6835 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6836 nfs4_set_sequence_privileged(&args.seq_args);
6837 status = nfs4_call_sync_sequence(clnt, server, &msg,
6838 &args.seq_args, &res.seq_res);
6839 if (status)
6840 return status;
6841
6842 renew_lease(server, now);
6843 return 0;
6844}
6845
6846#ifdef CONFIG_NFS_V4_1
6847
6848/*
6849 * This operation also signals the server that this client is
6850 * performing migration recovery. The server can stop asserting
6851 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6852 * performing this operation is identified in the SEQUENCE
6853 * operation in this compound.
6854 *
6855 * When the client supports GETATTR(fs_locations_info), it can
6856 * be plumbed in here.
6857 */
6858static int _nfs41_proc_get_locations(struct inode *inode,
6859 struct nfs4_fs_locations *locations,
6860 struct page *page, struct rpc_cred *cred)
6861{
6862 struct nfs_server *server = NFS_SERVER(inode);
6863 struct rpc_clnt *clnt = server->client;
6864 u32 bitmask[2] = {
6865 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6866 };
6867 struct nfs4_fs_locations_arg args = {
6868 .fh = NFS_FH(inode),
6869 .page = page,
6870 .bitmask = bitmask,
6871 .migration = 1, /* skip LOOKUP */
6872 };
6873 struct nfs4_fs_locations_res res = {
6874 .fs_locations = locations,
6875 .migration = 1,
6876 };
6877 struct rpc_message msg = {
6878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6879 .rpc_argp = &args,
6880 .rpc_resp = &res,
6881 .rpc_cred = cred,
6882 };
6883 int status;
6884
6885 nfs_fattr_init(&locations->fattr);
6886 locations->server = server;
6887 locations->nlocations = 0;
6888
6889 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6890 nfs4_set_sequence_privileged(&args.seq_args);
6891 status = nfs4_call_sync_sequence(clnt, server, &msg,
6892 &args.seq_args, &res.seq_res);
6893 if (status == NFS4_OK &&
6894 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6895 status = -NFS4ERR_LEASE_MOVED;
6896 return status;
6897}
6898
6899#endif /* CONFIG_NFS_V4_1 */
6900
6901/**
6902 * nfs4_proc_get_locations - discover locations for a migrated FSID
6903 * @inode: inode on FSID that is migrating
6904 * @locations: result of query
6905 * @page: buffer
6906 * @cred: credential to use for this operation
6907 *
6908 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6909 * operation failed, or a negative errno if a local error occurred.
6910 *
6911 * On success, "locations" is filled in, but if the server has
6912 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6913 * asserted.
6914 *
6915 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6916 * from this client that require migration recovery.
6917 */
6918int nfs4_proc_get_locations(struct inode *inode,
6919 struct nfs4_fs_locations *locations,
6920 struct page *page, struct rpc_cred *cred)
6921{
6922 struct nfs_server *server = NFS_SERVER(inode);
6923 struct nfs_client *clp = server->nfs_client;
6924 const struct nfs4_mig_recovery_ops *ops =
6925 clp->cl_mvops->mig_recovery_ops;
6926 struct nfs4_exception exception = { };
6927 int status;
6928
6929 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6930 (unsigned long long)server->fsid.major,
6931 (unsigned long long)server->fsid.minor,
6932 clp->cl_hostname);
6933 nfs_display_fhandle(NFS_FH(inode), __func__);
6934
6935 do {
6936 status = ops->get_locations(inode, locations, page, cred);
6937 if (status != -NFS4ERR_DELAY)
6938 break;
6939 nfs4_handle_exception(server, status, &exception);
6940 } while (exception.retry);
6941 return status;
6942}
6943
Chuck Lever44c99932013-10-17 14:13:30 -04006944/*
6945 * This operation also signals the server that this client is
6946 * performing "lease moved" recovery. The server can stop
6947 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6948 * is appended to this compound to identify the client ID which is
6949 * performing recovery.
6950 */
6951static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6952{
6953 struct nfs_server *server = NFS_SERVER(inode);
6954 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6955 struct rpc_clnt *clnt = server->client;
6956 struct nfs4_fsid_present_arg args = {
6957 .fh = NFS_FH(inode),
6958 .clientid = clp->cl_clientid,
6959 .renew = 1, /* append RENEW */
6960 };
6961 struct nfs4_fsid_present_res res = {
6962 .renew = 1,
6963 };
6964 struct rpc_message msg = {
6965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6966 .rpc_argp = &args,
6967 .rpc_resp = &res,
6968 .rpc_cred = cred,
6969 };
6970 unsigned long now = jiffies;
6971 int status;
6972
6973 res.fh = nfs_alloc_fhandle();
6974 if (res.fh == NULL)
6975 return -ENOMEM;
6976
6977 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6978 nfs4_set_sequence_privileged(&args.seq_args);
6979 status = nfs4_call_sync_sequence(clnt, server, &msg,
6980 &args.seq_args, &res.seq_res);
6981 nfs_free_fhandle(res.fh);
6982 if (status)
6983 return status;
6984
6985 do_renew_lease(clp, now);
6986 return 0;
6987}
6988
6989#ifdef CONFIG_NFS_V4_1
6990
6991/*
6992 * This operation also signals the server that this client is
6993 * performing "lease moved" recovery. The server can stop asserting
6994 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6995 * this operation is identified in the SEQUENCE operation in this
6996 * compound.
6997 */
6998static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6999{
7000 struct nfs_server *server = NFS_SERVER(inode);
7001 struct rpc_clnt *clnt = server->client;
7002 struct nfs4_fsid_present_arg args = {
7003 .fh = NFS_FH(inode),
7004 };
7005 struct nfs4_fsid_present_res res = {
7006 };
7007 struct rpc_message msg = {
7008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7009 .rpc_argp = &args,
7010 .rpc_resp = &res,
7011 .rpc_cred = cred,
7012 };
7013 int status;
7014
7015 res.fh = nfs_alloc_fhandle();
7016 if (res.fh == NULL)
7017 return -ENOMEM;
7018
7019 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7020 nfs4_set_sequence_privileged(&args.seq_args);
7021 status = nfs4_call_sync_sequence(clnt, server, &msg,
7022 &args.seq_args, &res.seq_res);
7023 nfs_free_fhandle(res.fh);
7024 if (status == NFS4_OK &&
7025 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7026 status = -NFS4ERR_LEASE_MOVED;
7027 return status;
7028}
7029
7030#endif /* CONFIG_NFS_V4_1 */
7031
7032/**
7033 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7034 * @inode: inode on FSID to check
7035 * @cred: credential to use for this operation
7036 *
7037 * Server indicates whether the FSID is present, moved, or not
7038 * recognized. This operation is necessary to clear a LEASE_MOVED
7039 * condition for this client ID.
7040 *
7041 * Returns NFS4_OK if the FSID is present on this server,
7042 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7043 * NFS4ERR code if some error occurred on the server, or a
7044 * negative errno if a local failure occurred.
7045 */
7046int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7047{
7048 struct nfs_server *server = NFS_SERVER(inode);
7049 struct nfs_client *clp = server->nfs_client;
7050 const struct nfs4_mig_recovery_ops *ops =
7051 clp->cl_mvops->mig_recovery_ops;
7052 struct nfs4_exception exception = { };
7053 int status;
7054
7055 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7056 (unsigned long long)server->fsid.major,
7057 (unsigned long long)server->fsid.minor,
7058 clp->cl_hostname);
7059 nfs_display_fhandle(NFS_FH(inode), __func__);
7060
7061 do {
7062 status = ops->fsid_present(inode, cred);
7063 if (status != -NFS4ERR_DELAY)
7064 break;
7065 nfs4_handle_exception(server, status, &exception);
7066 } while (exception.retry);
7067 return status;
7068}
7069
Andy Adamson5ec16a82013-08-08 10:57:55 -04007070/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007071 * If 'use_integrity' is true and the state managment nfs_client
7072 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7073 * and the machine credential as per RFC3530bis and RFC5661 Security
7074 * Considerations sections. Otherwise, just use the user cred with the
7075 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007076 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007077static 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 +00007078{
7079 int status;
7080 struct nfs4_secinfo_arg args = {
7081 .dir_fh = NFS_FH(dir),
7082 .name = name,
7083 };
7084 struct nfs4_secinfo_res res = {
7085 .flavors = flavors,
7086 };
7087 struct rpc_message msg = {
7088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7089 .rpc_argp = &args,
7090 .rpc_resp = &res,
7091 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007092 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007093 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007094
7095 if (use_integrity) {
7096 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007097 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7098 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007099 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007100
7101 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007102
7103 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7104 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7105
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007106 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7107 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007108 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007109
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007110 if (cred)
7111 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007112
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007113 return status;
7114}
7115
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007116int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7117 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007118{
7119 struct nfs4_exception exception = { };
7120 int err;
7121 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007122 err = -NFS4ERR_WRONGSEC;
7123
7124 /* try to use integrity protection with machine cred */
7125 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7126 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7127
7128 /*
7129 * if unable to use integrity protection, or SECINFO with
7130 * integrity protection returns NFS4ERR_WRONGSEC (which is
7131 * disallowed by spec, but exists in deployed servers) use
7132 * the current filesystem's rpc_client and the user cred.
7133 */
7134 if (err == -NFS4ERR_WRONGSEC)
7135 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7136
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007137 trace_nfs4_secinfo(dir, name, err);
7138 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007139 &exception);
7140 } while (exception.retry);
7141 return err;
7142}
7143
Andy Adamson557134a2009-04-01 09:21:53 -04007144#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007145/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007146 * Check the exchange flags returned by the server for invalid flags, having
7147 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7148 * DS flags set.
7149 */
7150static int nfs4_check_cl_exchange_flags(u32 flags)
7151{
7152 if (flags & ~EXCHGID4_FLAG_MASK_R)
7153 goto out_inval;
7154 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7155 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7156 goto out_inval;
7157 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7158 goto out_inval;
7159 return NFS_OK;
7160out_inval:
7161 return -NFS4ERR_INVAL;
7162}
7163
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007164static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007165nfs41_same_server_scope(struct nfs41_server_scope *a,
7166 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007167{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007168 if (a->server_scope_sz != b->server_scope_sz)
7169 return false;
7170 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007171}
7172
Andy Adamson02a95de2016-02-05 16:08:37 -05007173static void
7174nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7175{
7176}
7177
7178static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7179 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7180};
7181
Andy Adamson357f54d2010-12-14 10:11:57 -05007182/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007183 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007184 *
7185 * The 4.1 client currently uses the same TCP connection for the
7186 * fore and backchannel.
7187 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007188static
7189int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7190 struct rpc_xprt *xprt,
7191 struct nfs_client *clp,
7192 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007193{
7194 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007195 struct nfs41_bind_conn_to_session_args args = {
7196 .client = clp,
7197 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7198 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007199 struct nfs41_bind_conn_to_session_res res;
7200 struct rpc_message msg = {
7201 .rpc_proc =
7202 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007203 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007204 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007205 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007206 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007207 struct rpc_task_setup task_setup_data = {
7208 .rpc_client = clnt,
7209 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007210 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007211 .rpc_message = &msg,
7212 .flags = RPC_TASK_TIMEOUT,
7213 };
7214 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007215
Trond Myklebust71a097c2015-02-18 09:27:18 -08007216 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7217 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7218 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007220 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7221 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7222 args.dir = NFS4_CDFC4_FORE;
7223
7224 task = rpc_run_task(&task_setup_data);
7225 if (!IS_ERR(task)) {
7226 status = task->tk_status;
7227 rpc_put_task(task);
7228 } else
7229 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007230 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007231 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007232 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007233 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7234 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007235 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007236 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007237 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007238 dprintk("NFS: %s: Unexpected direction from server\n",
7239 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007240 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007241 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007242 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007243 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7244 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007245 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007246 }
7247 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007248
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007249 return status;
7250}
7251
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007252struct rpc_bind_conn_calldata {
7253 struct nfs_client *clp;
7254 struct rpc_cred *cred;
7255};
7256
7257static int
7258nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7259 struct rpc_xprt *xprt,
7260 void *calldata)
7261{
7262 struct rpc_bind_conn_calldata *p = calldata;
7263
7264 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7265}
7266
7267int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7268{
7269 struct rpc_bind_conn_calldata data = {
7270 .clp = clp,
7271 .cred = cred,
7272 };
7273 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7274 nfs4_proc_bind_conn_to_session_callback, &data);
7275}
7276
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007277/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007278 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7279 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007280 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007281static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7282 .how = SP4_MACH_CRED,
7283 .enforce.u.words = {
7284 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7285 1 << (OP_EXCHANGE_ID - 32) |
7286 1 << (OP_CREATE_SESSION - 32) |
7287 1 << (OP_DESTROY_SESSION - 32) |
7288 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007289 },
7290 .allow.u.words = {
7291 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007292 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007293 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007294 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007295 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007296 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007297 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007298 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007299 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007300 1 << (OP_FREE_STATEID - 32) |
7301 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007302 }
7303};
7304
7305/*
7306 * Select the state protection mode for client `clp' given the server results
7307 * from exchange_id in `sp'.
7308 *
7309 * Returns 0 on success, negative errno otherwise.
7310 */
7311static int nfs4_sp4_select_mode(struct nfs_client *clp,
7312 struct nfs41_state_protection *sp)
7313{
7314 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7315 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7316 1 << (OP_EXCHANGE_ID - 32) |
7317 1 << (OP_CREATE_SESSION - 32) |
7318 1 << (OP_DESTROY_SESSION - 32) |
7319 1 << (OP_DESTROY_CLIENTID - 32)
7320 };
7321 unsigned int i;
7322
7323 if (sp->how == SP4_MACH_CRED) {
7324 /* Print state protect result */
7325 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7326 for (i = 0; i <= LAST_NFS4_OP; i++) {
7327 if (test_bit(i, sp->enforce.u.longs))
7328 dfprintk(MOUNT, " enforce op %d\n", i);
7329 if (test_bit(i, sp->allow.u.longs))
7330 dfprintk(MOUNT, " allow op %d\n", i);
7331 }
7332
7333 /* make sure nothing is on enforce list that isn't supported */
7334 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7335 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7336 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7337 return -EINVAL;
7338 }
7339 }
7340
7341 /*
7342 * Minimal mode - state operations are allowed to use machine
7343 * credential. Note this already happens by default, so the
7344 * client doesn't have to do anything more than the negotiation.
7345 *
7346 * NOTE: we don't care if EXCHANGE_ID is in the list -
7347 * we're already using the machine cred for exchange_id
7348 * and will never use a different cred.
7349 */
7350 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7351 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7352 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7353 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7354 dfprintk(MOUNT, "sp4_mach_cred:\n");
7355 dfprintk(MOUNT, " minimal mode enabled\n");
7356 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7357 } else {
7358 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7359 return -EINVAL;
7360 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007361
7362 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007363 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7364 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007365 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7366 dfprintk(MOUNT, " cleanup mode enabled\n");
7367 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7368 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007369
Andrew Elble99ade3c2015-12-02 09:39:51 -05007370 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7371 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7372 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7373 &clp->cl_sp4_flags);
7374 }
7375
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007376 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7377 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7378 dfprintk(MOUNT, " secinfo mode enabled\n");
7379 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7380 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007381
7382 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7383 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7384 dfprintk(MOUNT, " stateid mode enabled\n");
7385 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7386 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007387
7388 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7389 dfprintk(MOUNT, " write mode enabled\n");
7390 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7391 }
7392
7393 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7394 dfprintk(MOUNT, " commit mode enabled\n");
7395 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7396 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007397 }
7398
7399 return 0;
7400}
7401
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402struct nfs41_exchange_id_data {
7403 struct nfs41_exchange_id_res res;
7404 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007405 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007406 int rpc_status;
7407};
7408
7409static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007410{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007411 struct nfs41_exchange_id_data *cdata =
7412 (struct nfs41_exchange_id_data *)data;
7413 struct nfs_client *clp = cdata->args.client;
7414 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007415
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007416 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007417
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007418 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007419 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007420
7421 if (cdata->xprt && status == 0) {
7422 status = nfs4_detect_session_trunking(clp, &cdata->res,
7423 cdata->xprt);
7424 goto out;
7425 }
7426
Andy Adamson8d89bd72016-09-09 09:22:18 -04007427 if (status == 0)
7428 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007429
Chuck Lever177313f2012-05-21 22:44:58 -04007430 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007431 clp->cl_clientid = cdata->res.clientid;
7432 clp->cl_exchange_flags = cdata->res.flags;
Chuck Lever838edb92017-06-08 11:52:52 -04007433 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007434 /* Client ID is not confirmed */
Chuck Lever838edb92017-06-08 11:52:52 -04007435 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R))
Trond Myklebuste11259f2015-03-03 20:35:31 -05007436 clear_bit(NFS4_SESSION_ESTABLISHED,
Chuck Lever838edb92017-06-08 11:52:52 -04007437 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007438
Chuck Leveracdeb692012-05-21 22:46:16 -04007439 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007440 clp->cl_serverowner = cdata->res.server_owner;
7441 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007442
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007443 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007444 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007445 clp->cl_implid = cdata->res.impl_id;
7446 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007447
Chuck Lever177313f2012-05-21 22:44:58 -04007448 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007449 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007450 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007451 dprintk("%s: server_scope mismatch detected\n",
7452 __func__);
7453 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007454 kfree(clp->cl_serverscope);
7455 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007456 }
7457
Chuck Lever177313f2012-05-21 22:44:58 -04007458 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007459 clp->cl_serverscope = cdata->res.server_scope;
7460 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007461 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007462 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustfd405592017-08-01 16:02:47 -04007463 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007464 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007465 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007466out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007468 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007469}
7470
7471static void nfs4_exchange_id_release(void *data)
7472{
7473 struct nfs41_exchange_id_data *cdata =
7474 (struct nfs41_exchange_id_data *)data;
7475
Andy Adamsonad0849a2016-09-09 09:22:28 -04007476 if (cdata->xprt) {
7477 xprt_put(cdata->xprt);
7478 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7479 }
Olga Kornievskaia63513232017-03-13 10:36:19 -04007480 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 kfree(cdata->res.impl_id);
7482 kfree(cdata->res.server_scope);
7483 kfree(cdata->res.server_owner);
7484 kfree(cdata);
7485}
7486
7487static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7488 .rpc_call_done = nfs4_exchange_id_done,
7489 .rpc_release = nfs4_exchange_id_release,
7490};
7491
Benny Halevy99fe60d2009-04-01 09:22:29 -04007492/*
7493 * _nfs4_proc_exchange_id()
7494 *
7495 * Wrapper for EXCHANGE_ID operation.
7496 */
7497static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007498 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007500 struct rpc_message msg = {
7501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007502 .rpc_cred = cred,
7503 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007504 struct rpc_task_setup task_setup_data = {
7505 .rpc_client = clp->cl_rpcclient,
7506 .callback_ops = &nfs4_exchange_id_call_ops,
7507 .rpc_message = &msg,
7508 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7509 };
7510 struct nfs41_exchange_id_data *calldata;
7511 struct rpc_task *task;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007512 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007513
7514 if (!atomic_inc_not_zero(&clp->cl_count))
Anna Schumakere917f0d2017-04-07 14:15:22 -04007515 return -EIO;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007516
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007518 if (!calldata) {
7519 nfs_put_client(clp);
Anna Schumakere917f0d2017-04-07 14:15:22 -04007520 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007521 }
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522
Trond Myklebustfd405592017-08-01 16:02:47 -04007523 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007524
7525 status = nfs4_init_uniform_client_string(clp);
7526 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007527 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007528
Andy Adamson8d89bd72016-09-09 09:22:18 -04007529 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7530 GFP_NOFS);
7531 status = -ENOMEM;
7532 if (unlikely(calldata->res.server_owner == NULL))
7533 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007534
Andy Adamson8d89bd72016-09-09 09:22:18 -04007535 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007536 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007537 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007538 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007539
Andy Adamson8d89bd72016-09-09 09:22:18 -04007540 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7541 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007542 goto out_server_scope;
7543
7544 switch (sp4_how) {
7545 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007546 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007547 break;
7548
7549 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007550 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007551 break;
7552
7553 default:
7554 /* unsupported! */
7555 WARN_ON_ONCE(1);
7556 status = -EINVAL;
7557 goto out_impl_id;
7558 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007559 if (xprt) {
7560 calldata->xprt = xprt;
7561 task_setup_data.rpc_xprt = xprt;
7562 task_setup_data.flags =
7563 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustfd405592017-08-01 16:02:47 -04007564 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7565 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007566 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007567 calldata->args.client = clp;
7568#ifdef CONFIG_NFS_V4_1_MIGRATION
7569 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7570 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7571 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7572#else
7573 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7574 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7575#endif
7576 msg.rpc_argp = &calldata->args;
7577 msg.rpc_resp = &calldata->res;
7578 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007579
Andy Adamson8d89bd72016-09-09 09:22:18 -04007580 task = rpc_run_task(&task_setup_data);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007581 if (IS_ERR(task))
7582 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007583
Andy Adamsonad0849a2016-09-09 09:22:28 -04007584 if (!xprt) {
7585 status = rpc_wait_for_completion_task(task);
7586 if (!status)
7587 status = calldata->rpc_status;
7588 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007589 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007590
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007592out:
Benny Halevy99fe60d2009-04-01 09:22:29 -04007593 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007594
7595out_impl_id:
7596 kfree(calldata->res.impl_id);
7597out_server_scope:
7598 kfree(calldata->res.server_scope);
7599out_server_owner:
7600 kfree(calldata->res.server_owner);
7601out_calldata:
7602 kfree(calldata);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007603 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007604 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007605}
7606
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007607/*
7608 * nfs4_proc_exchange_id()
7609 *
7610 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7611 *
7612 * Since the clientid has expired, all compounds using sessions
7613 * associated with the stale clientid will be returning
7614 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7615 * be in some phase of session reset.
7616 *
7617 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7618 */
7619int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7620{
7621 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7622 int status;
7623
7624 /* try SP4_MACH_CRED if krb5i/p */
7625 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7626 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007627 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007628 if (!status)
7629 return 0;
7630 }
7631
7632 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007633 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007634}
7635
Andy Adamson04fa2c62016-09-09 09:22:29 -04007636/**
7637 * nfs4_test_session_trunk
7638 *
7639 * This is an add_xprt_test() test function called from
7640 * rpc_clnt_setup_test_and_add_xprt.
7641 *
7642 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7643 * and is dereferrenced in nfs4_exchange_id_release
7644 *
7645 * Upon success, add the new transport to the rpc_clnt
7646 *
7647 * @clnt: struct rpc_clnt to get new transport
7648 * @xprt: the rpc_xprt to test
7649 * @data: call data for _nfs4_proc_exchange_id.
7650 */
7651int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7652 void *data)
7653{
7654 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7655 u32 sp4_how;
7656
7657 dprintk("--> %s try %s\n", __func__,
7658 xprt->address_strings[RPC_DISPLAY_ADDR]);
7659
7660 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7661
7662 /* Test connection for session trunking. Async exchange_id call */
7663 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7664}
7665EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7666
Trond Myklebust66245532012-05-25 17:18:09 -04007667static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7668 struct rpc_cred *cred)
7669{
7670 struct rpc_message msg = {
7671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7672 .rpc_argp = clp,
7673 .rpc_cred = cred,
7674 };
7675 int status;
7676
7677 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007678 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007679 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007680 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007681 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7682 return status;
7683}
7684
7685static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7686 struct rpc_cred *cred)
7687{
7688 unsigned int loop;
7689 int ret;
7690
7691 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7692 ret = _nfs4_proc_destroy_clientid(clp, cred);
7693 switch (ret) {
7694 case -NFS4ERR_DELAY:
7695 case -NFS4ERR_CLIENTID_BUSY:
7696 ssleep(1);
7697 break;
7698 default:
7699 return ret;
7700 }
7701 }
7702 return 0;
7703}
7704
7705int nfs4_destroy_clientid(struct nfs_client *clp)
7706{
7707 struct rpc_cred *cred;
7708 int ret = 0;
7709
7710 if (clp->cl_mvops->minor_version < 1)
7711 goto out;
7712 if (clp->cl_exchange_flags == 0)
7713 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007714 if (clp->cl_preserve_clid)
7715 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007716 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007717 ret = nfs4_proc_destroy_clientid(clp, cred);
7718 if (cred)
7719 put_rpccred(cred);
7720 switch (ret) {
7721 case 0:
7722 case -NFS4ERR_STALE_CLIENTID:
7723 clp->cl_exchange_flags = 0;
7724 }
7725out:
7726 return ret;
7727}
7728
Andy Adamson2050f0c2009-04-01 09:22:30 -04007729struct nfs4_get_lease_time_data {
7730 struct nfs4_get_lease_time_args *args;
7731 struct nfs4_get_lease_time_res *res;
7732 struct nfs_client *clp;
7733};
7734
7735static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7736 void *calldata)
7737{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007738 struct nfs4_get_lease_time_data *data =
7739 (struct nfs4_get_lease_time_data *)calldata;
7740
7741 dprintk("--> %s\n", __func__);
7742 /* just setup sequence, do not trigger session recovery
7743 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007744 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007745 &data->args->la_seq_args,
7746 &data->res->lr_seq_res,
7747 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007748 dprintk("<-- %s\n", __func__);
7749}
7750
7751/*
7752 * Called from nfs4_state_manager thread for session setup, so don't recover
7753 * from sequence operation or clientid errors.
7754 */
7755static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7756{
7757 struct nfs4_get_lease_time_data *data =
7758 (struct nfs4_get_lease_time_data *)calldata;
7759
7760 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007761 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7762 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007763 switch (task->tk_status) {
7764 case -NFS4ERR_DELAY:
7765 case -NFS4ERR_GRACE:
7766 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7767 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7768 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007769 /* fall through */
7770 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007771 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007772 return;
7773 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007774 dprintk("<-- %s\n", __func__);
7775}
7776
Trond Myklebust17280172012-03-11 13:11:00 -04007777static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007778 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7779 .rpc_call_done = nfs4_get_lease_time_done,
7780};
7781
7782int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7783{
7784 struct rpc_task *task;
7785 struct nfs4_get_lease_time_args args;
7786 struct nfs4_get_lease_time_res res = {
7787 .lr_fsinfo = fsinfo,
7788 };
7789 struct nfs4_get_lease_time_data data = {
7790 .args = &args,
7791 .res = &res,
7792 .clp = clp,
7793 };
7794 struct rpc_message msg = {
7795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7796 .rpc_argp = &args,
7797 .rpc_resp = &res,
7798 };
7799 struct rpc_task_setup task_setup = {
7800 .rpc_client = clp->cl_rpcclient,
7801 .rpc_message = &msg,
7802 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007803 .callback_data = &data,
7804 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007805 };
7806 int status;
7807
Chuck Levera9c92d62013-08-09 12:48:18 -04007808 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007809 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007810 task = rpc_run_task(&task_setup);
7811
7812 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007813 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007814
Anna Schumakerf6148712017-04-07 14:15:23 -04007815 status = task->tk_status;
7816 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007817 return status;
7818}
7819
Andy Adamsonfc931582009-04-01 09:22:31 -04007820/*
7821 * Initialize the values to be used by the client in CREATE_SESSION
7822 * If nfs4_init_session set the fore channel request and response sizes,
7823 * use them.
7824 *
7825 * Set the back channel max_resp_sz_cached to zero to force the client to
7826 * always set csa_cachethis to FALSE because the current implementation
7827 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7828 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007829static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7830 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007831{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007832 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007833 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007834
Andy Adamson18aad3d2013-06-26 12:21:49 -04007835 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7836 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7837
Andy Adamsonfc931582009-04-01 09:22:31 -04007838 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007839 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7840 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007841 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007842 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007843
7844 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007845 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007846 __func__,
7847 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007848 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007849
7850 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007851 args->bc_attrs.max_rqst_sz = max_bc_payload;
7852 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007853 args->bc_attrs.max_resp_sz_cached = 0;
7854 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007855 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007856
7857 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7858 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7859 __func__,
7860 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7861 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7862 args->bc_attrs.max_reqs);
7863}
7864
Trond Myklebust79969dd2015-02-18 11:30:18 -08007865static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7866 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007867{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007868 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007870
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007871 if (rcvd->max_resp_sz > sent->max_resp_sz)
7872 return -EINVAL;
7873 /*
7874 * Our requested max_ops is the minimum we need; we're not
7875 * prepared to break up compounds into smaller pieces than that.
7876 * So, no point even trying to continue if the server won't
7877 * cooperate:
7878 */
7879 if (rcvd->max_ops < sent->max_ops)
7880 return -EINVAL;
7881 if (rcvd->max_reqs == 0)
7882 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007883 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7884 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007885 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007886}
7887
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7889 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007890{
7891 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007893
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007894 if (!(res->flags & SESSION4_BACK_CHAN))
7895 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007896 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7897 return -EINVAL;
7898 if (rcvd->max_resp_sz < sent->max_resp_sz)
7899 return -EINVAL;
7900 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7901 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007902 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007903 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007904 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007905 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007906out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007907 return 0;
7908}
Andy Adamson8d353012009-04-01 09:22:32 -04007909
Andy Adamson8d353012009-04-01 09:22:32 -04007910static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007911 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007912{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007913 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007914
Trond Myklebust79969dd2015-02-18 11:30:18 -08007915 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007916 if (ret)
7917 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007918 return nfs4_verify_back_channel_attrs(args, res);
7919}
7920
7921static void nfs4_update_session(struct nfs4_session *session,
7922 struct nfs41_create_session_res *res)
7923{
7924 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007925 /* Mark client id and session as being confirmed */
7926 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7927 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007928 session->flags = res->flags;
7929 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007930 if (res->flags & SESSION4_BACK_CHAN)
7931 memcpy(&session->bc_attrs, &res->bc_attrs,
7932 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007933}
7934
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007935static int _nfs4_proc_create_session(struct nfs_client *clp,
7936 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007937{
7938 struct nfs4_session *session = clp->cl_session;
7939 struct nfs41_create_session_args args = {
7940 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007941 .clientid = clp->cl_clientid,
7942 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 .cb_program = NFS4_CALLBACK,
7944 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007945 struct nfs41_create_session_res res;
7946
Andy Adamsonfc931582009-04-01 09:22:31 -04007947 struct rpc_message msg = {
7948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7949 .rpc_argp = &args,
7950 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007951 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007952 };
7953 int status;
7954
Chuck Lever6b26cc82016-05-02 14:40:40 -04007955 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007956 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007957
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007958 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007959 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007960
Trond Myklebustb519d402016-09-11 14:50:01 -04007961 switch (status) {
7962 case -NFS4ERR_STALE_CLIENTID:
7963 case -NFS4ERR_DELAY:
7964 case -ETIMEDOUT:
7965 case -EACCES:
7966 case -EAGAIN:
7967 goto out;
7968 };
7969
7970 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007971 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007972 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007973 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007974 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007975 if (status)
7976 goto out;
7977 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007978 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007979out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007980 return status;
7981}
7982
7983/*
7984 * Issues a CREATE_SESSION operation to the server.
7985 * It is the responsibility of the caller to verify the session is
7986 * expired before calling this routine.
7987 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007988int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007989{
7990 int status;
7991 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007992 struct nfs4_session *session = clp->cl_session;
7993
7994 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7995
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007996 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007997 if (status)
7998 goto out;
7999
Andy Adamsonaacd5532011-11-09 13:58:21 -05008000 /* Init or reset the session slot tables */
8001 status = nfs4_setup_session_slot_tables(session);
8002 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008003 if (status)
8004 goto out;
8005
8006 ptr = (unsigned *)&session->sess_id.data[0];
8007 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8008 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008009out:
8010 dprintk("<-- %s\n", __func__);
8011 return status;
8012}
8013
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008014/*
8015 * Issue the over-the-wire RPC DESTROY_SESSION.
8016 * The caller must serialize access to this routine.
8017 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008018int nfs4_proc_destroy_session(struct nfs4_session *session,
8019 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008020{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008021 struct rpc_message msg = {
8022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8023 .rpc_argp = session,
8024 .rpc_cred = cred,
8025 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008026 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027
8028 dprintk("--> nfs4_proc_destroy_session\n");
8029
8030 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008031 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8032 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008033
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008034 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008035 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008036
8037 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008038 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008039 "Session has been destroyed regardless...\n", status);
8040
8041 dprintk("<-- nfs4_proc_destroy_session\n");
8042 return status;
8043}
8044
Trond Myklebust7b38c362012-05-23 13:23:31 -04008045/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008046 * Renew the cl_session lease.
8047 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008048struct nfs4_sequence_data {
8049 struct nfs_client *clp;
8050 struct nfs4_sequence_args args;
8051 struct nfs4_sequence_res res;
8052};
8053
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008054static void nfs41_sequence_release(void *data)
8055{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008056 struct nfs4_sequence_data *calldata = data;
8057 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008058
Alexandros Batsakis71358402010-02-05 03:45:05 -08008059 if (atomic_read(&clp->cl_count) > 1)
8060 nfs4_schedule_state_renewal(clp);
8061 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008062 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008063}
8064
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008065static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8066{
8067 switch(task->tk_status) {
8068 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008069 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8070 return -EAGAIN;
8071 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008072 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008073 }
8074 return 0;
8075}
8076
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008077static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008079 struct nfs4_sequence_data *calldata = data;
8080 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081
Trond Myklebust14516c32010-07-31 14:29:06 -04008082 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8083 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008085 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086 if (task->tk_status < 0) {
8087 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008088 if (atomic_read(&clp->cl_count) == 1)
8089 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008090
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008091 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8092 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093 return;
8094 }
8095 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008096 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008097out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008098 dprintk("<-- %s\n", __func__);
8099}
8100
8101static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8102{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008103 struct nfs4_sequence_data *calldata = data;
8104 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105 struct nfs4_sequence_args *args;
8106 struct nfs4_sequence_res *res;
8107
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008108 args = task->tk_msg.rpc_argp;
8109 res = task->tk_msg.rpc_resp;
8110
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008111 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112}
8113
8114static const struct rpc_call_ops nfs41_sequence_ops = {
8115 .rpc_call_done = nfs41_sequence_call_done,
8116 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008117 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008118};
8119
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008120static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8121 struct rpc_cred *cred,
8122 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008123{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008124 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008125 struct rpc_message msg = {
8126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8127 .rpc_cred = cred,
8128 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008129 struct rpc_task_setup task_setup_data = {
8130 .rpc_client = clp->cl_rpcclient,
8131 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008132 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008133 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008134 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135
Alexandros Batsakis71358402010-02-05 03:45:05 -08008136 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008137 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008138 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008139 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008140 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008141 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008142 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008143 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008144 if (is_privileged)
8145 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008146 msg.rpc_argp = &calldata->args;
8147 msg.rpc_resp = &calldata->res;
8148 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008149 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008150
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008151 return rpc_run_task(&task_setup_data);
8152}
8153
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008154static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008155{
8156 struct rpc_task *task;
8157 int ret = 0;
8158
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008159 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008160 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008161 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008162 if (IS_ERR(task))
8163 ret = PTR_ERR(task);
8164 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008165 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008166 dprintk("<-- %s status=%d\n", __func__, ret);
8167 return ret;
8168}
8169
8170static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8171{
8172 struct rpc_task *task;
8173 int ret;
8174
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008175 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008176 if (IS_ERR(task)) {
8177 ret = PTR_ERR(task);
8178 goto out;
8179 }
8180 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008181 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008182 ret = task->tk_status;
8183 rpc_put_task(task);
8184out:
8185 dprintk("<-- %s status=%d\n", __func__, ret);
8186 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187}
8188
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008189struct nfs4_reclaim_complete_data {
8190 struct nfs_client *clp;
8191 struct nfs41_reclaim_complete_args arg;
8192 struct nfs41_reclaim_complete_res res;
8193};
8194
8195static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8196{
8197 struct nfs4_reclaim_complete_data *calldata = data;
8198
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008199 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008200 &calldata->arg.seq_args,
8201 &calldata->res.seq_res,
8202 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008203}
8204
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008205static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8206{
8207 switch(task->tk_status) {
8208 case 0:
8209 case -NFS4ERR_COMPLETE_ALREADY:
8210 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8211 break;
8212 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008213 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008214 /* fall through */
8215 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008216 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008217 case -NFS4ERR_BADSESSION:
8218 case -NFS4ERR_DEADSESSION:
8219 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8220 nfs4_schedule_session_recovery(clp->cl_session,
8221 task->tk_status);
8222 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008223 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008224 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008225 }
8226 return 0;
8227}
8228
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008229static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8230{
8231 struct nfs4_reclaim_complete_data *calldata = data;
8232 struct nfs_client *clp = calldata->clp;
8233 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8234
8235 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008236 if (!nfs41_sequence_done(task, res))
8237 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008239 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008240 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8241 rpc_restart_call_prepare(task);
8242 return;
8243 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008244 dprintk("<-- %s\n", __func__);
8245}
8246
8247static void nfs4_free_reclaim_complete_data(void *data)
8248{
8249 struct nfs4_reclaim_complete_data *calldata = data;
8250
8251 kfree(calldata);
8252}
8253
8254static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8255 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8256 .rpc_call_done = nfs4_reclaim_complete_done,
8257 .rpc_release = nfs4_free_reclaim_complete_data,
8258};
8259
8260/*
8261 * Issue a global reclaim complete.
8262 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008263static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8264 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008265{
8266 struct nfs4_reclaim_complete_data *calldata;
8267 struct rpc_task *task;
8268 struct rpc_message msg = {
8269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008270 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008271 };
8272 struct rpc_task_setup task_setup_data = {
8273 .rpc_client = clp->cl_rpcclient,
8274 .rpc_message = &msg,
8275 .callback_ops = &nfs4_reclaim_complete_call_ops,
8276 .flags = RPC_TASK_ASYNC,
8277 };
8278 int status = -ENOMEM;
8279
8280 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008281 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282 if (calldata == NULL)
8283 goto out;
8284 calldata->clp = clp;
8285 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008286
Chuck Levera9c92d62013-08-09 12:48:18 -04008287 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008288 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008289 msg.rpc_argp = &calldata->arg;
8290 msg.rpc_resp = &calldata->res;
8291 task_setup_data.callback_data = calldata;
8292 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008293 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008294 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008295 goto out;
8296 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008297 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008298 if (status == 0)
8299 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008300 rpc_put_task(task);
8301out:
8302 dprintk("<-- %s status=%d\n", __func__, status);
8303 return status;
8304}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008305
8306static void
8307nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8308{
8309 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008310 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008311
8312 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008313 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008314 &lgp->res.seq_res, task);
8315 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008316}
8317
8318static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8319{
8320 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008321
8322 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008323 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008324 dprintk("<-- %s\n", __func__);
8325}
8326
8327static int
8328nfs4_layoutget_handle_exception(struct rpc_task *task,
8329 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8330{
Trond Myklebustee314c22012-10-01 17:25:48 -07008331 struct inode *inode = lgp->args.inode;
8332 struct nfs_server *server = NFS_SERVER(inode);
8333 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008334 int nfs4err = task->tk_status;
8335 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008336 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008337
Boaz Harroshed7e5422014-01-22 20:34:54 +02008338 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008339
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008340 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008341 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008342 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008343
8344 /*
8345 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8346 * on the file. set tk_status to -ENODATA to tell upper layer to
8347 * retry go inband.
8348 */
8349 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008350 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008351 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008352 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008353 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8354 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8355 */
8356 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008357 status = -EOVERFLOW;
8358 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008359 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008360 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008361 * (or clients) writing to the same RAID stripe except when
8362 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008363 *
8364 * Treat it like we would RECALLCONFLICT -- we retry for a little
8365 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008366 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008367 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008368 if (lgp->args.minlength == 0) {
8369 status = -EOVERFLOW;
8370 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008371 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008372 status = -EBUSY;
8373 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008374 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008375 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008376 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008377 case -NFS4ERR_DELEG_REVOKED:
8378 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008379 case -NFS4ERR_EXPIRED:
8380 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008381 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008382 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008383 lo = NFS_I(inode)->layout;
8384 /* If the open stateid was bad, then recover it. */
8385 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8386 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008387 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008388 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008389 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008390 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008391 break;
8392 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008393
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008394 /*
8395 * Mark the bad layout state as invalid, then retry
8396 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008397 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008398 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008399 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008400 pnfs_free_lseg_list(&head);
8401 status = -EAGAIN;
8402 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008403 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008404
Trond Myklebust8ac09252017-01-23 22:44:12 -05008405 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008406 err = nfs4_handle_exception(server, nfs4err, exception);
8407 if (!status) {
8408 if (exception->retry)
8409 status = -EAGAIN;
8410 else
8411 status = err;
8412 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008413out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008414 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008415 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008416}
8417
Idan Kedar85541162012-08-02 11:47:10 +03008418static size_t max_response_pages(struct nfs_server *server)
8419{
8420 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8421 return nfs_page_array_len(0, max_resp_sz);
8422}
8423
8424static void nfs4_free_pages(struct page **pages, size_t size)
8425{
8426 int i;
8427
8428 if (!pages)
8429 return;
8430
8431 for (i = 0; i < size; i++) {
8432 if (!pages[i])
8433 break;
8434 __free_page(pages[i]);
8435 }
8436 kfree(pages);
8437}
8438
8439static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8440{
8441 struct page **pages;
8442 int i;
8443
8444 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8445 if (!pages) {
8446 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8447 return NULL;
8448 }
8449
8450 for (i = 0; i < size; i++) {
8451 pages[i] = alloc_page(gfp_flags);
8452 if (!pages[i]) {
8453 dprintk("%s: failed to allocate page\n", __func__);
8454 nfs4_free_pages(pages, size);
8455 return NULL;
8456 }
8457 }
8458
8459 return pages;
8460}
8461
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008462static void nfs4_layoutget_release(void *calldata)
8463{
8464 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008465 struct inode *inode = lgp->args.inode;
8466 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008467 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008468
8469 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008470 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008471 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472 put_nfs_open_context(lgp->args.ctx);
8473 kfree(calldata);
8474 dprintk("<-- %s\n", __func__);
8475}
8476
8477static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8478 .rpc_call_prepare = nfs4_layoutget_prepare,
8479 .rpc_call_done = nfs4_layoutget_done,
8480 .rpc_release = nfs4_layoutget_release,
8481};
8482
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008483struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008484nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008485{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008486 struct inode *inode = lgp->args.inode;
8487 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008488 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008489 struct rpc_task *task;
8490 struct rpc_message msg = {
8491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8492 .rpc_argp = &lgp->args,
8493 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008494 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008495 };
8496 struct rpc_task_setup task_setup_data = {
8497 .rpc_client = server->client,
8498 .rpc_message = &msg,
8499 .callback_ops = &nfs4_layoutget_call_ops,
8500 .callback_data = lgp,
8501 .flags = RPC_TASK_ASYNC,
8502 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008503 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008504 struct nfs4_exception exception = {
8505 .inode = inode,
8506 .timeout = *timeout,
8507 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008508 int status = 0;
8509
8510 dprintk("--> %s\n", __func__);
8511
Peng Tao4bd5a982014-11-17 11:05:17 +08008512 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8513 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8514
Idan Kedar85541162012-08-02 11:47:10 +03008515 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8516 if (!lgp->args.layout.pages) {
8517 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008518 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008519 }
8520 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8521
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008522 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008523 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008524 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008525
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008526 task = rpc_run_task(&task_setup_data);
8527 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008528 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008529 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008530 if (status == 0) {
8531 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8532 *timeout = exception.timeout;
8533 }
8534
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008535 trace_nfs4_layoutget(lgp->args.ctx,
8536 &lgp->args.range,
8537 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008538 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008539 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008540
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008541 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8542 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008543 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008544 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008545 rpc_put_task(task);
8546 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008547 if (status)
8548 return ERR_PTR(status);
8549 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008550}
8551
Benny Halevycbe82602011-05-22 19:52:37 +03008552static void
8553nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8554{
8555 struct nfs4_layoutreturn *lrp = calldata;
8556
8557 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008558 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008559 &lrp->args.seq_args,
8560 &lrp->res.seq_res,
8561 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008562}
8563
8564static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8565{
8566 struct nfs4_layoutreturn *lrp = calldata;
8567 struct nfs_server *server;
8568
8569 dprintk("--> %s\n", __func__);
8570
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008571 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008572 return;
8573
8574 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008575 switch (task->tk_status) {
8576 default:
8577 task->tk_status = 0;
8578 case 0:
8579 break;
8580 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008581 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008582 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008583 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008584 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008585 return;
8586 }
Benny Halevycbe82602011-05-22 19:52:37 +03008587 dprintk("<-- %s\n", __func__);
8588}
8589
8590static void nfs4_layoutreturn_release(void *calldata)
8591{
8592 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008593 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008594
8595 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008596 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008597 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008598 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008599 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8600 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008601 pnfs_put_layout_hdr(lrp->args.layout);
8602 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008603 kfree(calldata);
8604 dprintk("<-- %s\n", __func__);
8605}
8606
8607static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8608 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8609 .rpc_call_done = nfs4_layoutreturn_done,
8610 .rpc_release = nfs4_layoutreturn_release,
8611};
8612
Peng Tao6c166052014-11-17 09:30:40 +08008613int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008614{
8615 struct rpc_task *task;
8616 struct rpc_message msg = {
8617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8618 .rpc_argp = &lrp->args,
8619 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008620 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008621 };
8622 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008623 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008624 .rpc_message = &msg,
8625 .callback_ops = &nfs4_layoutreturn_call_ops,
8626 .callback_data = lrp,
8627 };
Peng Tao6c166052014-11-17 09:30:40 +08008628 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008629
Andrew Elble99ade3c2015-12-02 09:39:51 -05008630 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8631 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8632 &task_setup_data.rpc_client, &msg);
8633
Benny Halevycbe82602011-05-22 19:52:37 +03008634 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008635 if (!sync) {
8636 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8637 if (!lrp->inode) {
8638 nfs4_layoutreturn_release(lrp);
8639 return -EAGAIN;
8640 }
8641 task_setup_data.flags |= RPC_TASK_ASYNC;
8642 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008643 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008644 task = rpc_run_task(&task_setup_data);
8645 if (IS_ERR(task))
8646 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008647 if (sync)
8648 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008649 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008650 dprintk("<-- %s status=%d\n", __func__, status);
8651 rpc_put_task(task);
8652 return status;
8653}
8654
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008656_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8657 struct pnfs_device *pdev,
8658 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008659{
8660 struct nfs4_getdeviceinfo_args args = {
8661 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008662 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8663 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008664 };
8665 struct nfs4_getdeviceinfo_res res = {
8666 .pdev = pdev,
8667 };
8668 struct rpc_message msg = {
8669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8670 .rpc_argp = &args,
8671 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008672 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008673 };
8674 int status;
8675
8676 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008677 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008678 if (res.notification & ~args.notify_types)
8679 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008680 if (res.notification != args.notify_types)
8681 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008682
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 dprintk("<-- %s status=%d\n", __func__, status);
8684
8685 return status;
8686}
8687
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008688int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8689 struct pnfs_device *pdev,
8690 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008691{
8692 struct nfs4_exception exception = { };
8693 int err;
8694
8695 do {
8696 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008697 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698 &exception);
8699 } while (exception.retry);
8700 return err;
8701}
8702EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8703
Andy Adamson863a3c62011-03-23 13:27:54 +00008704static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8705{
8706 struct nfs4_layoutcommit_data *data = calldata;
8707 struct nfs_server *server = NFS_SERVER(data->args.inode);
8708
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008709 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008710 &data->args.seq_args,
8711 &data->res.seq_res,
8712 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008713}
8714
8715static void
8716nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8717{
8718 struct nfs4_layoutcommit_data *data = calldata;
8719 struct nfs_server *server = NFS_SERVER(data->args.inode);
8720
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008721 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008722 return;
8723
8724 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008725 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8726 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8727 case -NFS4ERR_BADLAYOUT: /* no layout */
8728 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008729 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008730 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008731 break;
8732 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008733 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008734 rpc_restart_call_prepare(task);
8735 return;
8736 }
8737 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008738}
8739
8740static void nfs4_layoutcommit_release(void *calldata)
8741{
8742 struct nfs4_layoutcommit_data *data = calldata;
8743
Andy Adamsondb29c082011-07-30 20:52:38 -04008744 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008745 nfs_post_op_update_inode_force_wcc(data->args.inode,
8746 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008747 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008748 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008749 kfree(data);
8750}
8751
8752static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8753 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8754 .rpc_call_done = nfs4_layoutcommit_done,
8755 .rpc_release = nfs4_layoutcommit_release,
8756};
8757
8758int
Andy Adamsonef311532011-03-12 02:58:10 -05008759nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008760{
8761 struct rpc_message msg = {
8762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8763 .rpc_argp = &data->args,
8764 .rpc_resp = &data->res,
8765 .rpc_cred = data->cred,
8766 };
8767 struct rpc_task_setup task_setup_data = {
8768 .task = &data->task,
8769 .rpc_client = NFS_CLIENT(data->args.inode),
8770 .rpc_message = &msg,
8771 .callback_ops = &nfs4_layoutcommit_ops,
8772 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008773 };
8774 struct rpc_task *task;
8775 int status = 0;
8776
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008777 dprintk("NFS: initiating layoutcommit call. sync %d "
8778 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008779 data->args.lastbytewritten,
8780 data->args.inode->i_ino);
8781
Trond Myklebust472e2592015-02-05 16:50:30 -05008782 if (!sync) {
8783 data->inode = nfs_igrab_and_active(data->args.inode);
8784 if (data->inode == NULL) {
8785 nfs4_layoutcommit_release(data);
8786 return -EAGAIN;
8787 }
8788 task_setup_data.flags = RPC_TASK_ASYNC;
8789 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008790 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008791 task = rpc_run_task(&task_setup_data);
8792 if (IS_ERR(task))
8793 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008794 if (sync)
8795 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008796 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008797 dprintk("%s: status %d\n", __func__, status);
8798 rpc_put_task(task);
8799 return status;
8800}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801
Andy Adamson97431202013-08-08 10:57:56 -04008802/**
8803 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8804 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8805 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008806static int
8807_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008808 struct nfs_fsinfo *info,
8809 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008810{
8811 struct nfs41_secinfo_no_name_args args = {
8812 .style = SECINFO_STYLE_CURRENT_FH,
8813 };
8814 struct nfs4_secinfo_res res = {
8815 .flavors = flavors,
8816 };
8817 struct rpc_message msg = {
8818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8819 .rpc_argp = &args,
8820 .rpc_resp = &res,
8821 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008822 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008823 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008824 int status;
8825
8826 if (use_integrity) {
8827 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008828 cred = nfs4_get_clid_cred(server->nfs_client);
8829 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008830 }
8831
8832 dprintk("--> %s\n", __func__);
8833 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8834 &res.seq_res, 0);
8835 dprintk("<-- %s status=%d\n", __func__, status);
8836
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008837 if (cred)
8838 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008839
8840 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008841}
8842
8843static int
8844nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8845 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8846{
8847 struct nfs4_exception exception = { };
8848 int err;
8849 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008850 /* first try using integrity protection */
8851 err = -NFS4ERR_WRONGSEC;
8852
8853 /* try to use integrity protection with machine cred */
8854 if (_nfs4_is_integrity_protected(server->nfs_client))
8855 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8856 flavors, true);
8857
8858 /*
8859 * if unable to use integrity protection, or SECINFO with
8860 * integrity protection returns NFS4ERR_WRONGSEC (which is
8861 * disallowed by spec, but exists in deployed servers) use
8862 * the current filesystem's rpc_client and the user cred.
8863 */
8864 if (err == -NFS4ERR_WRONGSEC)
8865 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8866 flavors, false);
8867
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008868 switch (err) {
8869 case 0:
8870 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008871 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008872 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873 default:
8874 err = nfs4_handle_exception(server, err, &exception);
8875 }
8876 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008877out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878 return err;
8879}
8880
8881static int
8882nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8883 struct nfs_fsinfo *info)
8884{
8885 int err;
8886 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008887 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008889 struct nfs4_secinfo4 *secinfo;
8890 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008891
8892 page = alloc_page(GFP_KERNEL);
8893 if (!page) {
8894 err = -ENOMEM;
8895 goto out;
8896 }
8897
8898 flavors = page_address(page);
8899 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8900
8901 /*
8902 * Fall back on "guess and check" method if
8903 * the server doesn't support SECINFO_NO_NAME
8904 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008905 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008906 err = nfs4_find_root_sec(server, fhandle, info);
8907 goto out_freepage;
8908 }
8909 if (err)
8910 goto out_freepage;
8911
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008912 for (i = 0; i < flavors->num_flavors; i++) {
8913 secinfo = &flavors->flavors[i];
8914
8915 switch (secinfo->flavor) {
8916 case RPC_AUTH_NULL:
8917 case RPC_AUTH_UNIX:
8918 case RPC_AUTH_GSS:
8919 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8920 &secinfo->flavor_info);
8921 break;
8922 default:
8923 flavor = RPC_AUTH_MAXFLAVOR;
8924 break;
8925 }
8926
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008927 if (!nfs_auth_info_match(&server->auth_info, flavor))
8928 flavor = RPC_AUTH_MAXFLAVOR;
8929
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008930 if (flavor != RPC_AUTH_MAXFLAVOR) {
8931 err = nfs4_lookup_root_sec(server, fhandle,
8932 info, flavor);
8933 if (!err)
8934 break;
8935 }
8936 }
8937
8938 if (flavor == RPC_AUTH_MAXFLAVOR)
8939 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008940
8941out_freepage:
8942 put_page(page);
8943 if (err == -EACCES)
8944 return -EPERM;
8945out:
8946 return err;
8947}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008948
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008949static int _nfs41_test_stateid(struct nfs_server *server,
8950 nfs4_stateid *stateid,
8951 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008952{
8953 int status;
8954 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008955 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008956 };
8957 struct nfs41_test_stateid_res res;
8958 struct rpc_message msg = {
8959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8960 .rpc_argp = &args,
8961 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008962 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008963 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008964 struct rpc_clnt *rpc_client = server->client;
8965
8966 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8967 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008968
Chuck Lever38527b12012-07-11 16:30:23 -04008969 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008970 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008971 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008972 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008973 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008974 if (status != NFS_OK) {
8975 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008976 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008977 }
8978 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008979 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008980}
8981
Trond Myklebust43912bb2016-09-22 13:38:56 -04008982static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8983 int err, struct nfs4_exception *exception)
8984{
8985 exception->retry = 0;
8986 switch(err) {
8987 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008988 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008989 nfs4_handle_exception(server, err, exception);
8990 break;
8991 case -NFS4ERR_BADSESSION:
8992 case -NFS4ERR_BADSLOT:
8993 case -NFS4ERR_BAD_HIGH_SLOT:
8994 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8995 case -NFS4ERR_DEADSESSION:
8996 nfs4_do_handle_exception(server, err, exception);
8997 }
8998}
8999
Chuck Lever38527b12012-07-11 16:30:23 -04009000/**
9001 * nfs41_test_stateid - perform a TEST_STATEID operation
9002 *
9003 * @server: server / transport on which to perform the operation
9004 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009005 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009006 *
9007 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9008 * Otherwise a negative NFS4ERR value is returned if the operation
9009 * failed or the state ID is not currently valid.
9010 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009011static int nfs41_test_stateid(struct nfs_server *server,
9012 nfs4_stateid *stateid,
9013 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009014{
9015 struct nfs4_exception exception = { };
9016 int err;
9017 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009018 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009019 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009020 } while (exception.retry);
9021 return err;
9022}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009023
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009024struct nfs_free_stateid_data {
9025 struct nfs_server *server;
9026 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009027 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009028};
9029
9030static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9031{
9032 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009033 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009034 &data->args.seq_args,
9035 &data->res.seq_res,
9036 task);
9037}
9038
9039static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9040{
9041 struct nfs_free_stateid_data *data = calldata;
9042
9043 nfs41_sequence_done(task, &data->res.seq_res);
9044
9045 switch (task->tk_status) {
9046 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009047 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 rpc_restart_call_prepare(task);
9049 }
9050}
9051
9052static void nfs41_free_stateid_release(void *calldata)
9053{
9054 kfree(calldata);
9055}
9056
Trond Myklebust17f26b12013-08-21 15:48:42 -04009057static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009058 .rpc_call_prepare = nfs41_free_stateid_prepare,
9059 .rpc_call_done = nfs41_free_stateid_done,
9060 .rpc_release = nfs41_free_stateid_release,
9061};
9062
9063static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009064 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009066 bool privileged)
9067{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009068 struct rpc_message msg = {
9069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009070 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009071 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009072 struct rpc_task_setup task_setup = {
9073 .rpc_client = server->client,
9074 .rpc_message = &msg,
9075 .callback_ops = &nfs41_free_stateid_ops,
9076 .flags = RPC_TASK_ASYNC,
9077 };
9078 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009079
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009080 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9081 &task_setup.rpc_client, &msg);
9082
Chuck Lever38527b12012-07-11 16:30:23 -04009083 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009084 data = kmalloc(sizeof(*data), GFP_NOFS);
9085 if (!data)
9086 return ERR_PTR(-ENOMEM);
9087 data->server = server;
9088 nfs4_stateid_copy(&data->args.stateid, stateid);
9089
9090 task_setup.callback_data = data;
9091
9092 msg.rpc_argp = &data->args;
9093 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009094 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009095 if (privileged)
9096 nfs4_set_sequence_privileged(&data->args.seq_args);
9097
9098 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009099}
9100
Chuck Lever38527b12012-07-11 16:30:23 -04009101/**
9102 * nfs41_free_stateid - perform a FREE_STATEID operation
9103 *
9104 * @server: server / transport on which to perform the operation
9105 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009106 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009107 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009108 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009110 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009111static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 const nfs4_stateid *stateid,
9113 struct rpc_cred *cred,
9114 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009115{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009116 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009117
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009118 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009119 if (IS_ERR(task))
9120 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009121 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009122 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009123}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009124
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009125static void
9126nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009127{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009128 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009129
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009130 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009131 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009132}
9133
Trond Myklebust36281ca2012-03-04 18:13:56 -05009134static bool nfs41_match_stateid(const nfs4_stateid *s1,
9135 const nfs4_stateid *s2)
9136{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009137 if (s1->type != s2->type)
9138 return false;
9139
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009140 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009141 return false;
9142
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009143 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009145
Anna Schumaker045c5512017-01-11 16:59:48 -05009146 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009147}
9148
Andy Adamson557134a2009-04-01 09:21:53 -04009149#endif /* CONFIG_NFS_V4_1 */
9150
Trond Myklebust36281ca2012-03-04 18:13:56 -05009151static bool nfs4_match_stateid(const nfs4_stateid *s1,
9152 const nfs4_stateid *s2)
9153{
Trond Myklebustf597c532012-03-04 18:13:56 -05009154 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009155}
9156
9157
Trond Myklebust17280172012-03-11 13:11:00 -04009158static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009159 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009160 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 .recover_open = nfs4_open_reclaim,
9162 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009163 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009164 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165};
9166
Andy Adamson591d71c2009-04-01 09:22:47 -04009167#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009168static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009169 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9170 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9171 .recover_open = nfs4_open_reclaim,
9172 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009173 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009174 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009175 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009176};
9177#endif /* CONFIG_NFS_V4_1 */
9178
Trond Myklebust17280172012-03-11 13:11:00 -04009179static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009180 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009181 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009182 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009184 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185};
9186
Andy Adamson591d71c2009-04-01 09:22:47 -04009187#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009188static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009189 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9190 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009191 .recover_open = nfs41_open_expired,
9192 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009193 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009194};
9195#endif /* CONFIG_NFS_V4_1 */
9196
Trond Myklebust17280172012-03-11 13:11:00 -04009197static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009198 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009199 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009200 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009201};
9202
9203#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009204static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009205 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009206 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009207 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009208};
9209#endif
9210
Chuck Leverec011fe2013-10-17 14:12:39 -04009211static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009212 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009213 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009214};
9215
9216#if defined(CONFIG_NFS_V4_1)
9217static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009218 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009219 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009220};
9221#endif /* CONFIG_NFS_V4_1 */
9222
Trond Myklebust97dc1352010-06-16 09:52:26 -04009223static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9224 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009225 .init_caps = NFS_CAP_READDIRPLUS
9226 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009227 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009228 .init_client = nfs40_init_client,
9229 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009230 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009231 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009232 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009233 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009234 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009235 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009236 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9237 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9238 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009239 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009240};
9241
9242#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009243static struct nfs_seqid *
9244nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9245{
9246 return NULL;
9247}
9248
Trond Myklebust97dc1352010-06-16 09:52:26 -04009249static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9250 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009251 .init_caps = NFS_CAP_READDIRPLUS
9252 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009253 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009254 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009255 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009256 .init_client = nfs41_init_client,
9257 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009258 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009259 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009260 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009261 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009262 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009263 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009264 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009265 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9266 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9267 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009268 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009269};
9270#endif
9271
Steve Dickson42c2c422013-05-22 12:50:38 -04009272#if defined(CONFIG_NFS_V4_2)
9273static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9274 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009275 .init_caps = NFS_CAP_READDIRPLUS
9276 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009277 | NFS_CAP_POSIX_LOCK
9278 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009279 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009280 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009281 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009282 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009283 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009284 | NFS_CAP_LAYOUTSTATS
9285 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009286 .init_client = nfs41_init_client,
9287 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009288 .match_stateid = nfs41_match_stateid,
9289 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009290 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009291 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009292 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009293 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009294 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009295 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9296 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9297 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009298 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009299};
9300#endif
9301
Trond Myklebust97dc1352010-06-16 09:52:26 -04009302const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9303 [0] = &nfs_v4_0_minor_ops,
9304#if defined(CONFIG_NFS_V4_1)
9305 [1] = &nfs_v4_1_minor_ops,
9306#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009307#if defined(CONFIG_NFS_V4_2)
9308 [2] = &nfs_v4_2_minor_ops,
9309#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009310};
9311
Trond Myklebust13997822016-07-24 17:10:52 -04009312static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009313{
9314 ssize_t error, error2;
9315
9316 error = generic_listxattr(dentry, list, size);
9317 if (error < 0)
9318 return error;
9319 if (list) {
9320 list += error;
9321 size -= error;
9322 }
9323
9324 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9325 if (error2 < 0)
9326 return error2;
9327 return error + error2;
9328}
9329
Trond Myklebust17f26b12013-08-21 15:48:42 -04009330static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009331 .create = nfs_create,
9332 .lookup = nfs_lookup,
9333 .atomic_open = nfs_atomic_open,
9334 .link = nfs_link,
9335 .unlink = nfs_unlink,
9336 .symlink = nfs_symlink,
9337 .mkdir = nfs_mkdir,
9338 .rmdir = nfs_rmdir,
9339 .mknod = nfs_mknod,
9340 .rename = nfs_rename,
9341 .permission = nfs_permission,
9342 .getattr = nfs_getattr,
9343 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009344 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009345};
9346
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009347static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009348 .permission = nfs_permission,
9349 .getattr = nfs_getattr,
9350 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009351 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009352};
9353
David Howells509de812006-08-22 20:06:11 -04009354const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355 .version = 4, /* protocol version */
9356 .dentry_ops = &nfs4_dentry_operations,
9357 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009358 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009359 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009361 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009362 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 .getattr = nfs4_proc_getattr,
9364 .setattr = nfs4_proc_setattr,
9365 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009366 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 .access = nfs4_proc_access,
9368 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369 .create = nfs4_proc_create,
9370 .remove = nfs4_proc_remove,
9371 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009372 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009374 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009375 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009376 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 .link = nfs4_proc_link,
9378 .symlink = nfs4_proc_symlink,
9379 .mkdir = nfs4_proc_mkdir,
9380 .rmdir = nfs4_proc_remove,
9381 .readdir = nfs4_proc_readdir,
9382 .mknod = nfs4_proc_mknod,
9383 .statfs = nfs4_proc_statfs,
9384 .fsinfo = nfs4_proc_fsinfo,
9385 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009386 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009388 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009390 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009392 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009394 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009395 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009397 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009398 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009399 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009400 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009401 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009402 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009403 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009404 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009405 .create_server = nfs4_create_server,
9406 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407};
9408
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009409static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009410 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009411 .list = nfs4_xattr_list_nfs4_acl,
9412 .get = nfs4_xattr_get_nfs4_acl,
9413 .set = nfs4_xattr_set_nfs4_acl,
9414};
9415
9416const struct xattr_handler *nfs4_xattr_handlers[] = {
9417 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009418#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9419 &nfs4_xattr_nfs4_label_handler,
9420#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009421 NULL
9422};
9423
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424/*
9425 * Local variables:
9426 * c-basic-offset: 8
9427 * End:
9428 */