blob: f6a362ffcbd466d18dbb8f21e8e3a0bfc1fbc23e [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{
Al Viro0dbb4c62006-10-19 23:28:49 -0700278 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000281 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
283 return;
284 }
285
286 readdir->cookie = 0;
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
288 if (cookie == 2)
289 return;
290
291 /*
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
296 * instead of 1 or 2.
297 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800298 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (cookie == 0) {
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
318 p++;
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800326 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust26d36302016-09-22 13:39:05 -0400329static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
332{
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
334
335 ops->test_and_free_expired(server, stateid, cred);
336}
337
338static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
341{
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
344}
345
346static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
349{
350 nfs4_stateid tmp;
351
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
354}
355
NeilBrown8478eaa2014-09-18 16:09:27 +1000356static long nfs4_update_delay(long *timeout)
357{
358 long ret;
359 if (!timeout)
360 return NFS4_POLL_RETRY_MAX;
361 if (*timeout <= 0)
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
365 ret = *timeout;
366 *timeout <<= 1;
367 return ret;
368}
369
Trond Myklebust65de8722008-12-23 15:21:44 -0500370static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
371{
372 int res = 0;
373
374 might_sleep();
375
NeilBrown8478eaa2014-09-18 16:09:27 +1000376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 if (fatal_signal_pending(current))
379 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 return res;
381}
382
383/* This is the error handling routine for processes that are allowed
384 * to sleep.
385 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400386static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500388{
389 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500390 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400391 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500393 int ret = errorcode;
394
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400395 exception->delay = 0;
396 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400398
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
401
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 switch(errorcode) {
403 case 0:
404 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
411 stateid);
412 goto wait_on_recovery;
413 }
414 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400415 if (inode) {
416 int err;
417
418 err = nfs_async_inode_return_delegation(inode,
419 stateid);
420 if (err == 0)
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
424 break;
425 }
426 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500427 if (state == NULL)
428 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400429 ret = nfs4_schedule_stateid_recovery(server, state);
430 if (ret < 0)
431 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500434 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400437 case -NFS4ERR_MOVED:
438 ret = nfs4_schedule_migration_recovery(server);
439 if (ret < 0)
440 break;
441 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500445#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
454 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400456 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500457#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
461 * fail
462 */
463 ret = -EBUSY;
464 break;
465 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400467 nfs_inc_server_stats(server, NFSIOS_DELAY);
468 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400469 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400470 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400471 exception->delay = 1;
472 return 0;
473
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400474 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800477 break;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
486 "uid/gids. "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
489 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500490 }
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500493wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400494 exception->recovering = 1;
495 return 0;
496}
497
498/* This is the error handling routine for processes that are allowed
499 * to sleep.
500 */
501int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
502{
503 struct nfs_client *clp = server->nfs_client;
504 int ret;
505
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
509 goto out_retry;
510 }
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 return -EIO;
515 goto out_retry;
516 }
517 return ret;
518out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500519 if (ret == 0)
520 exception->retry = 1;
521 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500522}
523
Trond Myklebust037fc982015-09-20 15:51:00 -0400524static int
525nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
527{
528 struct nfs_client *clp = server->nfs_client;
529 int ret;
530
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
534 goto out_retry;
535 }
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
540 goto out_retry;
541 }
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
543 ret = -EIO;
544 return ret;
545out_retry:
546 if (ret == 0)
547 exception->retry = 1;
548 return ret;
549}
550
551static int
552nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
554{
555 struct nfs4_exception exception = {
556 .state = state,
557 };
558
559 if (task->tk_status >= 0)
560 return 0;
561 if (timeout)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
564 task->tk_status,
565 &exception);
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
568 if (exception.retry)
569 return -EAGAIN;
570 return 0;
571}
572
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400573/*
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
576 */
577static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
578{
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
580
581 if (flavor == RPC_AUTH_GSS_KRB5I ||
582 flavor == RPC_AUTH_GSS_KRB5P)
583 return true;
584
585 return false;
586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400625static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400626{
627 struct nfs4_slot *slot = res->sr_slot;
628 struct nfs4_slot_table *tbl;
629
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 tbl = slot->table;
631 spin_lock(&tbl->slot_tbl_lock);
632 if (!nfs41_wake_and_assign_slot(tbl, slot))
633 nfs4_free_slot(tbl, slot);
634 spin_unlock(&tbl->slot_tbl_lock);
635
636 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400637}
638
639static int nfs40_sequence_done(struct rpc_task *task,
640 struct nfs4_sequence_res *res)
641{
642 if (res->sr_slot != NULL)
643 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400644 return 1;
645}
646
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400647#if defined(CONFIG_NFS_V4_1)
648
Trond Myklebustd185a332010-06-16 09:52:25 -0400649static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400650{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500651 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400652 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500653 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500654 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400655
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500656 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500657 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500658
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400659 /* Bump the slot sequence number */
660 if (slot->seq_done)
661 slot->seq_nr++;
662 slot->seq_done = 0;
663
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500664 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500665 /* Be nice to the server: try to ensure that the last transmitted
666 * value for highest_user_slotid <= target_highest_slotid
667 */
668 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
669 send_new_highest_used_slotid = true;
670
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500671 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500672 send_new_highest_used_slotid = false;
673 goto out_unlock;
674 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500675 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500676
677 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
678 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500679out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500680 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400681 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500682 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400683 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400684 if (waitqueue_active(&tbl->slot_waitq))
685 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400686}
687
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400688static int nfs41_sequence_process(struct rpc_task *task,
689 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400690{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500691 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500692 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400693 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500694 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500695 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 if (slot == NULL)
698 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400699 /* don't increment the sequence number if the task wasn't sent */
700 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400701 goto out;
702
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500703 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500704
Trond Myklebustac20d162012-12-15 15:36:07 -0500705 if (slot->interrupted) {
706 slot->interrupted = 0;
707 interrupted = true;
708 }
709
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400710 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500711 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 switch (res->sr_status) {
713 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400714 /* If previous op on slot was interrupted and we reused
715 * the seq# and got a reply from the cache, then retry
716 */
717 if (task->tk_status == -EREMOTEIO && interrupted) {
718 ++slot->seq_nr;
719 goto retry_nowait;
720 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400721 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400722 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500724 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500725 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400726 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
727 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500728 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400729 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500730 case 1:
731 /*
732 * sr_status remains 1 if an RPC level error occurred.
733 * The server may or may not have processed the sequence
734 * operation..
735 * Mark the slot as having hosted an interrupted RPC call.
736 */
737 slot->interrupted = 1;
738 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400739 case -NFS4ERR_DELAY:
740 /* The server detected a resend of the RPC call and
741 * returned NFS4ERR_DELAY as per Section 2.10.6.2
742 * of RFC5661.
743 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500744 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500746 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500747 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400748 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500749 case -NFS4ERR_BADSLOT:
750 /*
751 * The slot id we used was probably retired. Try again
752 * using a different slot id.
753 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500754 goto retry_nowait;
755 case -NFS4ERR_SEQ_MISORDERED:
756 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500757 * Was the last operation on this sequence interrupted?
758 * If so, retry after bumping the sequence number.
759 */
760 if (interrupted) {
761 ++slot->seq_nr;
762 goto retry_nowait;
763 }
764 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500765 * Could this slot have been previously retired?
766 * If so, then the server may be expecting seq_nr = 1!
767 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500768 if (slot->seq_nr != 1) {
769 slot->seq_nr = 1;
770 goto retry_nowait;
771 }
772 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500773 case -NFS4ERR_SEQ_FALSE_RETRY:
774 ++slot->seq_nr;
775 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500776 case -NFS4ERR_DEADSESSION:
777 case -NFS4ERR_BADSESSION:
778 nfs4_schedule_session_recovery(session, res->sr_status);
779 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400780 default:
781 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400782 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400783 }
784out:
785 /* The session may be reset by one of the error handlers. */
786 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500787out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500788 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500789retry_nowait:
790 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400791 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500792 task->tk_status = 0;
793 ret = 0;
794 }
795 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400796out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400797 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 goto out;
799 rpc_delay(task, NFS4_POLL_RETRY_MAX);
800 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400802
803int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
804{
805 if (!nfs41_sequence_process(task, res))
806 return 0;
807 if (res->sr_slot != NULL)
808 nfs41_sequence_free_slot(res);
809 return 1;
810
811}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500812EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400813
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400814static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
815{
816 if (res->sr_slot == NULL)
817 return 1;
818 if (res->sr_slot->table->session != NULL)
819 return nfs41_sequence_process(task, res);
820 return nfs40_sequence_done(task, res);
821}
822
823static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
824{
825 if (res->sr_slot != NULL) {
826 if (res->sr_slot->table->session != NULL)
827 nfs41_sequence_free_slot(res);
828 else
829 nfs40_sequence_free_slot(res);
830 }
831}
832
Peng Tao2c4b1312014-06-11 05:24:16 +0800833int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400834{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500835 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400837 if (!res->sr_slot->table->session)
838 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400840}
Peng Tao2c4b1312014-06-11 05:24:16 +0800841EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842
Andy Adamsonce5039c2009-04-01 09:22:13 -0400843static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
844{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400845 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846
Trond Myklebust035168ab2010-06-16 09:52:26 -0400847 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
848
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500849 nfs4_setup_sequence(data->seq_server->nfs_client,
850 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400851}
852
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
854{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400855 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400856
Trond Myklebust14516c32010-07-31 14:29:06 -0400857 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400858}
859
Trond Myklebust17280172012-03-11 13:11:00 -0400860static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400861 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400862 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400863};
864
Chuck Lever3bd23842013-08-09 12:49:19 -0400865#else /* !CONFIG_NFS_V4_1 */
866
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400867static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
868{
869 return nfs40_sequence_done(task, res);
870}
871
872static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
873{
874 if (res->sr_slot != NULL)
875 nfs40_sequence_free_slot(res);
876}
877
Peng Tao2c4b1312014-06-11 05:24:16 +0800878int nfs4_sequence_done(struct rpc_task *task,
879 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400880{
Chuck Lever3bd23842013-08-09 12:49:19 -0400881 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400882}
Peng Tao2c4b1312014-06-11 05:24:16 +0800883EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400884
885#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400886
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887int nfs4_setup_sequence(const struct nfs_client *client,
888 struct nfs4_sequence_args *args,
889 struct nfs4_sequence_res *res,
890 struct rpc_task *task)
891{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500892 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500893 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500894 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500895
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500896 /* slot already allocated? */
897 if (res->sr_slot != NULL)
898 goto out_start;
899
Anna Schumaker76ee0352017-01-10 16:49:31 -0500900 if (session) {
901 tbl = &session->fc_slot_table;
902 task->tk_timeout = 0;
903 }
904
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500905 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500906 /* The state manager will wait until the slot table is empty */
907 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
908 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500909
Anna Schumaker3d358082017-01-11 10:54:04 -0500910 slot = nfs4_alloc_slot(tbl);
911 if (IS_ERR(slot)) {
912 /* Try again in 1/4 second */
913 if (slot == ERR_PTR(-ENOMEM))
914 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500915 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500916 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500917 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500918
Anna Schumaker3d358082017-01-11 10:54:04 -0500919 slot->privileged = args->sa_privileged ? 1 : 0;
920 args->sa_slot = slot;
921
922 res->sr_slot = slot;
923 if (session) {
924 res->sr_timestamp = jiffies;
925 res->sr_status_flags = 0;
926 res->sr_status = 1;
927#ifdef CONFIG_NFS_V4_1
928 trace_nfs4_setup_sequence(session, args);
929#endif /* CONFIG_NFS_V4_1 */
930 }
931
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500932out_start:
933 rpc_call_start(task);
934 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500935
936out_sleep:
937 if (args->sa_privileged)
938 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
939 NULL, RPC_PRIORITY_PRIVILEGED);
940 else
941 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
942 spin_unlock(&tbl->slot_tbl_lock);
943 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500944}
945EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
946
Chuck Lever9915ea72013-08-09 12:48:27 -0400947static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
948{
949 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500950 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400951 data->seq_args, data->seq_res, task);
952}
953
954static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
955{
956 struct nfs4_call_sync_data *data = calldata;
957 nfs4_sequence_done(task, data->seq_res);
958}
959
960static const struct rpc_call_ops nfs40_call_sync_ops = {
961 .rpc_call_prepare = nfs40_call_sync_prepare,
962 .rpc_call_done = nfs40_call_sync_done,
963};
964
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400965static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400966 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 struct nfs4_sequence_args *args,
969 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400970{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 int ret;
972 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400973 struct nfs_client *clp = server->nfs_client;
974 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100975 .seq_server = server,
976 .seq_args = args,
977 .seq_res = res,
978 };
979 struct rpc_task_setup task_setup = {
980 .rpc_client = clnt,
981 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400982 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100983 .callback_data = &data
984 };
985
986 task = rpc_run_task(&task_setup);
987 if (IS_ERR(task))
988 ret = PTR_ERR(task);
989 else {
990 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400991 rpc_put_task(task);
992 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993 return ret;
994}
995
Bryan Schumaker7c513052011-03-24 17:12:24 +0000996int nfs4_call_sync(struct rpc_clnt *clnt,
997 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000998 struct rpc_message *msg,
999 struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 int cache_reply)
1002{
Chuck Levera9c92d62013-08-09 12:48:18 -04001003 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001004 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001007static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1008 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct nfs_inode *nfsi = NFS_I(dir);
1011
1012 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001013 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001014 if (cinfo->atomic && cinfo->before == dir->i_version) {
1015 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1016 nfsi->attrtimeo_timestamp = jiffies;
1017 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001019 if (cinfo->before != dir->i_version)
1020 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1021 NFS_INO_INVALID_ACL;
1022 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001023 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001024 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001025 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001026 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 spin_unlock(&dir->i_lock);
1028}
1029
1030struct nfs4_opendata {
1031 struct kref kref;
1032 struct nfs_openargs o_arg;
1033 struct nfs_openres o_res;
1034 struct nfs_open_confirmargs c_arg;
1035 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001036 struct nfs4_string owner_name;
1037 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001038 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001040 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001042 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct nfs4_state_owner *owner;
1044 struct nfs4_state *state;
1045 struct iattr attrs;
1046 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001047 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001048 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001049 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001050 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int cancelled;
1052};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001053
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001054static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1055 int err, struct nfs4_exception *exception)
1056{
1057 if (err != -EINVAL)
1058 return false;
1059 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1060 return false;
1061 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1062 exception->retry = 1;
1063 return true;
1064}
1065
Trond Myklebust6ae37332015-01-30 14:21:14 -05001066static u32
1067nfs4_map_atomic_open_share(struct nfs_server *server,
1068 fmode_t fmode, int openflags)
1069{
1070 u32 res = 0;
1071
1072 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1073 case FMODE_READ:
1074 res = NFS4_SHARE_ACCESS_READ;
1075 break;
1076 case FMODE_WRITE:
1077 res = NFS4_SHARE_ACCESS_WRITE;
1078 break;
1079 case FMODE_READ|FMODE_WRITE:
1080 res = NFS4_SHARE_ACCESS_BOTH;
1081 }
1082 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1083 goto out;
1084 /* Want no delegation if we're using O_DIRECT */
1085 if (openflags & O_DIRECT)
1086 res |= NFS4_SHARE_WANT_NO_DELEG;
1087out:
1088 return res;
1089}
1090
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001091static enum open_claim_type4
1092nfs4_map_atomic_open_claim(struct nfs_server *server,
1093 enum open_claim_type4 claim)
1094{
1095 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1096 return claim;
1097 switch (claim) {
1098 default:
1099 return claim;
1100 case NFS4_OPEN_CLAIM_FH:
1101 return NFS4_OPEN_CLAIM_NULL;
1102 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1103 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1104 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1105 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1106 }
1107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110{
1111 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001112 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001113 p->o_res.seqid = p->o_arg.seqid;
1114 p->c_res.seqid = p->c_arg.seqid;
1115 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001116 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001117 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001118 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119}
1120
Al Viro82a2c1b2011-06-22 18:30:55 -04001121static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001122 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001123 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001124 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001125 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001126 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001127{
Al Viro82a2c1b2011-06-22 18:30:55 -04001128 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001129 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001130 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001131 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001132 struct nfs4_opendata *p;
1133
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001134 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001135 if (p == NULL)
1136 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001137
1138 p->f_label = nfs4_label_alloc(server, gfp_mask);
1139 if (IS_ERR(p->f_label))
1140 goto err_free_p;
1141
Kinglong Meea49c2692015-07-27 15:31:38 +08001142 p->a_label = nfs4_label_alloc(server, gfp_mask);
1143 if (IS_ERR(p->a_label))
1144 goto err_free_f;
1145
Trond Myklebust63f5f792015-01-23 19:19:25 -05001146 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1147 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001148 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001149 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001150 nfs_sb_active(dentry->d_sb);
1151 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001152 p->dir = parent;
1153 p->owner = sp;
1154 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 p->o_arg.open_flags = flags;
1156 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001157 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001158 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001159 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1160 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001161 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1162 * will return permission denied for all bits until close */
1163 if (!(flags & O_EXCL)) {
1164 /* ask server to check for all possible rights as results
1165 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001166 switch (p->o_arg.claim) {
1167 default:
1168 break;
1169 case NFS4_OPEN_CLAIM_NULL:
1170 case NFS4_OPEN_CLAIM_FH:
1171 p->o_arg.access = NFS4_ACCESS_READ |
1172 NFS4_ACCESS_MODIFY |
1173 NFS4_ACCESS_EXTEND |
1174 NFS4_ACCESS_EXECUTE;
1175 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001176 }
David Howells7539bba2006-08-22 20:06:09 -04001177 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001178 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1179 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001180 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001181 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001182 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001183 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001184 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001185 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001186 case NFS4_OPEN_CLAIM_NULL:
1187 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1188 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1189 p->o_arg.fh = NFS_FH(dir);
1190 break;
1191 case NFS4_OPEN_CLAIM_PREVIOUS:
1192 case NFS4_OPEN_CLAIM_FH:
1193 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1194 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001195 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001196 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001197 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001198 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001199
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 p->o_arg.u.attrs = &p->attrs;
1201 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001202
1203 verf[0] = jiffies;
1204 verf[1] = current->pid;
1205 memcpy(p->o_arg.u.verifier.data, verf,
1206 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001208 p->c_arg.fh = &p->o_res.fh;
1209 p->c_arg.stateid = &p->o_res.stateid;
1210 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001212 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001214
1215err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001216 nfs4_label_free(p->a_label);
1217err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001218 nfs4_label_free(p->f_label);
1219err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 kfree(p);
1221err:
1222 dput(parent);
1223 return NULL;
1224}
1225
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001226static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001228 struct nfs4_opendata *p = container_of(kref,
1229 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001230 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001231
1232 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001233 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001234 if (p->state != NULL)
1235 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001236 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001237
Kinglong Meea49c2692015-07-27 15:31:38 +08001238 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001239 nfs4_label_free(p->f_label);
1240
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001241 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001242 dput(p->dentry);
1243 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001244 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001245 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001246 kfree(p);
1247}
1248
1249static void nfs4_opendata_put(struct nfs4_opendata *p)
1250{
1251 if (p != NULL)
1252 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001253}
1254
Trond Myklebust06f814a2006-01-03 09:55:07 +01001255static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1256{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001257 int ret;
1258
Trond Myklebust06f814a2006-01-03 09:55:07 +01001259 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001260 return ret;
1261}
1262
Trond Myklebust24311f82015-09-20 10:50:17 -04001263static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1264 fmode_t fmode)
1265{
1266 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1267 case FMODE_READ|FMODE_WRITE:
1268 return state->n_rdwr != 0;
1269 case FMODE_WRITE:
1270 return state->n_wronly != 0;
1271 case FMODE_READ:
1272 return state->n_rdonly != 0;
1273 }
1274 WARN_ON_ONCE(1);
1275 return false;
1276}
1277
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001279{
1280 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281
Trond Myklebust536e43d2012-01-17 22:04:26 -05001282 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 goto out;
1284 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001285 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001286 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1287 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001288 break;
1289 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001290 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1291 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001292 break;
1293 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001294 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1295 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001296 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001297out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001298 return ret;
1299}
1300
Trond Myklebust2a606182015-08-19 22:30:00 -05001301static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1302 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001304 if (delegation == NULL)
1305 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001307 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001308 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1309 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001310 switch (claim) {
1311 case NFS4_OPEN_CLAIM_NULL:
1312 case NFS4_OPEN_CLAIM_FH:
1313 break;
1314 case NFS4_OPEN_CLAIM_PREVIOUS:
1315 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1316 break;
1317 default:
1318 return 0;
1319 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001320 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001321 return 1;
1322}
1323
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001324static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001325{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001327 case FMODE_WRITE:
1328 state->n_wronly++;
1329 break;
1330 case FMODE_READ:
1331 state->n_rdonly++;
1332 break;
1333 case FMODE_READ|FMODE_WRITE:
1334 state->n_rdwr++;
1335 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001336 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001337}
1338
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001339#ifdef CONFIG_NFS_V4_1
1340static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1341{
1342 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1343 return true;
1344 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1345 return true;
1346 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1347 return true;
1348 return false;
1349}
1350#endif /* CONFIG_NFS_V4_1 */
1351
Trond Myklebust4f14c192014-02-12 19:15:06 -05001352static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001353{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001354 struct nfs_client *clp = state->owner->so_server->nfs_client;
1355 bool need_recover = false;
1356
1357 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1358 need_recover = true;
1359 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1360 need_recover = true;
1361 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1362 need_recover = true;
1363 if (need_recover)
1364 nfs4_state_mark_reclaim_nograce(clp, state);
1365}
1366
Trond Myklebuste999e802014-02-10 18:20:47 -05001367static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001368 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001369{
1370 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1371 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001372 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001373 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001374 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001375 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001376 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001377 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1378 return true;
1379 return false;
1380}
1381
Trond Myklebustf95549c2015-01-23 18:06:09 -05001382static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1383{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001384 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1385 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001386 if (state->n_wronly)
1387 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1388 if (state->n_rdonly)
1389 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1390 if (state->n_rdwr)
1391 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001392 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001393}
1394
Trond Myklebust226056c2014-02-11 10:41:07 -05001395static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1396 nfs4_stateid *stateid, fmode_t fmode)
1397{
1398 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1399 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1400 case FMODE_WRITE:
1401 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1402 break;
1403 case FMODE_READ:
1404 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1405 break;
1406 case 0:
1407 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1408 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1409 clear_bit(NFS_OPEN_STATE, &state->flags);
1410 }
1411 if (stateid == NULL)
1412 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001413 /* Handle OPEN+OPEN_DOWNGRADE races */
1414 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1415 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001416 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001418 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001419 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001420 nfs4_stateid_copy(&state->stateid, stateid);
1421 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001422}
1423
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001424static void nfs_clear_open_stateid(struct nfs4_state *state,
1425 nfs4_stateid *arg_stateid,
1426 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001427{
1428 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001429 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1430 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1431 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001432 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001433 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1434 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001435}
1436
Trond Myklebust1393d962016-09-22 13:39:13 -04001437static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1438 const nfs4_stateid *stateid, fmode_t fmode,
1439 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001440{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001442 case FMODE_READ:
1443 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1444 break;
1445 case FMODE_WRITE:
1446 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1447 break;
1448 case FMODE_READ|FMODE_WRITE:
1449 set_bit(NFS_O_RDWR_STATE, &state->flags);
1450 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001451 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001452 return;
1453 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1454 nfs4_stateid_copy(&state->stateid, stateid);
1455 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001456}
1457
Trond Myklebust1393d962016-09-22 13:39:13 -04001458static void __update_open_stateid(struct nfs4_state *state,
1459 const nfs4_stateid *open_stateid,
1460 const nfs4_stateid *deleg_stateid,
1461 fmode_t fmode,
1462 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001464 /*
1465 * Protect the call to nfs4_state_set_mode_locked and
1466 * serialise the stateid update
1467 */
Andrew Elble361cad32015-12-02 09:20:57 -05001468 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001469 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001470 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001471 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001472 set_bit(NFS_DELEGATED_STATE, &state->flags);
1473 }
1474 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001475 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001476 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001478 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Trond Myklebust1393d962016-09-22 13:39:13 -04001481static int update_open_stateid(struct nfs4_state *state,
1482 const nfs4_stateid *open_stateid,
1483 const nfs4_stateid *delegation,
1484 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001485{
Trond Myklebust1393d962016-09-22 13:39:13 -04001486 struct nfs_server *server = NFS_SERVER(state->inode);
1487 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001488 struct nfs_inode *nfsi = NFS_I(state->inode);
1489 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001490 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001491 int ret = 0;
1492
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001494
1495 rcu_read_lock();
1496 deleg_cur = rcu_dereference(nfsi->delegation);
1497 if (deleg_cur == NULL)
1498 goto no_delegation;
1499
1500 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001501 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001502 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001504 goto no_delegation_unlock;
1505
1506 if (delegation == NULL)
1507 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001508 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001509 goto no_delegation_unlock;
1510
Trond Myklebustb7391f42008-12-23 15:21:52 -05001511 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001512 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1513 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001514 ret = 1;
1515no_delegation_unlock:
1516 spin_unlock(&deleg_cur->lock);
1517no_delegation:
1518 rcu_read_unlock();
1519
1520 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001521 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001522 ret = 1;
1523 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001524 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001525 nfs4_schedule_state_manager(clp);
1526 if (freeme.type != 0)
1527 nfs4_test_and_free_stateid(server, &freeme,
1528 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001529
1530 return ret;
1531}
1532
Trond Myklebust39071e62015-01-24 15:07:56 -05001533static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1534 const nfs4_stateid *stateid)
1535{
1536 struct nfs4_state *state = lsp->ls_state;
1537 bool ret = false;
1538
1539 spin_lock(&state->state_lock);
1540 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1541 goto out_noupdate;
1542 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1543 goto out_noupdate;
1544 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1545 ret = true;
1546out_noupdate:
1547 spin_unlock(&state->state_lock);
1548 return ret;
1549}
Trond Myklebust34310432008-12-23 15:21:38 -05001550
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001551static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001552{
1553 struct nfs_delegation *delegation;
1554
1555 rcu_read_lock();
1556 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001557 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 rcu_read_unlock();
1559 return;
1560 }
1561 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001562 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563}
1564
Trond Myklebust6ee41262007-07-08 14:11:36 -04001565static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001566{
1567 struct nfs4_state *state = opendata->state;
1568 struct nfs_inode *nfsi = NFS_I(state->inode);
1569 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001570 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001571 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001572 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001573 nfs4_stateid stateid;
1574 int ret = -EAGAIN;
1575
Trond Myklebustaac00a82007-07-05 19:02:21 -04001576 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001577 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001578 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001579 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001581 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001582 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001583 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001584 rcu_read_lock();
1585 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001586 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001587 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001588 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001589 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001591 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001592 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001593 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001594 if (!opendata->is_recover) {
1595 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1596 if (ret != 0)
1597 goto out;
1598 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001599 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001600
1601 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001602 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001603 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001604 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001605out:
1606 return ERR_PTR(ret);
1607out_return_state:
1608 atomic_inc(&state->count);
1609 return state;
1610}
1611
Andy Adamsone23008e2012-10-02 21:07:32 -04001612static void
1613nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1614{
1615 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1616 struct nfs_delegation *delegation;
1617 int delegation_flags = 0;
1618
1619 rcu_read_lock();
1620 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1621 if (delegation)
1622 delegation_flags = delegation->flags;
1623 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001624 switch (data->o_arg.claim) {
1625 default:
1626 break;
1627 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1628 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001629 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1630 "returning a delegation for "
1631 "OPEN(CLAIM_DELEGATE_CUR)\n",
1632 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001633 return;
1634 }
1635 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001636 nfs_inode_set_delegation(state->inode,
1637 data->owner->so_cred,
1638 &data->o_res);
1639 else
1640 nfs_inode_reclaim_delegation(state->inode,
1641 data->owner->so_cred,
1642 &data->o_res);
1643}
1644
1645/*
1646 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1647 * and update the nfs4_state.
1648 */
1649static struct nfs4_state *
1650_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1651{
1652 struct inode *inode = data->state->inode;
1653 struct nfs4_state *state = data->state;
1654 int ret;
1655
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001656 if (!data->rpc_done) {
1657 if (data->rpc_status) {
1658 ret = data->rpc_status;
1659 goto err;
1660 }
1661 /* cached opens have already been processed */
1662 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001663 }
1664
Andy Adamsone23008e2012-10-02 21:07:32 -04001665 ret = nfs_refresh_inode(inode, &data->f_attr);
1666 if (ret)
1667 goto err;
1668
1669 if (data->o_res.delegation_type != 0)
1670 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001671update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001672 update_open_stateid(state, &data->o_res.stateid, NULL,
1673 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001674 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001675
1676 return state;
1677err:
1678 return ERR_PTR(ret);
1679
1680}
1681
1682static struct nfs4_state *
1683_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684{
1685 struct inode *inode;
1686 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001687 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001688
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001690 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001691 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 goto out;
1693 }
1694
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001695 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001697 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001698 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001699 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001700 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001701 goto err;
1702 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 state = nfs4_get_open_state(inode, data->owner);
1704 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001705 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001706 if (data->o_res.delegation_type != 0)
1707 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001708 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001709 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001711out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001712 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001713 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001714err_put_inode:
1715 iput(inode);
1716err:
1717 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001718}
1719
Andy Adamsone23008e2012-10-02 21:07:32 -04001720static struct nfs4_state *
1721nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1722{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001723 struct nfs4_state *ret;
1724
Andy Adamsone23008e2012-10-02 21:07:32 -04001725 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001726 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1727 else
1728 ret = _nfs4_opendata_to_nfs4_state(data);
1729 nfs4_sequence_free_slot(&data->o_res.seq_res);
1730 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001731}
1732
Trond Myklebust864472e2006-01-03 09:55:15 +01001733static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1734{
1735 struct nfs_inode *nfsi = NFS_I(state->inode);
1736 struct nfs_open_context *ctx;
1737
1738 spin_lock(&state->inode->i_lock);
1739 list_for_each_entry(ctx, &nfsi->open_files, list) {
1740 if (ctx->state != state)
1741 continue;
1742 get_nfs_open_context(ctx);
1743 spin_unlock(&state->inode->i_lock);
1744 return ctx;
1745 }
1746 spin_unlock(&state->inode->i_lock);
1747 return ERR_PTR(-ENOENT);
1748}
1749
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001750static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1751 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001752{
1753 struct nfs4_opendata *opendata;
1754
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001755 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001756 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001757 if (opendata == NULL)
1758 return ERR_PTR(-ENOMEM);
1759 opendata->state = state;
1760 atomic_inc(&state->count);
1761 return opendata;
1762}
1763
Trond Myklebust24311f82015-09-20 10:50:17 -04001764static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1765 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001766{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001767 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001768 int ret;
1769
Trond Myklebust24311f82015-09-20 10:50:17 -04001770 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001771 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001772 opendata->o_arg.open_flags = 0;
1773 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001774 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1775 NFS_SB(opendata->dentry->d_sb),
1776 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001777 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1778 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1779 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001780 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001781 if (ret != 0)
1782 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001783 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001784 if (IS_ERR(newstate))
1785 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001786 if (newstate != opendata->state)
1787 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001788 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001789 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001790}
1791
1792static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1793{
Trond Myklebust864472e2006-01-03 09:55:15 +01001794 int ret;
1795
Trond Myklebust4f14c192014-02-12 19:15:06 -05001796 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1797 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1798 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1799 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001800 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001801 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001802 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001803 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001804 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1805 if (ret != 0)
1806 return ret;
1807 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1808 if (ret != 0)
1809 return ret;
1810 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1811 if (ret != 0)
1812 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 /*
1814 * We may have performed cached opens for all three recoveries.
1815 * Check if we need to update the current stateid.
1816 */
1817 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001818 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001819 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001820 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001821 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001822 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001823 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001824 return 0;
1825}
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
1828 * OPEN_RECLAIM:
1829 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001831static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001833 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001834 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int status;
1837
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001838 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1839 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001840 if (IS_ERR(opendata))
1841 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001842 rcu_read_lock();
1843 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001844 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001845 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001846 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001847 opendata->o_arg.u.delegation_type = delegation_type;
1848 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001849 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return status;
1851}
1852
Trond Myklebust539cd032007-06-05 11:46:42 -04001853static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct nfs_server *server = NFS_SERVER(state->inode);
1856 struct nfs4_exception exception = { };
1857 int err;
1858 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001859 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001860 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001861 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1862 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001863 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001864 break;
1865 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } while (exception.retry);
1867 return err;
1868}
1869
Trond Myklebust864472e2006-01-03 09:55:15 +01001870static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1871{
1872 struct nfs_open_context *ctx;
1873 int ret;
1874
1875 ctx = nfs4_state_find_open_context(state);
1876 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001877 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001878 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001879 put_nfs_open_context(ctx);
1880 return ret;
1881}
1882
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001883static 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 -07001884{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001885 switch (err) {
1886 default:
1887 printk(KERN_ERR "NFS: %s: unhandled error "
1888 "%d.\n", __func__, err);
1889 case 0:
1890 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001891 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001892 case -ESTALE:
1893 break;
1894 case -NFS4ERR_BADSESSION:
1895 case -NFS4ERR_BADSLOT:
1896 case -NFS4ERR_BAD_HIGH_SLOT:
1897 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1898 case -NFS4ERR_DEADSESSION:
1899 set_bit(NFS_DELEGATED_STATE, &state->flags);
1900 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1901 return -EAGAIN;
1902 case -NFS4ERR_STALE_CLIENTID:
1903 case -NFS4ERR_STALE_STATEID:
1904 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001905 /* Don't recall a delegation if it was lost */
1906 nfs4_schedule_lease_recovery(server->nfs_client);
1907 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001908 case -NFS4ERR_MOVED:
1909 nfs4_schedule_migration_recovery(server);
1910 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001911 case -NFS4ERR_LEASE_MOVED:
1912 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1913 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001914 case -NFS4ERR_DELEG_REVOKED:
1915 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001916 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001917 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001918 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001919 nfs_inode_find_state_and_recover(state->inode,
1920 stateid);
1921 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001922 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001923 case -NFS4ERR_DELAY:
1924 case -NFS4ERR_GRACE:
1925 set_bit(NFS_DELEGATED_STATE, &state->flags);
1926 ssleep(1);
1927 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001928 case -ENOMEM:
1929 case -NFS4ERR_DENIED:
1930 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1931 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return err;
1934}
1935
Trond Myklebust24311f82015-09-20 10:50:17 -04001936int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1937 struct nfs4_state *state, const nfs4_stateid *stateid,
1938 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001939{
1940 struct nfs_server *server = NFS_SERVER(state->inode);
1941 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001942 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001943
1944 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1945 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1946 if (IS_ERR(opendata))
1947 return PTR_ERR(opendata);
1948 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001949 write_seqlock(&state->seqlock);
1950 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1951 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001952 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1953 switch (type & (FMODE_READ|FMODE_WRITE)) {
1954 case FMODE_READ|FMODE_WRITE:
1955 case FMODE_WRITE:
1956 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1957 if (err)
1958 break;
1959 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1960 if (err)
1961 break;
1962 case FMODE_READ:
1963 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1964 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001965 nfs4_opendata_put(opendata);
1966 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1967}
1968
Chuck Leverbe05c862013-08-09 12:49:47 -04001969static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1970{
1971 struct nfs4_opendata *data = calldata;
1972
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001973 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1974 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001975}
1976
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001977static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1978{
1979 struct nfs4_opendata *data = calldata;
1980
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001981 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001982
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001984 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001985 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001986 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001987 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001988 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001989 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001990}
1991
1992static void nfs4_open_confirm_release(void *calldata)
1993{
1994 struct nfs4_opendata *data = calldata;
1995 struct nfs4_state *state = NULL;
1996
1997 /* If this request hasn't been cancelled, do nothing */
1998 if (data->cancelled == 0)
1999 goto out_free;
2000 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002001 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002003 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002004 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002005 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002006out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002007 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002008}
2009
2010static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002011 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002012 .rpc_call_done = nfs4_open_confirm_done,
2013 .rpc_release = nfs4_open_confirm_release,
2014};
2015
2016/*
2017 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2018 */
2019static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2020{
David Howells2b0143b2015-03-17 22:25:59 +00002021 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002022 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 struct rpc_message msg = {
2024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2025 .rpc_argp = &data->c_arg,
2026 .rpc_resp = &data->c_res,
2027 .rpc_cred = data->owner->so_cred,
2028 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002029 struct rpc_task_setup task_setup_data = {
2030 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002031 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002032 .callback_ops = &nfs4_open_confirm_ops,
2033 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002034 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002035 .flags = RPC_TASK_ASYNC,
2036 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int status;
2038
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002039 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002040 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002041 data->rpc_done = 0;
2042 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002043 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002044 if (data->is_recover)
2045 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002046 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002047 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002048 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002049 status = nfs4_wait_for_completion_rpc_task(task);
2050 if (status != 0) {
2051 data->cancelled = 1;
2052 smp_wmb();
2053 } else
2054 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002055 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return status;
2057}
2058
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002059static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002061 struct nfs4_opendata *data = calldata;
2062 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002063 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002064 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002065
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002066 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002067 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002068 /*
2069 * Check if we still need to send an OPEN call, or if we can use
2070 * a delegation instead.
2071 */
2072 if (data->state != NULL) {
2073 struct nfs_delegation *delegation;
2074
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002075 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002076 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002077 rcu_read_lock();
2078 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002079 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002080 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002081 rcu_read_unlock();
2082 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002083 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002084 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002085 switch (claim) {
2086 default:
2087 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002088 case NFS4_OPEN_CLAIM_PREVIOUS:
2089 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2090 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002091 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002092 case NFS4_OPEN_CLAIM_FH:
2093 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002094 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2095 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002096 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002097 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002098 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002099 &data->o_res.seq_res,
2100 task) != 0)
2101 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002102
2103 /* Set the create mode (note dependency on the session type) */
2104 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2105 if (data->o_arg.open_flags & O_EXCL) {
2106 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2107 if (nfs4_has_persistent_session(clp))
2108 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2109 else if (clp->cl_mvops->minor_version > 0)
2110 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2111 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002112 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002113unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002114 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002115 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002116out_no_action:
2117 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002118out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002119 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002120}
2121
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002122static void nfs4_open_done(struct rpc_task *task, void *calldata)
2123{
2124 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002126 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002127
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002128 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002129 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002130
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002132 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2133 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002134 case S_IFREG:
2135 break;
2136 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002137 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002138 break;
2139 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002141 break;
2142 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002144 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002145 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002146 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002147 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2148 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002149 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002150 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002151}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002152
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002153static void nfs4_open_release(void *calldata)
2154{
2155 struct nfs4_opendata *data = calldata;
2156 struct nfs4_state *state = NULL;
2157
2158 /* If this request hasn't been cancelled, do nothing */
2159 if (data->cancelled == 0)
2160 goto out_free;
2161 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002162 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 goto out_free;
2164 /* In case we need an open_confirm, no cleanup! */
2165 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2166 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002167 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002168 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002169 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002170out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002171 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002172}
2173
2174static const struct rpc_call_ops nfs4_open_ops = {
2175 .rpc_call_prepare = nfs4_open_prepare,
2176 .rpc_call_done = nfs4_open_done,
2177 .rpc_release = nfs4_open_release,
2178};
2179
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002180static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181{
David Howells2b0143b2015-03-17 22:25:59 +00002182 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002183 struct nfs_server *server = NFS_SERVER(dir);
2184 struct nfs_openargs *o_arg = &data->o_arg;
2185 struct nfs_openres *o_res = &data->o_res;
2186 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002187 struct rpc_message msg = {
2188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2189 .rpc_argp = o_arg,
2190 .rpc_resp = o_res,
2191 .rpc_cred = data->owner->so_cred,
2192 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 struct rpc_task_setup task_setup_data = {
2194 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002195 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002196 .callback_ops = &nfs4_open_ops,
2197 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002198 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002199 .flags = RPC_TASK_ASYNC,
2200 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 int status;
2202
Chuck Levera9c92d62013-08-09 12:48:18 -04002203 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002204 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002205 data->rpc_done = 0;
2206 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002207 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002208 data->is_recover = 0;
2209 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002210 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002211 data->is_recover = 1;
2212 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002213 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002214 if (IS_ERR(task))
2215 return PTR_ERR(task);
2216 status = nfs4_wait_for_completion_rpc_task(task);
2217 if (status != 0) {
2218 data->cancelled = 1;
2219 smp_wmb();
2220 } else
2221 status = data->rpc_status;
2222 rpc_put_task(task);
2223
2224 return status;
2225}
2226
2227static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2228{
David Howells2b0143b2015-03-17 22:25:59 +00002229 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002230 struct nfs_openres *o_res = &data->o_res;
2231 int status;
2232
2233 status = nfs4_run_open_task(data, 1);
2234 if (status != 0 || !data->rpc_done)
2235 return status;
2236
Trond Myklebust6926afd2012-01-07 13:22:46 -05002237 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2238
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002239 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2240 status = _nfs4_proc_open_confirm(data);
2241 if (status != 0)
2242 return status;
2243 }
2244
2245 return status;
2246}
2247
Trond Myklebustf3792d62014-07-10 08:54:32 -04002248/*
2249 * Additional permission checks in order to distinguish between an
2250 * open for read, and an open for execute. This works around the
2251 * fact that NFSv4 OPEN treats read and execute permissions as being
2252 * the same.
2253 * Note that in the non-execute case, we want to turn off permission
2254 * checking if we just created a new file (POSIX open() semantics).
2255 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256static int nfs4_opendata_access(struct rpc_cred *cred,
2257 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002258 struct nfs4_state *state, fmode_t fmode,
2259 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002260{
2261 struct nfs_access_entry cache;
2262 u32 mask;
2263
2264 /* access call failed or for some reason the server doesn't
2265 * support any access modes -- defer access call until later */
2266 if (opendata->o_res.access_supported == 0)
2267 return 0;
2268
2269 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002270 /*
2271 * Use openflags to check for exec, because fmode won't
2272 * always have FMODE_EXEC set when file open for exec.
2273 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002274 if (openflags & __FMODE_EXEC) {
2275 /* ONLY check for exec rights */
2276 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002277 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002278 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002279
2280 cache.cred = cred;
2281 cache.jiffies = jiffies;
2282 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2283 nfs_access_add_cache(state->inode, &cache);
2284
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002285 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002286 return 0;
2287
2288 /* even though OPEN succeeded, access is denied. Close the file */
2289 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002290 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002291}
2292
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002293/*
2294 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2295 */
2296static int _nfs4_proc_open(struct nfs4_opendata *data)
2297{
David Howells2b0143b2015-03-17 22:25:59 +00002298 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002299 struct nfs_server *server = NFS_SERVER(dir);
2300 struct nfs_openargs *o_arg = &data->o_arg;
2301 struct nfs_openres *o_res = &data->o_res;
2302 int status;
2303
2304 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002305 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002306 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002307 if (status != 0) {
2308 if (status == -NFS4ERR_BADNAME &&
2309 !(o_arg->open_flags & O_CREAT))
2310 return -ENOENT;
2311 return status;
2312 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002313
Trond Myklebust6926afd2012-01-07 13:22:46 -05002314 nfs_fattr_map_and_free_names(server, &data->f_attr);
2315
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002316 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002317 if (o_arg->open_flags & O_EXCL)
2318 data->file_created = 1;
2319 else if (o_res->cinfo.before != o_res->cinfo.after)
2320 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002321 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002322 update_changeattr(dir, &o_res->cinfo,
2323 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002324 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002325 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2326 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002328 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002330 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002333 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Andy Adamsond83217c2011-03-01 01:34:17 +00002337static int nfs4_recover_expired_lease(struct nfs_server *server)
2338{
2339 return nfs4_client_recover_expired_lease(server->nfs_client);
2340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342/*
2343 * OPEN_EXPIRED:
2344 * reclaim state on the server after a network partition.
2345 * Assumes caller holds the appropriate lock
2346 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002347static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002349 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002350 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002352 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002353 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002354 if (IS_ERR(opendata))
2355 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002356 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002357 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002358 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002359 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002363static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002364{
Trond Myklebust539cd032007-06-05 11:46:42 -04002365 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002366 struct nfs4_exception exception = { };
2367 int err;
2368
2369 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002370 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002371 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002372 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2373 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002374 switch (err) {
2375 default:
2376 goto out;
2377 case -NFS4ERR_GRACE:
2378 case -NFS4ERR_DELAY:
2379 nfs4_handle_exception(server, err, &exception);
2380 err = 0;
2381 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002382 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002383out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002384 return err;
2385}
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002390 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Trond Myklebust864472e2006-01-03 09:55:15 +01002392 ctx = nfs4_state_find_open_context(state);
2393 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002394 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002395 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002396 put_nfs_open_context(ctx);
2397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Trond Myklebust41020b62016-09-22 13:38:58 -04002400static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2401 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002402{
Trond Myklebust41020b62016-09-22 13:38:58 -04002403 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002404 write_seqlock(&state->seqlock);
2405 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2406 write_sequnlock(&state->seqlock);
2407 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2408}
2409
2410static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2411{
2412 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002413 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002414}
2415
2416static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2417{
2418 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2419 nfs40_clear_delegation_stateid(state);
2420 return nfs4_open_expired(sp, state);
2421}
2422
Trond Myklebust45870d62016-09-22 13:38:59 -04002423static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2424 nfs4_stateid *stateid,
2425 struct rpc_cred *cred)
2426{
2427 return -NFS4ERR_BAD_STATEID;
2428}
2429
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002430#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002431static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2432 nfs4_stateid *stateid,
2433 struct rpc_cred *cred)
2434{
2435 int status;
2436
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002437 switch (stateid->type) {
2438 default:
2439 break;
2440 case NFS4_INVALID_STATEID_TYPE:
2441 case NFS4_SPECIAL_STATEID_TYPE:
2442 return -NFS4ERR_BAD_STATEID;
2443 case NFS4_REVOKED_STATEID_TYPE:
2444 goto out_free;
2445 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002446
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002447 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002448 switch (status) {
2449 case -NFS4ERR_EXPIRED:
2450 case -NFS4ERR_ADMIN_REVOKED:
2451 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002452 break;
2453 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002454 return status;
2455 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002456out_free:
2457 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002458 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002459 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002460}
2461
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002462static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002463{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002464 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002465 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002466 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002467 struct rpc_cred *cred;
2468 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002469
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002470 /* Get the delegation credential for use by test/free_stateid */
2471 rcu_read_lock();
2472 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002473 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002474 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002475 return;
2476 }
2477
2478 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002479 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2480 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002481 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002482 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002483 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002484
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002485 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2486 rcu_read_unlock();
2487 return;
2488 }
2489
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002490 cred = get_rpccred(delegation->cred);
2491 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002492 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002493 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002494 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002495 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002496
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002497 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002498}
2499
2500/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002501 * nfs41_check_expired_locks - possibly free a lock stateid
2502 *
2503 * @state: NFSv4 state for an inode
2504 *
2505 * Returns NFS_OK if recovery for this stateid is now finished.
2506 * Otherwise a negative NFS4ERR value is returned.
2507 */
2508static int nfs41_check_expired_locks(struct nfs4_state *state)
2509{
2510 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002511 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002512 struct nfs_server *server = NFS_SERVER(state->inode);
2513
2514 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2515 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002516
2517 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002518 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2519 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2520 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2521
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002522 atomic_inc(&lsp->ls_count);
2523 spin_unlock(&state->state_lock);
2524
2525 nfs4_put_lock_state(prev);
2526 prev = lsp;
2527
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002528 status = nfs41_test_and_free_expired_stateid(server,
2529 &lsp->ls_stateid,
2530 cred);
2531 trace_nfs4_test_lock_stateid(state, lsp, status);
2532 if (status == -NFS4ERR_EXPIRED ||
2533 status == -NFS4ERR_BAD_STATEID) {
2534 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002535 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002536 if (!recover_lost_locks)
2537 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2538 } else if (status != NFS_OK) {
2539 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002540 nfs4_put_lock_state(prev);
2541 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002542 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002543 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002544 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002545 }
2546 spin_unlock(&state->state_lock);
2547 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002548out:
2549 return ret;
2550}
2551
2552/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002553 * nfs41_check_open_stateid - possibly free an open stateid
2554 *
2555 * @state: NFSv4 state for an inode
2556 *
2557 * Returns NFS_OK if recovery for this stateid is now finished.
2558 * Otherwise a negative NFS4ERR value is returned.
2559 */
2560static int nfs41_check_open_stateid(struct nfs4_state *state)
2561{
2562 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002563 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002564 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002565 int status;
2566
Trond Myklebustb134fc42016-09-22 13:39:16 -04002567 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002568 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2569 if (nfs4_have_delegation(state->inode, state->state))
2570 return NFS_OK;
2571 return -NFS4ERR_OPENMODE;
2572 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002573 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002574 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002575 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002576 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002577 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002578 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2579 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2580 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002581 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002582 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002583 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002584 if (status != NFS_OK)
2585 return status;
2586 if (nfs_open_stateid_recover_openmode(state))
2587 return -NFS4ERR_OPENMODE;
2588 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002589}
2590
2591static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2592{
Chuck Levereb64cf92012-07-11 16:30:05 -04002593 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002594
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002595 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002596 status = nfs41_check_expired_locks(state);
2597 if (status != NFS_OK)
2598 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002599 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002600 if (status != NFS_OK)
2601 status = nfs4_open_expired(sp, state);
2602 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002603}
2604#endif
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002607 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2608 * fields corresponding to attributes that were used to store the verifier.
2609 * Make sure we clobber those fields in the later setattr call
2610 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002611static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2612 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002613{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002614 const u32 *attrset = opendata->o_res.attrset;
2615
2616 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002617 !(sattr->ia_valid & ATTR_ATIME_SET))
2618 sattr->ia_valid |= ATTR_ATIME;
2619
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002620 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002621 !(sattr->ia_valid & ATTR_MTIME_SET))
2622 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002623
2624 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002625 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2626 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002627 sattr->ia_valid &= ~ATTR_MODE;
2628
2629 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2630 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002631}
2632
Trond Myklebustc21443c2013-02-07 14:26:21 -05002633static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2634 fmode_t fmode,
2635 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002636 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002637{
2638 struct nfs4_state_owner *sp = opendata->owner;
2639 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002640 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002641 struct nfs4_state *state;
2642 unsigned int seq;
2643 int ret;
2644
2645 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2646
2647 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002648 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002649 goto out;
2650
2651 state = nfs4_opendata_to_nfs4_state(opendata);
2652 ret = PTR_ERR(state);
2653 if (IS_ERR(state))
2654 goto out;
2655 if (server->caps & NFS_CAP_POSIX_LOCK)
2656 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002657 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2658 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002659
Trond Myklebust275bb302013-05-29 13:11:28 -04002660 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002661 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002662 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002663 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002664 alias = d_exact_alias(dentry, state->inode);
2665 if (!alias)
2666 alias = d_splice_alias(igrab(state->inode), dentry);
2667 /* d_splice_alias() can't fail here - it's a non-directory */
2668 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002669 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002670 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002671 }
2672 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002673 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002674 }
2675
Trond Myklebustc21443c2013-02-07 14:26:21 -05002676 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2677 if (ret != 0)
2678 goto out;
2679
Trond Myklebust3efb9722013-05-29 13:17:04 -04002680 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002681 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002682 nfs_inode_attach_open_context(ctx);
2683 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2684 nfs4_schedule_stateid_recovery(server, state);
2685 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002686out:
2687 return ret;
2688}
2689
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002690/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002691 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Andy Adamson82be4172012-05-23 05:02:35 -04002693static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002694 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002695 int flags,
2696 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002697 struct nfs4_label *label,
2698 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct nfs4_state_owner *sp;
2701 struct nfs4_state *state = NULL;
2702 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002703 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002704 struct dentry *dentry = ctx->dentry;
2705 struct rpc_cred *cred = ctx->cred;
2706 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2707 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002708 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002709 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002710 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002714 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2715 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2717 goto out_err;
2718 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002719 status = nfs4_recover_expired_lease(server);
2720 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002721 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002722 if (d_really_is_positive(dentry))
2723 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002724 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002725 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002726 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002727 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002728 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002729 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002730 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
David Quigley14c43f72013-05-22 12:50:43 -04002732 if (label) {
2733 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2734 if (IS_ERR(olabel)) {
2735 status = PTR_ERR(olabel);
2736 goto err_opendata_put;
2737 }
2738 }
2739
Trond Myklebuste911b812014-03-26 13:24:37 -07002740 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2741 if (!opendata->f_attr.mdsthreshold) {
2742 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2743 if (!opendata->f_attr.mdsthreshold)
2744 goto err_free_label;
2745 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002746 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002747 }
David Howells2b0143b2015-03-17 22:25:59 +00002748 if (d_really_is_positive(dentry))
2749 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002750
Trond Myklebust3efb9722013-05-29 13:17:04 -04002751 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002752 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002753 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002754 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002755
NeilBrownefcbc042015-07-30 13:00:56 +10002756 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002757 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002758 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002759 /*
2760 * send create attributes which was not set by open
2761 * with an extra setattr.
2762 */
2763 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2764 nfs_fattr_init(opendata->o_res.f_attr);
2765 status = nfs4_do_setattr(state->inode, cred,
2766 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002767 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002768 if (status == 0) {
2769 nfs_setattr_update_inode(state->inode, sattr,
2770 opendata->o_res.f_attr);
2771 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2772 }
David Quigley1775fd32013-05-22 12:50:42 -04002773 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002774 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002775 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002776 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002777
Trond Myklebuste911b812014-03-26 13:24:37 -07002778 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002779 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002780 opendata->f_attr.mdsthreshold = NULL;
2781 }
Andy Adamson82be4172012-05-23 05:02:35 -04002782
David Quigley14c43f72013-05-22 12:50:43 -04002783 nfs4_label_free(olabel);
2784
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002785 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002788err_free_label:
2789 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002790err_opendata_put:
2791 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002792err_put_state_owner:
2793 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return status;
2796}
2797
2798
Andy Adamson82be4172012-05-23 05:02:35 -04002799static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002800 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002801 int flags,
2802 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002803 struct nfs4_label *label,
2804 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002806 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 struct nfs4_exception exception = { };
2808 struct nfs4_state *res;
2809 int status;
2810
2811 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002812 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002813 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002814 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (status == 0)
2816 break;
2817 /* NOTE: BAD_SEQID means the server and client disagree about the
2818 * book-keeping w.r.t. state-changing operations
2819 * (OPEN/CLOSE/LOCK/LOCKU...)
2820 * It is actually a sign of a bug on the client or on the server.
2821 *
2822 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002823 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 * have unhashed the old state_owner for us, and that we can
2825 * therefore safely retry using a new one. We should still warn
2826 * the user though...
2827 */
2828 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002829 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002830 " returned a bad sequence-id error!\n",
2831 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 exception.retry = 1;
2833 continue;
2834 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002835 /*
2836 * BAD_STATEID on OPEN means that the server cancelled our
2837 * state before it received the OPEN_CONFIRM.
2838 * Recover by retrying the request as per the discussion
2839 * on Page 181 of RFC3530.
2840 */
2841 if (status == -NFS4ERR_BAD_STATEID) {
2842 exception.retry = 1;
2843 continue;
2844 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002845 if (status == -EAGAIN) {
2846 /* We must have found a delegation */
2847 exception.retry = 1;
2848 continue;
2849 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002850 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2851 continue;
2852 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 status, &exception));
2854 } while (exception.retry);
2855 return res;
2856}
2857
Trond Myklebust8487c472016-06-26 08:44:35 -04002858static int _nfs4_do_setattr(struct inode *inode,
2859 struct nfs_setattrargs *arg,
2860 struct nfs_setattrres *res,
2861 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002862 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002863{
2864 struct nfs_server *server = NFS_SERVER(inode);
2865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2867 .rpc_argp = arg,
2868 .rpc_resp = res,
2869 .rpc_cred = cred,
2870 };
2871 struct rpc_cred *delegation_cred = NULL;
2872 unsigned long timestamp = jiffies;
2873 fmode_t fmode;
2874 bool truncate;
2875 int status;
2876
2877 nfs_fattr_init(res->fattr);
2878
2879 /* Servers should only apply open mode checks for file size changes */
2880 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2881 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2882
2883 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2884 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002885 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002886 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002887 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002888 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002889 l_ctx = nfs_get_lock_context(ctx);
2890 if (IS_ERR(l_ctx))
2891 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002892 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2893 &arg->stateid, &delegation_cred);
2894 nfs_put_lock_context(l_ctx);
2895 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002896 return -EBADF;
2897 } else
2898 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2899 if (delegation_cred)
2900 msg.rpc_cred = delegation_cred;
2901
2902 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2903
2904 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002905 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002906 renew_lease(server, timestamp);
2907 trace_nfs4_setattr(inode, &arg->stateid, status);
2908 return status;
2909}
2910
2911static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2912 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002913 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002914 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002916 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002917 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002919 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 .iap = sattr,
2921 .server = server,
2922 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002923 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 };
2925 struct nfs_setattrres res = {
2926 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002927 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 .server = server,
2929 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002930 struct nfs4_exception exception = {
2931 .state = state,
2932 .inode = inode,
2933 .stateid = &arg.stateid,
2934 };
2935 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
David Quigleyaa9c2662013-05-22 12:50:44 -04002937 arg.bitmask = nfs4_bitmask(server, ilabel);
2938 if (ilabel)
2939 arg.bitmask = nfs4_bitmask(server, olabel);
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002942 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002943 switch (err) {
2944 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002945 if (!(sattr->ia_valid & ATTR_SIZE)) {
2946 pr_warn_once("NFSv4: server %s is incorrectly "
2947 "applying open mode checks to "
2948 "a SETATTR that is not "
2949 "changing file size.\n",
2950 server->nfs_client->cl_hostname);
2951 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002952 if (state && !(state->state & FMODE_WRITE)) {
2953 err = -EBADF;
2954 if (sattr->ia_valid & ATTR_OPEN)
2955 err = -EACCES;
2956 goto out;
2957 }
2958 }
2959 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return err;
2963}
2964
Peng Tao500d7012015-09-22 11:35:22 +08002965static bool
2966nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2967{
2968 if (inode == NULL || !nfs_have_layout(inode))
2969 return false;
2970
2971 return pnfs_wait_on_layoutreturn(inode, task);
2972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974struct nfs4_closedata {
2975 struct inode *inode;
2976 struct nfs4_state *state;
2977 struct nfs_closeargs arg;
2978 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002979 struct {
2980 struct nfs4_layoutreturn_args arg;
2981 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002982 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002983 u32 roc_barrier;
2984 bool roc;
2985 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002986 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002987 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988};
2989
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002990static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002991{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002992 struct nfs4_closedata *calldata = data;
2993 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002994 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002995
Trond Myklebustcf805162016-11-15 14:56:07 -05002996 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002997 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2998 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002999 nfs4_put_open_state(calldata->state);
3000 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003001 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003002 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003003 kfree(calldata);
3004}
3005
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003006static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003008 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003011 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Chuck Levera3ca5652012-03-01 17:00:40 -05003013 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003014 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3015 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003016 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003017
3018 /* Handle Layoutreturn errors */
3019 if (calldata->arg.lr_args && task->tk_status != 0) {
3020 switch (calldata->res.lr_ret) {
3021 default:
3022 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3023 break;
3024 case 0:
3025 calldata->arg.lr_args = NULL;
3026 calldata->res.lr_res = NULL;
3027 break;
3028 case -NFS4ERR_ADMIN_REVOKED:
3029 case -NFS4ERR_DELEG_REVOKED:
3030 case -NFS4ERR_EXPIRED:
3031 case -NFS4ERR_BAD_STATEID:
3032 case -NFS4ERR_OLD_STATEID:
3033 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3034 case -NFS4ERR_WRONG_CRED:
3035 calldata->arg.lr_args = NULL;
3036 calldata->res.lr_res = NULL;
3037 calldata->res.lr_ret = 0;
3038 rpc_restart_call_prepare(task);
3039 return;
3040 }
3041 }
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 /* hmm. we are done with the inode, and in the process of freeing
3044 * the state_owner. we keep this around to process errors
3045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 switch (task->tk_status) {
3047 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003048 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003049 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003050 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003051 case -NFS4ERR_ACCESS:
3052 if (calldata->arg.bitmask != NULL) {
3053 calldata->arg.bitmask = NULL;
3054 calldata->res.fattr = NULL;
3055 task->tk_status = 0;
3056 rpc_restart_call_prepare(task);
3057 goto out_release;
3058
3059 }
3060 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003061 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003063 case -NFS4ERR_EXPIRED:
3064 nfs4_free_revoked_stateid(server,
3065 &calldata->arg.stateid,
3066 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003067 case -NFS4ERR_OLD_STATEID:
3068 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003069 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003070 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003071 rpc_restart_call_prepare(task);
3072 goto out_release;
3073 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003074 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003077 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003078 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003079 goto out_release;
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003082 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3083 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003084out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003085 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003086 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003087 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003090static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003092 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003093 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003094 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003095 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003096 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003097
Chuck Levera3ca5652012-03-01 17:00:40 -05003098 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003099 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003100 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003101
Trond Myklebust88069f72009-12-08 08:33:16 -05003102 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003103 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003104 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3105 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3106 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003107 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003108 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003109 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003110 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003111 if (state->n_rdonly == 0)
3112 call_close |= is_rdonly;
3113 else if (is_rdonly)
3114 calldata->arg.fmode |= FMODE_READ;
3115 if (state->n_wronly == 0)
3116 call_close |= is_wronly;
3117 else if (is_wronly)
3118 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003119 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3120 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003121 } else if (is_rdwr)
3122 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3123
Trond Myklebust5cc78612016-11-14 11:19:56 -05003124 if (!nfs4_valid_open_stateid(state) ||
3125 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003126 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003127 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003128
3129 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003130 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003131 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003132 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003133
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003134 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003135 nfs_release_seqid(calldata->arg.seqid);
3136 goto out_wait;
3137 }
3138
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003139 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003140 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003141
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003142 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003143 /* Close-to-open cache consistency revalidation */
3144 if (!nfs4_have_delegation(inode, FMODE_READ))
3145 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3146 else
3147 calldata->arg.bitmask = NULL;
3148 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003149
Trond Myklebust6ae37332015-01-30 14:21:14 -05003150 calldata->arg.share_access =
3151 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3152 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003153
Trond Myklebustd8d84982016-12-19 12:14:44 -05003154 if (calldata->res.fattr == NULL)
3155 calldata->arg.bitmask = NULL;
3156 else if (calldata->arg.bitmask == NULL)
3157 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003158 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003159 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003160 &calldata->arg.seq_args,
3161 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003162 task) != 0)
3163 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003164 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003165 return;
3166out_no_action:
3167 task->tk_action = NULL;
3168out_wait:
3169 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003173 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003174 .rpc_call_done = nfs4_close_done,
3175 .rpc_release = nfs4_free_closedata,
3176};
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178/*
3179 * It is possible for data to be read/written from a mem-mapped file
3180 * after the sys_close call (which hits the vfs layer as a flush).
3181 * This means that we can't safely call nfsv4 close on a file until
3182 * the inode is cleared. This in turn means that we are not good
3183 * NFSv4 citizens - we do not indicate to the server to update the file's
3184 * share state even when we are done with one of the three share
3185 * stateid's in the inode.
3186 *
3187 * NOTE: Caller must be holding the sp->so_owner semaphore!
3188 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003189int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003191 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003192 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003194 struct nfs4_state_owner *sp = state->owner;
3195 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003196 struct rpc_message msg = {
3197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3198 .rpc_cred = state->owner->so_cred,
3199 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003200 struct rpc_task_setup task_setup_data = {
3201 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003202 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003203 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003204 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003205 .flags = RPC_TASK_ASYNC,
3206 };
Trond Myklebust95121352005-10-18 14:20:12 -07003207 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003209 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3210 &task_setup_data.rpc_client, &msg);
3211
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003212 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003214 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003215 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003216 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003218 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003220 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3221 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003222 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003223 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003224 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003225 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003226 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003227 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003228 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003229 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003230 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003231 calldata->lr.roc = pnfs_roc(state->inode,
3232 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3233 if (calldata->lr.roc) {
3234 calldata->arg.lr_args = &calldata->lr.arg;
3235 calldata->res.lr_res = &calldata->lr.res;
3236 }
Al Viro643168c2011-06-22 18:20:23 -04003237 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003238
Trond Myklebust1174dd12010-12-21 10:52:24 -05003239 msg.rpc_argp = &calldata->arg;
3240 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003241 task_setup_data.callback_data = calldata;
3242 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003243 if (IS_ERR(task))
3244 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003245 status = 0;
3246 if (wait)
3247 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003248 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003249 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003250out_free_calldata:
3251 kfree(calldata);
3252out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003253 nfs4_put_open_state(state);
3254 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003255 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
Trond Myklebust2b484292010-09-17 10:56:51 -04003258static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003259nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3260 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003263 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3264
3265 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Trond Myklebust565277f2007-10-15 18:17:53 -04003267 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003268 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003269
3270 nfs4_label_release_security(label);
3271
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003272 if (IS_ERR(state))
3273 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003274 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Trond Myklebust1185a552009-12-03 15:54:02 -05003277static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003278{
3279 if (ctx->state == NULL)
3280 return;
3281 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003282 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003283 else
Al Viro643168c2011-06-22 18:20:23 -04003284 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Trond Myklebustb944dba2013-11-04 15:20:20 -05003287#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3288#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003289#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3292{
Kinglong Mee8c612822015-08-26 21:12:58 +08003293 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003294 struct nfs4_server_caps_arg args = {
3295 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003296 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 struct nfs4_server_caps_res res = {};
3299 struct rpc_message msg = {
3300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003301 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 .rpc_resp = &res,
3303 };
3304 int status;
3305
Kinglong Mee8c612822015-08-26 21:12:58 +08003306 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3307 FATTR4_WORD0_FH_EXPIRE_TYPE |
3308 FATTR4_WORD0_LINK_SUPPORT |
3309 FATTR4_WORD0_SYMLINK_SUPPORT |
3310 FATTR4_WORD0_ACLSUPPORT;
3311 if (minorversion)
3312 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3313
Bryan Schumaker7c513052011-03-24 17:12:24 +00003314 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003316 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003317 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003318 case 0:
3319 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3320 res.attr_bitmask[2] = 0;
3321 break;
3322 case 1:
3323 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3324 break;
3325 case 2:
3326 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003329 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3330 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3331 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3332 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003333 NFS_CAP_CTIME|NFS_CAP_MTIME|
3334 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003335 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3336 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 server->caps |= NFS_CAP_ACLS;
3338 if (res.has_links != 0)
3339 server->caps |= NFS_CAP_HARDLINKS;
3340 if (res.has_symlinks != 0)
3341 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003342 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3343 server->caps |= NFS_CAP_FILEID;
3344 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3345 server->caps |= NFS_CAP_MODE;
3346 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3347 server->caps |= NFS_CAP_NLINK;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3349 server->caps |= NFS_CAP_OWNER;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3351 server->caps |= NFS_CAP_OWNER_GROUP;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3353 server->caps |= NFS_CAP_ATIME;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3355 server->caps |= NFS_CAP_CTIME;
3356 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3357 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003358#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3359 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3360 server->caps |= NFS_CAP_SECURITY_LABEL;
3361#endif
3362 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3363 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003364 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003365
Trond Myklebusta65318b2009-03-11 14:10:28 -04003366 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3367 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3368 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003369 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003370 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3371 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003373 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return status;
3377}
3378
Trond Myklebust55a97592006-06-09 09:34:19 -04003379int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 struct nfs4_exception exception = { };
3382 int err;
3383 do {
3384 err = nfs4_handle_exception(server,
3385 _nfs4_server_capabilities(server, fhandle),
3386 &exception);
3387 } while (exception.retry);
3388 return err;
3389}
3390
3391static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3392 struct nfs_fsinfo *info)
3393{
David Quigleyaa9c2662013-05-22 12:50:44 -04003394 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003396 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 };
3398 struct nfs4_lookup_res res = {
3399 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003400 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 .fh = fhandle,
3402 };
3403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3405 .rpc_argp = &args,
3406 .rpc_resp = &res,
3407 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003408
David Quigleyaa9c2662013-05-22 12:50:44 -04003409 bitmask[0] = nfs4_fattr_bitmap[0];
3410 bitmask[1] = nfs4_fattr_bitmap[1];
3411 /*
3412 * Process the label in the upcoming getfattr
3413 */
3414 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3415
Trond Myklebust0e574af2005-10-27 22:12:38 -04003416 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003417 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
3420static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3421 struct nfs_fsinfo *info)
3422{
3423 struct nfs4_exception exception = { };
3424 int err;
3425 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003426 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003427 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003428 switch (err) {
3429 case 0:
3430 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003431 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003432 default:
3433 err = nfs4_handle_exception(server, err, &exception);
3434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003436out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return err;
3438}
3439
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003440static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3441 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3442{
Trond Myklebustc2190662013-08-26 19:23:04 -04003443 struct rpc_auth_create_args auth_args = {
3444 .pseudoflavor = flavor,
3445 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003446 struct rpc_auth *auth;
3447 int ret;
3448
Trond Myklebustc2190662013-08-26 19:23:04 -04003449 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003450 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003451 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003452 goto out;
3453 }
3454 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003455out:
3456 return ret;
3457}
3458
Chuck Lever9a744ba2013-03-16 15:56:02 -04003459/*
3460 * Retry pseudoroot lookup with various security flavors. We do this when:
3461 *
3462 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3463 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3464 *
3465 * Returns zero on success, or a negative NFS4ERR value, or a
3466 * negative errno value.
3467 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003468static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003469 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003471 /* Per 3530bis 15.33.5 */
3472 static const rpc_authflavor_t flav_array[] = {
3473 RPC_AUTH_GSS_KRB5P,
3474 RPC_AUTH_GSS_KRB5I,
3475 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003476 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003477 RPC_AUTH_NULL,
3478 };
3479 int status = -EPERM;
3480 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003482 if (server->auth_info.flavor_len > 0) {
3483 /* try each flavor specified by user */
3484 for (i = 0; i < server->auth_info.flavor_len; i++) {
3485 status = nfs4_lookup_root_sec(server, fhandle, info,
3486 server->auth_info.flavors[i]);
3487 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3488 continue;
3489 break;
3490 }
3491 } else {
3492 /* no flavors specified by user, try default list */
3493 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3494 status = nfs4_lookup_root_sec(server, fhandle, info,
3495 flav_array[i]);
3496 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3497 continue;
3498 break;
3499 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003500 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003501
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003502 /*
3503 * -EACCESS could mean that the user doesn't have correct permissions
3504 * to access the mount. It could also mean that we tried to mount
3505 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3506 * existing mount programs don't handle -EACCES very well so it should
3507 * be mapped to -EPERM instead.
3508 */
3509 if (status == -EACCES)
3510 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003511 return status;
3512}
3513
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003514/**
3515 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3516 * @server: initialized nfs_server handle
3517 * @fhandle: we fill in the pseudo-fs root file handle
3518 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003519 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003520 *
3521 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003522 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003523int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003524 struct nfs_fsinfo *info,
3525 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003526{
Andre Przywarac7757072015-04-23 17:17:40 +01003527 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003528
Andre Przywarac7757072015-04-23 17:17:40 +01003529 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003530 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003531
3532 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003533 status = server->nfs_client->cl_mvops->find_root_sec(server,
3534 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (status == 0)
3537 status = nfs4_server_capabilities(server, fhandle);
3538 if (status == 0)
3539 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003540
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003541 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Bryan Schumakerbae36242012-05-10 15:07:31 -04003544static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3545 struct nfs_fsinfo *info)
3546{
3547 int error;
3548 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003549 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003550
3551 error = nfs4_server_capabilities(server, mntfh);
3552 if (error < 0) {
3553 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3554 return error;
3555 }
3556
David Quigley14c43f72013-05-22 12:50:43 -04003557 label = nfs4_label_alloc(server, GFP_KERNEL);
3558 if (IS_ERR(label))
3559 return PTR_ERR(label);
3560
David Quigley1775fd32013-05-22 12:50:42 -04003561 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003562 if (error < 0) {
3563 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003564 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003565 }
3566
3567 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3568 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3569 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3570
David Quigley14c43f72013-05-22 12:50:43 -04003571err_free_label:
3572 nfs4_label_free(label);
3573
Bryan Schumakerbae36242012-05-10 15:07:31 -04003574 return error;
3575}
3576
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577/*
3578 * Get locations and (maybe) other attributes of a referral.
3579 * Note that we'll actually follow the referral later when
3580 * we detect fsid mismatch in inode revalidation
3581 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003582static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3583 const struct qstr *name, struct nfs_fattr *fattr,
3584 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585{
3586 int status = -ENOMEM;
3587 struct page *page = NULL;
3588 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003589
3590 page = alloc_page(GFP_KERNEL);
3591 if (page == NULL)
3592 goto out;
3593 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3594 if (locations == NULL)
3595 goto out;
3596
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003597 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003598 if (status != 0)
3599 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003600
3601 /*
3602 * If the fsid didn't change, this is a migration event, not a
3603 * referral. Cause us to drop into the exception handler, which
3604 * will kick off migration recovery.
3605 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003606 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003607 dprintk("%s: server did not return a different fsid for"
3608 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003609 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003610 goto out;
3611 }
Andy Adamson533eb462011-06-13 18:25:56 -04003612 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3613 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614
Andy Adamson533eb462011-06-13 18:25:56 -04003615 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003616 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003617 memset(fhandle, 0, sizeof(struct nfs_fh));
3618out:
3619 if (page)
3620 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003621 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003622 return status;
3623}
3624
David Quigley1775fd32013-05-22 12:50:42 -04003625static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3626 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct nfs4_getattr_arg args = {
3629 .fh = fhandle,
3630 .bitmask = server->attr_bitmask,
3631 };
3632 struct nfs4_getattr_res res = {
3633 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003634 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 .server = server,
3636 };
3637 struct rpc_message msg = {
3638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3639 .rpc_argp = &args,
3640 .rpc_resp = &res,
3641 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003642
3643 args.bitmask = nfs4_bitmask(server, label);
3644
Trond Myklebust0e574af2005-10-27 22:12:38 -04003645 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003646 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
David Quigley1775fd32013-05-22 12:50:42 -04003649static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3650 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct nfs4_exception exception = { };
3653 int err;
3654 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003655 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3656 trace_nfs4_getattr(server, fhandle, fattr, err);
3657 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 &exception);
3659 } while (exception.retry);
3660 return err;
3661}
3662
3663/*
3664 * The file is not closed if it is opened due to the a request to change
3665 * the size of the file. The open call will not be needed once the
3666 * VFS layer lookup-intents are implemented.
3667 *
3668 * Close is called when the inode is destroyed.
3669 * If we haven't opened the file for O_WRONLY, we
3670 * need to in the size_change case to obtain a stateid.
3671 *
3672 * Got race?
3673 * Because OPEN is always done by name in nfsv4, it is
3674 * possible that we opened a different file by the same
3675 * name. We can recognize this race condition, but we
3676 * can't do anything about it besides returning an error.
3677 *
3678 * This will be fixed with VFS changes (lookup-intent).
3679 */
3680static int
3681nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3682 struct iattr *sattr)
3683{
David Howells2b0143b2015-03-17 22:25:59 +00003684 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003685 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003686 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003687 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 int status;
3689
Peng Tao88ac8152014-09-12 11:04:10 +08003690 if (pnfs_ld_layoutret_on_setattr(inode) &&
3691 sattr->ia_valid & ATTR_SIZE &&
3692 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003693 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003694
Trond Myklebust0e574af2005-10-27 22:12:38 -04003695 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Andy Adamson26699402012-05-30 16:12:24 -04003697 /* Deal with open(O_TRUNC) */
3698 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003699 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003700
3701 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003702 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003703 return 0;
3704
Trond Myklebustd5308382005-11-04 15:33:38 -05003705 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003706 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003707
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003708 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003709 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003710 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003711 }
3712
David Quigley14c43f72013-05-22 12:50:43 -04003713 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3714 if (IS_ERR(label))
3715 return PTR_ERR(label);
3716
NeilBrown29b59f92016-10-13 15:26:47 +11003717 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003718 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003719 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 nfs_setsecurity(inode, fattr, label);
3721 }
David Quigley14c43f72013-05-22 12:50:43 -04003722 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return status;
3724}
3725
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003726static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3727 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003728 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003729{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003730 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003731 int status;
3732 struct nfs4_lookup_arg args = {
3733 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003734 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003735 .name = name,
3736 };
3737 struct nfs4_lookup_res res = {
3738 .server = server,
3739 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003740 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003741 .fh = fhandle,
3742 };
3743 struct rpc_message msg = {
3744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3745 .rpc_argp = &args,
3746 .rpc_resp = &res,
3747 };
3748
David Quigleyaa9c2662013-05-22 12:50:44 -04003749 args.bitmask = nfs4_bitmask(server, label);
3750
David Howells2b3de442006-08-22 20:06:09 -04003751 nfs_fattr_init(fattr);
3752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003754 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 dprintk("NFS reply lookup: %d\n", status);
3756 return status;
3757}
3758
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003759static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003760{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003761 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003762 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003763 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3764 fattr->nlink = 2;
3765}
3766
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003767static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003768 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003769 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003770{
3771 struct nfs4_exception exception = { };
3772 struct rpc_clnt *client = *clnt;
3773 int err;
3774 do {
David Quigley1775fd32013-05-22 12:50:42 -04003775 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003776 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003777 switch (err) {
3778 case -NFS4ERR_BADNAME:
3779 err = -ENOENT;
3780 goto out;
3781 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003782 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003783 if (err == -NFS4ERR_MOVED)
3784 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003785 goto out;
3786 case -NFS4ERR_WRONGSEC:
3787 err = -EPERM;
3788 if (client != *clnt)
3789 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003790 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003791 if (IS_ERR(client))
3792 return PTR_ERR(client);
3793
3794 exception.retry = 1;
3795 break;
3796 default:
3797 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3798 }
3799 } while (exception.retry);
3800
3801out:
3802 if (err == 0)
3803 *clnt = client;
3804 else if (client != *clnt)
3805 rpc_shutdown_client(client);
3806
3807 return err;
3808}
3809
Al Virobeffb8f2016-07-20 16:34:42 -04003810static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003811 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3812 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814 int status;
3815 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003816
David Quigley1775fd32013-05-22 12:50:42 -04003817 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003818 if (client != NFS_CLIENT(dir)) {
3819 rpc_shutdown_client(client);
3820 nfs_fixup_secinfo_attributes(fattr);
3821 }
3822 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003825struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003826nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003827 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3828{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003829 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003830 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003831
David Quigley1775fd32013-05-22 12:50:42 -04003832 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003833 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003834 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003835 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003836}
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3839{
Trond Myklebust76b32992007-08-10 17:45:11 -04003840 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 struct nfs4_accessargs args = {
3842 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003843 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003845 struct nfs4_accessres res = {
3846 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003847 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3850 .rpc_argp = &args,
3851 .rpc_resp = &res,
3852 .rpc_cred = entry->cred,
3853 };
3854 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003855 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 /*
3858 * Determine which access bits we want to ask for...
3859 */
3860 if (mode & MAY_READ)
3861 args.access |= NFS4_ACCESS_READ;
3862 if (S_ISDIR(inode->i_mode)) {
3863 if (mode & MAY_WRITE)
3864 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3865 if (mode & MAY_EXEC)
3866 args.access |= NFS4_ACCESS_LOOKUP;
3867 } else {
3868 if (mode & MAY_WRITE)
3869 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3870 if (mode & MAY_EXEC)
3871 args.access |= NFS4_ACCESS_EXECUTE;
3872 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003873
3874 res.fattr = nfs_alloc_fattr();
3875 if (res.fattr == NULL)
3876 return -ENOMEM;
3877
Bryan Schumaker7c513052011-03-24 17:12:24 +00003878 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003880 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003881 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003883 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return status;
3885}
3886
3887static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3888{
3889 struct nfs4_exception exception = { };
3890 int err;
3891 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003892 err = _nfs4_proc_access(inode, entry);
3893 trace_nfs4_access(inode, err);
3894 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 &exception);
3896 } while (exception.retry);
3897 return err;
3898}
3899
3900/*
3901 * TODO: For the time being, we don't try to get any attributes
3902 * along with any of the zero-copy operations READ, READDIR,
3903 * READLINK, WRITE.
3904 *
3905 * In the case of the first three, we want to put the GETATTR
3906 * after the read-type operation -- this is because it is hard
3907 * to predict the length of a GETATTR response in v4, and thus
3908 * align the READ data correctly. This means that the GETATTR
3909 * may end up partially falling into the page cache, and we should
3910 * shift it into the 'tail' of the xdr_buf before processing.
3911 * To do this efficiently, we need to know the total length
3912 * of data received, which doesn't seem to be available outside
3913 * of the RPC layer.
3914 *
3915 * In the case of WRITE, we also want to put the GETATTR after
3916 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003917 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 *
3919 * Both of these changes to the XDR layer would in fact be quite
3920 * minor, but I decided to leave them for a subsequent patch.
3921 */
3922static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3923 unsigned int pgbase, unsigned int pglen)
3924{
3925 struct nfs4_readlink args = {
3926 .fh = NFS_FH(inode),
3927 .pgbase = pgbase,
3928 .pglen = pglen,
3929 .pages = &page,
3930 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003931 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 struct rpc_message msg = {
3933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3934 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003935 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 };
3937
Bryan Schumaker7c513052011-03-24 17:12:24 +00003938 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 -07003939}
3940
3941static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3942 unsigned int pgbase, unsigned int pglen)
3943{
3944 struct nfs4_exception exception = { };
3945 int err;
3946 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003947 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3948 trace_nfs4_readlink(inode, err);
3949 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 &exception);
3951 } while (exception.retry);
3952 return err;
3953}
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003956 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958static int
3959nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003960 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003962 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003963 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003964 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 int status = 0;
3967
NeilBrown532d4de2016-10-13 15:26:47 +11003968 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003969 if (IS_ERR(ctx))
3970 return PTR_ERR(ctx);
3971
David Quigleyaa9c2662013-05-22 12:50:44 -04003972 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3973
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003974 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3975 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003976 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (IS_ERR(state)) {
3978 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003982 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003983 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return status;
3985}
3986
Al Virobeffb8f2016-07-20 16:34:42 -04003987static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Trond Myklebust16e42952005-10-27 22:12:44 -04003989 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003990 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003992 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003994 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003995 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003996 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3999 .rpc_argp = &args,
4000 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004002 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004003 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004004
Bryan Schumaker7c513052011-03-24 17:12:24 +00004005 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004006 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004007 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return status;
4009}
4010
Al Virobeffb8f2016-07-20 16:34:42 -04004011static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
4013 struct nfs4_exception exception = { };
4014 int err;
4015 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004016 err = _nfs4_proc_remove(dir, name);
4017 trace_nfs4_remove(dir, name, err);
4018 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 &exception);
4020 } while (exception.retry);
4021 return err;
4022}
4023
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004024static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004026 struct nfs_server *server = NFS_SERVER(dir);
4027 struct nfs_removeargs *args = msg->rpc_argp;
4028 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004030 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004032 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004033
4034 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035}
4036
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004037static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4038{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004039 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004040 &data->args.seq_args,
4041 &data->res.seq_res,
4042 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004045static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004047 struct nfs_unlinkdata *data = task->tk_calldata;
4048 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004049
Trond Myklebust14516c32010-07-31 14:29:06 -04004050 if (!nfs4_sequence_done(task, &res->seq_res))
4051 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004052 if (nfs4_async_handle_error(task, res->server, NULL,
4053 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004054 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004055 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004056 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004057 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Jeff Laytond3d41522010-09-17 17:31:57 -04004060static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4061{
4062 struct nfs_server *server = NFS_SERVER(dir);
4063 struct nfs_renameargs *arg = msg->rpc_argp;
4064 struct nfs_renameres *res = msg->rpc_resp;
4065
4066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004067 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004068 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004069}
4070
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004071static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4072{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004073 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004074 &data->args.seq_args,
4075 &data->res.seq_res,
4076 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004077}
4078
4079static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4080 struct inode *new_dir)
4081{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004082 struct nfs_renamedata *data = task->tk_calldata;
4083 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004084
4085 if (!nfs4_sequence_done(task, &res->seq_res))
4086 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004087 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004088 return 0;
4089
Trond Myklebustc733c492017-01-11 12:32:26 -05004090 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004091 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004092 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004093 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004094 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004095 return 1;
4096}
4097
Al Virobeffb8f2016-07-20 16:34:42 -04004098static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004100 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 struct nfs4_link_arg arg = {
4102 .fh = NFS_FH(inode),
4103 .dir_fh = NFS_FH(dir),
4104 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004105 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004107 struct nfs4_link_res res = {
4108 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004109 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 struct rpc_message msg = {
4112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4113 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004114 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004116 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Trond Myklebust136f2622010-04-16 16:22:49 -04004118 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004119 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004120 goto out;
4121
David Quigley14c43f72013-05-22 12:50:43 -04004122 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4123 if (IS_ERR(res.label)) {
4124 status = PTR_ERR(res.label);
4125 goto out;
4126 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004127 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004128
Bryan Schumaker7c513052011-03-24 17:12:24 +00004129 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004130 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004131 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004132 status = nfs_post_op_update_inode(inode, res.fattr);
4133 if (!status)
4134 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004135 }
David Quigley14c43f72013-05-22 12:50:43 -04004136
4137
4138 nfs4_label_free(res.label);
4139
Trond Myklebust136f2622010-04-16 16:22:49 -04004140out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004141 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return status;
4143}
4144
Al Virobeffb8f2016-07-20 16:34:42 -04004145static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
4147 struct nfs4_exception exception = { };
4148 int err;
4149 do {
4150 err = nfs4_handle_exception(NFS_SERVER(inode),
4151 _nfs4_proc_link(inode, dir, name),
4152 &exception);
4153 } while (exception.retry);
4154 return err;
4155}
4156
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157struct nfs4_createdata {
4158 struct rpc_message msg;
4159 struct nfs4_create_arg arg;
4160 struct nfs4_create_res res;
4161 struct nfs_fh fh;
4162 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004163 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004164};
4165
4166static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004167 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004168{
4169 struct nfs4_createdata *data;
4170
4171 data = kzalloc(sizeof(*data), GFP_KERNEL);
4172 if (data != NULL) {
4173 struct nfs_server *server = NFS_SERVER(dir);
4174
David Quigley14c43f72013-05-22 12:50:43 -04004175 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4176 if (IS_ERR(data->label))
4177 goto out_free;
4178
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4180 data->msg.rpc_argp = &data->arg;
4181 data->msg.rpc_resp = &data->res;
4182 data->arg.dir_fh = NFS_FH(dir);
4183 data->arg.server = server;
4184 data->arg.name = name;
4185 data->arg.attrs = sattr;
4186 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004187 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004188 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004189 data->res.server = server;
4190 data->res.fh = &data->fh;
4191 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004192 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004193 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194 }
4195 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004196out_free:
4197 kfree(data);
4198 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004199}
4200
4201static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4202{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004203 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004204 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004206 update_changeattr(dir, &data->res.dir_cinfo,
4207 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004208 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209 }
4210 return status;
4211}
4212
4213static void nfs4_free_createdata(struct nfs4_createdata *data)
4214{
David Quigley14c43f72013-05-22 12:50:43 -04004215 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 kfree(data);
4217}
4218
Chuck Lever4f390c12006-08-22 20:06:22 -04004219static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004220 struct page *page, unsigned int len, struct iattr *sattr,
4221 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223 struct nfs4_createdata *data;
4224 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Chuck Lever94a6d752006-08-22 20:06:23 -04004226 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004228
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004229 status = -ENOMEM;
4230 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4231 if (data == NULL)
4232 goto out;
4233
4234 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4235 data->arg.u.symlink.pages = &page;
4236 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004237 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004239 status = nfs4_do_create(dir, dentry, data);
4240
4241 nfs4_free_createdata(data);
4242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return status;
4244}
4245
Chuck Lever4f390c12006-08-22 20:06:22 -04004246static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004247 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004250 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004252
4253 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004256 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4257 trace_nfs4_symlink(dir, &dentry->d_name, err);
4258 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 &exception);
4260 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004261
4262 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return err;
4264}
4265
4266static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004267 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004269 struct nfs4_createdata *data;
4270 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004272 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4273 if (data == NULL)
4274 goto out;
4275
David Quigley1775fd32013-05-22 12:50:42 -04004276 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004277 status = nfs4_do_create(dir, dentry, data);
4278
4279 nfs4_free_createdata(data);
4280out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return status;
4282}
4283
4284static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4285 struct iattr *sattr)
4286{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004287 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004289 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004291
David Quigleyaa9c2662013-05-22 12:50:44 -04004292 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4293
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004294 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4295 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004297 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4298 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4299 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 &exception);
4301 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004302 nfs4_label_release_security(label);
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return err;
4305}
4306
4307static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004308 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
David Howells2b0143b2015-03-17 22:25:59 +00004310 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 struct nfs4_readdir_arg args = {
4312 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004313 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 .pgbase = 0,
4315 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004316 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004317 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 };
4319 struct nfs4_readdir_res res;
4320 struct rpc_message msg = {
4321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4322 .rpc_argp = &args,
4323 .rpc_resp = &res,
4324 .rpc_cred = cred,
4325 };
4326 int status;
4327
Al Viro6de14722013-09-16 10:53:17 -04004328 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4329 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004330 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004331 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004333 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 -05004334 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004335 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004336 status += args.pgbase;
4337 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004338
4339 nfs_invalidate_atime(dir);
4340
Harvey Harrison3110ff82008-05-02 13:42:44 -07004341 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return status;
4343}
4344
4345static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004346 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
4348 struct nfs4_exception exception = { };
4349 int err;
4350 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004351 err = _nfs4_proc_readdir(dentry, cred, cookie,
4352 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004353 trace_nfs4_readdir(d_inode(dentry), err);
4354 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 &exception);
4356 } while (exception.retry);
4357 return err;
4358}
4359
4360static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004361 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004363 struct nfs4_createdata *data;
4364 int mode = sattr->ia_mode;
4365 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004367 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4368 if (data == NULL)
4369 goto out;
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004372 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004374 data->arg.ftype = NF4BLK;
4375 data->arg.u.device.specdata1 = MAJOR(rdev);
4376 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004379 data->arg.ftype = NF4CHR;
4380 data->arg.u.device.specdata1 = MAJOR(rdev);
4381 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004382 } else if (!S_ISSOCK(mode)) {
4383 status = -EINVAL;
4384 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
David Quigley1775fd32013-05-22 12:50:42 -04004386
David Quigleyaa9c2662013-05-22 12:50:44 -04004387 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004388 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004389out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390 nfs4_free_createdata(data);
4391out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return status;
4393}
4394
4395static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4396 struct iattr *sattr, dev_t rdev)
4397{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004398 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004400 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004402
David Quigleyaa9c2662013-05-22 12:50:44 -04004403 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4404
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004405 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4406 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004408 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4409 trace_nfs4_mknod(dir, &dentry->d_name, err);
4410 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 &exception);
4412 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004413
4414 nfs4_label_release_security(label);
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return err;
4417}
4418
4419static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4420 struct nfs_fsstat *fsstat)
4421{
4422 struct nfs4_statfs_arg args = {
4423 .fh = fhandle,
4424 .bitmask = server->attr_bitmask,
4425 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004426 struct nfs4_statfs_res res = {
4427 .fsstat = fsstat,
4428 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 struct rpc_message msg = {
4430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4431 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004432 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 };
4434
Trond Myklebust0e574af2005-10-27 22:12:38 -04004435 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004436 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437}
4438
4439static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4440{
4441 struct nfs4_exception exception = { };
4442 int err;
4443 do {
4444 err = nfs4_handle_exception(server,
4445 _nfs4_proc_statfs(server, fhandle, fsstat),
4446 &exception);
4447 } while (exception.retry);
4448 return err;
4449}
4450
4451static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4452 struct nfs_fsinfo *fsinfo)
4453{
4454 struct nfs4_fsinfo_arg args = {
4455 .fh = fhandle,
4456 .bitmask = server->attr_bitmask,
4457 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004458 struct nfs4_fsinfo_res res = {
4459 .fsinfo = fsinfo,
4460 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct rpc_message msg = {
4462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4463 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004464 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 };
4466
Bryan Schumaker7c513052011-03-24 17:12:24 +00004467 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468}
4469
4470static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4471{
4472 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004473 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 int err;
4475
4476 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004477 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004478 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004479 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004480 nfs4_set_lease_period(server->nfs_client,
4481 fsinfo->lease_time * HZ,
4482 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004483 break;
4484 }
4485 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 } while (exception.retry);
4487 return err;
4488}
4489
4490static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4491{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004492 int error;
4493
Trond Myklebust0e574af2005-10-27 22:12:38 -04004494 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004495 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004496 if (error == 0) {
4497 /* block layout checks this! */
4498 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004499 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004500 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004501
4502 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
4505static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4506 struct nfs_pathconf *pathconf)
4507{
4508 struct nfs4_pathconf_arg args = {
4509 .fh = fhandle,
4510 .bitmask = server->attr_bitmask,
4511 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004512 struct nfs4_pathconf_res res = {
4513 .pathconf = pathconf,
4514 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 struct rpc_message msg = {
4516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4517 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004518 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 };
4520
4521 /* None of the pathconf attributes are mandatory to implement */
4522 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4523 memset(pathconf, 0, sizeof(*pathconf));
4524 return 0;
4525 }
4526
Trond Myklebust0e574af2005-10-27 22:12:38 -04004527 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004528 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
4530
4531static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4532 struct nfs_pathconf *pathconf)
4533{
4534 struct nfs4_exception exception = { };
4535 int err;
4536
4537 do {
4538 err = nfs4_handle_exception(server,
4539 _nfs4_proc_pathconf(server, fhandle, pathconf),
4540 &exception);
4541 } while (exception.retry);
4542 return err;
4543}
4544
Trond Myklebust5521abf2013-03-16 20:54:34 -04004545int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004546 const struct nfs_open_context *ctx,
4547 const struct nfs_lock_context *l_ctx,
4548 fmode_t fmode)
4549{
NeilBrown17393472016-10-13 15:26:47 +11004550 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004551}
4552EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4553
Trond Myklebust5521abf2013-03-16 20:54:34 -04004554static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4555 const struct nfs_open_context *ctx,
4556 const struct nfs_lock_context *l_ctx,
4557 fmode_t fmode)
4558{
4559 nfs4_stateid current_stateid;
4560
Trond Myklebuste1253be2014-03-05 08:44:23 -05004561 /* If the current stateid represents a lost lock, then exit */
4562 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4563 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004564 return nfs4_stateid_match(stateid, &current_stateid);
4565}
4566
4567static bool nfs4_error_stateid_expired(int err)
4568{
4569 switch (err) {
4570 case -NFS4ERR_DELEG_REVOKED:
4571 case -NFS4ERR_ADMIN_REVOKED:
4572 case -NFS4ERR_BAD_STATEID:
4573 case -NFS4ERR_STALE_STATEID:
4574 case -NFS4ERR_OLD_STATEID:
4575 case -NFS4ERR_OPENMODE:
4576 case -NFS4ERR_EXPIRED:
4577 return true;
4578 }
4579 return false;
4580}
4581
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004582static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004584 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004586 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004587 if (task->tk_status < 0) {
4588 struct nfs4_exception exception = {
4589 .inode = hdr->inode,
4590 .state = hdr->args.context->state,
4591 .stateid = &hdr->args.stateid,
4592 };
4593 task->tk_status = nfs4_async_handle_exception(task,
4594 server, task->tk_status, &exception);
4595 if (exception.retry) {
4596 rpc_restart_call_prepare(task);
4597 return -EAGAIN;
4598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004602 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Trond Myklebust5521abf2013-03-16 20:54:34 -04004606static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004607 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004608{
4609
4610 if (!nfs4_error_stateid_expired(task->tk_status) ||
4611 nfs4_stateid_is_current(&args->stateid,
4612 args->context,
4613 args->lock_context,
4614 FMODE_READ))
4615 return false;
4616 rpc_restart_call_prepare(task);
4617 return true;
4618}
4619
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004621{
4622
4623 dprintk("--> %s\n", __func__);
4624
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004626 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004628 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004629 if (task->tk_status > 0)
4630 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004631 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4632 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004633}
4634
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4636 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004638 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004639 if (!hdr->pgio_done_cb)
4640 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004641 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004642 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
4644
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4646 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004647{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004648 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 &hdr->args.seq_args,
4650 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004651 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004652 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4654 hdr->args.lock_context,
4655 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004656 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004658 return -EIO;
4659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004662static int nfs4_write_done_cb(struct rpc_task *task,
4663 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004666
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004668 if (task->tk_status < 0) {
4669 struct nfs4_exception exception = {
4670 .inode = hdr->inode,
4671 .state = hdr->args.context->state,
4672 .stateid = &hdr->args.stateid,
4673 };
4674 task->tk_status = nfs4_async_handle_exception(task,
4675 NFS_SERVER(inode), task->tk_status,
4676 &exception);
4677 if (exception.retry) {
4678 rpc_restart_call_prepare(task);
4679 return -EAGAIN;
4680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004682 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004684 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004685 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687}
4688
Trond Myklebust5521abf2013-03-16 20:54:34 -04004689static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004690 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004691{
4692
4693 if (!nfs4_error_stateid_expired(task->tk_status) ||
4694 nfs4_stateid_is_current(&args->stateid,
4695 args->context,
4696 args->lock_context,
4697 FMODE_WRITE))
4698 return false;
4699 rpc_restart_call_prepare(task);
4700 return true;
4701}
4702
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004704{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004706 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004708 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4710 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004711}
4712
Trond Myklebust5a37f852012-04-28 14:55:16 -04004713static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004714bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004715{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004716 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004717 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004718 return false;
4719 /* Otherwise, request attributes if and only if we don't hold
4720 * a delegation
4721 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004722 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004723}
Fred Isamana69aef12011-03-03 15:13:47 +00004724
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004725static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4726 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004729
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004730 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4731 hdr->args.bitmask = NULL;
4732 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004733 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004735
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004736 if (!hdr->pgio_done_cb)
4737 hdr->pgio_done_cb = nfs4_write_done_cb;
4738 hdr->res.server = server;
4739 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004741 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004742 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Fred Isaman0b7c0152012-04-20 14:47:39 -04004745static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4746{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004747 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004748 &data->args.seq_args,
4749 &data->res.seq_res,
4750 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
Fred Isaman0b7c0152012-04-20 14:47:39 -04004753static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004756
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004757 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004758 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4759 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004760 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004761 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Fred Isaman0b7c0152012-04-20 14:47:39 -04004766static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004767{
4768 if (!nfs4_sequence_done(task, &data->res.seq_res))
4769 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004770 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004771}
4772
Fred Isaman0b7c0152012-04-20 14:47:39 -04004773static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004775 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004776
Fred Isaman0b7c0152012-04-20 14:47:39 -04004777 if (data->commit_done_cb == NULL)
4778 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004779 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004780 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004784struct nfs4_renewdata {
4785 struct nfs_client *client;
4786 unsigned long timestamp;
4787};
4788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789/*
4790 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4791 * standalone procedure for queueing an asynchronous RENEW.
4792 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004793static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004794{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004795 struct nfs4_renewdata *data = calldata;
4796 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004797
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004798 if (atomic_read(&clp->cl_count) > 1)
4799 nfs4_schedule_state_renewal(clp);
4800 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004801 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004802}
4803
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004804static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004806 struct nfs4_renewdata *data = calldata;
4807 struct nfs_client *clp = data->client;
4808 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004810 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004811 switch (task->tk_status) {
4812 case 0:
4813 break;
4814 case -NFS4ERR_LEASE_MOVED:
4815 nfs4_schedule_lease_moved_recovery(clp);
4816 break;
4817 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004818 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004819 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4820 return;
4821 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004822 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004823 return;
4824 }
4825 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004827 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004830static const struct rpc_call_ops nfs4_renew_ops = {
4831 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004832 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004833};
4834
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004835static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
4837 struct rpc_message msg = {
4838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4839 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004840 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004842 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004844 if (renew_flags == 0)
4845 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004846 if (!atomic_inc_not_zero(&clp->cl_count))
4847 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004848 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004849 if (data == NULL)
4850 return -ENOMEM;
4851 data->client = clp;
4852 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004853 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004854 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004857static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
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 };
4864 unsigned long now = jiffies;
4865 int status;
4866
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004867 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 if (status < 0)
4869 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004870 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return 0;
4872}
4873
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004874static inline int nfs4_server_supports_acls(struct nfs_server *server)
4875{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004876 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004877}
4878
Trond Myklebust21f498c2012-08-24 10:59:25 -04004879/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4880 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004881 * the stack.
4882 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004883#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004884
Neil Hormane9e3d722011-03-04 19:26:03 -05004885static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004886 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004887{
4888 struct page *newpage, **spages;
4889 int rc = 0;
4890 size_t len;
4891 spages = pages;
4892
4893 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004894 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004895 newpage = alloc_page(GFP_KERNEL);
4896
4897 if (newpage == NULL)
4898 goto unwind;
4899 memcpy(page_address(newpage), buf, len);
4900 buf += len;
4901 buflen -= len;
4902 *pages++ = newpage;
4903 rc++;
4904 } while (buflen != 0);
4905
4906 return rc;
4907
4908unwind:
4909 for(; rc > 0; rc--)
4910 __free_page(spages[rc-1]);
4911 return -ENOMEM;
4912}
4913
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004914struct nfs4_cached_acl {
4915 int cached;
4916 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004917 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004918};
4919
4920static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004921{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004922 struct nfs_inode *nfsi = NFS_I(inode);
4923
4924 spin_lock(&inode->i_lock);
4925 kfree(nfsi->nfs4_acl);
4926 nfsi->nfs4_acl = acl;
4927 spin_unlock(&inode->i_lock);
4928}
4929
4930static void nfs4_zap_acl_attr(struct inode *inode)
4931{
4932 nfs4_set_cached_acl(inode, NULL);
4933}
4934
4935static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4936{
4937 struct nfs_inode *nfsi = NFS_I(inode);
4938 struct nfs4_cached_acl *acl;
4939 int ret = -ENOENT;
4940
4941 spin_lock(&inode->i_lock);
4942 acl = nfsi->nfs4_acl;
4943 if (acl == NULL)
4944 goto out;
4945 if (buf == NULL) /* user is just asking for length */
4946 goto out_len;
4947 if (acl->cached == 0)
4948 goto out;
4949 ret = -ERANGE; /* see getxattr(2) man page */
4950 if (acl->len > buflen)
4951 goto out;
4952 memcpy(buf, acl->data, acl->len);
4953out_len:
4954 ret = acl->len;
4955out:
4956 spin_unlock(&inode->i_lock);
4957 return ret;
4958}
4959
Sachin Prabhu5794d212012-04-17 14:36:40 +01004960static 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 +00004961{
4962 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004963 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004964
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004965 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004966 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004967 if (acl == NULL)
4968 goto out;
4969 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004970 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004971 } else {
4972 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4973 if (acl == NULL)
4974 goto out;
4975 acl->cached = 0;
4976 }
4977 acl->len = acl_len;
4978out:
4979 nfs4_set_cached_acl(inode, acl);
4980}
4981
Andy Adamsonbf118a32011-12-07 11:55:27 -05004982/*
4983 * The getxattr API returns the required buffer length when called with a
4984 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4985 * the required buf. On a NULL buf, we send a page of data to the server
4986 * guessing that the ACL request can be serviced by a page. If so, we cache
4987 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4988 * the cache. If not so, we throw away the page, and cache the required
4989 * length. The next getxattr call will then produce another round trip to
4990 * the server, this time with the input buf of the required size.
4991 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004992static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004993{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004994 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004995 struct nfs_getaclargs args = {
4996 .fh = NFS_FH(inode),
4997 .acl_pages = pages,
4998 .acl_len = buflen,
4999 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005000 struct nfs_getaclres res = {
5001 .acl_len = buflen,
5002 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5005 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005006 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005007 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005008 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5009 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005010
Andy Adamsonbf118a32011-12-07 11:55:27 -05005011 /* As long as we're doing a round trip to the server anyway,
5012 * let's be prepared for a page of acl data. */
5013 if (npages == 0)
5014 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005015 if (npages > ARRAY_SIZE(pages))
5016 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005017
Andy Adamsonbf118a32011-12-07 11:55:27 -05005018 for (i = 0; i < npages; i++) {
5019 pages[i] = alloc_page(GFP_KERNEL);
5020 if (!pages[i])
5021 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005022 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005023
5024 /* for decoding across pages */
5025 res.acl_scratch = alloc_page(GFP_KERNEL);
5026 if (!res.acl_scratch)
5027 goto out_free;
5028
Andy Adamsonbf118a32011-12-07 11:55:27 -05005029 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005030
Peng Taode040be2012-01-10 22:42:47 +08005031 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005032 __func__, buf, buflen, npages, args.acl_len);
5033 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5034 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005035 if (ret)
5036 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005037
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005038 /* Handle the case where the passed-in buffer is too short */
5039 if (res.acl_flags & NFS4_ACL_TRUNC) {
5040 /* Did the user only issue a request for the acl length? */
5041 if (buf == NULL)
5042 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005043 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005044 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005046 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005047 if (buf) {
5048 if (res.acl_len > buflen) {
5049 ret = -ERANGE;
5050 goto out_free;
5051 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005052 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005053 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005054out_ok:
5055 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005057 for (i = 0; i < npages; i++)
5058 if (pages[i])
5059 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005060 if (res.acl_scratch)
5061 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005062 return ret;
5063}
5064
Trond Myklebust16b42892006-08-24 12:27:15 -04005065static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5066{
5067 struct nfs4_exception exception = { };
5068 ssize_t ret;
5069 do {
5070 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005071 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005072 if (ret >= 0)
5073 break;
5074 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5075 } while (exception.retry);
5076 return ret;
5077}
5078
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005079static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5080{
5081 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005082 int ret;
5083
5084 if (!nfs4_server_supports_acls(server))
5085 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005086 ret = nfs_revalidate_inode(server, inode);
5087 if (ret < 0)
5088 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005089 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5090 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005091 ret = nfs4_read_cached_acl(inode, buf, buflen);
5092 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005093 /* -ENOENT is returned if there is no ACL or if there is an ACL
5094 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005095 return ret;
5096 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005097}
5098
Trond Myklebust16b42892006-08-24 12:27:15 -04005099static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005100{
5101 struct nfs_server *server = NFS_SERVER(inode);
5102 struct page *pages[NFS4ACL_MAXPAGES];
5103 struct nfs_setaclargs arg = {
5104 .fh = NFS_FH(inode),
5105 .acl_pages = pages,
5106 .acl_len = buflen,
5107 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005108 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005109 struct rpc_message msg = {
5110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5111 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005112 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005113 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005114 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005115 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005116
5117 if (!nfs4_server_supports_acls(server))
5118 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005119 if (npages > ARRAY_SIZE(pages))
5120 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005121 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005122 if (i < 0)
5123 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005124 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005125 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005126
5127 /*
5128 * Free each page after tx, so the only ref left is
5129 * held by the network stack
5130 */
5131 for (; i > 0; i--)
5132 put_page(pages[i-1]);
5133
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005134 /*
5135 * Acl update can result in inode attribute update.
5136 * so mark the attribute cache invalid.
5137 */
5138 spin_lock(&inode->i_lock);
5139 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5140 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005141 nfs_access_zap_cache(inode);
5142 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005143 return ret;
5144}
5145
Trond Myklebust16b42892006-08-24 12:27:15 -04005146static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5147{
5148 struct nfs4_exception exception = { };
5149 int err;
5150 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005151 err = __nfs4_proc_set_acl(inode, buf, buflen);
5152 trace_nfs4_set_acl(inode, err);
5153 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005154 &exception);
5155 } while (exception.retry);
5156 return err;
5157}
5158
David Quigleyaa9c2662013-05-22 12:50:44 -04005159#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5160static int _nfs4_get_security_label(struct inode *inode, void *buf,
5161 size_t buflen)
5162{
5163 struct nfs_server *server = NFS_SERVER(inode);
5164 struct nfs_fattr fattr;
5165 struct nfs4_label label = {0, 0, buflen, buf};
5166
5167 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005168 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005169 .fh = NFS_FH(inode),
5170 .bitmask = bitmask,
5171 };
5172 struct nfs4_getattr_res res = {
5173 .fattr = &fattr,
5174 .label = &label,
5175 .server = server,
5176 };
5177 struct rpc_message msg = {
5178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005179 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005180 .rpc_resp = &res,
5181 };
5182 int ret;
5183
5184 nfs_fattr_init(&fattr);
5185
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005186 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005187 if (ret)
5188 return ret;
5189 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5190 return -ENOENT;
5191 if (buflen < label.len)
5192 return -ERANGE;
5193 return 0;
5194}
5195
5196static int nfs4_get_security_label(struct inode *inode, void *buf,
5197 size_t buflen)
5198{
5199 struct nfs4_exception exception = { };
5200 int err;
5201
5202 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5203 return -EOPNOTSUPP;
5204
5205 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005206 err = _nfs4_get_security_label(inode, buf, buflen);
5207 trace_nfs4_get_security_label(inode, err);
5208 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005209 &exception);
5210 } while (exception.retry);
5211 return err;
5212}
5213
5214static int _nfs4_do_set_security_label(struct inode *inode,
5215 struct nfs4_label *ilabel,
5216 struct nfs_fattr *fattr,
5217 struct nfs4_label *olabel)
5218{
5219
5220 struct iattr sattr = {0};
5221 struct nfs_server *server = NFS_SERVER(inode);
5222 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005223 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005224 .fh = NFS_FH(inode),
5225 .iap = &sattr,
5226 .server = server,
5227 .bitmask = bitmask,
5228 .label = ilabel,
5229 };
5230 struct nfs_setattrres res = {
5231 .fattr = fattr,
5232 .label = olabel,
5233 .server = server,
5234 };
5235 struct rpc_message msg = {
5236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005237 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005238 .rpc_resp = &res,
5239 };
5240 int status;
5241
Jeff Layton12207f62013-11-01 10:49:32 -04005242 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005243
Jeff Layton12207f62013-11-01 10:49:32 -04005244 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005245 if (status)
5246 dprintk("%s failed: %d\n", __func__, status);
5247
5248 return status;
5249}
5250
5251static int nfs4_do_set_security_label(struct inode *inode,
5252 struct nfs4_label *ilabel,
5253 struct nfs_fattr *fattr,
5254 struct nfs4_label *olabel)
5255{
5256 struct nfs4_exception exception = { };
5257 int err;
5258
5259 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005260 err = _nfs4_do_set_security_label(inode, ilabel,
5261 fattr, olabel);
5262 trace_nfs4_set_security_label(inode, err);
5263 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005264 &exception);
5265 } while (exception.retry);
5266 return err;
5267}
5268
5269static int
Al Viro59301222016-05-27 10:19:30 -04005270nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005271{
5272 struct nfs4_label ilabel, *olabel = NULL;
5273 struct nfs_fattr fattr;
5274 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005275 int status;
5276
5277 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5278 return -EOPNOTSUPP;
5279
5280 nfs_fattr_init(&fattr);
5281
5282 ilabel.pi = 0;
5283 ilabel.lfs = 0;
5284 ilabel.label = (char *)buf;
5285 ilabel.len = buflen;
5286
5287 cred = rpc_lookup_cred();
5288 if (IS_ERR(cred))
5289 return PTR_ERR(cred);
5290
5291 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5292 if (IS_ERR(olabel)) {
5293 status = -PTR_ERR(olabel);
5294 goto out;
5295 }
5296
5297 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5298 if (status == 0)
5299 nfs_setsecurity(inode, &fattr, olabel);
5300
5301 nfs4_label_free(olabel);
5302out:
5303 put_rpccred(cred);
5304 return status;
5305}
5306#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5307
5308
Chuck Leverf0920752012-05-21 22:45:41 -04005309static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5310 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005311{
5312 __be32 verf[2];
5313
Chuck Lever2c820d92012-05-21 22:45:33 -04005314 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5315 /* An impossible timestamp guarantees this value
5316 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005317 verf[0] = cpu_to_be32(U32_MAX);
5318 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005319 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005320 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005321 u64 ns = ktime_to_ns(nn->boot_time);
5322
5323 verf[0] = cpu_to_be32(ns >> 32);
5324 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005325 }
Chuck Levercd937102012-03-02 17:14:31 -05005326 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5327}
5328
Jeff Laytona3192682015-06-09 19:43:59 -04005329static int
5330nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005331{
Jeff Laytona3192682015-06-09 19:43:59 -04005332 size_t len;
5333 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005334
Trond Myklebustceb3a162015-01-03 15:16:04 -05005335 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005336 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005337
Jeff Laytona3192682015-06-09 19:43:59 -04005338 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005339 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005340 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5341 1 +
5342 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5343 1;
5344 rcu_read_unlock();
5345
5346 if (len > NFS4_OPAQUE_LIMIT + 1)
5347 return -EINVAL;
5348
5349 /*
5350 * Since this string is allocated at mount time, and held until the
5351 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5352 * about a memory-reclaim deadlock.
5353 */
5354 str = kmalloc(len, GFP_KERNEL);
5355 if (!str)
5356 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005357
Chuck Levere984a552012-09-14 17:24:21 -04005358 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005359 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005360 clp->cl_ipaddr,
5361 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5362 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005363 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005364
Jeff Laytona3192682015-06-09 19:43:59 -04005365 clp->cl_owner_id = str;
5366 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005367}
5368
Jeff Layton873e3852015-06-09 19:44:00 -04005369static int
5370nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005371{
Jeff Layton873e3852015-06-09 19:44:00 -04005372 size_t len;
5373 char *str;
5374
5375 len = 10 + 10 + 1 + 10 + 1 +
5376 strlen(nfs4_client_id_uniquifier) + 1 +
5377 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5378
5379 if (len > NFS4_OPAQUE_LIMIT + 1)
5380 return -EINVAL;
5381
5382 /*
5383 * Since this string is allocated at mount time, and held until the
5384 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5385 * about a memory-reclaim deadlock.
5386 */
5387 str = kmalloc(len, GFP_KERNEL);
5388 if (!str)
5389 return -ENOMEM;
5390
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005391 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005392 clp->rpc_ops->version, clp->cl_minorversion,
5393 nfs4_client_id_uniquifier,
5394 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005395 clp->cl_owner_id = str;
5396 return 0;
5397}
5398
5399static int
5400nfs4_init_uniform_client_string(struct nfs_client *clp)
5401{
Jeff Layton873e3852015-06-09 19:44:00 -04005402 size_t len;
5403 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005404
5405 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005406 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005407
5408 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005409 return nfs4_init_uniquifier_client_string(clp);
5410
5411 len = 10 + 10 + 1 + 10 + 1 +
5412 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5413
5414 if (len > NFS4_OPAQUE_LIMIT + 1)
5415 return -EINVAL;
5416
5417 /*
5418 * Since this string is allocated at mount time, and held until the
5419 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5420 * about a memory-reclaim deadlock.
5421 */
5422 str = kmalloc(len, GFP_KERNEL);
5423 if (!str)
5424 return -ENOMEM;
5425
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005426 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005427 clp->rpc_ops->version, clp->cl_minorversion,
5428 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005429 clp->cl_owner_id = str;
5430 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005431}
5432
Chuck Lever706cb8d2014-03-12 12:51:47 -04005433/*
5434 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5435 * services. Advertise one based on the address family of the
5436 * clientaddr.
5437 */
5438static unsigned int
5439nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5440{
5441 if (strchr(clp->cl_ipaddr, ':') != NULL)
5442 return scnprintf(buf, len, "tcp6");
5443 else
5444 return scnprintf(buf, len, "tcp");
5445}
5446
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005447static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5448{
5449 struct nfs4_setclientid *sc = calldata;
5450
5451 if (task->tk_status == 0)
5452 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5453}
5454
5455static const struct rpc_call_ops nfs4_setclientid_ops = {
5456 .rpc_call_done = nfs4_setclientid_done,
5457};
5458
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005459/**
5460 * nfs4_proc_setclientid - Negotiate client ID
5461 * @clp: state data structure
5462 * @program: RPC program for NFSv4 callback service
5463 * @port: IP port number for NFS4 callback service
5464 * @cred: RPC credential to use for this call
5465 * @res: where to place the result
5466 *
5467 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5468 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005469int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5470 unsigned short port, struct rpc_cred *cred,
5471 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472{
5473 nfs4_verifier sc_verifier;
5474 struct nfs4_setclientid setclientid = {
5475 .sc_verifier = &sc_verifier,
5476 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005477 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 };
5479 struct rpc_message msg = {
5480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5481 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005482 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005483 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005485 struct rpc_task *task;
5486 struct rpc_task_setup task_setup_data = {
5487 .rpc_client = clp->cl_rpcclient,
5488 .rpc_message = &msg,
5489 .callback_ops = &nfs4_setclientid_ops,
5490 .callback_data = &setclientid,
5491 .flags = RPC_TASK_TIMEOUT,
5492 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005493 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
Chuck Leverde734832012-07-11 16:30:50 -04005495 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005496 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005497
5498 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5499 status = nfs4_init_uniform_client_string(clp);
5500 else
Jeff Laytona3192682015-06-09 19:43:59 -04005501 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005502
5503 if (status)
5504 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005505
Chuck Leverde734832012-07-11 16:30:50 -04005506 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005507 setclientid.sc_netid_len =
5508 nfs4_init_callback_netid(clp,
5509 setclientid.sc_netid,
5510 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005511 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005512 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 clp->cl_ipaddr, port >> 8, port & 255);
5514
Jeff Layton3a6bb732015-06-09 19:43:57 -04005515 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005516 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005517 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005518 task = rpc_run_task(&task_setup_data);
5519 if (IS_ERR(task)) {
5520 status = PTR_ERR(task);
5521 goto out;
5522 }
5523 status = task->tk_status;
5524 if (setclientid.sc_cred) {
5525 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5526 put_rpccred(setclientid.sc_cred);
5527 }
5528 rpc_put_task(task);
5529out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005530 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005531 dprintk("NFS reply setclientid: %d\n", status);
5532 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533}
5534
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005535/**
5536 * nfs4_proc_setclientid_confirm - Confirm client ID
5537 * @clp: state data structure
5538 * @res: result of a previous SETCLIENTID
5539 * @cred: RPC credential to use for this call
5540 *
5541 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5542 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005543int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005544 struct nfs4_setclientid_res *arg,
5545 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 struct rpc_message msg = {
5548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005549 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005550 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 int status;
5553
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005554 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5555 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5556 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005557 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005558 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005559 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 return status;
5561}
5562
Trond Myklebustfe650402006-01-03 09:55:18 +01005563struct nfs4_delegreturndata {
5564 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005565 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005566 struct nfs_fh fh;
5567 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005568 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005569 struct {
5570 struct nfs4_layoutreturn_args arg;
5571 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005572 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005573 u32 roc_barrier;
5574 bool roc;
5575 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005576 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005577 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005578 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005579};
5580
Trond Myklebustfe650402006-01-03 09:55:18 +01005581static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5582{
5583 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005584
Trond Myklebust14516c32010-07-31 14:29:06 -04005585 if (!nfs4_sequence_done(task, &data->res.seq_res))
5586 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005587
Trond Myklebustca8acf82013-08-13 10:36:56 -04005588 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005589
5590 /* Handle Layoutreturn errors */
5591 if (data->args.lr_args && task->tk_status != 0) {
5592 switch(data->res.lr_ret) {
5593 default:
5594 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5595 break;
5596 case 0:
5597 data->args.lr_args = NULL;
5598 data->res.lr_res = NULL;
5599 break;
5600 case -NFS4ERR_ADMIN_REVOKED:
5601 case -NFS4ERR_DELEG_REVOKED:
5602 case -NFS4ERR_EXPIRED:
5603 case -NFS4ERR_BAD_STATEID:
5604 case -NFS4ERR_OLD_STATEID:
5605 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5606 case -NFS4ERR_WRONG_CRED:
5607 data->args.lr_args = NULL;
5608 data->res.lr_res = NULL;
5609 data->res.lr_ret = 0;
5610 rpc_restart_call_prepare(task);
5611 return;
5612 }
5613 }
5614
Ricardo Labiaga79708862009-12-07 09:23:21 -05005615 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005616 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005617 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005618 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005619 case -NFS4ERR_ADMIN_REVOKED:
5620 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005621 case -NFS4ERR_EXPIRED:
5622 nfs4_free_revoked_stateid(data->res.server,
5623 data->args.stateid,
5624 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005625 case -NFS4ERR_BAD_STATEID:
5626 case -NFS4ERR_OLD_STATEID:
5627 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005628 task->tk_status = 0;
5629 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005630 case -NFS4ERR_ACCESS:
5631 if (data->args.bitmask) {
5632 data->args.bitmask = NULL;
5633 data->res.fattr = NULL;
5634 task->tk_status = 0;
5635 rpc_restart_call_prepare(task);
5636 return;
5637 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005638 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005639 if (nfs4_async_handle_error(task, data->res.server,
5640 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005641 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005642 return;
5643 }
5644 }
5645 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005646}
5647
5648static void nfs4_delegreturn_release(void *calldata)
5649{
Peng Tao039b7562014-07-03 13:05:02 +08005650 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005651 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005652
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005653 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005654 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005655 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5656 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005657 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005658 nfs_iput_and_deactive(inode);
5659 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005660 kfree(calldata);
5661}
5662
Andy Adamson938e1012009-04-01 09:22:28 -04005663static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5664{
5665 struct nfs4_delegreturndata *d_data;
5666
5667 d_data = (struct nfs4_delegreturndata *)data;
5668
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005669 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005670 return;
5671
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005672 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005673 &d_data->args.seq_args,
5674 &d_data->res.seq_res,
5675 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005676}
Andy Adamson938e1012009-04-01 09:22:28 -04005677
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005678static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005679 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005680 .rpc_call_done = nfs4_delegreturn_done,
5681 .rpc_release = nfs4_delegreturn_release,
5682};
5683
Trond Myklebuste6f81072008-01-24 18:14:34 -05005684static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005685{
5686 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005687 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005688 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005689 struct rpc_message msg = {
5690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5691 .rpc_cred = cred,
5692 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005693 struct rpc_task_setup task_setup_data = {
5694 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005695 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005696 .callback_ops = &nfs4_delegreturn_ops,
5697 .flags = RPC_TASK_ASYNC,
5698 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005699 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005700
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005701 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005702 if (data == NULL)
5703 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005704 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005705
5706 nfs4_state_protect(server->nfs_client,
5707 NFS_SP4_MACH_CRED_CLEANUP,
5708 &task_setup_data.rpc_client, &msg);
5709
Trond Myklebustfe650402006-01-03 09:55:18 +01005710 data->args.fhandle = &data->fh;
5711 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005712 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005713 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005714 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005715 data->res.fattr = &data->fattr;
5716 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005717 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005718 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005719 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005720 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005721 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005722 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005723 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005724 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005725 if (data->lr.roc) {
5726 data->args.lr_args = &data->lr.arg;
5727 data->res.lr_res = &data->lr.res;
5728 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005729 } else if (data->lr.roc) {
5730 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5731 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005732 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005733
Trond Myklebustc970aa82007-07-14 15:39:59 -04005734 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005735 msg.rpc_argp = &data->args;
5736 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005737 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005738 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005739 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005740 if (!issync)
5741 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005742 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005743 if (status != 0)
5744 goto out;
5745 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005746out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005747 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005748 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
Trond Myklebuste6f81072008-01-24 18:14:34 -05005751int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752{
5753 struct nfs_server *server = NFS_SERVER(inode);
5754 struct nfs4_exception exception = { };
5755 int err;
5756 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005757 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005758 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 switch (err) {
5760 case -NFS4ERR_STALE_STATEID:
5761 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 case 0:
5763 return 0;
5764 }
5765 err = nfs4_handle_exception(server, err, &exception);
5766 } while (exception.retry);
5767 return err;
5768}
5769
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5771{
5772 struct inode *inode = state->inode;
5773 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005774 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005775 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005777 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005779 struct nfs_lockt_res res = {
5780 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 };
5782 struct rpc_message msg = {
5783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5784 .rpc_argp = &arg,
5785 .rpc_resp = &res,
5786 .rpc_cred = state->owner->so_cred,
5787 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 struct nfs4_lock_state *lsp;
5789 int status;
5790
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005791 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005792 status = nfs4_set_lock_state(state, request);
5793 if (status != 0)
5794 goto out;
5795 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005796 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005797 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005798 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005799 switch (status) {
5800 case 0:
5801 request->fl_type = F_UNLCK;
5802 break;
5803 case -NFS4ERR_DENIED:
5804 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005806 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005807 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005808out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 return status;
5810}
5811
5812static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5813{
5814 struct nfs4_exception exception = { };
5815 int err;
5816
5817 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005818 err = _nfs4_proc_getlk(state, cmd, request);
5819 trace_nfs4_get_lock(request, state, cmd, err);
5820 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 &exception);
5822 } while (exception.retry);
5823 return err;
5824}
5825
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005827 struct nfs_locku_args arg;
5828 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829 struct nfs4_lock_state *lsp;
5830 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005831 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005832 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005833 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005834};
5835
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005836static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5837 struct nfs_open_context *ctx,
5838 struct nfs4_lock_state *lsp,
5839 struct nfs_seqid *seqid)
5840{
5841 struct nfs4_unlockdata *p;
5842 struct inode *inode = lsp->ls_state->inode;
5843
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005844 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005845 if (p == NULL)
5846 return NULL;
5847 p->arg.fh = NFS_FH(inode);
5848 p->arg.fl = &p->fl;
5849 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005850 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005851 p->lsp = lsp;
5852 atomic_inc(&lsp->ls_count);
5853 /* Ensure we don't close file until we're done freeing locks! */
5854 p->ctx = get_nfs_open_context(ctx);
5855 memcpy(&p->fl, fl, sizeof(p->fl));
5856 p->server = NFS_SERVER(inode);
5857 return p;
5858}
5859
Trond Myklebust06f814a2006-01-03 09:55:07 +01005860static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005861{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005862 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005863 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005864 nfs4_put_lock_state(calldata->lsp);
5865 put_nfs_open_context(calldata->ctx);
5866 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005867}
5868
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005869static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005870{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005871 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872
Trond Myklebust14516c32010-07-31 14:29:06 -04005873 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5874 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875 switch (task->tk_status) {
5876 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005877 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005878 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005879 if (nfs4_update_lock_stateid(calldata->lsp,
5880 &calldata->res.stateid))
5881 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005882 case -NFS4ERR_ADMIN_REVOKED:
5883 case -NFS4ERR_EXPIRED:
5884 nfs4_free_revoked_stateid(calldata->server,
5885 &calldata->arg.stateid,
5886 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005887 case -NFS4ERR_BAD_STATEID:
5888 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005889 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005890 if (!nfs4_stateid_match(&calldata->arg.stateid,
5891 &calldata->lsp->ls_stateid))
5892 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005893 break;
5894 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005895 if (nfs4_async_handle_error(task, calldata->server,
5896 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005897 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005898 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005899 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005900}
5901
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005902static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005903{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005904 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005906 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005907 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005908 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005909 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005910 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005911 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005912 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005913 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005914 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005915 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005916 &calldata->res.seq_res,
5917 task) != 0)
5918 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005919 return;
5920out_no_action:
5921 task->tk_action = NULL;
5922out_wait:
5923 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924}
5925
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005926static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005927 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005928 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005929 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005930};
5931
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005932static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5933 struct nfs_open_context *ctx,
5934 struct nfs4_lock_state *lsp,
5935 struct nfs_seqid *seqid)
5936{
5937 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005938 struct rpc_message msg = {
5939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5940 .rpc_cred = ctx->cred,
5941 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005944 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005945 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005946 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005947 .flags = RPC_TASK_ASYNC,
5948 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005950 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5951 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5952
Frank Filz137d6ac2007-07-09 15:32:29 -07005953 /* Ensure this is an unlock - when canceling a lock, the
5954 * canceled lock is passed in, and it won't be an unlock.
5955 */
5956 fl->fl_type = F_UNLCK;
5957
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005958 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5959 if (data == NULL) {
5960 nfs_free_seqid(seqid);
5961 return ERR_PTR(-ENOMEM);
5962 }
5963
Chuck Levera9c92d62013-08-09 12:48:18 -04005964 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005965 msg.rpc_argp = &data->arg;
5966 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005967 task_setup_data.callback_data = data;
5968 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005969}
5970
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5972{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005973 struct inode *inode = state->inode;
5974 struct nfs4_state_owner *sp = state->owner;
5975 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005976 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005977 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005978 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005979 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005980 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005981 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Trond Myklebust9b073572006-06-29 16:38:34 -04005983 status = nfs4_set_lock_state(state, request);
5984 /* Unlock _before_ we do the RPC call */
5985 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005986 /* Exclude nfs_delegation_claim_locks() */
5987 mutex_lock(&sp->so_delegreturn_mutex);
5988 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005989 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005990 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005991 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005992 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005993 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005994 }
5995 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005996 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005997 if (status != 0)
5998 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005999 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006000 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006001 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6002 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006003 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6004 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006005 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006006 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006007 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006008 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006009 status = PTR_ERR(task);
6010 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006011 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006013 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006014out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006015 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006016 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006017 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018}
6019
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020struct nfs4_lockdata {
6021 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006022 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006023 struct nfs4_lock_state *lsp;
6024 struct nfs_open_context *ctx;
6025 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006026 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006027 int rpc_status;
6028 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006029 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030};
6031
6032static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006033 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6034 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035{
6036 struct nfs4_lockdata *p;
6037 struct inode *inode = lsp->ls_state->inode;
6038 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006039 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006041 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006042 if (p == NULL)
6043 return NULL;
6044
6045 p->arg.fh = NFS_FH(inode);
6046 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006047 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006048 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006049 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006050 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6051 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006052 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006053 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006054 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006055 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006056 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006057 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006058 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006059 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006060 atomic_inc(&lsp->ls_count);
6061 p->ctx = get_nfs_open_context(ctx);
6062 memcpy(&p->fl, fl, sizeof(p->fl));
6063 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006064out_free_seqid:
6065 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066out_free:
6067 kfree(p);
6068 return NULL;
6069}
6070
6071static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6072{
6073 struct nfs4_lockdata *data = calldata;
6074 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Harvey Harrison3110ff82008-05-02 13:42:44 -07006076 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006077 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006078 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006079 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006080 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006081 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006082 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006083 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006084 nfs4_stateid_copy(&data->arg.open_stateid,
6085 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006086 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006087 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006088 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006089 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006090 nfs4_stateid_copy(&data->arg.lock_stateid,
6091 &data->lsp->ls_stateid);
6092 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006093 if (!nfs4_valid_open_stateid(state)) {
6094 data->rpc_status = -EBADF;
6095 task->tk_action = NULL;
6096 goto out_release_open_seqid;
6097 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006098 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006099 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006100 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006101 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006102 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006103 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006104out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006105 nfs_release_seqid(data->arg.open_seqid);
6106out_release_lock_seqid:
6107 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006108out_wait:
6109 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006110 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006111}
6112
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6114{
6115 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006116 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006117
Harvey Harrison3110ff82008-05-02 13:42:44 -07006118 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006119
Trond Myklebust14516c32010-07-31 14:29:06 -04006120 if (!nfs4_sequence_done(task, &data->res.seq_res))
6121 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006122
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006123 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006124 switch (task->tk_status) {
6125 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006126 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006127 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006128 if (data->arg.new_lock) {
6129 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006130 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006131 rpc_restart_call_prepare(task);
6132 break;
6133 }
6134 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006135 if (data->arg.new_lock_owner != 0) {
6136 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6137 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6138 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6139 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6140 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006141 break;
6142 case -NFS4ERR_BAD_STATEID:
6143 case -NFS4ERR_OLD_STATEID:
6144 case -NFS4ERR_STALE_STATEID:
6145 case -NFS4ERR_EXPIRED:
6146 if (data->arg.new_lock_owner != 0) {
6147 if (!nfs4_stateid_match(&data->arg.open_stateid,
6148 &lsp->ls_state->open_stateid))
6149 rpc_restart_call_prepare(task);
6150 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6151 &lsp->ls_stateid))
6152 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006154 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006155}
6156
6157static void nfs4_lock_release(void *calldata)
6158{
6159 struct nfs4_lockdata *data = calldata;
6160
Harvey Harrison3110ff82008-05-02 13:42:44 -07006161 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006162 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006163 if (data->cancelled != 0) {
6164 struct rpc_task *task;
6165 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6166 data->arg.lock_seqid);
6167 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006168 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006169 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170 } else
6171 nfs_free_seqid(data->arg.lock_seqid);
6172 nfs4_put_lock_state(data->lsp);
6173 put_nfs_open_context(data->ctx);
6174 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006175 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006176}
6177
6178static const struct rpc_call_ops nfs4_lock_ops = {
6179 .rpc_call_prepare = nfs4_lock_prepare,
6180 .rpc_call_done = nfs4_lock_done,
6181 .rpc_release = nfs4_lock_release,
6182};
6183
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006184static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6185{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006186 switch (error) {
6187 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006188 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006189 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006190 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006191 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006192 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006193 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006194 break;
6195 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006196 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006197 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006198 };
6199}
6200
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006201static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202{
6203 struct nfs4_lockdata *data;
6204 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006205 struct rpc_message msg = {
6206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6207 .rpc_cred = state->owner->so_cred,
6208 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006209 struct rpc_task_setup task_setup_data = {
6210 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006211 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006212 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006213 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006214 .flags = RPC_TASK_ASYNC,
6215 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006216 int ret;
6217
Harvey Harrison3110ff82008-05-02 13:42:44 -07006218 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006219 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006220 fl->fl_u.nfs4_fl.owner,
6221 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 if (data == NULL)
6223 return -ENOMEM;
6224 if (IS_SETLKW(cmd))
6225 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006226 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006227 msg.rpc_argp = &data->arg;
6228 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006229 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006230 if (recovery_type > NFS_LOCK_NEW) {
6231 if (recovery_type == NFS_LOCK_RECLAIM)
6232 data->arg.reclaim = NFS_LOCK_RECLAIM;
6233 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006234 } else
6235 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006236 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006237 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006238 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006239 ret = nfs4_wait_for_completion_rpc_task(task);
6240 if (ret == 0) {
6241 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006242 if (ret)
6243 nfs4_handle_setlk_error(data->server, data->lsp,
6244 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006245 } else
6246 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006247 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006248 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006249 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
6253static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6254{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006255 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006256 struct nfs4_exception exception = {
6257 .inode = state->inode,
6258 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006259 int err;
6260
6261 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006262 /* Cache the lock if possible... */
6263 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6264 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006265 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006266 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006267 break;
6268 nfs4_handle_exception(server, err, &exception);
6269 } while (exception.retry);
6270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271}
6272
6273static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6274{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006275 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006276 struct nfs4_exception exception = {
6277 .inode = state->inode,
6278 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006279 int err;
6280
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006281 err = nfs4_set_lock_state(state, request);
6282 if (err != 0)
6283 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006284 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006285 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6286 return 0;
6287 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006288 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006289 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6290 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006291 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006292 switch (err) {
6293 default:
6294 goto out;
6295 case -NFS4ERR_GRACE:
6296 case -NFS4ERR_DELAY:
6297 nfs4_handle_exception(server, err, &exception);
6298 err = 0;
6299 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006300 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006301out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303}
6304
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006305#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006306static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6307{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006308 struct nfs4_lock_state *lsp;
6309 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006310
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006311 status = nfs4_set_lock_state(state, request);
6312 if (status != 0)
6313 return status;
6314 lsp = request->fl_u.nfs4_fl.owner;
6315 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6316 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6317 return 0;
6318 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006319 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006320}
6321#endif
6322
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6324{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006325 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006326 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006327 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006328 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
Trond Myklebust01c3b862006-06-29 16:38:39 -04006330 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006331 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006332 if (status < 0)
6333 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006334 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006335 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006336 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006337 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006338 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006339 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006340 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006341 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006342 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006343 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006344 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006345 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006346 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006347 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006348out:
6349 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 return status;
6351}
6352
6353static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6354{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006355 struct nfs4_exception exception = {
6356 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006357 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006358 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 int err;
6360
6361 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006362 err = _nfs4_proc_setlk(state, cmd, request);
6363 if (err == -NFS4ERR_DENIED)
6364 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006366 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 } while (exception.retry);
6368 return err;
6369}
6370
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006371#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6372#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6373
6374static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006375nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6376 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006377{
6378 int status = -ERESTARTSYS;
6379 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6380
6381 while(!signalled()) {
6382 status = nfs4_proc_setlk(state, cmd, request);
6383 if ((status != -EAGAIN) || IS_SETLK(cmd))
6384 break;
6385 freezable_schedule_timeout_interruptible(timeout);
6386 timeout *= 2;
6387 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6388 status = -ERESTARTSYS;
6389 }
6390 return status;
6391}
6392
Jeff Laytona1d617d82016-09-17 18:17:39 -04006393#ifdef CONFIG_NFS_V4_1
6394struct nfs4_lock_waiter {
6395 struct task_struct *task;
6396 struct inode *inode;
6397 struct nfs_lowner *owner;
6398 bool notified;
6399};
6400
6401static int
6402nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6403{
6404 int ret;
6405 struct cb_notify_lock_args *cbnl = key;
6406 struct nfs4_lock_waiter *waiter = wait->private;
6407 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6408 *wowner = waiter->owner;
6409
6410 /* Only wake if the callback was for the same owner */
6411 if (lowner->clientid != wowner->clientid ||
6412 lowner->id != wowner->id ||
6413 lowner->s_dev != wowner->s_dev)
6414 return 0;
6415
6416 /* Make sure it's for the right inode */
6417 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6418 return 0;
6419
6420 waiter->notified = true;
6421
6422 /* override "private" so we can use default_wake_function */
6423 wait->private = waiter->task;
6424 ret = autoremove_wake_function(wait, mode, flags, key);
6425 wait->private = waiter;
6426 return ret;
6427}
6428
6429static int
6430nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6431{
6432 int status = -ERESTARTSYS;
6433 unsigned long flags;
6434 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6435 struct nfs_server *server = NFS_SERVER(state->inode);
6436 struct nfs_client *clp = server->nfs_client;
6437 wait_queue_head_t *q = &clp->cl_lock_waitq;
6438 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6439 .id = lsp->ls_seqid.owner_id,
6440 .s_dev = server->s_dev };
6441 struct nfs4_lock_waiter waiter = { .task = current,
6442 .inode = state->inode,
6443 .owner = &owner,
6444 .notified = false };
6445 wait_queue_t wait;
6446
6447 /* Don't bother with waitqueue if we don't expect a callback */
6448 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6449 return nfs4_retry_setlk_simple(state, cmd, request);
6450
6451 init_wait(&wait);
6452 wait.private = &waiter;
6453 wait.func = nfs4_wake_lock_waiter;
6454 add_wait_queue(q, &wait);
6455
6456 while(!signalled()) {
6457 status = nfs4_proc_setlk(state, cmd, request);
6458 if ((status != -EAGAIN) || IS_SETLK(cmd))
6459 break;
6460
6461 status = -ERESTARTSYS;
6462 spin_lock_irqsave(&q->lock, flags);
6463 if (waiter.notified) {
6464 spin_unlock_irqrestore(&q->lock, flags);
6465 continue;
6466 }
6467 set_current_state(TASK_INTERRUPTIBLE);
6468 spin_unlock_irqrestore(&q->lock, flags);
6469
6470 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6471 }
6472
6473 finish_wait(q, &wait);
6474 return status;
6475}
6476#else /* !CONFIG_NFS_V4_1 */
6477static inline int
6478nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6479{
6480 return nfs4_retry_setlk_simple(state, cmd, request);
6481}
6482#endif
6483
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484static int
6485nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6486{
6487 struct nfs_open_context *ctx;
6488 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 int status;
6490
6491 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006492 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 state = ctx->state;
6494
6495 if (request->fl_start < 0 || request->fl_end < 0)
6496 return -EINVAL;
6497
Trond Myklebustd9531262009-07-21 19:22:38 -04006498 if (IS_GETLK(cmd)) {
6499 if (state != NULL)
6500 return nfs4_proc_getlk(state, F_GETLK, request);
6501 return 0;
6502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503
6504 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6505 return -EINVAL;
6506
Trond Myklebustd9531262009-07-21 19:22:38 -04006507 if (request->fl_type == F_UNLCK) {
6508 if (state != NULL)
6509 return nfs4_proc_unlck(state, cmd, request);
6510 return 0;
6511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
Trond Myklebustd9531262009-07-21 19:22:38 -04006513 if (state == NULL)
6514 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006515
6516 if ((request->fl_flags & FL_POSIX) &&
6517 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6518 return -ENOLCK;
6519
Trond Myklebust55725512012-04-18 12:48:35 -04006520 /*
6521 * Don't rely on the VFS having checked the file open mode,
6522 * since it won't do this for flock() locks.
6523 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006524 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006525 case F_RDLCK:
6526 if (!(filp->f_mode & FMODE_READ))
6527 return -EBADF;
6528 break;
6529 case F_WRLCK:
6530 if (!(filp->f_mode & FMODE_WRITE))
6531 return -EBADF;
6532 }
6533
Jeff Layton1ea67db2016-09-17 18:17:37 -04006534 status = nfs4_set_lock_state(state, request);
6535 if (status != 0)
6536 return status;
6537
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006538 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539}
6540
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006541int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006542{
6543 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006544 int err;
6545
6546 err = nfs4_set_lock_state(state, fl);
6547 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006548 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006549 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006550 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006551}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006552
Trond Myklebustcf470c32012-03-07 13:49:12 -05006553struct nfs_release_lockowner_data {
6554 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006555 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006556 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006557 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006558 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006559};
6560
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006561static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6562{
6563 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006564 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006565 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6566 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006567 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006568 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006569}
6570
6571static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6572{
6573 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006574 struct nfs_server *server = data->server;
6575
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006576 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006577
6578 switch (task->tk_status) {
6579 case 0:
6580 renew_lease(server, data->timestamp);
6581 break;
6582 case -NFS4ERR_STALE_CLIENTID:
6583 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006584 nfs4_schedule_lease_recovery(server->nfs_client);
6585 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006586 case -NFS4ERR_LEASE_MOVED:
6587 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006588 if (nfs4_async_handle_error(task, server,
6589 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006590 rpc_restart_call_prepare(task);
6591 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006592}
6593
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006594static void nfs4_release_lockowner_release(void *calldata)
6595{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006596 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006597 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006598 kfree(calldata);
6599}
6600
Trond Myklebust17280172012-03-11 13:11:00 -04006601static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006602 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6603 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006604 .rpc_release = nfs4_release_lockowner_release,
6605};
6606
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006607static void
6608nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006609{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006610 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006611 struct rpc_message msg = {
6612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6613 };
6614
6615 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006616 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006617
Trond Myklebustcf470c32012-03-07 13:49:12 -05006618 data = kmalloc(sizeof(*data), GFP_NOFS);
6619 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006620 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006621 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006622 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006623 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6624 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6625 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006626
Trond Myklebustcf470c32012-03-07 13:49:12 -05006627 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006628 msg.rpc_resp = &data->res;
6629 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006630 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006631}
6632
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006633#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6634
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006635static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006636 struct dentry *unused, struct inode *inode,
6637 const char *key, const void *buf,
6638 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006639{
Al Viro59301222016-05-27 10:19:30 -04006640 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006641}
6642
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006643static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006644 struct dentry *unused, struct inode *inode,
6645 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006646{
Al Virob2968212016-04-10 20:48:24 -04006647 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006648}
6649
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006650static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006651{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006652 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006653}
6654
David Quigleyc9bccef2013-05-22 12:50:45 -04006655#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006656
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006657static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006658 struct dentry *unused, struct inode *inode,
6659 const char *key, const void *buf,
6660 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006661{
6662 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006663 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006664
6665 return -EOPNOTSUPP;
6666}
6667
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006668static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006669 struct dentry *unused, struct inode *inode,
6670 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006671{
6672 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006673 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006674 return -EOPNOTSUPP;
6675}
6676
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006677static ssize_t
6678nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006679{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006680 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006681
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006682 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6683 len = security_inode_listsecurity(inode, list, list_len);
6684 if (list_len && len > list_len)
6685 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006686 }
6687 return len;
6688}
6689
6690static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6691 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006692 .get = nfs4_xattr_get_nfs4_label,
6693 .set = nfs4_xattr_set_nfs4_label,
6694};
David Quigleyc9bccef2013-05-22 12:50:45 -04006695
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006696#else
6697
6698static ssize_t
6699nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6700{
6701 return 0;
6702}
6703
6704#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006705
Andy Adamson533eb462011-06-13 18:25:56 -04006706/*
6707 * nfs_fhget will use either the mounted_on_fileid or the fileid
6708 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006709static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6710{
Andy Adamson533eb462011-06-13 18:25:56 -04006711 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6712 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6713 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006714 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006715 return;
6716
6717 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006718 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006719 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6720 fattr->nlink = 2;
6721}
6722
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006723static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6724 const struct qstr *name,
6725 struct nfs4_fs_locations *fs_locations,
6726 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006727{
6728 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006729 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006730 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006731 };
6732 struct nfs4_fs_locations_arg args = {
6733 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006734 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006735 .page = page,
6736 .bitmask = bitmask,
6737 };
Benny Halevy22958462009-04-01 09:22:02 -04006738 struct nfs4_fs_locations_res res = {
6739 .fs_locations = fs_locations,
6740 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006741 struct rpc_message msg = {
6742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6743 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006744 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006745 };
6746 int status;
6747
Harvey Harrison3110ff82008-05-02 13:42:44 -07006748 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006749
6750 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6751 * is not supported */
6752 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6753 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6754 else
6755 bitmask[0] |= FATTR4_WORD0_FILEID;
6756
Trond Myklebustc228fd32007-01-13 02:28:11 -05006757 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006758 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006759 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006760 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006761 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006762 return status;
6763}
6764
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006765int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6766 const struct qstr *name,
6767 struct nfs4_fs_locations *fs_locations,
6768 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006769{
6770 struct nfs4_exception exception = { };
6771 int err;
6772 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006773 err = _nfs4_proc_fs_locations(client, dir, name,
6774 fs_locations, page);
6775 trace_nfs4_get_fs_locations(dir, name, err);
6776 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006777 &exception);
6778 } while (exception.retry);
6779 return err;
6780}
6781
Chuck Leverb03d7352013-10-17 14:12:50 -04006782/*
6783 * This operation also signals the server that this client is
6784 * performing migration recovery. The server can stop returning
6785 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6786 * appended to this compound to identify the client ID which is
6787 * performing recovery.
6788 */
6789static int _nfs40_proc_get_locations(struct inode *inode,
6790 struct nfs4_fs_locations *locations,
6791 struct page *page, struct rpc_cred *cred)
6792{
6793 struct nfs_server *server = NFS_SERVER(inode);
6794 struct rpc_clnt *clnt = server->client;
6795 u32 bitmask[2] = {
6796 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6797 };
6798 struct nfs4_fs_locations_arg args = {
6799 .clientid = server->nfs_client->cl_clientid,
6800 .fh = NFS_FH(inode),
6801 .page = page,
6802 .bitmask = bitmask,
6803 .migration = 1, /* skip LOOKUP */
6804 .renew = 1, /* append RENEW */
6805 };
6806 struct nfs4_fs_locations_res res = {
6807 .fs_locations = locations,
6808 .migration = 1,
6809 .renew = 1,
6810 };
6811 struct rpc_message msg = {
6812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6813 .rpc_argp = &args,
6814 .rpc_resp = &res,
6815 .rpc_cred = cred,
6816 };
6817 unsigned long now = jiffies;
6818 int status;
6819
6820 nfs_fattr_init(&locations->fattr);
6821 locations->server = server;
6822 locations->nlocations = 0;
6823
6824 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6825 nfs4_set_sequence_privileged(&args.seq_args);
6826 status = nfs4_call_sync_sequence(clnt, server, &msg,
6827 &args.seq_args, &res.seq_res);
6828 if (status)
6829 return status;
6830
6831 renew_lease(server, now);
6832 return 0;
6833}
6834
6835#ifdef CONFIG_NFS_V4_1
6836
6837/*
6838 * This operation also signals the server that this client is
6839 * performing migration recovery. The server can stop asserting
6840 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6841 * performing this operation is identified in the SEQUENCE
6842 * operation in this compound.
6843 *
6844 * When the client supports GETATTR(fs_locations_info), it can
6845 * be plumbed in here.
6846 */
6847static int _nfs41_proc_get_locations(struct inode *inode,
6848 struct nfs4_fs_locations *locations,
6849 struct page *page, struct rpc_cred *cred)
6850{
6851 struct nfs_server *server = NFS_SERVER(inode);
6852 struct rpc_clnt *clnt = server->client;
6853 u32 bitmask[2] = {
6854 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6855 };
6856 struct nfs4_fs_locations_arg args = {
6857 .fh = NFS_FH(inode),
6858 .page = page,
6859 .bitmask = bitmask,
6860 .migration = 1, /* skip LOOKUP */
6861 };
6862 struct nfs4_fs_locations_res res = {
6863 .fs_locations = locations,
6864 .migration = 1,
6865 };
6866 struct rpc_message msg = {
6867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6868 .rpc_argp = &args,
6869 .rpc_resp = &res,
6870 .rpc_cred = cred,
6871 };
6872 int status;
6873
6874 nfs_fattr_init(&locations->fattr);
6875 locations->server = server;
6876 locations->nlocations = 0;
6877
6878 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6879 nfs4_set_sequence_privileged(&args.seq_args);
6880 status = nfs4_call_sync_sequence(clnt, server, &msg,
6881 &args.seq_args, &res.seq_res);
6882 if (status == NFS4_OK &&
6883 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6884 status = -NFS4ERR_LEASE_MOVED;
6885 return status;
6886}
6887
6888#endif /* CONFIG_NFS_V4_1 */
6889
6890/**
6891 * nfs4_proc_get_locations - discover locations for a migrated FSID
6892 * @inode: inode on FSID that is migrating
6893 * @locations: result of query
6894 * @page: buffer
6895 * @cred: credential to use for this operation
6896 *
6897 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6898 * operation failed, or a negative errno if a local error occurred.
6899 *
6900 * On success, "locations" is filled in, but if the server has
6901 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6902 * asserted.
6903 *
6904 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6905 * from this client that require migration recovery.
6906 */
6907int nfs4_proc_get_locations(struct inode *inode,
6908 struct nfs4_fs_locations *locations,
6909 struct page *page, struct rpc_cred *cred)
6910{
6911 struct nfs_server *server = NFS_SERVER(inode);
6912 struct nfs_client *clp = server->nfs_client;
6913 const struct nfs4_mig_recovery_ops *ops =
6914 clp->cl_mvops->mig_recovery_ops;
6915 struct nfs4_exception exception = { };
6916 int status;
6917
6918 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6919 (unsigned long long)server->fsid.major,
6920 (unsigned long long)server->fsid.minor,
6921 clp->cl_hostname);
6922 nfs_display_fhandle(NFS_FH(inode), __func__);
6923
6924 do {
6925 status = ops->get_locations(inode, locations, page, cred);
6926 if (status != -NFS4ERR_DELAY)
6927 break;
6928 nfs4_handle_exception(server, status, &exception);
6929 } while (exception.retry);
6930 return status;
6931}
6932
Chuck Lever44c99932013-10-17 14:13:30 -04006933/*
6934 * This operation also signals the server that this client is
6935 * performing "lease moved" recovery. The server can stop
6936 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6937 * is appended to this compound to identify the client ID which is
6938 * performing recovery.
6939 */
6940static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6941{
6942 struct nfs_server *server = NFS_SERVER(inode);
6943 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6944 struct rpc_clnt *clnt = server->client;
6945 struct nfs4_fsid_present_arg args = {
6946 .fh = NFS_FH(inode),
6947 .clientid = clp->cl_clientid,
6948 .renew = 1, /* append RENEW */
6949 };
6950 struct nfs4_fsid_present_res res = {
6951 .renew = 1,
6952 };
6953 struct rpc_message msg = {
6954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6955 .rpc_argp = &args,
6956 .rpc_resp = &res,
6957 .rpc_cred = cred,
6958 };
6959 unsigned long now = jiffies;
6960 int status;
6961
6962 res.fh = nfs_alloc_fhandle();
6963 if (res.fh == NULL)
6964 return -ENOMEM;
6965
6966 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6967 nfs4_set_sequence_privileged(&args.seq_args);
6968 status = nfs4_call_sync_sequence(clnt, server, &msg,
6969 &args.seq_args, &res.seq_res);
6970 nfs_free_fhandle(res.fh);
6971 if (status)
6972 return status;
6973
6974 do_renew_lease(clp, now);
6975 return 0;
6976}
6977
6978#ifdef CONFIG_NFS_V4_1
6979
6980/*
6981 * This operation also signals the server that this client is
6982 * performing "lease moved" recovery. The server can stop asserting
6983 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6984 * this operation is identified in the SEQUENCE operation in this
6985 * compound.
6986 */
6987static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6988{
6989 struct nfs_server *server = NFS_SERVER(inode);
6990 struct rpc_clnt *clnt = server->client;
6991 struct nfs4_fsid_present_arg args = {
6992 .fh = NFS_FH(inode),
6993 };
6994 struct nfs4_fsid_present_res res = {
6995 };
6996 struct rpc_message msg = {
6997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6998 .rpc_argp = &args,
6999 .rpc_resp = &res,
7000 .rpc_cred = cred,
7001 };
7002 int status;
7003
7004 res.fh = nfs_alloc_fhandle();
7005 if (res.fh == NULL)
7006 return -ENOMEM;
7007
7008 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7009 nfs4_set_sequence_privileged(&args.seq_args);
7010 status = nfs4_call_sync_sequence(clnt, server, &msg,
7011 &args.seq_args, &res.seq_res);
7012 nfs_free_fhandle(res.fh);
7013 if (status == NFS4_OK &&
7014 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7015 status = -NFS4ERR_LEASE_MOVED;
7016 return status;
7017}
7018
7019#endif /* CONFIG_NFS_V4_1 */
7020
7021/**
7022 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7023 * @inode: inode on FSID to check
7024 * @cred: credential to use for this operation
7025 *
7026 * Server indicates whether the FSID is present, moved, or not
7027 * recognized. This operation is necessary to clear a LEASE_MOVED
7028 * condition for this client ID.
7029 *
7030 * Returns NFS4_OK if the FSID is present on this server,
7031 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7032 * NFS4ERR code if some error occurred on the server, or a
7033 * negative errno if a local failure occurred.
7034 */
7035int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7036{
7037 struct nfs_server *server = NFS_SERVER(inode);
7038 struct nfs_client *clp = server->nfs_client;
7039 const struct nfs4_mig_recovery_ops *ops =
7040 clp->cl_mvops->mig_recovery_ops;
7041 struct nfs4_exception exception = { };
7042 int status;
7043
7044 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7045 (unsigned long long)server->fsid.major,
7046 (unsigned long long)server->fsid.minor,
7047 clp->cl_hostname);
7048 nfs_display_fhandle(NFS_FH(inode), __func__);
7049
7050 do {
7051 status = ops->fsid_present(inode, cred);
7052 if (status != -NFS4ERR_DELAY)
7053 break;
7054 nfs4_handle_exception(server, status, &exception);
7055 } while (exception.retry);
7056 return status;
7057}
7058
Andy Adamson5ec16a82013-08-08 10:57:55 -04007059/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007060 * If 'use_integrity' is true and the state managment nfs_client
7061 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7062 * and the machine credential as per RFC3530bis and RFC5661 Security
7063 * Considerations sections. Otherwise, just use the user cred with the
7064 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007065 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007066static 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 +00007067{
7068 int status;
7069 struct nfs4_secinfo_arg args = {
7070 .dir_fh = NFS_FH(dir),
7071 .name = name,
7072 };
7073 struct nfs4_secinfo_res res = {
7074 .flavors = flavors,
7075 };
7076 struct rpc_message msg = {
7077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7078 .rpc_argp = &args,
7079 .rpc_resp = &res,
7080 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007081 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007082 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007083
7084 if (use_integrity) {
7085 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007086 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7087 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007088 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007089
7090 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007091
7092 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7093 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7094
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007095 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7096 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007097 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007098
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007099 if (cred)
7100 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007101
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007102 return status;
7103}
7104
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007105int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7106 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007107{
7108 struct nfs4_exception exception = { };
7109 int err;
7110 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007111 err = -NFS4ERR_WRONGSEC;
7112
7113 /* try to use integrity protection with machine cred */
7114 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7115 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7116
7117 /*
7118 * if unable to use integrity protection, or SECINFO with
7119 * integrity protection returns NFS4ERR_WRONGSEC (which is
7120 * disallowed by spec, but exists in deployed servers) use
7121 * the current filesystem's rpc_client and the user cred.
7122 */
7123 if (err == -NFS4ERR_WRONGSEC)
7124 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7125
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007126 trace_nfs4_secinfo(dir, name, err);
7127 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007128 &exception);
7129 } while (exception.retry);
7130 return err;
7131}
7132
Andy Adamson557134a2009-04-01 09:21:53 -04007133#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007134/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007135 * Check the exchange flags returned by the server for invalid flags, having
7136 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7137 * DS flags set.
7138 */
7139static int nfs4_check_cl_exchange_flags(u32 flags)
7140{
7141 if (flags & ~EXCHGID4_FLAG_MASK_R)
7142 goto out_inval;
7143 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7144 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7145 goto out_inval;
7146 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7147 goto out_inval;
7148 return NFS_OK;
7149out_inval:
7150 return -NFS4ERR_INVAL;
7151}
7152
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007153static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007154nfs41_same_server_scope(struct nfs41_server_scope *a,
7155 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007156{
7157 if (a->server_scope_sz == b->server_scope_sz &&
7158 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7159 return true;
7160
7161 return false;
7162}
7163
Andy Adamson02a95de2016-02-05 16:08:37 -05007164static void
7165nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7166{
7167}
7168
7169static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7170 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7171};
7172
Andy Adamson357f54d2010-12-14 10:11:57 -05007173/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007174 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007175 *
7176 * The 4.1 client currently uses the same TCP connection for the
7177 * fore and backchannel.
7178 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007179static
7180int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7181 struct rpc_xprt *xprt,
7182 struct nfs_client *clp,
7183 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007184{
7185 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007186 struct nfs41_bind_conn_to_session_args args = {
7187 .client = clp,
7188 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7189 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007190 struct nfs41_bind_conn_to_session_res res;
7191 struct rpc_message msg = {
7192 .rpc_proc =
7193 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007194 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007195 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007196 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007197 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007198 struct rpc_task_setup task_setup_data = {
7199 .rpc_client = clnt,
7200 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007201 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007202 .rpc_message = &msg,
7203 .flags = RPC_TASK_TIMEOUT,
7204 };
7205 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007206
7207 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007208
Trond Myklebust71a097c2015-02-18 09:27:18 -08007209 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7210 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7211 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007212
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007213 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7214 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7215 args.dir = NFS4_CDFC4_FORE;
7216
7217 task = rpc_run_task(&task_setup_data);
7218 if (!IS_ERR(task)) {
7219 status = task->tk_status;
7220 rpc_put_task(task);
7221 } else
7222 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007223 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007224 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007225 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007226 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7227 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7228 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007229 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007231 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007232 dprintk("NFS: %s: Unexpected direction from server\n",
7233 __func__);
7234 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007235 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007236 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007237 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007238 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7239 __func__);
7240 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007241 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007242 }
7243 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007244out:
7245 dprintk("<-- %s status= %d\n", __func__, status);
7246 return status;
7247}
7248
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007249struct rpc_bind_conn_calldata {
7250 struct nfs_client *clp;
7251 struct rpc_cred *cred;
7252};
7253
7254static int
7255nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7256 struct rpc_xprt *xprt,
7257 void *calldata)
7258{
7259 struct rpc_bind_conn_calldata *p = calldata;
7260
7261 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7262}
7263
7264int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7265{
7266 struct rpc_bind_conn_calldata data = {
7267 .clp = clp,
7268 .cred = cred,
7269 };
7270 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7271 nfs4_proc_bind_conn_to_session_callback, &data);
7272}
7273
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007274/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007275 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7276 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007277 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007278static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7279 .how = SP4_MACH_CRED,
7280 .enforce.u.words = {
7281 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7282 1 << (OP_EXCHANGE_ID - 32) |
7283 1 << (OP_CREATE_SESSION - 32) |
7284 1 << (OP_DESTROY_SESSION - 32) |
7285 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007286 },
7287 .allow.u.words = {
7288 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007289 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007290 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007291 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007292 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007293 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007294 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007295 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007296 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007297 1 << (OP_FREE_STATEID - 32) |
7298 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007299 }
7300};
7301
7302/*
7303 * Select the state protection mode for client `clp' given the server results
7304 * from exchange_id in `sp'.
7305 *
7306 * Returns 0 on success, negative errno otherwise.
7307 */
7308static int nfs4_sp4_select_mode(struct nfs_client *clp,
7309 struct nfs41_state_protection *sp)
7310{
7311 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7312 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7313 1 << (OP_EXCHANGE_ID - 32) |
7314 1 << (OP_CREATE_SESSION - 32) |
7315 1 << (OP_DESTROY_SESSION - 32) |
7316 1 << (OP_DESTROY_CLIENTID - 32)
7317 };
7318 unsigned int i;
7319
7320 if (sp->how == SP4_MACH_CRED) {
7321 /* Print state protect result */
7322 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7323 for (i = 0; i <= LAST_NFS4_OP; i++) {
7324 if (test_bit(i, sp->enforce.u.longs))
7325 dfprintk(MOUNT, " enforce op %d\n", i);
7326 if (test_bit(i, sp->allow.u.longs))
7327 dfprintk(MOUNT, " allow op %d\n", i);
7328 }
7329
7330 /* make sure nothing is on enforce list that isn't supported */
7331 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7332 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7333 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7334 return -EINVAL;
7335 }
7336 }
7337
7338 /*
7339 * Minimal mode - state operations are allowed to use machine
7340 * credential. Note this already happens by default, so the
7341 * client doesn't have to do anything more than the negotiation.
7342 *
7343 * NOTE: we don't care if EXCHANGE_ID is in the list -
7344 * we're already using the machine cred for exchange_id
7345 * and will never use a different cred.
7346 */
7347 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7348 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7349 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7350 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7351 dfprintk(MOUNT, "sp4_mach_cred:\n");
7352 dfprintk(MOUNT, " minimal mode enabled\n");
7353 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7354 } else {
7355 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7356 return -EINVAL;
7357 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007358
7359 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007360 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7361 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007362 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7363 dfprintk(MOUNT, " cleanup mode enabled\n");
7364 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7365 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007366
Andrew Elble99ade3c2015-12-02 09:39:51 -05007367 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7368 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7369 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7370 &clp->cl_sp4_flags);
7371 }
7372
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007373 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7374 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7375 dfprintk(MOUNT, " secinfo mode enabled\n");
7376 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7377 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007378
7379 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7380 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7381 dfprintk(MOUNT, " stateid mode enabled\n");
7382 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7383 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007384
7385 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7386 dfprintk(MOUNT, " write mode enabled\n");
7387 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7388 }
7389
7390 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7391 dfprintk(MOUNT, " commit mode enabled\n");
7392 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7393 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007394 }
7395
7396 return 0;
7397}
7398
Andy Adamson8d89bd72016-09-09 09:22:18 -04007399struct nfs41_exchange_id_data {
7400 struct nfs41_exchange_id_res res;
7401 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007402 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007403 int rpc_status;
7404};
7405
7406static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007407{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 struct nfs41_exchange_id_data *cdata =
7409 (struct nfs41_exchange_id_data *)data;
7410 struct nfs_client *clp = cdata->args.client;
7411 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007412
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007413 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007414
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007415 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007416 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007417
7418 if (cdata->xprt && status == 0) {
7419 status = nfs4_detect_session_trunking(clp, &cdata->res,
7420 cdata->xprt);
7421 goto out;
7422 }
7423
Andy Adamson8d89bd72016-09-09 09:22:18 -04007424 if (status == 0)
7425 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007426
Chuck Lever177313f2012-05-21 22:44:58 -04007427 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428 clp->cl_clientid = cdata->res.clientid;
7429 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007430 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007431 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007432 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007433 &clp->cl_session->session_state);
7434 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007435 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007436
Chuck Leveracdeb692012-05-21 22:46:16 -04007437 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007438 clp->cl_serverowner = cdata->res.server_owner;
7439 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007440
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007441 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007442 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007443 clp->cl_implid = cdata->res.impl_id;
7444 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007445
Chuck Lever177313f2012-05-21 22:44:58 -04007446 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007447 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007448 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007449 dprintk("%s: server_scope mismatch detected\n",
7450 __func__);
7451 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007452 kfree(clp->cl_serverscope);
7453 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007454 }
7455
Chuck Lever177313f2012-05-21 22:44:58 -04007456 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457 clp->cl_serverscope = cdata->res.server_scope;
7458 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007459 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007460 /* Save the EXCHANGE_ID verifier session trunk tests */
7461 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7462 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007464out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007465 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007466 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467}
7468
7469static void nfs4_exchange_id_release(void *data)
7470{
7471 struct nfs41_exchange_id_data *cdata =
7472 (struct nfs41_exchange_id_data *)data;
7473
7474 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007475 if (cdata->xprt) {
7476 xprt_put(cdata->xprt);
7477 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7478 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 kfree(cdata->res.impl_id);
7480 kfree(cdata->res.server_scope);
7481 kfree(cdata->res.server_owner);
7482 kfree(cdata);
7483}
7484
7485static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7486 .rpc_call_done = nfs4_exchange_id_done,
7487 .rpc_release = nfs4_exchange_id_release,
7488};
7489
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490/*
7491 * _nfs4_proc_exchange_id()
7492 *
7493 * Wrapper for EXCHANGE_ID operation.
7494 */
7495static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007496 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007497{
7498 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499 struct rpc_message msg = {
7500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007501 .rpc_cred = cred,
7502 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503 struct rpc_task_setup task_setup_data = {
7504 .rpc_client = clp->cl_rpcclient,
7505 .callback_ops = &nfs4_exchange_id_call_ops,
7506 .rpc_message = &msg,
7507 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7508 };
7509 struct nfs41_exchange_id_data *calldata;
7510 struct rpc_task *task;
7511 int status = -EIO;
7512
7513 if (!atomic_inc_not_zero(&clp->cl_count))
7514 goto out;
7515
7516 status = -ENOMEM;
7517 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7518 if (!calldata)
7519 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007520
Andy Adamsonad0849a2016-09-09 09:22:28 -04007521 if (!xprt)
7522 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007523
7524 status = nfs4_init_uniform_client_string(clp);
7525 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007527
7528 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7529 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7530 clp->cl_owner_id);
7531
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7533 GFP_NOFS);
7534 status = -ENOMEM;
7535 if (unlikely(calldata->res.server_owner == NULL))
7536 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007537
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007539 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007540 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007541 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007542
Andy Adamson8d89bd72016-09-09 09:22:18 -04007543 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7544 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007545 goto out_server_scope;
7546
7547 switch (sp4_how) {
7548 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007549 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007550 break;
7551
7552 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007553 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007554 break;
7555
7556 default:
7557 /* unsupported! */
7558 WARN_ON_ONCE(1);
7559 status = -EINVAL;
7560 goto out_impl_id;
7561 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007562 if (xprt) {
7563 calldata->xprt = xprt;
7564 task_setup_data.rpc_xprt = xprt;
7565 task_setup_data.flags =
7566 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7567 calldata->args.verifier = &clp->cl_confirm;
7568 } else {
7569 calldata->args.verifier = &verifier;
7570 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007571 calldata->args.client = clp;
7572#ifdef CONFIG_NFS_V4_1_MIGRATION
7573 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7574 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7575 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7576#else
7577 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7578 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7579#endif
7580 msg.rpc_argp = &calldata->args;
7581 msg.rpc_resp = &calldata->res;
7582 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007583
Andy Adamson8d89bd72016-09-09 09:22:18 -04007584 task = rpc_run_task(&task_setup_data);
7585 if (IS_ERR(task)) {
7586 status = PTR_ERR(task);
7587 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007588 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007589
Andy Adamsonad0849a2016-09-09 09:22:28 -04007590 if (!xprt) {
7591 status = rpc_wait_for_completion_task(task);
7592 if (!status)
7593 status = calldata->rpc_status;
7594 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007595 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007596
Andy Adamson8d89bd72016-09-09 09:22:18 -04007597 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007598out:
Chuck Lever177313f2012-05-21 22:44:58 -04007599 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007600 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007601 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007602 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007603 clp->cl_implid->date.seconds,
7604 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007605 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007606 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007607
7608out_impl_id:
7609 kfree(calldata->res.impl_id);
7610out_server_scope:
7611 kfree(calldata->res.server_scope);
7612out_server_owner:
7613 kfree(calldata->res.server_owner);
7614out_calldata:
7615 kfree(calldata);
7616 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007617}
7618
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007619/*
7620 * nfs4_proc_exchange_id()
7621 *
7622 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7623 *
7624 * Since the clientid has expired, all compounds using sessions
7625 * associated with the stale clientid will be returning
7626 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7627 * be in some phase of session reset.
7628 *
7629 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7630 */
7631int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7632{
7633 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7634 int status;
7635
7636 /* try SP4_MACH_CRED if krb5i/p */
7637 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7638 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007639 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007640 if (!status)
7641 return 0;
7642 }
7643
7644 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007645 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007646}
7647
Andy Adamson04fa2c62016-09-09 09:22:29 -04007648/**
7649 * nfs4_test_session_trunk
7650 *
7651 * This is an add_xprt_test() test function called from
7652 * rpc_clnt_setup_test_and_add_xprt.
7653 *
7654 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7655 * and is dereferrenced in nfs4_exchange_id_release
7656 *
7657 * Upon success, add the new transport to the rpc_clnt
7658 *
7659 * @clnt: struct rpc_clnt to get new transport
7660 * @xprt: the rpc_xprt to test
7661 * @data: call data for _nfs4_proc_exchange_id.
7662 */
7663int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7664 void *data)
7665{
7666 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7667 u32 sp4_how;
7668
7669 dprintk("--> %s try %s\n", __func__,
7670 xprt->address_strings[RPC_DISPLAY_ADDR]);
7671
7672 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7673
7674 /* Test connection for session trunking. Async exchange_id call */
7675 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7676}
7677EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7678
Trond Myklebust66245532012-05-25 17:18:09 -04007679static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7680 struct rpc_cred *cred)
7681{
7682 struct rpc_message msg = {
7683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7684 .rpc_argp = clp,
7685 .rpc_cred = cred,
7686 };
7687 int status;
7688
7689 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007690 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007691 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007692 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007693 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7694 return status;
7695}
7696
7697static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7698 struct rpc_cred *cred)
7699{
7700 unsigned int loop;
7701 int ret;
7702
7703 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7704 ret = _nfs4_proc_destroy_clientid(clp, cred);
7705 switch (ret) {
7706 case -NFS4ERR_DELAY:
7707 case -NFS4ERR_CLIENTID_BUSY:
7708 ssleep(1);
7709 break;
7710 default:
7711 return ret;
7712 }
7713 }
7714 return 0;
7715}
7716
7717int nfs4_destroy_clientid(struct nfs_client *clp)
7718{
7719 struct rpc_cred *cred;
7720 int ret = 0;
7721
7722 if (clp->cl_mvops->minor_version < 1)
7723 goto out;
7724 if (clp->cl_exchange_flags == 0)
7725 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007726 if (clp->cl_preserve_clid)
7727 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007728 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007729 ret = nfs4_proc_destroy_clientid(clp, cred);
7730 if (cred)
7731 put_rpccred(cred);
7732 switch (ret) {
7733 case 0:
7734 case -NFS4ERR_STALE_CLIENTID:
7735 clp->cl_exchange_flags = 0;
7736 }
7737out:
7738 return ret;
7739}
7740
Andy Adamson2050f0c2009-04-01 09:22:30 -04007741struct nfs4_get_lease_time_data {
7742 struct nfs4_get_lease_time_args *args;
7743 struct nfs4_get_lease_time_res *res;
7744 struct nfs_client *clp;
7745};
7746
7747static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7748 void *calldata)
7749{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007750 struct nfs4_get_lease_time_data *data =
7751 (struct nfs4_get_lease_time_data *)calldata;
7752
7753 dprintk("--> %s\n", __func__);
7754 /* just setup sequence, do not trigger session recovery
7755 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007756 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007757 &data->args->la_seq_args,
7758 &data->res->lr_seq_res,
7759 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007760 dprintk("<-- %s\n", __func__);
7761}
7762
7763/*
7764 * Called from nfs4_state_manager thread for session setup, so don't recover
7765 * from sequence operation or clientid errors.
7766 */
7767static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7768{
7769 struct nfs4_get_lease_time_data *data =
7770 (struct nfs4_get_lease_time_data *)calldata;
7771
7772 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007773 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7774 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007775 switch (task->tk_status) {
7776 case -NFS4ERR_DELAY:
7777 case -NFS4ERR_GRACE:
7778 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7779 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7780 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007781 /* fall through */
7782 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007783 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007784 return;
7785 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007786 dprintk("<-- %s\n", __func__);
7787}
7788
Trond Myklebust17280172012-03-11 13:11:00 -04007789static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007790 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7791 .rpc_call_done = nfs4_get_lease_time_done,
7792};
7793
7794int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7795{
7796 struct rpc_task *task;
7797 struct nfs4_get_lease_time_args args;
7798 struct nfs4_get_lease_time_res res = {
7799 .lr_fsinfo = fsinfo,
7800 };
7801 struct nfs4_get_lease_time_data data = {
7802 .args = &args,
7803 .res = &res,
7804 .clp = clp,
7805 };
7806 struct rpc_message msg = {
7807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7808 .rpc_argp = &args,
7809 .rpc_resp = &res,
7810 };
7811 struct rpc_task_setup task_setup = {
7812 .rpc_client = clp->cl_rpcclient,
7813 .rpc_message = &msg,
7814 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007815 .callback_data = &data,
7816 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007817 };
7818 int status;
7819
Chuck Levera9c92d62013-08-09 12:48:18 -04007820 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007821 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007822 dprintk("--> %s\n", __func__);
7823 task = rpc_run_task(&task_setup);
7824
7825 if (IS_ERR(task))
7826 status = PTR_ERR(task);
7827 else {
7828 status = task->tk_status;
7829 rpc_put_task(task);
7830 }
7831 dprintk("<-- %s return %d\n", __func__, status);
7832
7833 return status;
7834}
7835
Andy Adamsonfc931582009-04-01 09:22:31 -04007836/*
7837 * Initialize the values to be used by the client in CREATE_SESSION
7838 * If nfs4_init_session set the fore channel request and response sizes,
7839 * use them.
7840 *
7841 * Set the back channel max_resp_sz_cached to zero to force the client to
7842 * always set csa_cachethis to FALSE because the current implementation
7843 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7844 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007845static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7846 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007847{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007848 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007849 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007850
Andy Adamson18aad3d2013-06-26 12:21:49 -04007851 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7852 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7853
Andy Adamsonfc931582009-04-01 09:22:31 -04007854 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007855 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7856 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007857 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007858 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007859
7860 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007861 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007862 __func__,
7863 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007864 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007865
7866 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007867 args->bc_attrs.max_rqst_sz = max_bc_payload;
7868 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007869 args->bc_attrs.max_resp_sz_cached = 0;
7870 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007871 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007872
7873 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7874 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7875 __func__,
7876 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7877 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7878 args->bc_attrs.max_reqs);
7879}
7880
Trond Myklebust79969dd2015-02-18 11:30:18 -08007881static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7882 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007883{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007885 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007886
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007887 if (rcvd->max_resp_sz > sent->max_resp_sz)
7888 return -EINVAL;
7889 /*
7890 * Our requested max_ops is the minimum we need; we're not
7891 * prepared to break up compounds into smaller pieces than that.
7892 * So, no point even trying to continue if the server won't
7893 * cooperate:
7894 */
7895 if (rcvd->max_ops < sent->max_ops)
7896 return -EINVAL;
7897 if (rcvd->max_reqs == 0)
7898 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007899 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7900 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007901 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007902}
7903
Trond Myklebust79969dd2015-02-18 11:30:18 -08007904static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7905 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007906{
7907 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007908 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007909
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007910 if (!(res->flags & SESSION4_BACK_CHAN))
7911 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007912 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7913 return -EINVAL;
7914 if (rcvd->max_resp_sz < sent->max_resp_sz)
7915 return -EINVAL;
7916 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7917 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007918 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007919 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007920 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007921 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007922out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007923 return 0;
7924}
Andy Adamson8d353012009-04-01 09:22:32 -04007925
Andy Adamson8d353012009-04-01 09:22:32 -04007926static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007927 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007928{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007929 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007930
Trond Myklebust79969dd2015-02-18 11:30:18 -08007931 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007932 if (ret)
7933 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007934 return nfs4_verify_back_channel_attrs(args, res);
7935}
7936
7937static void nfs4_update_session(struct nfs4_session *session,
7938 struct nfs41_create_session_res *res)
7939{
7940 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007941 /* Mark client id and session as being confirmed */
7942 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7943 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007944 session->flags = res->flags;
7945 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007946 if (res->flags & SESSION4_BACK_CHAN)
7947 memcpy(&session->bc_attrs, &res->bc_attrs,
7948 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007949}
7950
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007951static int _nfs4_proc_create_session(struct nfs_client *clp,
7952 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007953{
7954 struct nfs4_session *session = clp->cl_session;
7955 struct nfs41_create_session_args args = {
7956 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007957 .clientid = clp->cl_clientid,
7958 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007959 .cb_program = NFS4_CALLBACK,
7960 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007961 struct nfs41_create_session_res res;
7962
Andy Adamsonfc931582009-04-01 09:22:31 -04007963 struct rpc_message msg = {
7964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7965 .rpc_argp = &args,
7966 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007967 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007968 };
7969 int status;
7970
Chuck Lever6b26cc82016-05-02 14:40:40 -04007971 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007972 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007973
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007974 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007975 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007976
Trond Myklebustb519d402016-09-11 14:50:01 -04007977 switch (status) {
7978 case -NFS4ERR_STALE_CLIENTID:
7979 case -NFS4ERR_DELAY:
7980 case -ETIMEDOUT:
7981 case -EACCES:
7982 case -EAGAIN:
7983 goto out;
7984 };
7985
7986 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007987 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007988 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007989 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007990 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007991 if (status)
7992 goto out;
7993 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007995out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007996 return status;
7997}
7998
7999/*
8000 * Issues a CREATE_SESSION operation to the server.
8001 * It is the responsibility of the caller to verify the session is
8002 * expired before calling this routine.
8003 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008004int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008005{
8006 int status;
8007 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008008 struct nfs4_session *session = clp->cl_session;
8009
8010 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8011
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008012 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008013 if (status)
8014 goto out;
8015
Andy Adamsonaacd5532011-11-09 13:58:21 -05008016 /* Init or reset the session slot tables */
8017 status = nfs4_setup_session_slot_tables(session);
8018 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008019 if (status)
8020 goto out;
8021
8022 ptr = (unsigned *)&session->sess_id.data[0];
8023 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8024 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008025out:
8026 dprintk("<-- %s\n", __func__);
8027 return status;
8028}
8029
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008030/*
8031 * Issue the over-the-wire RPC DESTROY_SESSION.
8032 * The caller must serialize access to this routine.
8033 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008034int nfs4_proc_destroy_session(struct nfs4_session *session,
8035 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008036{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008037 struct rpc_message msg = {
8038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8039 .rpc_argp = session,
8040 .rpc_cred = cred,
8041 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008042 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008043
8044 dprintk("--> nfs4_proc_destroy_session\n");
8045
8046 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008047 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8048 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008049
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008050 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008051 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008052
8053 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008054 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008055 "Session has been destroyed regardless...\n", status);
8056
8057 dprintk("<-- nfs4_proc_destroy_session\n");
8058 return status;
8059}
8060
Trond Myklebust7b38c362012-05-23 13:23:31 -04008061/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008062 * Renew the cl_session lease.
8063 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008064struct nfs4_sequence_data {
8065 struct nfs_client *clp;
8066 struct nfs4_sequence_args args;
8067 struct nfs4_sequence_res res;
8068};
8069
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008070static void nfs41_sequence_release(void *data)
8071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008072 struct nfs4_sequence_data *calldata = data;
8073 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008074
Alexandros Batsakis71358402010-02-05 03:45:05 -08008075 if (atomic_read(&clp->cl_count) > 1)
8076 nfs4_schedule_state_renewal(clp);
8077 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008078 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008079}
8080
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008081static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8082{
8083 switch(task->tk_status) {
8084 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008085 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8086 return -EAGAIN;
8087 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008088 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008089 }
8090 return 0;
8091}
8092
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008093static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008095 struct nfs4_sequence_data *calldata = data;
8096 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008097
Trond Myklebust14516c32010-07-31 14:29:06 -04008098 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8099 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008101 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008102 if (task->tk_status < 0) {
8103 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008104 if (atomic_read(&clp->cl_count) == 1)
8105 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008106
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008107 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8108 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109 return;
8110 }
8111 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008113out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114 dprintk("<-- %s\n", __func__);
8115}
8116
8117static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8118{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008119 struct nfs4_sequence_data *calldata = data;
8120 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008121 struct nfs4_sequence_args *args;
8122 struct nfs4_sequence_res *res;
8123
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008124 args = task->tk_msg.rpc_argp;
8125 res = task->tk_msg.rpc_resp;
8126
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008127 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008128}
8129
8130static const struct rpc_call_ops nfs41_sequence_ops = {
8131 .rpc_call_done = nfs41_sequence_call_done,
8132 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008133 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008134};
8135
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008136static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8137 struct rpc_cred *cred,
8138 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008139{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008140 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141 struct rpc_message msg = {
8142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8143 .rpc_cred = cred,
8144 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008145 struct rpc_task_setup task_setup_data = {
8146 .rpc_client = clp->cl_rpcclient,
8147 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008148 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008149 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008150 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008151
Alexandros Batsakis71358402010-02-05 03:45:05 -08008152 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008153 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008154 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008155 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008156 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008157 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008158 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008159 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008160 if (is_privileged)
8161 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008162 msg.rpc_argp = &calldata->args;
8163 msg.rpc_resp = &calldata->res;
8164 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008165 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008166
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008167 return rpc_run_task(&task_setup_data);
8168}
8169
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008170static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008171{
8172 struct rpc_task *task;
8173 int ret = 0;
8174
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008175 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008176 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008177 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008178 if (IS_ERR(task))
8179 ret = PTR_ERR(task);
8180 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008181 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008182 dprintk("<-- %s status=%d\n", __func__, ret);
8183 return ret;
8184}
8185
8186static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8187{
8188 struct rpc_task *task;
8189 int ret;
8190
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008191 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008192 if (IS_ERR(task)) {
8193 ret = PTR_ERR(task);
8194 goto out;
8195 }
8196 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008197 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008198 ret = task->tk_status;
8199 rpc_put_task(task);
8200out:
8201 dprintk("<-- %s status=%d\n", __func__, ret);
8202 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008203}
8204
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008205struct nfs4_reclaim_complete_data {
8206 struct nfs_client *clp;
8207 struct nfs41_reclaim_complete_args arg;
8208 struct nfs41_reclaim_complete_res res;
8209};
8210
8211static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8212{
8213 struct nfs4_reclaim_complete_data *calldata = data;
8214
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008215 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008216 &calldata->arg.seq_args,
8217 &calldata->res.seq_res,
8218 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008219}
8220
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008221static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8222{
8223 switch(task->tk_status) {
8224 case 0:
8225 case -NFS4ERR_COMPLETE_ALREADY:
8226 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8227 break;
8228 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008230 /* fall through */
8231 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008232 return -EAGAIN;
8233 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008234 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008235 }
8236 return 0;
8237}
8238
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008239static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8240{
8241 struct nfs4_reclaim_complete_data *calldata = data;
8242 struct nfs_client *clp = calldata->clp;
8243 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8244
8245 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008246 if (!nfs41_sequence_done(task, res))
8247 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008249 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008250 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8251 rpc_restart_call_prepare(task);
8252 return;
8253 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008254 dprintk("<-- %s\n", __func__);
8255}
8256
8257static void nfs4_free_reclaim_complete_data(void *data)
8258{
8259 struct nfs4_reclaim_complete_data *calldata = data;
8260
8261 kfree(calldata);
8262}
8263
8264static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8265 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8266 .rpc_call_done = nfs4_reclaim_complete_done,
8267 .rpc_release = nfs4_free_reclaim_complete_data,
8268};
8269
8270/*
8271 * Issue a global reclaim complete.
8272 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008273static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8274 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008275{
8276 struct nfs4_reclaim_complete_data *calldata;
8277 struct rpc_task *task;
8278 struct rpc_message msg = {
8279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008280 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008281 };
8282 struct rpc_task_setup task_setup_data = {
8283 .rpc_client = clp->cl_rpcclient,
8284 .rpc_message = &msg,
8285 .callback_ops = &nfs4_reclaim_complete_call_ops,
8286 .flags = RPC_TASK_ASYNC,
8287 };
8288 int status = -ENOMEM;
8289
8290 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008291 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008292 if (calldata == NULL)
8293 goto out;
8294 calldata->clp = clp;
8295 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008296
Chuck Levera9c92d62013-08-09 12:48:18 -04008297 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008298 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008299 msg.rpc_argp = &calldata->arg;
8300 msg.rpc_resp = &calldata->res;
8301 task_setup_data.callback_data = calldata;
8302 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008303 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008304 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008305 goto out;
8306 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008307 status = nfs4_wait_for_completion_rpc_task(task);
8308 if (status == 0)
8309 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008310 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008311 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008312out:
8313 dprintk("<-- %s status=%d\n", __func__, status);
8314 return status;
8315}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008316
8317static void
8318nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8319{
8320 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008321 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008322
8323 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008324 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008325 &lgp->res.seq_res, task);
8326 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008327}
8328
8329static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8330{
8331 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008332
8333 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008334 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008335 dprintk("<-- %s\n", __func__);
8336}
8337
8338static int
8339nfs4_layoutget_handle_exception(struct rpc_task *task,
8340 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8341{
Trond Myklebustee314c22012-10-01 17:25:48 -07008342 struct inode *inode = lgp->args.inode;
8343 struct nfs_server *server = NFS_SERVER(inode);
8344 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008345 int nfs4err = task->tk_status;
8346 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008347 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008348
Boaz Harroshed7e5422014-01-22 20:34:54 +02008349 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008350
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008351 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008352 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008353 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008354
8355 /*
8356 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8357 * on the file. set tk_status to -ENODATA to tell upper layer to
8358 * retry go inband.
8359 */
8360 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008361 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008362 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008363 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008364 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8365 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8366 */
8367 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008368 status = -EOVERFLOW;
8369 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008370 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008371 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008372 * (or clients) writing to the same RAID stripe except when
8373 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008374 *
8375 * Treat it like we would RECALLCONFLICT -- we retry for a little
8376 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008377 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008378 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008379 if (lgp->args.minlength == 0) {
8380 status = -EOVERFLOW;
8381 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008382 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008383 status = -EBUSY;
8384 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008385 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008386 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008387 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008388 case -NFS4ERR_DELEG_REVOKED:
8389 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008390 case -NFS4ERR_EXPIRED:
8391 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008392 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008393 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008394 lo = NFS_I(inode)->layout;
8395 /* If the open stateid was bad, then recover it. */
8396 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8397 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008398 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008399 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008400 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008401 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008402 break;
8403 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008404
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008405 /*
8406 * Mark the bad layout state as invalid, then retry
8407 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008408 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008409 spin_unlock(&inode->i_lock);
8410 pnfs_free_lseg_list(&head);
8411 status = -EAGAIN;
8412 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008413 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008414
Trond Myklebust8ac09252017-01-23 22:44:12 -05008415 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008416 err = nfs4_handle_exception(server, nfs4err, exception);
8417 if (!status) {
8418 if (exception->retry)
8419 status = -EAGAIN;
8420 else
8421 status = err;
8422 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008423out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008424 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008425 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008426}
8427
Idan Kedar85541162012-08-02 11:47:10 +03008428static size_t max_response_pages(struct nfs_server *server)
8429{
8430 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8431 return nfs_page_array_len(0, max_resp_sz);
8432}
8433
8434static void nfs4_free_pages(struct page **pages, size_t size)
8435{
8436 int i;
8437
8438 if (!pages)
8439 return;
8440
8441 for (i = 0; i < size; i++) {
8442 if (!pages[i])
8443 break;
8444 __free_page(pages[i]);
8445 }
8446 kfree(pages);
8447}
8448
8449static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8450{
8451 struct page **pages;
8452 int i;
8453
8454 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8455 if (!pages) {
8456 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8457 return NULL;
8458 }
8459
8460 for (i = 0; i < size; i++) {
8461 pages[i] = alloc_page(gfp_flags);
8462 if (!pages[i]) {
8463 dprintk("%s: failed to allocate page\n", __func__);
8464 nfs4_free_pages(pages, size);
8465 return NULL;
8466 }
8467 }
8468
8469 return pages;
8470}
8471
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472static void nfs4_layoutget_release(void *calldata)
8473{
8474 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008475 struct inode *inode = lgp->args.inode;
8476 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008477 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478
8479 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008480 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008481 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008482 put_nfs_open_context(lgp->args.ctx);
8483 kfree(calldata);
8484 dprintk("<-- %s\n", __func__);
8485}
8486
8487static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8488 .rpc_call_prepare = nfs4_layoutget_prepare,
8489 .rpc_call_done = nfs4_layoutget_done,
8490 .rpc_release = nfs4_layoutget_release,
8491};
8492
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008493struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008494nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008495{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008496 struct inode *inode = lgp->args.inode;
8497 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008498 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008499 struct rpc_task *task;
8500 struct rpc_message msg = {
8501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8502 .rpc_argp = &lgp->args,
8503 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008504 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505 };
8506 struct rpc_task_setup task_setup_data = {
8507 .rpc_client = server->client,
8508 .rpc_message = &msg,
8509 .callback_ops = &nfs4_layoutget_call_ops,
8510 .callback_data = lgp,
8511 .flags = RPC_TASK_ASYNC,
8512 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008513 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008514 struct nfs4_exception exception = {
8515 .inode = inode,
8516 .timeout = *timeout,
8517 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518 int status = 0;
8519
8520 dprintk("--> %s\n", __func__);
8521
Peng Tao4bd5a982014-11-17 11:05:17 +08008522 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8523 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8524
Idan Kedar85541162012-08-02 11:47:10 +03008525 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8526 if (!lgp->args.layout.pages) {
8527 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008528 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008529 }
8530 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8531
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008532 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008533 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008534 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008535
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008536 task = rpc_run_task(&task_setup_data);
8537 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008538 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008540 if (status == 0) {
8541 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8542 *timeout = exception.timeout;
8543 }
8544
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008545 trace_nfs4_layoutget(lgp->args.ctx,
8546 &lgp->args.range,
8547 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008548 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008549 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008550
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008551 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8552 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008553 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008554 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008555 rpc_put_task(task);
8556 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008557 if (status)
8558 return ERR_PTR(status);
8559 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008560}
8561
Benny Halevycbe82602011-05-22 19:52:37 +03008562static void
8563nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8564{
8565 struct nfs4_layoutreturn *lrp = calldata;
8566
8567 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008568 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008569 &lrp->args.seq_args,
8570 &lrp->res.seq_res,
8571 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008572}
8573
8574static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8575{
8576 struct nfs4_layoutreturn *lrp = calldata;
8577 struct nfs_server *server;
8578
8579 dprintk("--> %s\n", __func__);
8580
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008581 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008582 return;
8583
8584 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008585 switch (task->tk_status) {
8586 default:
8587 task->tk_status = 0;
8588 case 0:
8589 break;
8590 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008591 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008592 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008593 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008594 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008595 return;
8596 }
Benny Halevycbe82602011-05-22 19:52:37 +03008597 dprintk("<-- %s\n", __func__);
8598}
8599
8600static void nfs4_layoutreturn_release(void *calldata)
8601{
8602 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008603 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008604
8605 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008606 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008607 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008608 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008609 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8610 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008611 pnfs_put_layout_hdr(lrp->args.layout);
8612 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008613 kfree(calldata);
8614 dprintk("<-- %s\n", __func__);
8615}
8616
8617static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8618 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8619 .rpc_call_done = nfs4_layoutreturn_done,
8620 .rpc_release = nfs4_layoutreturn_release,
8621};
8622
Peng Tao6c166052014-11-17 09:30:40 +08008623int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008624{
8625 struct rpc_task *task;
8626 struct rpc_message msg = {
8627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8628 .rpc_argp = &lrp->args,
8629 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008630 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008631 };
8632 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008633 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008634 .rpc_message = &msg,
8635 .callback_ops = &nfs4_layoutreturn_call_ops,
8636 .callback_data = lrp,
8637 };
Peng Tao6c166052014-11-17 09:30:40 +08008638 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008639
Andrew Elble99ade3c2015-12-02 09:39:51 -05008640 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8641 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8642 &task_setup_data.rpc_client, &msg);
8643
Benny Halevycbe82602011-05-22 19:52:37 +03008644 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008645 if (!sync) {
8646 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8647 if (!lrp->inode) {
8648 nfs4_layoutreturn_release(lrp);
8649 return -EAGAIN;
8650 }
8651 task_setup_data.flags |= RPC_TASK_ASYNC;
8652 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008653 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008654 task = rpc_run_task(&task_setup_data);
8655 if (IS_ERR(task))
8656 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008657 if (sync)
8658 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008659 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008660 dprintk("<-- %s status=%d\n", __func__, status);
8661 rpc_put_task(task);
8662 return status;
8663}
8664
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008665static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008666_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8667 struct pnfs_device *pdev,
8668 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008669{
8670 struct nfs4_getdeviceinfo_args args = {
8671 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008672 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8673 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008674 };
8675 struct nfs4_getdeviceinfo_res res = {
8676 .pdev = pdev,
8677 };
8678 struct rpc_message msg = {
8679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8680 .rpc_argp = &args,
8681 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008682 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 };
8684 int status;
8685
8686 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008687 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008688 if (res.notification & ~args.notify_types)
8689 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008690 if (res.notification != args.notify_types)
8691 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008692
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008693 dprintk("<-- %s status=%d\n", __func__, status);
8694
8695 return status;
8696}
8697
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008698int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8699 struct pnfs_device *pdev,
8700 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008701{
8702 struct nfs4_exception exception = { };
8703 int err;
8704
8705 do {
8706 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008707 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008708 &exception);
8709 } while (exception.retry);
8710 return err;
8711}
8712EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8713
Andy Adamson863a3c62011-03-23 13:27:54 +00008714static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8715{
8716 struct nfs4_layoutcommit_data *data = calldata;
8717 struct nfs_server *server = NFS_SERVER(data->args.inode);
8718
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008719 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008720 &data->args.seq_args,
8721 &data->res.seq_res,
8722 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008723}
8724
8725static void
8726nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8727{
8728 struct nfs4_layoutcommit_data *data = calldata;
8729 struct nfs_server *server = NFS_SERVER(data->args.inode);
8730
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008731 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008732 return;
8733
8734 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008735 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8736 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8737 case -NFS4ERR_BADLAYOUT: /* no layout */
8738 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008739 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008740 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008741 break;
8742 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008743 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008744 rpc_restart_call_prepare(task);
8745 return;
8746 }
8747 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008748}
8749
8750static void nfs4_layoutcommit_release(void *calldata)
8751{
8752 struct nfs4_layoutcommit_data *data = calldata;
8753
Andy Adamsondb29c082011-07-30 20:52:38 -04008754 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008755 nfs_post_op_update_inode_force_wcc(data->args.inode,
8756 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008757 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008758 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008759 kfree(data);
8760}
8761
8762static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8763 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8764 .rpc_call_done = nfs4_layoutcommit_done,
8765 .rpc_release = nfs4_layoutcommit_release,
8766};
8767
8768int
Andy Adamsonef311532011-03-12 02:58:10 -05008769nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008770{
8771 struct rpc_message msg = {
8772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8773 .rpc_argp = &data->args,
8774 .rpc_resp = &data->res,
8775 .rpc_cred = data->cred,
8776 };
8777 struct rpc_task_setup task_setup_data = {
8778 .task = &data->task,
8779 .rpc_client = NFS_CLIENT(data->args.inode),
8780 .rpc_message = &msg,
8781 .callback_ops = &nfs4_layoutcommit_ops,
8782 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008783 };
8784 struct rpc_task *task;
8785 int status = 0;
8786
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008787 dprintk("NFS: initiating layoutcommit call. sync %d "
8788 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008789 data->args.lastbytewritten,
8790 data->args.inode->i_ino);
8791
Trond Myklebust472e2592015-02-05 16:50:30 -05008792 if (!sync) {
8793 data->inode = nfs_igrab_and_active(data->args.inode);
8794 if (data->inode == NULL) {
8795 nfs4_layoutcommit_release(data);
8796 return -EAGAIN;
8797 }
8798 task_setup_data.flags = RPC_TASK_ASYNC;
8799 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008800 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008801 task = rpc_run_task(&task_setup_data);
8802 if (IS_ERR(task))
8803 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008804 if (sync)
8805 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008806 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008807 dprintk("%s: status %d\n", __func__, status);
8808 rpc_put_task(task);
8809 return status;
8810}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008811
Andy Adamson97431202013-08-08 10:57:56 -04008812/**
8813 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8814 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8815 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008816static int
8817_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008818 struct nfs_fsinfo *info,
8819 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008820{
8821 struct nfs41_secinfo_no_name_args args = {
8822 .style = SECINFO_STYLE_CURRENT_FH,
8823 };
8824 struct nfs4_secinfo_res res = {
8825 .flavors = flavors,
8826 };
8827 struct rpc_message msg = {
8828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8829 .rpc_argp = &args,
8830 .rpc_resp = &res,
8831 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008832 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008833 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008834 int status;
8835
8836 if (use_integrity) {
8837 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008838 cred = nfs4_get_clid_cred(server->nfs_client);
8839 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008840 }
8841
8842 dprintk("--> %s\n", __func__);
8843 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8844 &res.seq_res, 0);
8845 dprintk("<-- %s status=%d\n", __func__, status);
8846
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008847 if (cred)
8848 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008849
8850 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008851}
8852
8853static int
8854nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8855 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8856{
8857 struct nfs4_exception exception = { };
8858 int err;
8859 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008860 /* first try using integrity protection */
8861 err = -NFS4ERR_WRONGSEC;
8862
8863 /* try to use integrity protection with machine cred */
8864 if (_nfs4_is_integrity_protected(server->nfs_client))
8865 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8866 flavors, true);
8867
8868 /*
8869 * if unable to use integrity protection, or SECINFO with
8870 * integrity protection returns NFS4ERR_WRONGSEC (which is
8871 * disallowed by spec, but exists in deployed servers) use
8872 * the current filesystem's rpc_client and the user cred.
8873 */
8874 if (err == -NFS4ERR_WRONGSEC)
8875 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8876 flavors, false);
8877
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878 switch (err) {
8879 case 0:
8880 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008881 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008882 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008883 default:
8884 err = nfs4_handle_exception(server, err, &exception);
8885 }
8886 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008887out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888 return err;
8889}
8890
8891static int
8892nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8893 struct nfs_fsinfo *info)
8894{
8895 int err;
8896 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008897 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008898 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008899 struct nfs4_secinfo4 *secinfo;
8900 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008901
8902 page = alloc_page(GFP_KERNEL);
8903 if (!page) {
8904 err = -ENOMEM;
8905 goto out;
8906 }
8907
8908 flavors = page_address(page);
8909 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8910
8911 /*
8912 * Fall back on "guess and check" method if
8913 * the server doesn't support SECINFO_NO_NAME
8914 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008915 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008916 err = nfs4_find_root_sec(server, fhandle, info);
8917 goto out_freepage;
8918 }
8919 if (err)
8920 goto out_freepage;
8921
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008922 for (i = 0; i < flavors->num_flavors; i++) {
8923 secinfo = &flavors->flavors[i];
8924
8925 switch (secinfo->flavor) {
8926 case RPC_AUTH_NULL:
8927 case RPC_AUTH_UNIX:
8928 case RPC_AUTH_GSS:
8929 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8930 &secinfo->flavor_info);
8931 break;
8932 default:
8933 flavor = RPC_AUTH_MAXFLAVOR;
8934 break;
8935 }
8936
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008937 if (!nfs_auth_info_match(&server->auth_info, flavor))
8938 flavor = RPC_AUTH_MAXFLAVOR;
8939
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008940 if (flavor != RPC_AUTH_MAXFLAVOR) {
8941 err = nfs4_lookup_root_sec(server, fhandle,
8942 info, flavor);
8943 if (!err)
8944 break;
8945 }
8946 }
8947
8948 if (flavor == RPC_AUTH_MAXFLAVOR)
8949 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008950
8951out_freepage:
8952 put_page(page);
8953 if (err == -EACCES)
8954 return -EPERM;
8955out:
8956 return err;
8957}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008958
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008959static int _nfs41_test_stateid(struct nfs_server *server,
8960 nfs4_stateid *stateid,
8961 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008962{
8963 int status;
8964 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008965 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008966 };
8967 struct nfs41_test_stateid_res res;
8968 struct rpc_message msg = {
8969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8970 .rpc_argp = &args,
8971 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008972 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008973 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008974 struct rpc_clnt *rpc_client = server->client;
8975
8976 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8977 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008978
Chuck Lever38527b12012-07-11 16:30:23 -04008979 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008980 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008981 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008982 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008983 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008984 if (status != NFS_OK) {
8985 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008986 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008987 }
8988 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008989 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008990}
8991
Trond Myklebust43912bb2016-09-22 13:38:56 -04008992static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8993 int err, struct nfs4_exception *exception)
8994{
8995 exception->retry = 0;
8996 switch(err) {
8997 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008998 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008999 nfs4_handle_exception(server, err, exception);
9000 break;
9001 case -NFS4ERR_BADSESSION:
9002 case -NFS4ERR_BADSLOT:
9003 case -NFS4ERR_BAD_HIGH_SLOT:
9004 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9005 case -NFS4ERR_DEADSESSION:
9006 nfs4_do_handle_exception(server, err, exception);
9007 }
9008}
9009
Chuck Lever38527b12012-07-11 16:30:23 -04009010/**
9011 * nfs41_test_stateid - perform a TEST_STATEID operation
9012 *
9013 * @server: server / transport on which to perform the operation
9014 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009015 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009016 *
9017 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9018 * Otherwise a negative NFS4ERR value is returned if the operation
9019 * failed or the state ID is not currently valid.
9020 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009021static int nfs41_test_stateid(struct nfs_server *server,
9022 nfs4_stateid *stateid,
9023 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009024{
9025 struct nfs4_exception exception = { };
9026 int err;
9027 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009028 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009029 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009030 } while (exception.retry);
9031 return err;
9032}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009033
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009034struct nfs_free_stateid_data {
9035 struct nfs_server *server;
9036 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009037 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009038};
9039
9040static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9041{
9042 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009043 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009044 &data->args.seq_args,
9045 &data->res.seq_res,
9046 task);
9047}
9048
9049static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9050{
9051 struct nfs_free_stateid_data *data = calldata;
9052
9053 nfs41_sequence_done(task, &data->res.seq_res);
9054
9055 switch (task->tk_status) {
9056 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009057 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009058 rpc_restart_call_prepare(task);
9059 }
9060}
9061
9062static void nfs41_free_stateid_release(void *calldata)
9063{
9064 kfree(calldata);
9065}
9066
Trond Myklebust17f26b12013-08-21 15:48:42 -04009067static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009068 .rpc_call_prepare = nfs41_free_stateid_prepare,
9069 .rpc_call_done = nfs41_free_stateid_done,
9070 .rpc_release = nfs41_free_stateid_release,
9071};
9072
9073static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009074 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009075 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009076 bool privileged)
9077{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009078 struct rpc_message msg = {
9079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009080 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009081 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009082 struct rpc_task_setup task_setup = {
9083 .rpc_client = server->client,
9084 .rpc_message = &msg,
9085 .callback_ops = &nfs41_free_stateid_ops,
9086 .flags = RPC_TASK_ASYNC,
9087 };
9088 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009089
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009090 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9091 &task_setup.rpc_client, &msg);
9092
Chuck Lever38527b12012-07-11 16:30:23 -04009093 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009094 data = kmalloc(sizeof(*data), GFP_NOFS);
9095 if (!data)
9096 return ERR_PTR(-ENOMEM);
9097 data->server = server;
9098 nfs4_stateid_copy(&data->args.stateid, stateid);
9099
9100 task_setup.callback_data = data;
9101
9102 msg.rpc_argp = &data->args;
9103 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009104 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009105 if (privileged)
9106 nfs4_set_sequence_privileged(&data->args.seq_args);
9107
9108 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009109}
9110
Chuck Lever38527b12012-07-11 16:30:23 -04009111/**
9112 * nfs41_free_stateid - perform a FREE_STATEID operation
9113 *
9114 * @server: server / transport on which to perform the operation
9115 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009116 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009117 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009118 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009119 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009120 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009121static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009122 const nfs4_stateid *stateid,
9123 struct rpc_cred *cred,
9124 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009125{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009126 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009127
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009128 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009129 if (IS_ERR(task))
9130 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009131 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009132 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009133}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009134
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009135static void
9136nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009137{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009138 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009139
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009140 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009141 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009142}
9143
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144static bool nfs41_match_stateid(const nfs4_stateid *s1,
9145 const nfs4_stateid *s2)
9146{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009147 if (s1->type != s2->type)
9148 return false;
9149
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009150 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009151 return false;
9152
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009153 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009154 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009155 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009156 return true;
9157
9158 return false;
9159}
9160
Andy Adamson557134a2009-04-01 09:21:53 -04009161#endif /* CONFIG_NFS_V4_1 */
9162
Trond Myklebust36281ca2012-03-04 18:13:56 -05009163static bool nfs4_match_stateid(const nfs4_stateid *s1,
9164 const nfs4_stateid *s2)
9165{
Trond Myklebustf597c532012-03-04 18:13:56 -05009166 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009167}
9168
9169
Trond Myklebust17280172012-03-11 13:11:00 -04009170static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009171 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009172 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173 .recover_open = nfs4_open_reclaim,
9174 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009175 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009176 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177};
9178
Andy Adamson591d71c2009-04-01 09:22:47 -04009179#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009180static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009181 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9182 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9183 .recover_open = nfs4_open_reclaim,
9184 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009185 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009186 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009187 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009188};
9189#endif /* CONFIG_NFS_V4_1 */
9190
Trond Myklebust17280172012-03-11 13:11:00 -04009191static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009192 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009193 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009194 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009196 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197};
9198
Andy Adamson591d71c2009-04-01 09:22:47 -04009199#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009200static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009201 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9202 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009203 .recover_open = nfs41_open_expired,
9204 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009205 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009206};
9207#endif /* CONFIG_NFS_V4_1 */
9208
Trond Myklebust17280172012-03-11 13:11:00 -04009209static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009210 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009211 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009212 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009213};
9214
9215#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009216static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009217 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009218 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009219 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009220};
9221#endif
9222
Chuck Leverec011fe2013-10-17 14:12:39 -04009223static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009224 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009225 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009226};
9227
9228#if defined(CONFIG_NFS_V4_1)
9229static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009230 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009231 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009232};
9233#endif /* CONFIG_NFS_V4_1 */
9234
Trond Myklebust97dc1352010-06-16 09:52:26 -04009235static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9236 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009237 .init_caps = NFS_CAP_READDIRPLUS
9238 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009239 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009240 .init_client = nfs40_init_client,
9241 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009242 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009243 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009244 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009245 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009246 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009247 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009248 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9249 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9250 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009251 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009252};
9253
9254#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009255static struct nfs_seqid *
9256nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9257{
9258 return NULL;
9259}
9260
Trond Myklebust97dc1352010-06-16 09:52:26 -04009261static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9262 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009263 .init_caps = NFS_CAP_READDIRPLUS
9264 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009265 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009266 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009267 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009268 .init_client = nfs41_init_client,
9269 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009270 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009271 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009272 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009273 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009274 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009275 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009276 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009277 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9278 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9279 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009280 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009281};
9282#endif
9283
Steve Dickson42c2c422013-05-22 12:50:38 -04009284#if defined(CONFIG_NFS_V4_2)
9285static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9286 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009287 .init_caps = NFS_CAP_READDIRPLUS
9288 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009289 | NFS_CAP_POSIX_LOCK
9290 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009291 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009292 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009293 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009294 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009295 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009296 | NFS_CAP_LAYOUTSTATS
9297 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009298 .init_client = nfs41_init_client,
9299 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009300 .match_stateid = nfs41_match_stateid,
9301 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009302 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009303 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009304 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009305 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009306 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009307 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9308 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9309 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009310 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009311};
9312#endif
9313
Trond Myklebust97dc1352010-06-16 09:52:26 -04009314const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9315 [0] = &nfs_v4_0_minor_ops,
9316#if defined(CONFIG_NFS_V4_1)
9317 [1] = &nfs_v4_1_minor_ops,
9318#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009319#if defined(CONFIG_NFS_V4_2)
9320 [2] = &nfs_v4_2_minor_ops,
9321#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009322};
9323
Trond Myklebust13997822016-07-24 17:10:52 -04009324static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009325{
9326 ssize_t error, error2;
9327
9328 error = generic_listxattr(dentry, list, size);
9329 if (error < 0)
9330 return error;
9331 if (list) {
9332 list += error;
9333 size -= error;
9334 }
9335
9336 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9337 if (error2 < 0)
9338 return error2;
9339 return error + error2;
9340}
9341
Trond Myklebust17f26b12013-08-21 15:48:42 -04009342static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009343 .create = nfs_create,
9344 .lookup = nfs_lookup,
9345 .atomic_open = nfs_atomic_open,
9346 .link = nfs_link,
9347 .unlink = nfs_unlink,
9348 .symlink = nfs_symlink,
9349 .mkdir = nfs_mkdir,
9350 .rmdir = nfs_rmdir,
9351 .mknod = nfs_mknod,
9352 .rename = nfs_rename,
9353 .permission = nfs_permission,
9354 .getattr = nfs_getattr,
9355 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009356 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009357};
9358
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009359static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009360 .permission = nfs_permission,
9361 .getattr = nfs_getattr,
9362 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009363 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009364};
9365
David Howells509de812006-08-22 20:06:11 -04009366const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 .version = 4, /* protocol version */
9368 .dentry_ops = &nfs4_dentry_operations,
9369 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009370 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009371 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009373 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009374 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 .getattr = nfs4_proc_getattr,
9376 .setattr = nfs4_proc_setattr,
9377 .lookup = nfs4_proc_lookup,
9378 .access = nfs4_proc_access,
9379 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380 .create = nfs4_proc_create,
9381 .remove = nfs4_proc_remove,
9382 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009383 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009385 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009386 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009387 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 .link = nfs4_proc_link,
9389 .symlink = nfs4_proc_symlink,
9390 .mkdir = nfs4_proc_mkdir,
9391 .rmdir = nfs4_proc_remove,
9392 .readdir = nfs4_proc_readdir,
9393 .mknod = nfs4_proc_mknod,
9394 .statfs = nfs4_proc_statfs,
9395 .fsinfo = nfs4_proc_fsinfo,
9396 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009397 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009399 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009401 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009403 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009405 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009406 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009408 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009409 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009410 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009411 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009412 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009413 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009414 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009415 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009416 .create_server = nfs4_create_server,
9417 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418};
9419
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009420static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009421 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009422 .list = nfs4_xattr_list_nfs4_acl,
9423 .get = nfs4_xattr_get_nfs4_acl,
9424 .set = nfs4_xattr_set_nfs4_acl,
9425};
9426
9427const struct xattr_handler *nfs4_xattr_handlers[] = {
9428 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009429#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9430 &nfs4_xattr_nfs4_label_handler,
9431#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009432 NULL
9433};
9434
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435/*
9436 * Local variables:
9437 * c-basic-offset: 8
9438 * End:
9439 */