blob: ae03f976fbd3320dee1e8bed91afa096439ececf [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;
Anna Schumaker3d358082017-01-11 10:54:04 -0500927 }
928
Anna Schumakerad05cc02017-01-11 13:37:06 -0500929 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500930out_start:
931 rpc_call_start(task);
932 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500933
934out_sleep:
935 if (args->sa_privileged)
936 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
937 NULL, RPC_PRIORITY_PRIVILEGED);
938 else
939 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
940 spin_unlock(&tbl->slot_tbl_lock);
941 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500942}
943EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
944
Chuck Lever9915ea72013-08-09 12:48:27 -0400945static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
946{
947 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500948 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400949 data->seq_args, data->seq_res, task);
950}
951
952static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
953{
954 struct nfs4_call_sync_data *data = calldata;
955 nfs4_sequence_done(task, data->seq_res);
956}
957
958static const struct rpc_call_ops nfs40_call_sync_ops = {
959 .rpc_call_prepare = nfs40_call_sync_prepare,
960 .rpc_call_done = nfs40_call_sync_done,
961};
962
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400963static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400964 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966 struct nfs4_sequence_args *args,
967 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400968{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 int ret;
970 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400971 struct nfs_client *clp = server->nfs_client;
972 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973 .seq_server = server,
974 .seq_args = args,
975 .seq_res = res,
976 };
977 struct rpc_task_setup task_setup = {
978 .rpc_client = clnt,
979 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400980 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981 .callback_data = &data
982 };
983
984 task = rpc_run_task(&task_setup);
985 if (IS_ERR(task))
986 ret = PTR_ERR(task);
987 else {
988 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400989 rpc_put_task(task);
990 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 return ret;
992}
993
Bryan Schumaker7c513052011-03-24 17:12:24 +0000994int nfs4_call_sync(struct rpc_clnt *clnt,
995 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000996 struct rpc_message *msg,
997 struct nfs4_sequence_args *args,
998 struct nfs4_sequence_res *res,
999 int cache_reply)
1000{
Chuck Levera9c92d62013-08-09 12:48:18 -04001001 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001002 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001005static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1006 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct nfs_inode *nfsi = NFS_I(dir);
1009
1010 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001011 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001012 if (cinfo->atomic && cinfo->before == dir->i_version) {
1013 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1014 nfsi->attrtimeo_timestamp = jiffies;
1015 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001017 if (cinfo->before != dir->i_version)
1018 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1019 NFS_INO_INVALID_ACL;
1020 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001021 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001022 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001023 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001024 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 spin_unlock(&dir->i_lock);
1026}
1027
1028struct nfs4_opendata {
1029 struct kref kref;
1030 struct nfs_openargs o_arg;
1031 struct nfs_openres o_res;
1032 struct nfs_open_confirmargs c_arg;
1033 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001034 struct nfs4_string owner_name;
1035 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001036 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001038 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001040 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct nfs4_state_owner *owner;
1042 struct nfs4_state *state;
1043 struct iattr attrs;
1044 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001045 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001046 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001047 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001048 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int cancelled;
1050};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001051
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001052static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1053 int err, struct nfs4_exception *exception)
1054{
1055 if (err != -EINVAL)
1056 return false;
1057 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1058 return false;
1059 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1060 exception->retry = 1;
1061 return true;
1062}
1063
Trond Myklebust6ae37332015-01-30 14:21:14 -05001064static u32
1065nfs4_map_atomic_open_share(struct nfs_server *server,
1066 fmode_t fmode, int openflags)
1067{
1068 u32 res = 0;
1069
1070 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1071 case FMODE_READ:
1072 res = NFS4_SHARE_ACCESS_READ;
1073 break;
1074 case FMODE_WRITE:
1075 res = NFS4_SHARE_ACCESS_WRITE;
1076 break;
1077 case FMODE_READ|FMODE_WRITE:
1078 res = NFS4_SHARE_ACCESS_BOTH;
1079 }
1080 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1081 goto out;
1082 /* Want no delegation if we're using O_DIRECT */
1083 if (openflags & O_DIRECT)
1084 res |= NFS4_SHARE_WANT_NO_DELEG;
1085out:
1086 return res;
1087}
1088
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001089static enum open_claim_type4
1090nfs4_map_atomic_open_claim(struct nfs_server *server,
1091 enum open_claim_type4 claim)
1092{
1093 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1094 return claim;
1095 switch (claim) {
1096 default:
1097 return claim;
1098 case NFS4_OPEN_CLAIM_FH:
1099 return NFS4_OPEN_CLAIM_NULL;
1100 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1101 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1102 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1103 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1104 }
1105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001108{
1109 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001111 p->o_res.seqid = p->o_arg.seqid;
1112 p->c_res.seqid = p->c_arg.seqid;
1113 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001114 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001116 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001117}
1118
Al Viro82a2c1b2011-06-22 18:30:55 -04001119static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001121 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001122 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001123 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001124 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125{
Al Viro82a2c1b2011-06-22 18:30:55 -04001126 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001127 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001129 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001130 struct nfs4_opendata *p;
1131
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001132 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001133 if (p == NULL)
1134 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001135
1136 p->f_label = nfs4_label_alloc(server, gfp_mask);
1137 if (IS_ERR(p->f_label))
1138 goto err_free_p;
1139
Kinglong Meea49c2692015-07-27 15:31:38 +08001140 p->a_label = nfs4_label_alloc(server, gfp_mask);
1141 if (IS_ERR(p->a_label))
1142 goto err_free_f;
1143
Trond Myklebust63f5f792015-01-23 19:19:25 -05001144 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1145 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001146 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001147 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001148 nfs_sb_active(dentry->d_sb);
1149 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150 p->dir = parent;
1151 p->owner = sp;
1152 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 p->o_arg.open_flags = flags;
1154 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001155 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001156 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001157 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1158 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001159 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1160 * will return permission denied for all bits until close */
1161 if (!(flags & O_EXCL)) {
1162 /* ask server to check for all possible rights as results
1163 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001164 switch (p->o_arg.claim) {
1165 default:
1166 break;
1167 case NFS4_OPEN_CLAIM_NULL:
1168 case NFS4_OPEN_CLAIM_FH:
1169 p->o_arg.access = NFS4_ACCESS_READ |
1170 NFS4_ACCESS_MODIFY |
1171 NFS4_ACCESS_EXTEND |
1172 NFS4_ACCESS_EXECUTE;
1173 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001174 }
David Howells7539bba2006-08-22 20:06:09 -04001175 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001176 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1177 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001178 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001180 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001181 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001182 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001183 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001184 case NFS4_OPEN_CLAIM_NULL:
1185 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1186 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1187 p->o_arg.fh = NFS_FH(dir);
1188 break;
1189 case NFS4_OPEN_CLAIM_PREVIOUS:
1190 case NFS4_OPEN_CLAIM_FH:
1191 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1192 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001193 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001194 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001195 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001196 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001197
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 p->o_arg.u.attrs = &p->attrs;
1199 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001200
1201 verf[0] = jiffies;
1202 verf[1] = current->pid;
1203 memcpy(p->o_arg.u.verifier.data, verf,
1204 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001206 p->c_arg.fh = &p->o_res.fh;
1207 p->c_arg.stateid = &p->o_res.stateid;
1208 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001210 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001212
1213err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001214 nfs4_label_free(p->a_label);
1215err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001216 nfs4_label_free(p->f_label);
1217err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001218 kfree(p);
1219err:
1220 dput(parent);
1221 return NULL;
1222}
1223
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001225{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001226 struct nfs4_opendata *p = container_of(kref,
1227 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001228 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229
1230 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001231 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232 if (p->state != NULL)
1233 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001235
Kinglong Meea49c2692015-07-27 15:31:38 +08001236 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001237 nfs4_label_free(p->f_label);
1238
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001239 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001240 dput(p->dentry);
1241 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001242 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001243 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001244 kfree(p);
1245}
1246
1247static void nfs4_opendata_put(struct nfs4_opendata *p)
1248{
1249 if (p != NULL)
1250 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251}
1252
Trond Myklebust06f814a2006-01-03 09:55:07 +01001253static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1254{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001255 int ret;
1256
Trond Myklebust06f814a2006-01-03 09:55:07 +01001257 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001258 return ret;
1259}
1260
Trond Myklebust24311f82015-09-20 10:50:17 -04001261static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1262 fmode_t fmode)
1263{
1264 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1265 case FMODE_READ|FMODE_WRITE:
1266 return state->n_rdwr != 0;
1267 case FMODE_WRITE:
1268 return state->n_wronly != 0;
1269 case FMODE_READ:
1270 return state->n_rdonly != 0;
1271 }
1272 WARN_ON_ONCE(1);
1273 return false;
1274}
1275
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277{
1278 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279
Trond Myklebust536e43d2012-01-17 22:04:26 -05001280 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 goto out;
1282 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001283 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001284 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1285 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001286 break;
1287 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001288 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1289 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001290 break;
1291 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001292 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1293 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001294 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001295out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001296 return ret;
1297}
1298
Trond Myklebust2a606182015-08-19 22:30:00 -05001299static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1300 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001301{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001302 if (delegation == NULL)
1303 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001305 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001306 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1307 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001308 switch (claim) {
1309 case NFS4_OPEN_CLAIM_NULL:
1310 case NFS4_OPEN_CLAIM_FH:
1311 break;
1312 case NFS4_OPEN_CLAIM_PREVIOUS:
1313 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1314 break;
1315 default:
1316 return 0;
1317 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001318 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001319 return 1;
1320}
1321
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001322static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001323{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001324 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001325 case FMODE_WRITE:
1326 state->n_wronly++;
1327 break;
1328 case FMODE_READ:
1329 state->n_rdonly++;
1330 break;
1331 case FMODE_READ|FMODE_WRITE:
1332 state->n_rdwr++;
1333 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001335}
1336
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001337#ifdef CONFIG_NFS_V4_1
1338static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1339{
1340 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1341 return true;
1342 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1343 return true;
1344 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1345 return true;
1346 return false;
1347}
1348#endif /* CONFIG_NFS_V4_1 */
1349
Trond Myklebust4f14c192014-02-12 19:15:06 -05001350static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001351{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001352 struct nfs_client *clp = state->owner->so_server->nfs_client;
1353 bool need_recover = false;
1354
1355 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1356 need_recover = true;
1357 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1358 need_recover = true;
1359 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1360 need_recover = true;
1361 if (need_recover)
1362 nfs4_state_mark_reclaim_nograce(clp, state);
1363}
1364
Trond Myklebuste999e802014-02-10 18:20:47 -05001365static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001366 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001367{
1368 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1369 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001370 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001371 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001372 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001373 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001374 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001375 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1376 return true;
1377 return false;
1378}
1379
Trond Myklebustf95549c2015-01-23 18:06:09 -05001380static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1381{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001382 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1383 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001384 if (state->n_wronly)
1385 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1386 if (state->n_rdonly)
1387 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1388 if (state->n_rdwr)
1389 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001390 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001391}
1392
Trond Myklebust226056c2014-02-11 10:41:07 -05001393static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1394 nfs4_stateid *stateid, fmode_t fmode)
1395{
1396 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1397 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1398 case FMODE_WRITE:
1399 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1400 break;
1401 case FMODE_READ:
1402 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1403 break;
1404 case 0:
1405 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1406 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1407 clear_bit(NFS_OPEN_STATE, &state->flags);
1408 }
1409 if (stateid == NULL)
1410 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001411 /* Handle OPEN+OPEN_DOWNGRADE races */
1412 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1413 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001414 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001415 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001416 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001417 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001418 nfs4_stateid_copy(&state->stateid, stateid);
1419 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001420}
1421
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001422static void nfs_clear_open_stateid(struct nfs4_state *state,
1423 nfs4_stateid *arg_stateid,
1424 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001425{
1426 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001427 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1428 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1429 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001430 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001431 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1432 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001433}
1434
Trond Myklebust1393d962016-09-22 13:39:13 -04001435static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1436 const nfs4_stateid *stateid, fmode_t fmode,
1437 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001438{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001439 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001440 case FMODE_READ:
1441 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1442 break;
1443 case FMODE_WRITE:
1444 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1445 break;
1446 case FMODE_READ|FMODE_WRITE:
1447 set_bit(NFS_O_RDWR_STATE, &state->flags);
1448 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001449 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001450 return;
1451 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1452 nfs4_stateid_copy(&state->stateid, stateid);
1453 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001454}
1455
Trond Myklebust1393d962016-09-22 13:39:13 -04001456static void __update_open_stateid(struct nfs4_state *state,
1457 const nfs4_stateid *open_stateid,
1458 const nfs4_stateid *deleg_stateid,
1459 fmode_t fmode,
1460 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001462 /*
1463 * Protect the call to nfs4_state_set_mode_locked and
1464 * serialise the stateid update
1465 */
Andrew Elble361cad32015-12-02 09:20:57 -05001466 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001467 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001468 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001469 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001470 set_bit(NFS_DELEGATED_STATE, &state->flags);
1471 }
1472 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001473 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001474 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001476 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Trond Myklebust1393d962016-09-22 13:39:13 -04001479static int update_open_stateid(struct nfs4_state *state,
1480 const nfs4_stateid *open_stateid,
1481 const nfs4_stateid *delegation,
1482 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001483{
Trond Myklebust1393d962016-09-22 13:39:13 -04001484 struct nfs_server *server = NFS_SERVER(state->inode);
1485 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001486 struct nfs_inode *nfsi = NFS_I(state->inode);
1487 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001488 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001489 int ret = 0;
1490
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001491 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001492
1493 rcu_read_lock();
1494 deleg_cur = rcu_dereference(nfsi->delegation);
1495 if (deleg_cur == NULL)
1496 goto no_delegation;
1497
1498 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001499 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001500 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001501 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001502 goto no_delegation_unlock;
1503
1504 if (delegation == NULL)
1505 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001506 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001507 goto no_delegation_unlock;
1508
Trond Myklebustb7391f42008-12-23 15:21:52 -05001509 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1511 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001512 ret = 1;
1513no_delegation_unlock:
1514 spin_unlock(&deleg_cur->lock);
1515no_delegation:
1516 rcu_read_unlock();
1517
1518 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001519 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001520 ret = 1;
1521 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001522 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001523 nfs4_schedule_state_manager(clp);
1524 if (freeme.type != 0)
1525 nfs4_test_and_free_stateid(server, &freeme,
1526 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001527
1528 return ret;
1529}
1530
Trond Myklebust39071e62015-01-24 15:07:56 -05001531static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1532 const nfs4_stateid *stateid)
1533{
1534 struct nfs4_state *state = lsp->ls_state;
1535 bool ret = false;
1536
1537 spin_lock(&state->state_lock);
1538 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1539 goto out_noupdate;
1540 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1541 goto out_noupdate;
1542 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1543 ret = true;
1544out_noupdate:
1545 spin_unlock(&state->state_lock);
1546 return ret;
1547}
Trond Myklebust34310432008-12-23 15:21:38 -05001548
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001549static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001550{
1551 struct nfs_delegation *delegation;
1552
1553 rcu_read_lock();
1554 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001555 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001556 rcu_read_unlock();
1557 return;
1558 }
1559 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001560 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001561}
1562
Trond Myklebust6ee41262007-07-08 14:11:36 -04001563static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001564{
1565 struct nfs4_state *state = opendata->state;
1566 struct nfs_inode *nfsi = NFS_I(state->inode);
1567 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001568 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001569 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001570 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001571 nfs4_stateid stateid;
1572 int ret = -EAGAIN;
1573
Trond Myklebustaac00a82007-07-05 19:02:21 -04001574 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001575 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001576 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001577 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001578 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001579 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001581 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001582 rcu_read_lock();
1583 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001584 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001585 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001586 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001587 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001588 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001589 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001591 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001592 if (!opendata->is_recover) {
1593 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1594 if (ret != 0)
1595 goto out;
1596 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001597 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001598
1599 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001600 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001601 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001602 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001603out:
1604 return ERR_PTR(ret);
1605out_return_state:
1606 atomic_inc(&state->count);
1607 return state;
1608}
1609
Andy Adamsone23008e2012-10-02 21:07:32 -04001610static void
1611nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1612{
1613 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1614 struct nfs_delegation *delegation;
1615 int delegation_flags = 0;
1616
1617 rcu_read_lock();
1618 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1619 if (delegation)
1620 delegation_flags = delegation->flags;
1621 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001622 switch (data->o_arg.claim) {
1623 default:
1624 break;
1625 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1626 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001627 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1628 "returning a delegation for "
1629 "OPEN(CLAIM_DELEGATE_CUR)\n",
1630 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001631 return;
1632 }
1633 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001634 nfs_inode_set_delegation(state->inode,
1635 data->owner->so_cred,
1636 &data->o_res);
1637 else
1638 nfs_inode_reclaim_delegation(state->inode,
1639 data->owner->so_cred,
1640 &data->o_res);
1641}
1642
1643/*
1644 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1645 * and update the nfs4_state.
1646 */
1647static struct nfs4_state *
1648_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1649{
1650 struct inode *inode = data->state->inode;
1651 struct nfs4_state *state = data->state;
1652 int ret;
1653
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001654 if (!data->rpc_done) {
1655 if (data->rpc_status) {
1656 ret = data->rpc_status;
1657 goto err;
1658 }
1659 /* cached opens have already been processed */
1660 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001661 }
1662
Andy Adamsone23008e2012-10-02 21:07:32 -04001663 ret = nfs_refresh_inode(inode, &data->f_attr);
1664 if (ret)
1665 goto err;
1666
1667 if (data->o_res.delegation_type != 0)
1668 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001669update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001670 update_open_stateid(state, &data->o_res.stateid, NULL,
1671 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001672 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001673
1674 return state;
1675err:
1676 return ERR_PTR(ret);
1677
1678}
1679
1680static struct nfs4_state *
1681_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682{
1683 struct inode *inode;
1684 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686
Trond Myklebustaac00a82007-07-05 19:02:21 -04001687 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001688 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001689 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001690 goto out;
1691 }
1692
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001693 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001695 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001696 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001697 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001698 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001699 goto err;
1700 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701 state = nfs4_get_open_state(inode, data->owner);
1702 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001703 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001704 if (data->o_res.delegation_type != 0)
1705 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001706 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001707 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001710 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001711 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001712err_put_inode:
1713 iput(inode);
1714err:
1715 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001716}
1717
Andy Adamsone23008e2012-10-02 21:07:32 -04001718static struct nfs4_state *
1719nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1720{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001721 struct nfs4_state *ret;
1722
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001724 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1725 else
1726 ret = _nfs4_opendata_to_nfs4_state(data);
1727 nfs4_sequence_free_slot(&data->o_res.seq_res);
1728 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001729}
1730
Trond Myklebust864472e2006-01-03 09:55:15 +01001731static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1732{
1733 struct nfs_inode *nfsi = NFS_I(state->inode);
1734 struct nfs_open_context *ctx;
1735
1736 spin_lock(&state->inode->i_lock);
1737 list_for_each_entry(ctx, &nfsi->open_files, list) {
1738 if (ctx->state != state)
1739 continue;
1740 get_nfs_open_context(ctx);
1741 spin_unlock(&state->inode->i_lock);
1742 return ctx;
1743 }
1744 spin_unlock(&state->inode->i_lock);
1745 return ERR_PTR(-ENOENT);
1746}
1747
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001748static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1749 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001750{
1751 struct nfs4_opendata *opendata;
1752
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001753 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001754 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001755 if (opendata == NULL)
1756 return ERR_PTR(-ENOMEM);
1757 opendata->state = state;
1758 atomic_inc(&state->count);
1759 return opendata;
1760}
1761
Trond Myklebust24311f82015-09-20 10:50:17 -04001762static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1763 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001764{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001765 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001766 int ret;
1767
Trond Myklebust24311f82015-09-20 10:50:17 -04001768 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001769 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001770 opendata->o_arg.open_flags = 0;
1771 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001772 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1773 NFS_SB(opendata->dentry->d_sb),
1774 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001775 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1776 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1777 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001778 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001779 if (ret != 0)
1780 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001781 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001782 if (IS_ERR(newstate))
1783 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 if (newstate != opendata->state)
1785 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001786 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001787 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001788}
1789
1790static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1791{
Trond Myklebust864472e2006-01-03 09:55:15 +01001792 int ret;
1793
Trond Myklebust4f14c192014-02-12 19:15:06 -05001794 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1795 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1796 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1797 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001798 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001799 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001800 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001801 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001802 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1803 if (ret != 0)
1804 return ret;
1805 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1806 if (ret != 0)
1807 return ret;
1808 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1809 if (ret != 0)
1810 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001811 /*
1812 * We may have performed cached opens for all three recoveries.
1813 * Check if we need to update the current stateid.
1814 */
1815 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001816 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001817 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001818 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001819 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001820 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001821 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001822 return 0;
1823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/*
1826 * OPEN_RECLAIM:
1827 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001829static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001831 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001833 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int status;
1835
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001836 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1837 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001838 if (IS_ERR(opendata))
1839 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001840 rcu_read_lock();
1841 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001842 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001843 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001844 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 opendata->o_arg.u.delegation_type = delegation_type;
1846 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001847 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return status;
1849}
1850
Trond Myklebust539cd032007-06-05 11:46:42 -04001851static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 struct nfs_server *server = NFS_SERVER(state->inode);
1854 struct nfs4_exception exception = { };
1855 int err;
1856 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001857 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001858 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001859 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1860 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001861 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001862 break;
1863 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 } while (exception.retry);
1865 return err;
1866}
1867
Trond Myklebust864472e2006-01-03 09:55:15 +01001868static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1869{
1870 struct nfs_open_context *ctx;
1871 int ret;
1872
1873 ctx = nfs4_state_find_open_context(state);
1874 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001875 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001876 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001877 put_nfs_open_context(ctx);
1878 return ret;
1879}
1880
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001881static 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 -07001882{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001883 switch (err) {
1884 default:
1885 printk(KERN_ERR "NFS: %s: unhandled error "
1886 "%d.\n", __func__, err);
1887 case 0:
1888 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001889 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001890 case -ESTALE:
1891 break;
1892 case -NFS4ERR_BADSESSION:
1893 case -NFS4ERR_BADSLOT:
1894 case -NFS4ERR_BAD_HIGH_SLOT:
1895 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1896 case -NFS4ERR_DEADSESSION:
1897 set_bit(NFS_DELEGATED_STATE, &state->flags);
1898 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1899 return -EAGAIN;
1900 case -NFS4ERR_STALE_CLIENTID:
1901 case -NFS4ERR_STALE_STATEID:
1902 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001903 /* Don't recall a delegation if it was lost */
1904 nfs4_schedule_lease_recovery(server->nfs_client);
1905 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001906 case -NFS4ERR_MOVED:
1907 nfs4_schedule_migration_recovery(server);
1908 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001909 case -NFS4ERR_LEASE_MOVED:
1910 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1911 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001912 case -NFS4ERR_DELEG_REVOKED:
1913 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001914 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001915 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001916 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001917 nfs_inode_find_state_and_recover(state->inode,
1918 stateid);
1919 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001920 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001921 case -NFS4ERR_DELAY:
1922 case -NFS4ERR_GRACE:
1923 set_bit(NFS_DELEGATED_STATE, &state->flags);
1924 ssleep(1);
1925 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001926 case -ENOMEM:
1927 case -NFS4ERR_DENIED:
1928 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1929 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return err;
1932}
1933
Trond Myklebust24311f82015-09-20 10:50:17 -04001934int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1935 struct nfs4_state *state, const nfs4_stateid *stateid,
1936 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001937{
1938 struct nfs_server *server = NFS_SERVER(state->inode);
1939 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001940 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001941
1942 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1943 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1944 if (IS_ERR(opendata))
1945 return PTR_ERR(opendata);
1946 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001947 write_seqlock(&state->seqlock);
1948 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1949 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001950 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1951 switch (type & (FMODE_READ|FMODE_WRITE)) {
1952 case FMODE_READ|FMODE_WRITE:
1953 case FMODE_WRITE:
1954 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1955 if (err)
1956 break;
1957 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1958 if (err)
1959 break;
1960 case FMODE_READ:
1961 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1962 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001963 nfs4_opendata_put(opendata);
1964 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1965}
1966
Chuck Leverbe05c862013-08-09 12:49:47 -04001967static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1968{
1969 struct nfs4_opendata *data = calldata;
1970
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001971 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1972 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001973}
1974
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001975static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1976{
1977 struct nfs4_opendata *data = calldata;
1978
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001979 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001980
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001981 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001982 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001983 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001984 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001985 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001986 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001987 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988}
1989
1990static void nfs4_open_confirm_release(void *calldata)
1991{
1992 struct nfs4_opendata *data = calldata;
1993 struct nfs4_state *state = NULL;
1994
1995 /* If this request hasn't been cancelled, do nothing */
1996 if (data->cancelled == 0)
1997 goto out_free;
1998 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001999 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002000 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002001 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002002 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002003 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002004out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002005 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002006}
2007
2008static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002009 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002010 .rpc_call_done = nfs4_open_confirm_done,
2011 .rpc_release = nfs4_open_confirm_release,
2012};
2013
2014/*
2015 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2016 */
2017static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2018{
David Howells2b0143b2015-03-17 22:25:59 +00002019 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002020 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002021 struct rpc_message msg = {
2022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2023 .rpc_argp = &data->c_arg,
2024 .rpc_resp = &data->c_res,
2025 .rpc_cred = data->owner->so_cred,
2026 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002027 struct rpc_task_setup task_setup_data = {
2028 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002029 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002030 .callback_ops = &nfs4_open_confirm_ops,
2031 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002032 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002033 .flags = RPC_TASK_ASYNC,
2034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int status;
2036
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002037 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002038 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002039 data->rpc_done = 0;
2040 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002041 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002042 if (data->is_recover)
2043 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002044 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002045 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002046 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002047 status = nfs4_wait_for_completion_rpc_task(task);
2048 if (status != 0) {
2049 data->cancelled = 1;
2050 smp_wmb();
2051 } else
2052 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002053 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return status;
2055}
2056
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002057static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002059 struct nfs4_opendata *data = calldata;
2060 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002061 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002062 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002063
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002064 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002065 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002066 /*
2067 * Check if we still need to send an OPEN call, or if we can use
2068 * a delegation instead.
2069 */
2070 if (data->state != NULL) {
2071 struct nfs_delegation *delegation;
2072
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002073 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002074 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002075 rcu_read_lock();
2076 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002077 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002078 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002079 rcu_read_unlock();
2080 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002081 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002082 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002083 switch (claim) {
2084 default:
2085 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002086 case NFS4_OPEN_CLAIM_PREVIOUS:
2087 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2088 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002089 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002090 case NFS4_OPEN_CLAIM_FH:
2091 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002092 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2093 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002094 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002095 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002096 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002097 &data->o_res.seq_res,
2098 task) != 0)
2099 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002100
2101 /* Set the create mode (note dependency on the session type) */
2102 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2103 if (data->o_arg.open_flags & O_EXCL) {
2104 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2105 if (nfs4_has_persistent_session(clp))
2106 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2107 else if (clp->cl_mvops->minor_version > 0)
2108 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2109 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002110 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002111unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002112 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002113 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002114out_no_action:
2115 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002116out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002117 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002118}
2119
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120static void nfs4_open_done(struct rpc_task *task, void *calldata)
2121{
2122 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002125
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002126 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002127 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002129 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002130 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2131 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132 case S_IFREG:
2133 break;
2134 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002135 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002136 break;
2137 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002139 break;
2140 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002141 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002142 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002143 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002144 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002145 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2146 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002147 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002148 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002149}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002150
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002151static void nfs4_open_release(void *calldata)
2152{
2153 struct nfs4_opendata *data = calldata;
2154 struct nfs4_state *state = NULL;
2155
2156 /* If this request hasn't been cancelled, do nothing */
2157 if (data->cancelled == 0)
2158 goto out_free;
2159 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002160 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161 goto out_free;
2162 /* In case we need an open_confirm, no cleanup! */
2163 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2164 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002165 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002166 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002167 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002168out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002169 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002170}
2171
2172static const struct rpc_call_ops nfs4_open_ops = {
2173 .rpc_call_prepare = nfs4_open_prepare,
2174 .rpc_call_done = nfs4_open_done,
2175 .rpc_release = nfs4_open_release,
2176};
2177
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002178static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002179{
David Howells2b0143b2015-03-17 22:25:59 +00002180 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 struct nfs_server *server = NFS_SERVER(dir);
2182 struct nfs_openargs *o_arg = &data->o_arg;
2183 struct nfs_openres *o_res = &data->o_res;
2184 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 struct rpc_message msg = {
2186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2187 .rpc_argp = o_arg,
2188 .rpc_resp = o_res,
2189 .rpc_cred = data->owner->so_cred,
2190 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002191 struct rpc_task_setup task_setup_data = {
2192 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002193 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002194 .callback_ops = &nfs4_open_ops,
2195 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002196 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002197 .flags = RPC_TASK_ASYNC,
2198 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002199 int status;
2200
Chuck Levera9c92d62013-08-09 12:48:18 -04002201 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002202 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002203 data->rpc_done = 0;
2204 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002205 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002206 data->is_recover = 0;
2207 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002208 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002209 data->is_recover = 1;
2210 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002211 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212 if (IS_ERR(task))
2213 return PTR_ERR(task);
2214 status = nfs4_wait_for_completion_rpc_task(task);
2215 if (status != 0) {
2216 data->cancelled = 1;
2217 smp_wmb();
2218 } else
2219 status = data->rpc_status;
2220 rpc_put_task(task);
2221
2222 return status;
2223}
2224
2225static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2226{
David Howells2b0143b2015-03-17 22:25:59 +00002227 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002228 struct nfs_openres *o_res = &data->o_res;
2229 int status;
2230
2231 status = nfs4_run_open_task(data, 1);
2232 if (status != 0 || !data->rpc_done)
2233 return status;
2234
Trond Myklebust6926afd12012-01-07 13:22:46 -05002235 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2236
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002237 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2238 status = _nfs4_proc_open_confirm(data);
2239 if (status != 0)
2240 return status;
2241 }
2242
2243 return status;
2244}
2245
Trond Myklebustf3792d62014-07-10 08:54:32 -04002246/*
2247 * Additional permission checks in order to distinguish between an
2248 * open for read, and an open for execute. This works around the
2249 * fact that NFSv4 OPEN treats read and execute permissions as being
2250 * the same.
2251 * Note that in the non-execute case, we want to turn off permission
2252 * checking if we just created a new file (POSIX open() semantics).
2253 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002254static int nfs4_opendata_access(struct rpc_cred *cred,
2255 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002256 struct nfs4_state *state, fmode_t fmode,
2257 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002258{
2259 struct nfs_access_entry cache;
2260 u32 mask;
2261
2262 /* access call failed or for some reason the server doesn't
2263 * support any access modes -- defer access call until later */
2264 if (opendata->o_res.access_supported == 0)
2265 return 0;
2266
2267 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002268 /*
2269 * Use openflags to check for exec, because fmode won't
2270 * always have FMODE_EXEC set when file open for exec.
2271 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002272 if (openflags & __FMODE_EXEC) {
2273 /* ONLY check for exec rights */
2274 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002275 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002276 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002277
2278 cache.cred = cred;
2279 cache.jiffies = jiffies;
2280 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2281 nfs_access_add_cache(state->inode, &cache);
2282
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002283 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002284 return 0;
2285
2286 /* even though OPEN succeeded, access is denied. Close the file */
2287 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002288 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002289}
2290
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002291/*
2292 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2293 */
2294static int _nfs4_proc_open(struct nfs4_opendata *data)
2295{
David Howells2b0143b2015-03-17 22:25:59 +00002296 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002297 struct nfs_server *server = NFS_SERVER(dir);
2298 struct nfs_openargs *o_arg = &data->o_arg;
2299 struct nfs_openres *o_res = &data->o_res;
2300 int status;
2301
2302 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002303 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002304 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002305 if (status != 0) {
2306 if (status == -NFS4ERR_BADNAME &&
2307 !(o_arg->open_flags & O_CREAT))
2308 return -ENOENT;
2309 return status;
2310 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311
Trond Myklebust6926afd12012-01-07 13:22:46 -05002312 nfs_fattr_map_and_free_names(server, &data->f_attr);
2313
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002314 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002315 if (o_arg->open_flags & O_EXCL)
2316 data->file_created = 1;
2317 else if (o_res->cinfo.before != o_res->cinfo.after)
2318 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002319 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002320 update_changeattr(dir, &o_res->cinfo,
2321 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002322 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002323 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2324 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002326 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002328 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002331 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Andy Adamsond83217c2011-03-01 01:34:17 +00002335static int nfs4_recover_expired_lease(struct nfs_server *server)
2336{
2337 return nfs4_client_recover_expired_lease(server->nfs_client);
2338}
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340/*
2341 * OPEN_EXPIRED:
2342 * reclaim state on the server after a network partition.
2343 * Assumes caller holds the appropriate lock
2344 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002345static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002347 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002348 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002350 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002351 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002352 if (IS_ERR(opendata))
2353 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002354 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002355 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002356 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002357 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002361static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002362{
Trond Myklebust539cd032007-06-05 11:46:42 -04002363 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002364 struct nfs4_exception exception = { };
2365 int err;
2366
2367 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002368 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002369 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002370 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2371 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002372 switch (err) {
2373 default:
2374 goto out;
2375 case -NFS4ERR_GRACE:
2376 case -NFS4ERR_DELAY:
2377 nfs4_handle_exception(server, err, &exception);
2378 err = 0;
2379 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002380 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002381out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002382 return err;
2383}
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002388 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Trond Myklebust864472e2006-01-03 09:55:15 +01002390 ctx = nfs4_state_find_open_context(state);
2391 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002392 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002393 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002394 put_nfs_open_context(ctx);
2395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
Trond Myklebust41020b62016-09-22 13:38:58 -04002398static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2399 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002400{
Trond Myklebust41020b62016-09-22 13:38:58 -04002401 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002402 write_seqlock(&state->seqlock);
2403 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2404 write_sequnlock(&state->seqlock);
2405 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2406}
2407
2408static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2409{
2410 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002411 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002412}
2413
2414static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2415{
2416 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2417 nfs40_clear_delegation_stateid(state);
2418 return nfs4_open_expired(sp, state);
2419}
2420
Trond Myklebust45870d62016-09-22 13:38:59 -04002421static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2422 nfs4_stateid *stateid,
2423 struct rpc_cred *cred)
2424{
2425 return -NFS4ERR_BAD_STATEID;
2426}
2427
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002428#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002429static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2430 nfs4_stateid *stateid,
2431 struct rpc_cred *cred)
2432{
2433 int status;
2434
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002435 switch (stateid->type) {
2436 default:
2437 break;
2438 case NFS4_INVALID_STATEID_TYPE:
2439 case NFS4_SPECIAL_STATEID_TYPE:
2440 return -NFS4ERR_BAD_STATEID;
2441 case NFS4_REVOKED_STATEID_TYPE:
2442 goto out_free;
2443 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002444
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002445 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002446 switch (status) {
2447 case -NFS4ERR_EXPIRED:
2448 case -NFS4ERR_ADMIN_REVOKED:
2449 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002450 break;
2451 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002452 return status;
2453 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002454out_free:
2455 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002456 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002457 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002458}
2459
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002460static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002461{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002462 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002463 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002464 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002465 struct rpc_cred *cred;
2466 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002467
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002468 /* Get the delegation credential for use by test/free_stateid */
2469 rcu_read_lock();
2470 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002471 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002472 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002473 return;
2474 }
2475
2476 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002477 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2478 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002479 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002480 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002481 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002482
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002483 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2484 rcu_read_unlock();
2485 return;
2486 }
2487
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002488 cred = get_rpccred(delegation->cred);
2489 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002490 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002491 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002492 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002493 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002494
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002495 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002496}
2497
2498/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002499 * nfs41_check_expired_locks - possibly free a lock stateid
2500 *
2501 * @state: NFSv4 state for an inode
2502 *
2503 * Returns NFS_OK if recovery for this stateid is now finished.
2504 * Otherwise a negative NFS4ERR value is returned.
2505 */
2506static int nfs41_check_expired_locks(struct nfs4_state *state)
2507{
2508 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002509 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002510 struct nfs_server *server = NFS_SERVER(state->inode);
2511
2512 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2513 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002514
2515 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002516 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2517 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2518 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2519
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002520 atomic_inc(&lsp->ls_count);
2521 spin_unlock(&state->state_lock);
2522
2523 nfs4_put_lock_state(prev);
2524 prev = lsp;
2525
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002526 status = nfs41_test_and_free_expired_stateid(server,
2527 &lsp->ls_stateid,
2528 cred);
2529 trace_nfs4_test_lock_stateid(state, lsp, status);
2530 if (status == -NFS4ERR_EXPIRED ||
2531 status == -NFS4ERR_BAD_STATEID) {
2532 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002533 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002534 if (!recover_lost_locks)
2535 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2536 } else if (status != NFS_OK) {
2537 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002538 nfs4_put_lock_state(prev);
2539 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002540 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002541 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002542 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002543 }
2544 spin_unlock(&state->state_lock);
2545 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002546out:
2547 return ret;
2548}
2549
2550/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002551 * nfs41_check_open_stateid - possibly free an open stateid
2552 *
2553 * @state: NFSv4 state for an inode
2554 *
2555 * Returns NFS_OK if recovery for this stateid is now finished.
2556 * Otherwise a negative NFS4ERR value is returned.
2557 */
2558static int nfs41_check_open_stateid(struct nfs4_state *state)
2559{
2560 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002561 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002562 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002563 int status;
2564
Trond Myklebustb134fc42016-09-22 13:39:16 -04002565 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002566 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2567 if (nfs4_have_delegation(state->inode, state->state))
2568 return NFS_OK;
2569 return -NFS4ERR_OPENMODE;
2570 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002571 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002572 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002573 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002574 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002575 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002576 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2577 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2578 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002579 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002580 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002581 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002582 if (status != NFS_OK)
2583 return status;
2584 if (nfs_open_stateid_recover_openmode(state))
2585 return -NFS4ERR_OPENMODE;
2586 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002587}
2588
2589static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2590{
Chuck Levereb64cf92012-07-11 16:30:05 -04002591 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002592
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002593 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002594 status = nfs41_check_expired_locks(state);
2595 if (status != NFS_OK)
2596 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002597 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002598 if (status != NFS_OK)
2599 status = nfs4_open_expired(sp, state);
2600 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002601}
2602#endif
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002605 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2606 * fields corresponding to attributes that were used to store the verifier.
2607 * Make sure we clobber those fields in the later setattr call
2608 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002609static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2610 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002611{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002612 const u32 *attrset = opendata->o_res.attrset;
2613
2614 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002615 !(sattr->ia_valid & ATTR_ATIME_SET))
2616 sattr->ia_valid |= ATTR_ATIME;
2617
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002618 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002619 !(sattr->ia_valid & ATTR_MTIME_SET))
2620 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002621
2622 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002623 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2624 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002625 sattr->ia_valid &= ~ATTR_MODE;
2626
2627 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2628 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002629}
2630
Trond Myklebustc21443c2013-02-07 14:26:21 -05002631static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2632 fmode_t fmode,
2633 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002634 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002635{
2636 struct nfs4_state_owner *sp = opendata->owner;
2637 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002638 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002639 struct nfs4_state *state;
2640 unsigned int seq;
2641 int ret;
2642
2643 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2644
2645 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002646 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002647 goto out;
2648
2649 state = nfs4_opendata_to_nfs4_state(opendata);
2650 ret = PTR_ERR(state);
2651 if (IS_ERR(state))
2652 goto out;
2653 if (server->caps & NFS_CAP_POSIX_LOCK)
2654 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002655 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2656 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002657
Trond Myklebust275bb302013-05-29 13:11:28 -04002658 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002659 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002660 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002661 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002662 alias = d_exact_alias(dentry, state->inode);
2663 if (!alias)
2664 alias = d_splice_alias(igrab(state->inode), dentry);
2665 /* d_splice_alias() can't fail here - it's a non-directory */
2666 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002667 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002668 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002669 }
2670 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002671 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002672 }
2673
Trond Myklebustc21443c2013-02-07 14:26:21 -05002674 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2675 if (ret != 0)
2676 goto out;
2677
Trond Myklebust3efb9722013-05-29 13:17:04 -04002678 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002679 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002680 nfs_inode_attach_open_context(ctx);
2681 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2682 nfs4_schedule_stateid_recovery(server, state);
2683 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002684out:
2685 return ret;
2686}
2687
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002688/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002689 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 */
Andy Adamson82be4172012-05-23 05:02:35 -04002691static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002692 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002693 int flags,
2694 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002695 struct nfs4_label *label,
2696 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
2698 struct nfs4_state_owner *sp;
2699 struct nfs4_state *state = NULL;
2700 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002701 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002702 struct dentry *dentry = ctx->dentry;
2703 struct rpc_cred *cred = ctx->cred;
2704 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2705 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002706 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002707 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002708 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002712 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2713 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2715 goto out_err;
2716 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002717 status = nfs4_recover_expired_lease(server);
2718 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002719 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002720 if (d_really_is_positive(dentry))
2721 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002722 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002723 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002724 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002725 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002726 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002727 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002728 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
David Quigley14c43f72013-05-22 12:50:43 -04002730 if (label) {
2731 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2732 if (IS_ERR(olabel)) {
2733 status = PTR_ERR(olabel);
2734 goto err_opendata_put;
2735 }
2736 }
2737
Trond Myklebuste911b812014-03-26 13:24:37 -07002738 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2739 if (!opendata->f_attr.mdsthreshold) {
2740 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2741 if (!opendata->f_attr.mdsthreshold)
2742 goto err_free_label;
2743 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002744 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002745 }
David Howells2b0143b2015-03-17 22:25:59 +00002746 if (d_really_is_positive(dentry))
2747 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002748
Trond Myklebust3efb9722013-05-29 13:17:04 -04002749 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002750 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002751 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002752 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002753
NeilBrownefcbc042015-07-30 13:00:56 +10002754 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002755 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002756 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002757 /*
2758 * send create attributes which was not set by open
2759 * with an extra setattr.
2760 */
2761 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2762 nfs_fattr_init(opendata->o_res.f_attr);
2763 status = nfs4_do_setattr(state->inode, cred,
2764 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002765 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002766 if (status == 0) {
2767 nfs_setattr_update_inode(state->inode, sattr,
2768 opendata->o_res.f_attr);
2769 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2770 }
David Quigley1775fd32013-05-22 12:50:42 -04002771 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002772 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002773 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002774 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002775
Trond Myklebuste911b812014-03-26 13:24:37 -07002776 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002777 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002778 opendata->f_attr.mdsthreshold = NULL;
2779 }
Andy Adamson82be4172012-05-23 05:02:35 -04002780
David Quigley14c43f72013-05-22 12:50:43 -04002781 nfs4_label_free(olabel);
2782
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002783 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002786err_free_label:
2787 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002788err_opendata_put:
2789 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002790err_put_state_owner:
2791 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return status;
2794}
2795
2796
Andy Adamson82be4172012-05-23 05:02:35 -04002797static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002798 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002799 int flags,
2800 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002801 struct nfs4_label *label,
2802 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002804 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 struct nfs4_exception exception = { };
2806 struct nfs4_state *res;
2807 int status;
2808
2809 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002810 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002811 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002812 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (status == 0)
2814 break;
2815 /* NOTE: BAD_SEQID means the server and client disagree about the
2816 * book-keeping w.r.t. state-changing operations
2817 * (OPEN/CLOSE/LOCK/LOCKU...)
2818 * It is actually a sign of a bug on the client or on the server.
2819 *
2820 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002821 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 * have unhashed the old state_owner for us, and that we can
2823 * therefore safely retry using a new one. We should still warn
2824 * the user though...
2825 */
2826 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002827 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002828 " returned a bad sequence-id error!\n",
2829 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 exception.retry = 1;
2831 continue;
2832 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002833 /*
2834 * BAD_STATEID on OPEN means that the server cancelled our
2835 * state before it received the OPEN_CONFIRM.
2836 * Recover by retrying the request as per the discussion
2837 * on Page 181 of RFC3530.
2838 */
2839 if (status == -NFS4ERR_BAD_STATEID) {
2840 exception.retry = 1;
2841 continue;
2842 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002843 if (status == -EAGAIN) {
2844 /* We must have found a delegation */
2845 exception.retry = 1;
2846 continue;
2847 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002848 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2849 continue;
2850 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 status, &exception));
2852 } while (exception.retry);
2853 return res;
2854}
2855
Trond Myklebust8487c472016-06-26 08:44:35 -04002856static int _nfs4_do_setattr(struct inode *inode,
2857 struct nfs_setattrargs *arg,
2858 struct nfs_setattrres *res,
2859 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002860 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002861{
2862 struct nfs_server *server = NFS_SERVER(inode);
2863 struct rpc_message msg = {
2864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2865 .rpc_argp = arg,
2866 .rpc_resp = res,
2867 .rpc_cred = cred,
2868 };
2869 struct rpc_cred *delegation_cred = NULL;
2870 unsigned long timestamp = jiffies;
2871 fmode_t fmode;
2872 bool truncate;
2873 int status;
2874
2875 nfs_fattr_init(res->fattr);
2876
2877 /* Servers should only apply open mode checks for file size changes */
2878 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2879 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2880
2881 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2882 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002883 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002884 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002885 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002886 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002887 l_ctx = nfs_get_lock_context(ctx);
2888 if (IS_ERR(l_ctx))
2889 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002890 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2891 &arg->stateid, &delegation_cred);
2892 nfs_put_lock_context(l_ctx);
2893 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002894 return -EBADF;
2895 } else
2896 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2897 if (delegation_cred)
2898 msg.rpc_cred = delegation_cred;
2899
2900 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2901
2902 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002903 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002904 renew_lease(server, timestamp);
2905 trace_nfs4_setattr(inode, &arg->stateid, status);
2906 return status;
2907}
2908
2909static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2910 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002911 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002912 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002914 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002915 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002917 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 .iap = sattr,
2919 .server = server,
2920 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002921 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 };
2923 struct nfs_setattrres res = {
2924 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002925 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 .server = server,
2927 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002928 struct nfs4_exception exception = {
2929 .state = state,
2930 .inode = inode,
2931 .stateid = &arg.stateid,
2932 };
2933 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
David Quigleyaa9c2662013-05-22 12:50:44 -04002935 arg.bitmask = nfs4_bitmask(server, ilabel);
2936 if (ilabel)
2937 arg.bitmask = nfs4_bitmask(server, olabel);
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002940 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002941 switch (err) {
2942 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002943 if (!(sattr->ia_valid & ATTR_SIZE)) {
2944 pr_warn_once("NFSv4: server %s is incorrectly "
2945 "applying open mode checks to "
2946 "a SETATTR that is not "
2947 "changing file size.\n",
2948 server->nfs_client->cl_hostname);
2949 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002950 if (state && !(state->state & FMODE_WRITE)) {
2951 err = -EBADF;
2952 if (sattr->ia_valid & ATTR_OPEN)
2953 err = -EACCES;
2954 goto out;
2955 }
2956 }
2957 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002959out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return err;
2961}
2962
Peng Tao500d7012015-09-22 11:35:22 +08002963static bool
2964nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2965{
2966 if (inode == NULL || !nfs_have_layout(inode))
2967 return false;
2968
2969 return pnfs_wait_on_layoutreturn(inode, task);
2970}
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972struct nfs4_closedata {
2973 struct inode *inode;
2974 struct nfs4_state *state;
2975 struct nfs_closeargs arg;
2976 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002977 struct {
2978 struct nfs4_layoutreturn_args arg;
2979 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002980 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002981 u32 roc_barrier;
2982 bool roc;
2983 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002984 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002985 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986};
2987
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002988static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002989{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002990 struct nfs4_closedata *calldata = data;
2991 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002992 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002993
Trond Myklebustcf805162016-11-15 14:56:07 -05002994 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002995 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2996 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002997 nfs4_put_open_state(calldata->state);
2998 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002999 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003000 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003001 kfree(calldata);
3002}
3003
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003004static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003006 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003009 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Chuck Levera3ca5652012-03-01 17:00:40 -05003011 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003012 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3013 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003014 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003015
3016 /* Handle Layoutreturn errors */
3017 if (calldata->arg.lr_args && task->tk_status != 0) {
3018 switch (calldata->res.lr_ret) {
3019 default:
3020 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3021 break;
3022 case 0:
3023 calldata->arg.lr_args = NULL;
3024 calldata->res.lr_res = NULL;
3025 break;
3026 case -NFS4ERR_ADMIN_REVOKED:
3027 case -NFS4ERR_DELEG_REVOKED:
3028 case -NFS4ERR_EXPIRED:
3029 case -NFS4ERR_BAD_STATEID:
3030 case -NFS4ERR_OLD_STATEID:
3031 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3032 case -NFS4ERR_WRONG_CRED:
3033 calldata->arg.lr_args = NULL;
3034 calldata->res.lr_res = NULL;
3035 calldata->res.lr_ret = 0;
3036 rpc_restart_call_prepare(task);
3037 return;
3038 }
3039 }
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 /* hmm. we are done with the inode, and in the process of freeing
3042 * the state_owner. we keep this around to process errors
3043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 switch (task->tk_status) {
3045 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003046 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003047 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003048 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003049 case -NFS4ERR_ACCESS:
3050 if (calldata->arg.bitmask != NULL) {
3051 calldata->arg.bitmask = NULL;
3052 calldata->res.fattr = NULL;
3053 task->tk_status = 0;
3054 rpc_restart_call_prepare(task);
3055 goto out_release;
3056
3057 }
3058 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003059 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003061 case -NFS4ERR_EXPIRED:
3062 nfs4_free_revoked_stateid(server,
3063 &calldata->arg.stateid,
3064 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003065 case -NFS4ERR_OLD_STATEID:
3066 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003067 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003068 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003069 rpc_restart_call_prepare(task);
3070 goto out_release;
3071 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003072 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003075 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003076 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003077 goto out_release;
3078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003080 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3081 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003082out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003083 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003084 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003085 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003088static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003090 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003091 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003092 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003093 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003094 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003095
Chuck Levera3ca5652012-03-01 17:00:40 -05003096 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003097 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003098 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003099
Trond Myklebust88069f72009-12-08 08:33:16 -05003100 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003101 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003102 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3103 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3104 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003105 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003106 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003107 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003108 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003109 if (state->n_rdonly == 0)
3110 call_close |= is_rdonly;
3111 else if (is_rdonly)
3112 calldata->arg.fmode |= FMODE_READ;
3113 if (state->n_wronly == 0)
3114 call_close |= is_wronly;
3115 else if (is_wronly)
3116 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003117 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3118 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003119 } else if (is_rdwr)
3120 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3121
Trond Myklebust5cc78612016-11-14 11:19:56 -05003122 if (!nfs4_valid_open_stateid(state) ||
3123 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003124 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003125 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003126
3127 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003128 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003129 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003130 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003131
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003132 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003133 nfs_release_seqid(calldata->arg.seqid);
3134 goto out_wait;
3135 }
3136
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003137 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003138 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003139
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003140 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003141 /* Close-to-open cache consistency revalidation */
3142 if (!nfs4_have_delegation(inode, FMODE_READ))
3143 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3144 else
3145 calldata->arg.bitmask = NULL;
3146 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003147
Trond Myklebust6ae37332015-01-30 14:21:14 -05003148 calldata->arg.share_access =
3149 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3150 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003151
Trond Myklebustd8d84982016-12-19 12:14:44 -05003152 if (calldata->res.fattr == NULL)
3153 calldata->arg.bitmask = NULL;
3154 else if (calldata->arg.bitmask == NULL)
3155 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003156 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003157 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003158 &calldata->arg.seq_args,
3159 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003160 task) != 0)
3161 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003162 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003163 return;
3164out_no_action:
3165 task->tk_action = NULL;
3166out_wait:
3167 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003170static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003171 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172 .rpc_call_done = nfs4_close_done,
3173 .rpc_release = nfs4_free_closedata,
3174};
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176/*
3177 * It is possible for data to be read/written from a mem-mapped file
3178 * after the sys_close call (which hits the vfs layer as a flush).
3179 * This means that we can't safely call nfsv4 close on a file until
3180 * the inode is cleared. This in turn means that we are not good
3181 * NFSv4 citizens - we do not indicate to the server to update the file's
3182 * share state even when we are done with one of the three share
3183 * stateid's in the inode.
3184 *
3185 * NOTE: Caller must be holding the sp->so_owner semaphore!
3186 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003187int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003189 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003190 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003192 struct nfs4_state_owner *sp = state->owner;
3193 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3196 .rpc_cred = state->owner->so_cred,
3197 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003198 struct rpc_task_setup task_setup_data = {
3199 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003200 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003201 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003202 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003203 .flags = RPC_TASK_ASYNC,
3204 };
Trond Myklebust95121352005-10-18 14:20:12 -07003205 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003207 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3208 &task_setup_data.rpc_client, &msg);
3209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003210 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003212 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003213 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003214 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003216 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003218 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3219 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003220 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003221 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003222 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003223 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003224 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003225 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003226 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003227 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003228 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003229 calldata->lr.roc = pnfs_roc(state->inode,
3230 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3231 if (calldata->lr.roc) {
3232 calldata->arg.lr_args = &calldata->lr.arg;
3233 calldata->res.lr_res = &calldata->lr.res;
3234 }
Al Viro643168c2011-06-22 18:20:23 -04003235 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003236
Trond Myklebust1174dd12010-12-21 10:52:24 -05003237 msg.rpc_argp = &calldata->arg;
3238 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003239 task_setup_data.callback_data = calldata;
3240 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003241 if (IS_ERR(task))
3242 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003243 status = 0;
3244 if (wait)
3245 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003246 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003247 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003248out_free_calldata:
3249 kfree(calldata);
3250out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003251 nfs4_put_open_state(state);
3252 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003253 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
Trond Myklebust2b484292010-09-17 10:56:51 -04003256static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003257nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3258 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003261 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3262
3263 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Trond Myklebust565277f2007-10-15 18:17:53 -04003265 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003266 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003267
3268 nfs4_label_release_security(label);
3269
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003270 if (IS_ERR(state))
3271 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003272 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Trond Myklebust1185a552009-12-03 15:54:02 -05003275static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003276{
3277 if (ctx->state == NULL)
3278 return;
3279 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003280 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003281 else
Al Viro643168c2011-06-22 18:20:23 -04003282 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Trond Myklebustb944dba2013-11-04 15:20:20 -05003285#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3286#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003287#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3290{
Kinglong Mee8c612822015-08-26 21:12:58 +08003291 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003292 struct nfs4_server_caps_arg args = {
3293 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003294 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 struct nfs4_server_caps_res res = {};
3297 struct rpc_message msg = {
3298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003299 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .rpc_resp = &res,
3301 };
3302 int status;
3303
Kinglong Mee8c612822015-08-26 21:12:58 +08003304 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3305 FATTR4_WORD0_FH_EXPIRE_TYPE |
3306 FATTR4_WORD0_LINK_SUPPORT |
3307 FATTR4_WORD0_SYMLINK_SUPPORT |
3308 FATTR4_WORD0_ACLSUPPORT;
3309 if (minorversion)
3310 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3311
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003314 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003315 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003316 case 0:
3317 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3318 res.attr_bitmask[2] = 0;
3319 break;
3320 case 1:
3321 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3322 break;
3323 case 2:
3324 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003327 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3328 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3329 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3330 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003331 NFS_CAP_CTIME|NFS_CAP_MTIME|
3332 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003333 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3334 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 server->caps |= NFS_CAP_ACLS;
3336 if (res.has_links != 0)
3337 server->caps |= NFS_CAP_HARDLINKS;
3338 if (res.has_symlinks != 0)
3339 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003340 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3341 server->caps |= NFS_CAP_FILEID;
3342 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3343 server->caps |= NFS_CAP_MODE;
3344 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3345 server->caps |= NFS_CAP_NLINK;
3346 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3347 server->caps |= NFS_CAP_OWNER;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3349 server->caps |= NFS_CAP_OWNER_GROUP;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3351 server->caps |= NFS_CAP_ATIME;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3353 server->caps |= NFS_CAP_CTIME;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3355 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003356#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3357 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3358 server->caps |= NFS_CAP_SECURITY_LABEL;
3359#endif
3360 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3361 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003362 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003363
Trond Myklebusta65318b2009-03-11 14:10:28 -04003364 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3365 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3366 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003367 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003368 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3369 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003371 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return status;
3375}
3376
Trond Myklebust55a97592006-06-09 09:34:19 -04003377int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
3379 struct nfs4_exception exception = { };
3380 int err;
3381 do {
3382 err = nfs4_handle_exception(server,
3383 _nfs4_server_capabilities(server, fhandle),
3384 &exception);
3385 } while (exception.retry);
3386 return err;
3387}
3388
3389static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3390 struct nfs_fsinfo *info)
3391{
David Quigleyaa9c2662013-05-22 12:50:44 -04003392 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003394 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 };
3396 struct nfs4_lookup_res res = {
3397 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003398 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 .fh = fhandle,
3400 };
3401 struct rpc_message msg = {
3402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3403 .rpc_argp = &args,
3404 .rpc_resp = &res,
3405 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003406
David Quigleyaa9c2662013-05-22 12:50:44 -04003407 bitmask[0] = nfs4_fattr_bitmap[0];
3408 bitmask[1] = nfs4_fattr_bitmap[1];
3409 /*
3410 * Process the label in the upcoming getfattr
3411 */
3412 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3413
Trond Myklebust0e574af2005-10-27 22:12:38 -04003414 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003415 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3419 struct nfs_fsinfo *info)
3420{
3421 struct nfs4_exception exception = { };
3422 int err;
3423 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003424 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003425 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003426 switch (err) {
3427 case 0:
3428 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003429 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003430 default:
3431 err = nfs4_handle_exception(server, err, &exception);
3432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return err;
3436}
3437
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003438static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3439 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3440{
Trond Myklebustc2190662013-08-26 19:23:04 -04003441 struct rpc_auth_create_args auth_args = {
3442 .pseudoflavor = flavor,
3443 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003444 struct rpc_auth *auth;
3445 int ret;
3446
Trond Myklebustc2190662013-08-26 19:23:04 -04003447 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003448 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003449 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003450 goto out;
3451 }
3452 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003453out:
3454 return ret;
3455}
3456
Chuck Lever9a744ba2013-03-16 15:56:02 -04003457/*
3458 * Retry pseudoroot lookup with various security flavors. We do this when:
3459 *
3460 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3461 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3462 *
3463 * Returns zero on success, or a negative NFS4ERR value, or a
3464 * negative errno value.
3465 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003466static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003467 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003469 /* Per 3530bis 15.33.5 */
3470 static const rpc_authflavor_t flav_array[] = {
3471 RPC_AUTH_GSS_KRB5P,
3472 RPC_AUTH_GSS_KRB5I,
3473 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003474 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003475 RPC_AUTH_NULL,
3476 };
3477 int status = -EPERM;
3478 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003480 if (server->auth_info.flavor_len > 0) {
3481 /* try each flavor specified by user */
3482 for (i = 0; i < server->auth_info.flavor_len; i++) {
3483 status = nfs4_lookup_root_sec(server, fhandle, info,
3484 server->auth_info.flavors[i]);
3485 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3486 continue;
3487 break;
3488 }
3489 } else {
3490 /* no flavors specified by user, try default list */
3491 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3492 status = nfs4_lookup_root_sec(server, fhandle, info,
3493 flav_array[i]);
3494 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3495 continue;
3496 break;
3497 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003498 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003499
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003500 /*
3501 * -EACCESS could mean that the user doesn't have correct permissions
3502 * to access the mount. It could also mean that we tried to mount
3503 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3504 * existing mount programs don't handle -EACCES very well so it should
3505 * be mapped to -EPERM instead.
3506 */
3507 if (status == -EACCES)
3508 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003509 return status;
3510}
3511
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003512/**
3513 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3514 * @server: initialized nfs_server handle
3515 * @fhandle: we fill in the pseudo-fs root file handle
3516 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003517 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003518 *
3519 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003520 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003521int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003522 struct nfs_fsinfo *info,
3523 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003524{
Andre Przywarac7757072015-04-23 17:17:40 +01003525 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003526
Andre Przywarac7757072015-04-23 17:17:40 +01003527 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003528 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003529
3530 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003531 status = server->nfs_client->cl_mvops->find_root_sec(server,
3532 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 if (status == 0)
3535 status = nfs4_server_capabilities(server, fhandle);
3536 if (status == 0)
3537 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003538
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003539 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Bryan Schumakerbae36242012-05-10 15:07:31 -04003542static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3543 struct nfs_fsinfo *info)
3544{
3545 int error;
3546 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003547 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003548
3549 error = nfs4_server_capabilities(server, mntfh);
3550 if (error < 0) {
3551 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3552 return error;
3553 }
3554
David Quigley14c43f72013-05-22 12:50:43 -04003555 label = nfs4_label_alloc(server, GFP_KERNEL);
3556 if (IS_ERR(label))
3557 return PTR_ERR(label);
3558
David Quigley1775fd32013-05-22 12:50:42 -04003559 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003560 if (error < 0) {
3561 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003562 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003563 }
3564
3565 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3566 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3567 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3568
David Quigley14c43f72013-05-22 12:50:43 -04003569err_free_label:
3570 nfs4_label_free(label);
3571
Bryan Schumakerbae36242012-05-10 15:07:31 -04003572 return error;
3573}
3574
Manoj Naik6b97fd32006-06-09 09:34:29 -04003575/*
3576 * Get locations and (maybe) other attributes of a referral.
3577 * Note that we'll actually follow the referral later when
3578 * we detect fsid mismatch in inode revalidation
3579 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003580static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3581 const struct qstr *name, struct nfs_fattr *fattr,
3582 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003583{
3584 int status = -ENOMEM;
3585 struct page *page = NULL;
3586 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003587
3588 page = alloc_page(GFP_KERNEL);
3589 if (page == NULL)
3590 goto out;
3591 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3592 if (locations == NULL)
3593 goto out;
3594
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003595 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003596 if (status != 0)
3597 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003598
3599 /*
3600 * If the fsid didn't change, this is a migration event, not a
3601 * referral. Cause us to drop into the exception handler, which
3602 * will kick off migration recovery.
3603 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003604 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003605 dprintk("%s: server did not return a different fsid for"
3606 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003607 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003608 goto out;
3609 }
Andy Adamson533eb462011-06-13 18:25:56 -04003610 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3611 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003612
Andy Adamson533eb462011-06-13 18:25:56 -04003613 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003615 memset(fhandle, 0, sizeof(struct nfs_fh));
3616out:
3617 if (page)
3618 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003619 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003620 return status;
3621}
3622
David Quigley1775fd32013-05-22 12:50:42 -04003623static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3624 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 struct nfs4_getattr_arg args = {
3627 .fh = fhandle,
3628 .bitmask = server->attr_bitmask,
3629 };
3630 struct nfs4_getattr_res res = {
3631 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003632 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 .server = server,
3634 };
3635 struct rpc_message msg = {
3636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3637 .rpc_argp = &args,
3638 .rpc_resp = &res,
3639 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003640
3641 args.bitmask = nfs4_bitmask(server, label);
3642
Trond Myklebust0e574af2005-10-27 22:12:38 -04003643 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003644 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
David Quigley1775fd32013-05-22 12:50:42 -04003647static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3648 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 struct nfs4_exception exception = { };
3651 int err;
3652 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003653 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3654 trace_nfs4_getattr(server, fhandle, fattr, err);
3655 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 &exception);
3657 } while (exception.retry);
3658 return err;
3659}
3660
3661/*
3662 * The file is not closed if it is opened due to the a request to change
3663 * the size of the file. The open call will not be needed once the
3664 * VFS layer lookup-intents are implemented.
3665 *
3666 * Close is called when the inode is destroyed.
3667 * If we haven't opened the file for O_WRONLY, we
3668 * need to in the size_change case to obtain a stateid.
3669 *
3670 * Got race?
3671 * Because OPEN is always done by name in nfsv4, it is
3672 * possible that we opened a different file by the same
3673 * name. We can recognize this race condition, but we
3674 * can't do anything about it besides returning an error.
3675 *
3676 * This will be fixed with VFS changes (lookup-intent).
3677 */
3678static int
3679nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3680 struct iattr *sattr)
3681{
David Howells2b0143b2015-03-17 22:25:59 +00003682 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003683 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003684 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003685 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 int status;
3687
Peng Tao88ac8152014-09-12 11:04:10 +08003688 if (pnfs_ld_layoutret_on_setattr(inode) &&
3689 sattr->ia_valid & ATTR_SIZE &&
3690 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003691 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003692
Trond Myklebust0e574af2005-10-27 22:12:38 -04003693 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
Andy Adamson26699402012-05-30 16:12:24 -04003695 /* Deal with open(O_TRUNC) */
3696 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003697 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003698
3699 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003700 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003701 return 0;
3702
Trond Myklebustd5308382005-11-04 15:33:38 -05003703 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003704 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003705
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003706 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003707 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003708 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003709 }
3710
David Quigley14c43f72013-05-22 12:50:43 -04003711 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3712 if (IS_ERR(label))
3713 return PTR_ERR(label);
3714
NeilBrown29b59f92016-10-13 15:26:47 +11003715 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003716 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003717 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003718 nfs_setsecurity(inode, fattr, label);
3719 }
David Quigley14c43f72013-05-22 12:50:43 -04003720 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return status;
3722}
3723
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003724static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3725 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003726 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003727{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003728 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003729 int status;
3730 struct nfs4_lookup_arg args = {
3731 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003732 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003733 .name = name,
3734 };
3735 struct nfs4_lookup_res res = {
3736 .server = server,
3737 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003738 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003739 .fh = fhandle,
3740 };
3741 struct rpc_message msg = {
3742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3743 .rpc_argp = &args,
3744 .rpc_resp = &res,
3745 };
3746
David Quigleyaa9c2662013-05-22 12:50:44 -04003747 args.bitmask = nfs4_bitmask(server, label);
3748
David Howells2b3de442006-08-22 20:06:09 -04003749 nfs_fattr_init(fattr);
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003752 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 dprintk("NFS reply lookup: %d\n", status);
3754 return status;
3755}
3756
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003757static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003758{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003759 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003760 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003761 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3762 fattr->nlink = 2;
3763}
3764
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003765static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003766 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003767 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003768{
3769 struct nfs4_exception exception = { };
3770 struct rpc_clnt *client = *clnt;
3771 int err;
3772 do {
David Quigley1775fd32013-05-22 12:50:42 -04003773 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003774 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003775 switch (err) {
3776 case -NFS4ERR_BADNAME:
3777 err = -ENOENT;
3778 goto out;
3779 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003780 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003781 if (err == -NFS4ERR_MOVED)
3782 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003783 goto out;
3784 case -NFS4ERR_WRONGSEC:
3785 err = -EPERM;
3786 if (client != *clnt)
3787 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003788 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003789 if (IS_ERR(client))
3790 return PTR_ERR(client);
3791
3792 exception.retry = 1;
3793 break;
3794 default:
3795 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3796 }
3797 } while (exception.retry);
3798
3799out:
3800 if (err == 0)
3801 *clnt = client;
3802 else if (client != *clnt)
3803 rpc_shutdown_client(client);
3804
3805 return err;
3806}
3807
Al Virobeffb8f2016-07-20 16:34:42 -04003808static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003809 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3810 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003812 int status;
3813 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003814
David Quigley1775fd32013-05-22 12:50:42 -04003815 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003816 if (client != NFS_CLIENT(dir)) {
3817 rpc_shutdown_client(client);
3818 nfs_fixup_secinfo_attributes(fattr);
3819 }
3820 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003823struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003824nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003825 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3826{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003827 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003828 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003829
David Quigley1775fd32013-05-22 12:50:42 -04003830 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003831 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003832 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003833 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003834}
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3837{
Trond Myklebust76b32992007-08-10 17:45:11 -04003838 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 struct nfs4_accessargs args = {
3840 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003841 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003843 struct nfs4_accessres res = {
3844 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003845 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3848 .rpc_argp = &args,
3849 .rpc_resp = &res,
3850 .rpc_cred = entry->cred,
3851 };
3852 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003853 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 /*
3856 * Determine which access bits we want to ask for...
3857 */
3858 if (mode & MAY_READ)
3859 args.access |= NFS4_ACCESS_READ;
3860 if (S_ISDIR(inode->i_mode)) {
3861 if (mode & MAY_WRITE)
3862 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3863 if (mode & MAY_EXEC)
3864 args.access |= NFS4_ACCESS_LOOKUP;
3865 } else {
3866 if (mode & MAY_WRITE)
3867 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3868 if (mode & MAY_EXEC)
3869 args.access |= NFS4_ACCESS_EXECUTE;
3870 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003871
3872 res.fattr = nfs_alloc_fattr();
3873 if (res.fattr == NULL)
3874 return -ENOMEM;
3875
Bryan Schumaker7c513052011-03-24 17:12:24 +00003876 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003878 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003879 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003881 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return status;
3883}
3884
3885static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3886{
3887 struct nfs4_exception exception = { };
3888 int err;
3889 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003890 err = _nfs4_proc_access(inode, entry);
3891 trace_nfs4_access(inode, err);
3892 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 &exception);
3894 } while (exception.retry);
3895 return err;
3896}
3897
3898/*
3899 * TODO: For the time being, we don't try to get any attributes
3900 * along with any of the zero-copy operations READ, READDIR,
3901 * READLINK, WRITE.
3902 *
3903 * In the case of the first three, we want to put the GETATTR
3904 * after the read-type operation -- this is because it is hard
3905 * to predict the length of a GETATTR response in v4, and thus
3906 * align the READ data correctly. This means that the GETATTR
3907 * may end up partially falling into the page cache, and we should
3908 * shift it into the 'tail' of the xdr_buf before processing.
3909 * To do this efficiently, we need to know the total length
3910 * of data received, which doesn't seem to be available outside
3911 * of the RPC layer.
3912 *
3913 * In the case of WRITE, we also want to put the GETATTR after
3914 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003915 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 *
3917 * Both of these changes to the XDR layer would in fact be quite
3918 * minor, but I decided to leave them for a subsequent patch.
3919 */
3920static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3921 unsigned int pgbase, unsigned int pglen)
3922{
3923 struct nfs4_readlink args = {
3924 .fh = NFS_FH(inode),
3925 .pgbase = pgbase,
3926 .pglen = pglen,
3927 .pages = &page,
3928 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003929 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 struct rpc_message msg = {
3931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3932 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003933 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 };
3935
Bryan Schumaker7c513052011-03-24 17:12:24 +00003936 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 -07003937}
3938
3939static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3940 unsigned int pgbase, unsigned int pglen)
3941{
3942 struct nfs4_exception exception = { };
3943 int err;
3944 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003945 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3946 trace_nfs4_readlink(inode, err);
3947 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 &exception);
3949 } while (exception.retry);
3950 return err;
3951}
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003954 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956static int
3957nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003958 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003960 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003961 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003962 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 int status = 0;
3965
NeilBrown532d4de2016-10-13 15:26:47 +11003966 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003967 if (IS_ERR(ctx))
3968 return PTR_ERR(ctx);
3969
David Quigleyaa9c2662013-05-22 12:50:44 -04003970 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3971
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003972 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3973 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003974 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 if (IS_ERR(state)) {
3976 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003980 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003981 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return status;
3983}
3984
Al Virobeffb8f2016-07-20 16:34:42 -04003985static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Trond Myklebust16e42952005-10-27 22:12:44 -04003987 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003988 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003990 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003992 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003993 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3997 .rpc_argp = &args,
3998 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004000 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004001 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004002
Bryan Schumaker7c513052011-03-24 17:12:24 +00004003 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004004 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004005 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return status;
4007}
4008
Al Virobeffb8f2016-07-20 16:34:42 -04004009static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 struct nfs4_exception exception = { };
4012 int err;
4013 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004014 err = _nfs4_proc_remove(dir, name);
4015 trace_nfs4_remove(dir, name, err);
4016 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 &exception);
4018 } while (exception.retry);
4019 return err;
4020}
4021
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004022static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004024 struct nfs_server *server = NFS_SERVER(dir);
4025 struct nfs_removeargs *args = msg->rpc_argp;
4026 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004028 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004030 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004031
4032 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004035static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4036{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004037 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004038 &data->args.seq_args,
4039 &data->res.seq_res,
4040 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004043static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004045 struct nfs_unlinkdata *data = task->tk_calldata;
4046 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004047
Trond Myklebust14516c32010-07-31 14:29:06 -04004048 if (!nfs4_sequence_done(task, &res->seq_res))
4049 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004050 if (nfs4_async_handle_error(task, res->server, NULL,
4051 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004052 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004053 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004054 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004055 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
Jeff Laytond3d41522010-09-17 17:31:57 -04004058static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4059{
4060 struct nfs_server *server = NFS_SERVER(dir);
4061 struct nfs_renameargs *arg = msg->rpc_argp;
4062 struct nfs_renameres *res = msg->rpc_resp;
4063
4064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004065 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004066 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004067}
4068
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004069static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4070{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004071 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004072 &data->args.seq_args,
4073 &data->res.seq_res,
4074 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004075}
4076
4077static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4078 struct inode *new_dir)
4079{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004080 struct nfs_renamedata *data = task->tk_calldata;
4081 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004082
4083 if (!nfs4_sequence_done(task, &res->seq_res))
4084 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004085 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004086 return 0;
4087
Trond Myklebustc733c492017-01-11 12:32:26 -05004088 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004089 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004090 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004091 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004092 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004093 return 1;
4094}
4095
Al Virobeffb8f2016-07-20 16:34:42 -04004096static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004098 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 struct nfs4_link_arg arg = {
4100 .fh = NFS_FH(inode),
4101 .dir_fh = NFS_FH(dir),
4102 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004103 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004105 struct nfs4_link_res res = {
4106 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004107 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4111 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004112 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004114 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Trond Myklebust136f2622010-04-16 16:22:49 -04004116 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004117 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004118 goto out;
4119
David Quigley14c43f72013-05-22 12:50:43 -04004120 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4121 if (IS_ERR(res.label)) {
4122 status = PTR_ERR(res.label);
4123 goto out;
4124 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004125 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004126
Bryan Schumaker7c513052011-03-24 17:12:24 +00004127 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004128 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004129 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004130 status = nfs_post_op_update_inode(inode, res.fattr);
4131 if (!status)
4132 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004133 }
David Quigley14c43f72013-05-22 12:50:43 -04004134
4135
4136 nfs4_label_free(res.label);
4137
Trond Myklebust136f2622010-04-16 16:22:49 -04004138out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004139 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return status;
4141}
4142
Al Virobeffb8f2016-07-20 16:34:42 -04004143static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 struct nfs4_exception exception = { };
4146 int err;
4147 do {
4148 err = nfs4_handle_exception(NFS_SERVER(inode),
4149 _nfs4_proc_link(inode, dir, name),
4150 &exception);
4151 } while (exception.retry);
4152 return err;
4153}
4154
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004155struct nfs4_createdata {
4156 struct rpc_message msg;
4157 struct nfs4_create_arg arg;
4158 struct nfs4_create_res res;
4159 struct nfs_fh fh;
4160 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004161 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004162};
4163
4164static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004165 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004166{
4167 struct nfs4_createdata *data;
4168
4169 data = kzalloc(sizeof(*data), GFP_KERNEL);
4170 if (data != NULL) {
4171 struct nfs_server *server = NFS_SERVER(dir);
4172
David Quigley14c43f72013-05-22 12:50:43 -04004173 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4174 if (IS_ERR(data->label))
4175 goto out_free;
4176
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004177 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4178 data->msg.rpc_argp = &data->arg;
4179 data->msg.rpc_resp = &data->res;
4180 data->arg.dir_fh = NFS_FH(dir);
4181 data->arg.server = server;
4182 data->arg.name = name;
4183 data->arg.attrs = sattr;
4184 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004185 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004186 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 data->res.server = server;
4188 data->res.fh = &data->fh;
4189 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004190 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192 }
4193 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004194out_free:
4195 kfree(data);
4196 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004197}
4198
4199static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4200{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004201 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004202 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004203 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004204 update_changeattr(dir, &data->res.dir_cinfo,
4205 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004206 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 }
4208 return status;
4209}
4210
4211static void nfs4_free_createdata(struct nfs4_createdata *data)
4212{
David Quigley14c43f72013-05-22 12:50:43 -04004213 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004214 kfree(data);
4215}
4216
Chuck Lever4f390c12006-08-22 20:06:22 -04004217static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004218 struct page *page, unsigned int len, struct iattr *sattr,
4219 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 struct nfs4_createdata *data;
4222 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Chuck Lever94a6d752006-08-22 20:06:23 -04004224 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004225 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004226
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 status = -ENOMEM;
4228 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4229 if (data == NULL)
4230 goto out;
4231
4232 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4233 data->arg.u.symlink.pages = &page;
4234 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004235 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 status = nfs4_do_create(dir, dentry, data);
4238
4239 nfs4_free_createdata(data);
4240out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return status;
4242}
4243
Chuck Lever4f390c12006-08-22 20:06:22 -04004244static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004245 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004248 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004250
4251 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004254 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4255 trace_nfs4_symlink(dir, &dentry->d_name, err);
4256 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 &exception);
4258 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004259
4260 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 return err;
4262}
4263
4264static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004265 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267 struct nfs4_createdata *data;
4268 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004270 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4271 if (data == NULL)
4272 goto out;
4273
David Quigley1775fd32013-05-22 12:50:42 -04004274 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 status = nfs4_do_create(dir, dentry, data);
4276
4277 nfs4_free_createdata(data);
4278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return status;
4280}
4281
4282static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4283 struct iattr *sattr)
4284{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004285 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004287 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004289
David Quigleyaa9c2662013-05-22 12:50:44 -04004290 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4291
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004292 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4293 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004295 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4296 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4297 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 &exception);
4299 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004300 nfs4_label_release_security(label);
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return err;
4303}
4304
4305static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004306 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
David Howells2b0143b2015-03-17 22:25:59 +00004308 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 struct nfs4_readdir_arg args = {
4310 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004311 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 .pgbase = 0,
4313 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004314 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004315 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 };
4317 struct nfs4_readdir_res res;
4318 struct rpc_message msg = {
4319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4320 .rpc_argp = &args,
4321 .rpc_resp = &res,
4322 .rpc_cred = cred,
4323 };
4324 int status;
4325
Al Viro6de14722013-09-16 10:53:17 -04004326 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4327 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004328 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004329 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004331 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 -05004332 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004333 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004334 status += args.pgbase;
4335 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004336
4337 nfs_invalidate_atime(dir);
4338
Harvey Harrison3110ff82008-05-02 13:42:44 -07004339 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return status;
4341}
4342
4343static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004344 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 struct nfs4_exception exception = { };
4347 int err;
4348 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004349 err = _nfs4_proc_readdir(dentry, cred, cookie,
4350 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004351 trace_nfs4_readdir(d_inode(dentry), err);
4352 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 &exception);
4354 } while (exception.retry);
4355 return err;
4356}
4357
4358static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004359 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004361 struct nfs4_createdata *data;
4362 int mode = sattr->ia_mode;
4363 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004365 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4366 if (data == NULL)
4367 goto out;
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004370 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004372 data->arg.ftype = NF4BLK;
4373 data->arg.u.device.specdata1 = MAJOR(rdev);
4374 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 }
4376 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004377 data->arg.ftype = NF4CHR;
4378 data->arg.u.device.specdata1 = MAJOR(rdev);
4379 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004380 } else if (!S_ISSOCK(mode)) {
4381 status = -EINVAL;
4382 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 }
David Quigley1775fd32013-05-22 12:50:42 -04004384
David Quigleyaa9c2662013-05-22 12:50:44 -04004385 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004387out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004388 nfs4_free_createdata(data);
4389out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return status;
4391}
4392
4393static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4394 struct iattr *sattr, dev_t rdev)
4395{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004396 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004398 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004400
David Quigleyaa9c2662013-05-22 12:50:44 -04004401 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4402
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004403 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4404 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004406 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4407 trace_nfs4_mknod(dir, &dentry->d_name, err);
4408 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 &exception);
4410 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004411
4412 nfs4_label_release_security(label);
4413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return err;
4415}
4416
4417static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4418 struct nfs_fsstat *fsstat)
4419{
4420 struct nfs4_statfs_arg args = {
4421 .fh = fhandle,
4422 .bitmask = server->attr_bitmask,
4423 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004424 struct nfs4_statfs_res res = {
4425 .fsstat = fsstat,
4426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 struct rpc_message msg = {
4428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4429 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004430 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 };
4432
Trond Myklebust0e574af2005-10-27 22:12:38 -04004433 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004434 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
4437static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4438{
4439 struct nfs4_exception exception = { };
4440 int err;
4441 do {
4442 err = nfs4_handle_exception(server,
4443 _nfs4_proc_statfs(server, fhandle, fsstat),
4444 &exception);
4445 } while (exception.retry);
4446 return err;
4447}
4448
4449static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4450 struct nfs_fsinfo *fsinfo)
4451{
4452 struct nfs4_fsinfo_arg args = {
4453 .fh = fhandle,
4454 .bitmask = server->attr_bitmask,
4455 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004456 struct nfs4_fsinfo_res res = {
4457 .fsinfo = fsinfo,
4458 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 struct rpc_message msg = {
4460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4461 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004462 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 };
4464
Bryan Schumaker7c513052011-03-24 17:12:24 +00004465 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
4468static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4469{
4470 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004471 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 int err;
4473
4474 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004475 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004476 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004477 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004478 nfs4_set_lease_period(server->nfs_client,
4479 fsinfo->lease_time * HZ,
4480 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004481 break;
4482 }
4483 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 } while (exception.retry);
4485 return err;
4486}
4487
4488static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4489{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004490 int error;
4491
Trond Myklebust0e574af2005-10-27 22:12:38 -04004492 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004493 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004494 if (error == 0) {
4495 /* block layout checks this! */
4496 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004497 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004498 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004499
4500 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
4503static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4504 struct nfs_pathconf *pathconf)
4505{
4506 struct nfs4_pathconf_arg args = {
4507 .fh = fhandle,
4508 .bitmask = server->attr_bitmask,
4509 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004510 struct nfs4_pathconf_res res = {
4511 .pathconf = pathconf,
4512 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 struct rpc_message msg = {
4514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4515 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004516 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 };
4518
4519 /* None of the pathconf attributes are mandatory to implement */
4520 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4521 memset(pathconf, 0, sizeof(*pathconf));
4522 return 0;
4523 }
4524
Trond Myklebust0e574af2005-10-27 22:12:38 -04004525 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004526 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
4529static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4530 struct nfs_pathconf *pathconf)
4531{
4532 struct nfs4_exception exception = { };
4533 int err;
4534
4535 do {
4536 err = nfs4_handle_exception(server,
4537 _nfs4_proc_pathconf(server, fhandle, pathconf),
4538 &exception);
4539 } while (exception.retry);
4540 return err;
4541}
4542
Trond Myklebust5521abf2013-03-16 20:54:34 -04004543int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004544 const struct nfs_open_context *ctx,
4545 const struct nfs_lock_context *l_ctx,
4546 fmode_t fmode)
4547{
NeilBrown17393472016-10-13 15:26:47 +11004548 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004549}
4550EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4551
Trond Myklebust5521abf2013-03-16 20:54:34 -04004552static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4553 const struct nfs_open_context *ctx,
4554 const struct nfs_lock_context *l_ctx,
4555 fmode_t fmode)
4556{
4557 nfs4_stateid current_stateid;
4558
Trond Myklebuste1253be2014-03-05 08:44:23 -05004559 /* If the current stateid represents a lost lock, then exit */
4560 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4561 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004562 return nfs4_stateid_match(stateid, &current_stateid);
4563}
4564
4565static bool nfs4_error_stateid_expired(int err)
4566{
4567 switch (err) {
4568 case -NFS4ERR_DELEG_REVOKED:
4569 case -NFS4ERR_ADMIN_REVOKED:
4570 case -NFS4ERR_BAD_STATEID:
4571 case -NFS4ERR_STALE_STATEID:
4572 case -NFS4ERR_OLD_STATEID:
4573 case -NFS4ERR_OPENMODE:
4574 case -NFS4ERR_EXPIRED:
4575 return true;
4576 }
4577 return false;
4578}
4579
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004580static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004582 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004584 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004585 if (task->tk_status < 0) {
4586 struct nfs4_exception exception = {
4587 .inode = hdr->inode,
4588 .state = hdr->args.context->state,
4589 .stateid = &hdr->args.stateid,
4590 };
4591 task->tk_status = nfs4_async_handle_exception(task,
4592 server, task->tk_status, &exception);
4593 if (exception.retry) {
4594 rpc_restart_call_prepare(task);
4595 return -EAGAIN;
4596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004600 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
4603
Trond Myklebust5521abf2013-03-16 20:54:34 -04004604static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004605 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004606{
4607
4608 if (!nfs4_error_stateid_expired(task->tk_status) ||
4609 nfs4_stateid_is_current(&args->stateid,
4610 args->context,
4611 args->lock_context,
4612 FMODE_READ))
4613 return false;
4614 rpc_restart_call_prepare(task);
4615 return true;
4616}
4617
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004618static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004619{
4620
4621 dprintk("--> %s\n", __func__);
4622
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004624 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004626 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004627 if (task->tk_status > 0)
4628 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004629 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4630 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004631}
4632
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004633static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4634 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004636 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004637 if (!hdr->pgio_done_cb)
4638 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004639 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4644 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004645{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004646 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004647 &hdr->args.seq_args,
4648 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004649 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004650 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004651 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4652 hdr->args.lock_context,
4653 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004654 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004655 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004656 return -EIO;
4657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660static int nfs4_write_done_cb(struct rpc_task *task,
4661 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004664
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004666 if (task->tk_status < 0) {
4667 struct nfs4_exception exception = {
4668 .inode = hdr->inode,
4669 .state = hdr->args.context->state,
4670 .stateid = &hdr->args.stateid,
4671 };
4672 task->tk_status = nfs4_async_handle_exception(task,
4673 NFS_SERVER(inode), task->tk_status,
4674 &exception);
4675 if (exception.retry) {
4676 rpc_restart_call_prepare(task);
4677 return -EAGAIN;
4678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004680 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004682 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004683 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
Trond Myklebust5521abf2013-03-16 20:54:34 -04004687static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004688 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004689{
4690
4691 if (!nfs4_error_stateid_expired(task->tk_status) ||
4692 nfs4_stateid_is_current(&args->stateid,
4693 args->context,
4694 args->lock_context,
4695 FMODE_WRITE))
4696 return false;
4697 rpc_restart_call_prepare(task);
4698 return true;
4699}
4700
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004702{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004704 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004706 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4708 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004709}
4710
Trond Myklebust5a37f852012-04-28 14:55:16 -04004711static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004713{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004714 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004716 return false;
4717 /* Otherwise, request attributes if and only if we don't hold
4718 * a delegation
4719 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004720 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004721}
Fred Isamana69aef12011-03-03 15:13:47 +00004722
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004723static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4724 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004727
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4729 hdr->args.bitmask = NULL;
4730 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004731 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004732 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004733
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734 if (!hdr->pgio_done_cb)
4735 hdr->pgio_done_cb = nfs4_write_done_cb;
4736 hdr->res.server = server;
4737 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004739 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
Fred Isaman0b7c0152012-04-20 14:47:39 -04004743static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4744{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004745 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004746 &data->args.seq_args,
4747 &data->res.seq_res,
4748 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
Fred Isaman0b7c0152012-04-20 14:47:39 -04004751static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004754
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004755 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004756 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4757 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004758 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004759 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Fred Isaman0b7c0152012-04-20 14:47:39 -04004764static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004765{
4766 if (!nfs4_sequence_done(task, &data->res.seq_res))
4767 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004768 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004769}
4770
Fred Isaman0b7c0152012-04-20 14:47:39 -04004771static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004773 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004774
Fred Isaman0b7c0152012-04-20 14:47:39 -04004775 if (data->commit_done_cb == NULL)
4776 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004777 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004778 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004779 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004782struct nfs4_renewdata {
4783 struct nfs_client *client;
4784 unsigned long timestamp;
4785};
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787/*
4788 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4789 * standalone procedure for queueing an asynchronous RENEW.
4790 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004791static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004792{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004793 struct nfs4_renewdata *data = calldata;
4794 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004795
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004796 if (atomic_read(&clp->cl_count) > 1)
4797 nfs4_schedule_state_renewal(clp);
4798 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004799 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004800}
4801
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004802static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004804 struct nfs4_renewdata *data = calldata;
4805 struct nfs_client *clp = data->client;
4806 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004808 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004809 switch (task->tk_status) {
4810 case 0:
4811 break;
4812 case -NFS4ERR_LEASE_MOVED:
4813 nfs4_schedule_lease_moved_recovery(clp);
4814 break;
4815 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004816 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004817 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4818 return;
4819 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004820 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004821 return;
4822 }
4823 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004825 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004828static const struct rpc_call_ops nfs4_renew_ops = {
4829 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004830 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004831};
4832
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004833static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
4835 struct rpc_message msg = {
4836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4837 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004838 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004840 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004842 if (renew_flags == 0)
4843 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004844 if (!atomic_inc_not_zero(&clp->cl_count))
4845 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004846 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004847 if (data == NULL)
4848 return -ENOMEM;
4849 data->client = clp;
4850 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004851 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004852 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004855static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
4857 struct rpc_message msg = {
4858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4859 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004860 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 };
4862 unsigned long now = jiffies;
4863 int status;
4864
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004865 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 if (status < 0)
4867 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004868 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 return 0;
4870}
4871
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004872static inline int nfs4_server_supports_acls(struct nfs_server *server)
4873{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004874 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004875}
4876
Trond Myklebust21f498c2012-08-24 10:59:25 -04004877/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4878 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004879 * the stack.
4880 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004881#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004882
Neil Hormane9e3d722011-03-04 19:26:03 -05004883static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004884 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004885{
4886 struct page *newpage, **spages;
4887 int rc = 0;
4888 size_t len;
4889 spages = pages;
4890
4891 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004892 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004893 newpage = alloc_page(GFP_KERNEL);
4894
4895 if (newpage == NULL)
4896 goto unwind;
4897 memcpy(page_address(newpage), buf, len);
4898 buf += len;
4899 buflen -= len;
4900 *pages++ = newpage;
4901 rc++;
4902 } while (buflen != 0);
4903
4904 return rc;
4905
4906unwind:
4907 for(; rc > 0; rc--)
4908 __free_page(spages[rc-1]);
4909 return -ENOMEM;
4910}
4911
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004912struct nfs4_cached_acl {
4913 int cached;
4914 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004915 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004916};
4917
4918static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004919{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004920 struct nfs_inode *nfsi = NFS_I(inode);
4921
4922 spin_lock(&inode->i_lock);
4923 kfree(nfsi->nfs4_acl);
4924 nfsi->nfs4_acl = acl;
4925 spin_unlock(&inode->i_lock);
4926}
4927
4928static void nfs4_zap_acl_attr(struct inode *inode)
4929{
4930 nfs4_set_cached_acl(inode, NULL);
4931}
4932
4933static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4934{
4935 struct nfs_inode *nfsi = NFS_I(inode);
4936 struct nfs4_cached_acl *acl;
4937 int ret = -ENOENT;
4938
4939 spin_lock(&inode->i_lock);
4940 acl = nfsi->nfs4_acl;
4941 if (acl == NULL)
4942 goto out;
4943 if (buf == NULL) /* user is just asking for length */
4944 goto out_len;
4945 if (acl->cached == 0)
4946 goto out;
4947 ret = -ERANGE; /* see getxattr(2) man page */
4948 if (acl->len > buflen)
4949 goto out;
4950 memcpy(buf, acl->data, acl->len);
4951out_len:
4952 ret = acl->len;
4953out:
4954 spin_unlock(&inode->i_lock);
4955 return ret;
4956}
4957
Sachin Prabhu5794d212012-04-17 14:36:40 +01004958static 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 +00004959{
4960 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004961 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004962
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004963 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004964 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004965 if (acl == NULL)
4966 goto out;
4967 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004968 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004969 } else {
4970 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4971 if (acl == NULL)
4972 goto out;
4973 acl->cached = 0;
4974 }
4975 acl->len = acl_len;
4976out:
4977 nfs4_set_cached_acl(inode, acl);
4978}
4979
Andy Adamsonbf118a32011-12-07 11:55:27 -05004980/*
4981 * The getxattr API returns the required buffer length when called with a
4982 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4983 * the required buf. On a NULL buf, we send a page of data to the server
4984 * guessing that the ACL request can be serviced by a page. If so, we cache
4985 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4986 * the cache. If not so, we throw away the page, and cache the required
4987 * length. The next getxattr call will then produce another round trip to
4988 * the server, this time with the input buf of the required size.
4989 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004990static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004991{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004992 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004993 struct nfs_getaclargs args = {
4994 .fh = NFS_FH(inode),
4995 .acl_pages = pages,
4996 .acl_len = buflen,
4997 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004998 struct nfs_getaclres res = {
4999 .acl_len = buflen,
5000 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005001 struct rpc_message msg = {
5002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5003 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005004 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005005 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005006 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5007 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005008
Andy Adamsonbf118a32011-12-07 11:55:27 -05005009 /* As long as we're doing a round trip to the server anyway,
5010 * let's be prepared for a page of acl data. */
5011 if (npages == 0)
5012 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005013 if (npages > ARRAY_SIZE(pages))
5014 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005015
Andy Adamsonbf118a32011-12-07 11:55:27 -05005016 for (i = 0; i < npages; i++) {
5017 pages[i] = alloc_page(GFP_KERNEL);
5018 if (!pages[i])
5019 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005020 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005021
5022 /* for decoding across pages */
5023 res.acl_scratch = alloc_page(GFP_KERNEL);
5024 if (!res.acl_scratch)
5025 goto out_free;
5026
Andy Adamsonbf118a32011-12-07 11:55:27 -05005027 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005028
Peng Taode040be2012-01-10 22:42:47 +08005029 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005030 __func__, buf, buflen, npages, args.acl_len);
5031 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5032 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005033 if (ret)
5034 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005035
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005036 /* Handle the case where the passed-in buffer is too short */
5037 if (res.acl_flags & NFS4_ACL_TRUNC) {
5038 /* Did the user only issue a request for the acl length? */
5039 if (buf == NULL)
5040 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005041 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005042 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005043 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005044 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005045 if (buf) {
5046 if (res.acl_len > buflen) {
5047 ret = -ERANGE;
5048 goto out_free;
5049 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005050 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005051 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005052out_ok:
5053 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005055 for (i = 0; i < npages; i++)
5056 if (pages[i])
5057 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005058 if (res.acl_scratch)
5059 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005060 return ret;
5061}
5062
Trond Myklebust16b42892006-08-24 12:27:15 -04005063static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5064{
5065 struct nfs4_exception exception = { };
5066 ssize_t ret;
5067 do {
5068 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005069 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005070 if (ret >= 0)
5071 break;
5072 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5073 } while (exception.retry);
5074 return ret;
5075}
5076
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005077static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5078{
5079 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005080 int ret;
5081
5082 if (!nfs4_server_supports_acls(server))
5083 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005084 ret = nfs_revalidate_inode(server, inode);
5085 if (ret < 0)
5086 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005087 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5088 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089 ret = nfs4_read_cached_acl(inode, buf, buflen);
5090 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005091 /* -ENOENT is returned if there is no ACL or if there is an ACL
5092 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005093 return ret;
5094 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005095}
5096
Trond Myklebust16b42892006-08-24 12:27:15 -04005097static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005098{
5099 struct nfs_server *server = NFS_SERVER(inode);
5100 struct page *pages[NFS4ACL_MAXPAGES];
5101 struct nfs_setaclargs arg = {
5102 .fh = NFS_FH(inode),
5103 .acl_pages = pages,
5104 .acl_len = buflen,
5105 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005106 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005107 struct rpc_message msg = {
5108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5109 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005110 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005111 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005112 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005113 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005114
5115 if (!nfs4_server_supports_acls(server))
5116 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005117 if (npages > ARRAY_SIZE(pages))
5118 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005119 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005120 if (i < 0)
5121 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005122 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005123 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005124
5125 /*
5126 * Free each page after tx, so the only ref left is
5127 * held by the network stack
5128 */
5129 for (; i > 0; i--)
5130 put_page(pages[i-1]);
5131
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005132 /*
5133 * Acl update can result in inode attribute update.
5134 * so mark the attribute cache invalid.
5135 */
5136 spin_lock(&inode->i_lock);
5137 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5138 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005139 nfs_access_zap_cache(inode);
5140 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005141 return ret;
5142}
5143
Trond Myklebust16b42892006-08-24 12:27:15 -04005144static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5145{
5146 struct nfs4_exception exception = { };
5147 int err;
5148 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005149 err = __nfs4_proc_set_acl(inode, buf, buflen);
5150 trace_nfs4_set_acl(inode, err);
5151 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005152 &exception);
5153 } while (exception.retry);
5154 return err;
5155}
5156
David Quigleyaa9c2662013-05-22 12:50:44 -04005157#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5158static int _nfs4_get_security_label(struct inode *inode, void *buf,
5159 size_t buflen)
5160{
5161 struct nfs_server *server = NFS_SERVER(inode);
5162 struct nfs_fattr fattr;
5163 struct nfs4_label label = {0, 0, buflen, buf};
5164
5165 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005166 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005167 .fh = NFS_FH(inode),
5168 .bitmask = bitmask,
5169 };
5170 struct nfs4_getattr_res res = {
5171 .fattr = &fattr,
5172 .label = &label,
5173 .server = server,
5174 };
5175 struct rpc_message msg = {
5176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005177 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005178 .rpc_resp = &res,
5179 };
5180 int ret;
5181
5182 nfs_fattr_init(&fattr);
5183
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005184 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005185 if (ret)
5186 return ret;
5187 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5188 return -ENOENT;
5189 if (buflen < label.len)
5190 return -ERANGE;
5191 return 0;
5192}
5193
5194static int nfs4_get_security_label(struct inode *inode, void *buf,
5195 size_t buflen)
5196{
5197 struct nfs4_exception exception = { };
5198 int err;
5199
5200 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5201 return -EOPNOTSUPP;
5202
5203 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005204 err = _nfs4_get_security_label(inode, buf, buflen);
5205 trace_nfs4_get_security_label(inode, err);
5206 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005207 &exception);
5208 } while (exception.retry);
5209 return err;
5210}
5211
5212static int _nfs4_do_set_security_label(struct inode *inode,
5213 struct nfs4_label *ilabel,
5214 struct nfs_fattr *fattr,
5215 struct nfs4_label *olabel)
5216{
5217
5218 struct iattr sattr = {0};
5219 struct nfs_server *server = NFS_SERVER(inode);
5220 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005221 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005222 .fh = NFS_FH(inode),
5223 .iap = &sattr,
5224 .server = server,
5225 .bitmask = bitmask,
5226 .label = ilabel,
5227 };
5228 struct nfs_setattrres res = {
5229 .fattr = fattr,
5230 .label = olabel,
5231 .server = server,
5232 };
5233 struct rpc_message msg = {
5234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005235 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005236 .rpc_resp = &res,
5237 };
5238 int status;
5239
Jeff Layton12207f62013-11-01 10:49:32 -04005240 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005241
Jeff Layton12207f62013-11-01 10:49:32 -04005242 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005243 if (status)
5244 dprintk("%s failed: %d\n", __func__, status);
5245
5246 return status;
5247}
5248
5249static int nfs4_do_set_security_label(struct inode *inode,
5250 struct nfs4_label *ilabel,
5251 struct nfs_fattr *fattr,
5252 struct nfs4_label *olabel)
5253{
5254 struct nfs4_exception exception = { };
5255 int err;
5256
5257 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005258 err = _nfs4_do_set_security_label(inode, ilabel,
5259 fattr, olabel);
5260 trace_nfs4_set_security_label(inode, err);
5261 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005262 &exception);
5263 } while (exception.retry);
5264 return err;
5265}
5266
5267static int
Al Viro59301222016-05-27 10:19:30 -04005268nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005269{
5270 struct nfs4_label ilabel, *olabel = NULL;
5271 struct nfs_fattr fattr;
5272 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005273 int status;
5274
5275 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5276 return -EOPNOTSUPP;
5277
5278 nfs_fattr_init(&fattr);
5279
5280 ilabel.pi = 0;
5281 ilabel.lfs = 0;
5282 ilabel.label = (char *)buf;
5283 ilabel.len = buflen;
5284
5285 cred = rpc_lookup_cred();
5286 if (IS_ERR(cred))
5287 return PTR_ERR(cred);
5288
5289 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5290 if (IS_ERR(olabel)) {
5291 status = -PTR_ERR(olabel);
5292 goto out;
5293 }
5294
5295 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5296 if (status == 0)
5297 nfs_setsecurity(inode, &fattr, olabel);
5298
5299 nfs4_label_free(olabel);
5300out:
5301 put_rpccred(cred);
5302 return status;
5303}
5304#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5305
5306
Chuck Leverf0920752012-05-21 22:45:41 -04005307static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5308 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005309{
5310 __be32 verf[2];
5311
Chuck Lever2c820d92012-05-21 22:45:33 -04005312 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5313 /* An impossible timestamp guarantees this value
5314 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005315 verf[0] = cpu_to_be32(U32_MAX);
5316 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005317 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005318 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005319 u64 ns = ktime_to_ns(nn->boot_time);
5320
5321 verf[0] = cpu_to_be32(ns >> 32);
5322 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005323 }
Chuck Levercd937102012-03-02 17:14:31 -05005324 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5325}
5326
Jeff Laytona3192682015-06-09 19:43:59 -04005327static int
5328nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005329{
Jeff Laytona3192682015-06-09 19:43:59 -04005330 size_t len;
5331 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005332
Trond Myklebustceb3a162015-01-03 15:16:04 -05005333 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005334 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005335
Jeff Laytona3192682015-06-09 19:43:59 -04005336 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005337 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005338 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5339 1 +
5340 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5341 1;
5342 rcu_read_unlock();
5343
5344 if (len > NFS4_OPAQUE_LIMIT + 1)
5345 return -EINVAL;
5346
5347 /*
5348 * Since this string is allocated at mount time, and held until the
5349 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5350 * about a memory-reclaim deadlock.
5351 */
5352 str = kmalloc(len, GFP_KERNEL);
5353 if (!str)
5354 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005355
Chuck Levere984a552012-09-14 17:24:21 -04005356 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005357 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005358 clp->cl_ipaddr,
5359 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5360 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005361 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005362
Jeff Laytona3192682015-06-09 19:43:59 -04005363 clp->cl_owner_id = str;
5364 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005365}
5366
Jeff Layton873e3852015-06-09 19:44:00 -04005367static int
5368nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005369{
Jeff Layton873e3852015-06-09 19:44:00 -04005370 size_t len;
5371 char *str;
5372
5373 len = 10 + 10 + 1 + 10 + 1 +
5374 strlen(nfs4_client_id_uniquifier) + 1 +
5375 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5376
5377 if (len > NFS4_OPAQUE_LIMIT + 1)
5378 return -EINVAL;
5379
5380 /*
5381 * Since this string is allocated at mount time, and held until the
5382 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5383 * about a memory-reclaim deadlock.
5384 */
5385 str = kmalloc(len, GFP_KERNEL);
5386 if (!str)
5387 return -ENOMEM;
5388
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005389 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005390 clp->rpc_ops->version, clp->cl_minorversion,
5391 nfs4_client_id_uniquifier,
5392 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005393 clp->cl_owner_id = str;
5394 return 0;
5395}
5396
5397static int
5398nfs4_init_uniform_client_string(struct nfs_client *clp)
5399{
Jeff Layton873e3852015-06-09 19:44:00 -04005400 size_t len;
5401 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005402
5403 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005404 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005405
5406 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005407 return nfs4_init_uniquifier_client_string(clp);
5408
5409 len = 10 + 10 + 1 + 10 + 1 +
5410 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5411
5412 if (len > NFS4_OPAQUE_LIMIT + 1)
5413 return -EINVAL;
5414
5415 /*
5416 * Since this string is allocated at mount time, and held until the
5417 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5418 * about a memory-reclaim deadlock.
5419 */
5420 str = kmalloc(len, GFP_KERNEL);
5421 if (!str)
5422 return -ENOMEM;
5423
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005424 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005425 clp->rpc_ops->version, clp->cl_minorversion,
5426 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005427 clp->cl_owner_id = str;
5428 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005429}
5430
Chuck Lever706cb8d2014-03-12 12:51:47 -04005431/*
5432 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5433 * services. Advertise one based on the address family of the
5434 * clientaddr.
5435 */
5436static unsigned int
5437nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5438{
5439 if (strchr(clp->cl_ipaddr, ':') != NULL)
5440 return scnprintf(buf, len, "tcp6");
5441 else
5442 return scnprintf(buf, len, "tcp");
5443}
5444
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005445static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5446{
5447 struct nfs4_setclientid *sc = calldata;
5448
5449 if (task->tk_status == 0)
5450 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5451}
5452
5453static const struct rpc_call_ops nfs4_setclientid_ops = {
5454 .rpc_call_done = nfs4_setclientid_done,
5455};
5456
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005457/**
5458 * nfs4_proc_setclientid - Negotiate client ID
5459 * @clp: state data structure
5460 * @program: RPC program for NFSv4 callback service
5461 * @port: IP port number for NFS4 callback service
5462 * @cred: RPC credential to use for this call
5463 * @res: where to place the result
5464 *
5465 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5466 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005467int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5468 unsigned short port, struct rpc_cred *cred,
5469 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470{
5471 nfs4_verifier sc_verifier;
5472 struct nfs4_setclientid setclientid = {
5473 .sc_verifier = &sc_verifier,
5474 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005475 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 };
5477 struct rpc_message msg = {
5478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5479 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005480 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005481 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005483 struct rpc_task *task;
5484 struct rpc_task_setup task_setup_data = {
5485 .rpc_client = clp->cl_rpcclient,
5486 .rpc_message = &msg,
5487 .callback_ops = &nfs4_setclientid_ops,
5488 .callback_data = &setclientid,
5489 .flags = RPC_TASK_TIMEOUT,
5490 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005491 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Chuck Leverde734832012-07-11 16:30:50 -04005493 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005494 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005495
5496 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5497 status = nfs4_init_uniform_client_string(clp);
5498 else
Jeff Laytona3192682015-06-09 19:43:59 -04005499 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005500
5501 if (status)
5502 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005503
Chuck Leverde734832012-07-11 16:30:50 -04005504 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005505 setclientid.sc_netid_len =
5506 nfs4_init_callback_netid(clp,
5507 setclientid.sc_netid,
5508 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005509 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005510 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 clp->cl_ipaddr, port >> 8, port & 255);
5512
Jeff Layton3a6bb732015-06-09 19:43:57 -04005513 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005514 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005515 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005516 task = rpc_run_task(&task_setup_data);
5517 if (IS_ERR(task)) {
5518 status = PTR_ERR(task);
5519 goto out;
5520 }
5521 status = task->tk_status;
5522 if (setclientid.sc_cred) {
5523 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5524 put_rpccred(setclientid.sc_cred);
5525 }
5526 rpc_put_task(task);
5527out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005528 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005529 dprintk("NFS reply setclientid: %d\n", status);
5530 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005533/**
5534 * nfs4_proc_setclientid_confirm - Confirm client ID
5535 * @clp: state data structure
5536 * @res: result of a previous SETCLIENTID
5537 * @cred: RPC credential to use for this call
5538 *
5539 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5540 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005541int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005542 struct nfs4_setclientid_res *arg,
5543 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 struct rpc_message msg = {
5546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005547 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005548 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 int status;
5551
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005552 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5553 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5554 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005555 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005556 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005557 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 return status;
5559}
5560
Trond Myklebustfe650402006-01-03 09:55:18 +01005561struct nfs4_delegreturndata {
5562 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005563 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005564 struct nfs_fh fh;
5565 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005566 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005567 struct {
5568 struct nfs4_layoutreturn_args arg;
5569 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005570 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005571 u32 roc_barrier;
5572 bool roc;
5573 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005574 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005575 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005576 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005577};
5578
Trond Myklebustfe650402006-01-03 09:55:18 +01005579static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5580{
5581 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005582
Trond Myklebust14516c32010-07-31 14:29:06 -04005583 if (!nfs4_sequence_done(task, &data->res.seq_res))
5584 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005585
Trond Myklebustca8acf82013-08-13 10:36:56 -04005586 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005587
5588 /* Handle Layoutreturn errors */
5589 if (data->args.lr_args && task->tk_status != 0) {
5590 switch(data->res.lr_ret) {
5591 default:
5592 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5593 break;
5594 case 0:
5595 data->args.lr_args = NULL;
5596 data->res.lr_res = NULL;
5597 break;
5598 case -NFS4ERR_ADMIN_REVOKED:
5599 case -NFS4ERR_DELEG_REVOKED:
5600 case -NFS4ERR_EXPIRED:
5601 case -NFS4ERR_BAD_STATEID:
5602 case -NFS4ERR_OLD_STATEID:
5603 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5604 case -NFS4ERR_WRONG_CRED:
5605 data->args.lr_args = NULL;
5606 data->res.lr_res = NULL;
5607 data->res.lr_ret = 0;
5608 rpc_restart_call_prepare(task);
5609 return;
5610 }
5611 }
5612
Ricardo Labiaga79708862009-12-07 09:23:21 -05005613 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005614 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005615 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005616 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005617 case -NFS4ERR_ADMIN_REVOKED:
5618 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005619 case -NFS4ERR_EXPIRED:
5620 nfs4_free_revoked_stateid(data->res.server,
5621 data->args.stateid,
5622 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005623 case -NFS4ERR_BAD_STATEID:
5624 case -NFS4ERR_OLD_STATEID:
5625 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005626 task->tk_status = 0;
5627 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005628 case -NFS4ERR_ACCESS:
5629 if (data->args.bitmask) {
5630 data->args.bitmask = NULL;
5631 data->res.fattr = NULL;
5632 task->tk_status = 0;
5633 rpc_restart_call_prepare(task);
5634 return;
5635 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005636 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005637 if (nfs4_async_handle_error(task, data->res.server,
5638 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005639 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005640 return;
5641 }
5642 }
5643 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005644}
5645
5646static void nfs4_delegreturn_release(void *calldata)
5647{
Peng Tao039b7562014-07-03 13:05:02 +08005648 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005649 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005650
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005651 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005652 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005653 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5654 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005655 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005656 nfs_iput_and_deactive(inode);
5657 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005658 kfree(calldata);
5659}
5660
Andy Adamson938e1012009-04-01 09:22:28 -04005661static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5662{
5663 struct nfs4_delegreturndata *d_data;
5664
5665 d_data = (struct nfs4_delegreturndata *)data;
5666
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005667 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005668 return;
5669
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005670 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005671 &d_data->args.seq_args,
5672 &d_data->res.seq_res,
5673 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005674}
Andy Adamson938e1012009-04-01 09:22:28 -04005675
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005676static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005677 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005678 .rpc_call_done = nfs4_delegreturn_done,
5679 .rpc_release = nfs4_delegreturn_release,
5680};
5681
Trond Myklebuste6f81072008-01-24 18:14:34 -05005682static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005683{
5684 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005685 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005686 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005687 struct rpc_message msg = {
5688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5689 .rpc_cred = cred,
5690 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005691 struct rpc_task_setup task_setup_data = {
5692 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005693 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005694 .callback_ops = &nfs4_delegreturn_ops,
5695 .flags = RPC_TASK_ASYNC,
5696 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005697 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005698
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005699 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005700 if (data == NULL)
5701 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005702 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005703
5704 nfs4_state_protect(server->nfs_client,
5705 NFS_SP4_MACH_CRED_CLEANUP,
5706 &task_setup_data.rpc_client, &msg);
5707
Trond Myklebustfe650402006-01-03 09:55:18 +01005708 data->args.fhandle = &data->fh;
5709 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005710 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005711 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005712 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005713 data->res.fattr = &data->fattr;
5714 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005715 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005716 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005717 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005718 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005719 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005720 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005721 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005722 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005723 if (data->lr.roc) {
5724 data->args.lr_args = &data->lr.arg;
5725 data->res.lr_res = &data->lr.res;
5726 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005727 } else if (data->lr.roc) {
5728 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5729 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005730 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005731
Trond Myklebustc970aa82007-07-14 15:39:59 -04005732 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005733 msg.rpc_argp = &data->args;
5734 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005735 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005736 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005737 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005738 if (!issync)
5739 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005740 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005741 if (status != 0)
5742 goto out;
5743 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005744out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005745 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005746 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747}
5748
Trond Myklebuste6f81072008-01-24 18:14:34 -05005749int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750{
5751 struct nfs_server *server = NFS_SERVER(inode);
5752 struct nfs4_exception exception = { };
5753 int err;
5754 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005755 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005756 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 switch (err) {
5758 case -NFS4ERR_STALE_STATEID:
5759 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 case 0:
5761 return 0;
5762 }
5763 err = nfs4_handle_exception(server, err, &exception);
5764 } while (exception.retry);
5765 return err;
5766}
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5769{
5770 struct inode *inode = state->inode;
5771 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005772 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005773 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005775 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005777 struct nfs_lockt_res res = {
5778 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 };
5780 struct rpc_message msg = {
5781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5782 .rpc_argp = &arg,
5783 .rpc_resp = &res,
5784 .rpc_cred = state->owner->so_cred,
5785 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 struct nfs4_lock_state *lsp;
5787 int status;
5788
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005789 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005790 status = nfs4_set_lock_state(state, request);
5791 if (status != 0)
5792 goto out;
5793 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005794 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005795 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005796 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005797 switch (status) {
5798 case 0:
5799 request->fl_type = F_UNLCK;
5800 break;
5801 case -NFS4ERR_DENIED:
5802 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005804 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005805 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005806out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 return status;
5808}
5809
5810static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5811{
5812 struct nfs4_exception exception = { };
5813 int err;
5814
5815 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005816 err = _nfs4_proc_getlk(state, cmd, request);
5817 trace_nfs4_get_lock(request, state, cmd, err);
5818 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 &exception);
5820 } while (exception.retry);
5821 return err;
5822}
5823
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005824struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005825 struct nfs_locku_args arg;
5826 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005827 struct nfs4_lock_state *lsp;
5828 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005829 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005830 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005831 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005832};
5833
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005834static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5835 struct nfs_open_context *ctx,
5836 struct nfs4_lock_state *lsp,
5837 struct nfs_seqid *seqid)
5838{
5839 struct nfs4_unlockdata *p;
5840 struct inode *inode = lsp->ls_state->inode;
5841
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005842 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005843 if (p == NULL)
5844 return NULL;
5845 p->arg.fh = NFS_FH(inode);
5846 p->arg.fl = &p->fl;
5847 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005848 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005849 p->lsp = lsp;
5850 atomic_inc(&lsp->ls_count);
5851 /* Ensure we don't close file until we're done freeing locks! */
5852 p->ctx = get_nfs_open_context(ctx);
5853 memcpy(&p->fl, fl, sizeof(p->fl));
5854 p->server = NFS_SERVER(inode);
5855 return p;
5856}
5857
Trond Myklebust06f814a2006-01-03 09:55:07 +01005858static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005859{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005860 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005861 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005862 nfs4_put_lock_state(calldata->lsp);
5863 put_nfs_open_context(calldata->ctx);
5864 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005865}
5866
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005867static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005868{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005869 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005870
Trond Myklebust14516c32010-07-31 14:29:06 -04005871 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5872 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005873 switch (task->tk_status) {
5874 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005875 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005876 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005877 if (nfs4_update_lock_stateid(calldata->lsp,
5878 &calldata->res.stateid))
5879 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005880 case -NFS4ERR_ADMIN_REVOKED:
5881 case -NFS4ERR_EXPIRED:
5882 nfs4_free_revoked_stateid(calldata->server,
5883 &calldata->arg.stateid,
5884 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005885 case -NFS4ERR_BAD_STATEID:
5886 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005887 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005888 if (!nfs4_stateid_match(&calldata->arg.stateid,
5889 &calldata->lsp->ls_stateid))
5890 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005891 break;
5892 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005893 if (nfs4_async_handle_error(task, calldata->server,
5894 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005895 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005896 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005897 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005898}
5899
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005900static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005901{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005902 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005904 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005905 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005906 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005907 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005908 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005909 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005910 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005911 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005912 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005913 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005914 &calldata->res.seq_res,
5915 task) != 0)
5916 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005917 return;
5918out_no_action:
5919 task->tk_action = NULL;
5920out_wait:
5921 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922}
5923
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005924static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005925 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005926 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005927 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005928};
5929
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5931 struct nfs_open_context *ctx,
5932 struct nfs4_lock_state *lsp,
5933 struct nfs_seqid *seqid)
5934{
5935 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005936 struct rpc_message msg = {
5937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5938 .rpc_cred = ctx->cred,
5939 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 struct rpc_task_setup task_setup_data = {
5941 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005942 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005943 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005944 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005945 .flags = RPC_TASK_ASYNC,
5946 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005947
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005948 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5949 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5950
Frank Filz137d6ac2007-07-09 15:32:29 -07005951 /* Ensure this is an unlock - when canceling a lock, the
5952 * canceled lock is passed in, and it won't be an unlock.
5953 */
5954 fl->fl_type = F_UNLCK;
5955
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005956 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5957 if (data == NULL) {
5958 nfs_free_seqid(seqid);
5959 return ERR_PTR(-ENOMEM);
5960 }
5961
Chuck Levera9c92d62013-08-09 12:48:18 -04005962 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005963 msg.rpc_argp = &data->arg;
5964 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005965 task_setup_data.callback_data = data;
5966 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005967}
5968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5970{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005971 struct inode *inode = state->inode;
5972 struct nfs4_state_owner *sp = state->owner;
5973 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005974 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005975 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005976 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005977 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005978 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005979 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
Trond Myklebust9b073572006-06-29 16:38:34 -04005981 status = nfs4_set_lock_state(state, request);
5982 /* Unlock _before_ we do the RPC call */
5983 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005984 /* Exclude nfs_delegation_claim_locks() */
5985 mutex_lock(&sp->so_delegreturn_mutex);
5986 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005987 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005988 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005989 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005990 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005991 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005992 }
5993 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005994 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005995 if (status != 0)
5996 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005997 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005998 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005999 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6000 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006001 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6002 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006003 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006004 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006005 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006006 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007 status = PTR_ERR(task);
6008 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006009 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006011 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006012out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006013 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006014 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006015 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016}
6017
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006018struct nfs4_lockdata {
6019 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006020 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006021 struct nfs4_lock_state *lsp;
6022 struct nfs_open_context *ctx;
6023 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006024 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 int rpc_status;
6026 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006027 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006028};
6029
6030static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006031 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6032 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006033{
6034 struct nfs4_lockdata *p;
6035 struct inode *inode = lsp->ls_state->inode;
6036 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006037 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006038
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006039 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040 if (p == NULL)
6041 return NULL;
6042
6043 p->arg.fh = NFS_FH(inode);
6044 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006045 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006046 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006047 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006048 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6049 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006050 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006051 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006052 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006053 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006054 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006055 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006057 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006058 atomic_inc(&lsp->ls_count);
6059 p->ctx = get_nfs_open_context(ctx);
6060 memcpy(&p->fl, fl, sizeof(p->fl));
6061 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006062out_free_seqid:
6063 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064out_free:
6065 kfree(p);
6066 return NULL;
6067}
6068
6069static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6070{
6071 struct nfs4_lockdata *data = calldata;
6072 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Harvey Harrison3110ff82008-05-02 13:42:44 -07006074 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006075 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006076 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006077 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006078 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006079 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006080 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006081 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006082 nfs4_stateid_copy(&data->arg.open_stateid,
6083 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006084 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006085 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006086 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006087 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006088 nfs4_stateid_copy(&data->arg.lock_stateid,
6089 &data->lsp->ls_stateid);
6090 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006091 if (!nfs4_valid_open_stateid(state)) {
6092 data->rpc_status = -EBADF;
6093 task->tk_action = NULL;
6094 goto out_release_open_seqid;
6095 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006096 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006097 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006098 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006099 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006100 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006101 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006102out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006103 nfs_release_seqid(data->arg.open_seqid);
6104out_release_lock_seqid:
6105 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006106out_wait:
6107 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006108 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006109}
6110
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006111static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6112{
6113 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006114 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006115
Harvey Harrison3110ff82008-05-02 13:42:44 -07006116 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006117
Trond Myklebust14516c32010-07-31 14:29:06 -04006118 if (!nfs4_sequence_done(task, &data->res.seq_res))
6119 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006120
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006121 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006122 switch (task->tk_status) {
6123 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006124 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006125 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006126 if (data->arg.new_lock) {
6127 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006128 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006129 rpc_restart_call_prepare(task);
6130 break;
6131 }
6132 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006133 if (data->arg.new_lock_owner != 0) {
6134 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6135 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6136 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6137 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6138 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006139 break;
6140 case -NFS4ERR_BAD_STATEID:
6141 case -NFS4ERR_OLD_STATEID:
6142 case -NFS4ERR_STALE_STATEID:
6143 case -NFS4ERR_EXPIRED:
6144 if (data->arg.new_lock_owner != 0) {
6145 if (!nfs4_stateid_match(&data->arg.open_stateid,
6146 &lsp->ls_state->open_stateid))
6147 rpc_restart_call_prepare(task);
6148 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6149 &lsp->ls_stateid))
6150 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006152 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153}
6154
6155static void nfs4_lock_release(void *calldata)
6156{
6157 struct nfs4_lockdata *data = calldata;
6158
Harvey Harrison3110ff82008-05-02 13:42:44 -07006159 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006160 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161 if (data->cancelled != 0) {
6162 struct rpc_task *task;
6163 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6164 data->arg.lock_seqid);
6165 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006166 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006167 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168 } else
6169 nfs_free_seqid(data->arg.lock_seqid);
6170 nfs4_put_lock_state(data->lsp);
6171 put_nfs_open_context(data->ctx);
6172 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006173 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006174}
6175
6176static const struct rpc_call_ops nfs4_lock_ops = {
6177 .rpc_call_prepare = nfs4_lock_prepare,
6178 .rpc_call_done = nfs4_lock_done,
6179 .rpc_release = nfs4_lock_release,
6180};
6181
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006182static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6183{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006184 switch (error) {
6185 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006186 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006187 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006188 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006189 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006190 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006191 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006192 break;
6193 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006194 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006195 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006196 };
6197}
6198
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006199static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200{
6201 struct nfs4_lockdata *data;
6202 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006203 struct rpc_message msg = {
6204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6205 .rpc_cred = state->owner->so_cred,
6206 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006207 struct rpc_task_setup task_setup_data = {
6208 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006209 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006210 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006211 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006212 .flags = RPC_TASK_ASYNC,
6213 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006214 int ret;
6215
Harvey Harrison3110ff82008-05-02 13:42:44 -07006216 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006217 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006218 fl->fl_u.nfs4_fl.owner,
6219 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006220 if (data == NULL)
6221 return -ENOMEM;
6222 if (IS_SETLKW(cmd))
6223 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006224 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006225 msg.rpc_argp = &data->arg;
6226 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006227 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006228 if (recovery_type > NFS_LOCK_NEW) {
6229 if (recovery_type == NFS_LOCK_RECLAIM)
6230 data->arg.reclaim = NFS_LOCK_RECLAIM;
6231 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006232 } else
6233 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006234 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006235 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006236 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006237 ret = nfs4_wait_for_completion_rpc_task(task);
6238 if (ret == 0) {
6239 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006240 if (ret)
6241 nfs4_handle_setlk_error(data->server, data->lsp,
6242 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006243 } else
6244 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006245 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006246 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006247 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249}
6250
6251static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6252{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006253 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006254 struct nfs4_exception exception = {
6255 .inode = state->inode,
6256 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006257 int err;
6258
6259 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006260 /* Cache the lock if possible... */
6261 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6262 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006263 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006264 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006265 break;
6266 nfs4_handle_exception(server, err, &exception);
6267 } while (exception.retry);
6268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269}
6270
6271static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6272{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006273 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006274 struct nfs4_exception exception = {
6275 .inode = state->inode,
6276 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006277 int err;
6278
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006279 err = nfs4_set_lock_state(state, request);
6280 if (err != 0)
6281 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006282 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006283 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6284 return 0;
6285 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006286 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006287 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6288 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006289 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006290 switch (err) {
6291 default:
6292 goto out;
6293 case -NFS4ERR_GRACE:
6294 case -NFS4ERR_DELAY:
6295 nfs4_handle_exception(server, err, &exception);
6296 err = 0;
6297 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006298 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006299out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301}
6302
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006303#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006304static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6305{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006306 struct nfs4_lock_state *lsp;
6307 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006308
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006309 status = nfs4_set_lock_state(state, request);
6310 if (status != 0)
6311 return status;
6312 lsp = request->fl_u.nfs4_fl.owner;
6313 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6314 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6315 return 0;
6316 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006317 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006318}
6319#endif
6320
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6322{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006323 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006324 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006325 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006326 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
Trond Myklebust01c3b862006-06-29 16:38:39 -04006328 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006329 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006330 if (status < 0)
6331 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006332 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006333 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006334 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006335 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006336 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006337 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006338 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006339 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006340 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006341 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006342 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006343 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006344 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006345 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006346out:
6347 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 return status;
6349}
6350
6351static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6352{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006353 struct nfs4_exception exception = {
6354 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006355 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006356 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 int err;
6358
6359 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006360 err = _nfs4_proc_setlk(state, cmd, request);
6361 if (err == -NFS4ERR_DENIED)
6362 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006364 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 } while (exception.retry);
6366 return err;
6367}
6368
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006369#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6370#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6371
6372static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006373nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6374 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006375{
6376 int status = -ERESTARTSYS;
6377 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6378
6379 while(!signalled()) {
6380 status = nfs4_proc_setlk(state, cmd, request);
6381 if ((status != -EAGAIN) || IS_SETLK(cmd))
6382 break;
6383 freezable_schedule_timeout_interruptible(timeout);
6384 timeout *= 2;
6385 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6386 status = -ERESTARTSYS;
6387 }
6388 return status;
6389}
6390
Jeff Laytona1d617d82016-09-17 18:17:39 -04006391#ifdef CONFIG_NFS_V4_1
6392struct nfs4_lock_waiter {
6393 struct task_struct *task;
6394 struct inode *inode;
6395 struct nfs_lowner *owner;
6396 bool notified;
6397};
6398
6399static int
6400nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6401{
6402 int ret;
6403 struct cb_notify_lock_args *cbnl = key;
6404 struct nfs4_lock_waiter *waiter = wait->private;
6405 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6406 *wowner = waiter->owner;
6407
6408 /* Only wake if the callback was for the same owner */
6409 if (lowner->clientid != wowner->clientid ||
6410 lowner->id != wowner->id ||
6411 lowner->s_dev != wowner->s_dev)
6412 return 0;
6413
6414 /* Make sure it's for the right inode */
6415 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6416 return 0;
6417
6418 waiter->notified = true;
6419
6420 /* override "private" so we can use default_wake_function */
6421 wait->private = waiter->task;
6422 ret = autoremove_wake_function(wait, mode, flags, key);
6423 wait->private = waiter;
6424 return ret;
6425}
6426
6427static int
6428nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6429{
6430 int status = -ERESTARTSYS;
6431 unsigned long flags;
6432 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6433 struct nfs_server *server = NFS_SERVER(state->inode);
6434 struct nfs_client *clp = server->nfs_client;
6435 wait_queue_head_t *q = &clp->cl_lock_waitq;
6436 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6437 .id = lsp->ls_seqid.owner_id,
6438 .s_dev = server->s_dev };
6439 struct nfs4_lock_waiter waiter = { .task = current,
6440 .inode = state->inode,
6441 .owner = &owner,
6442 .notified = false };
6443 wait_queue_t wait;
6444
6445 /* Don't bother with waitqueue if we don't expect a callback */
6446 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6447 return nfs4_retry_setlk_simple(state, cmd, request);
6448
6449 init_wait(&wait);
6450 wait.private = &waiter;
6451 wait.func = nfs4_wake_lock_waiter;
6452 add_wait_queue(q, &wait);
6453
6454 while(!signalled()) {
6455 status = nfs4_proc_setlk(state, cmd, request);
6456 if ((status != -EAGAIN) || IS_SETLK(cmd))
6457 break;
6458
6459 status = -ERESTARTSYS;
6460 spin_lock_irqsave(&q->lock, flags);
6461 if (waiter.notified) {
6462 spin_unlock_irqrestore(&q->lock, flags);
6463 continue;
6464 }
6465 set_current_state(TASK_INTERRUPTIBLE);
6466 spin_unlock_irqrestore(&q->lock, flags);
6467
6468 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6469 }
6470
6471 finish_wait(q, &wait);
6472 return status;
6473}
6474#else /* !CONFIG_NFS_V4_1 */
6475static inline int
6476nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6477{
6478 return nfs4_retry_setlk_simple(state, cmd, request);
6479}
6480#endif
6481
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482static int
6483nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6484{
6485 struct nfs_open_context *ctx;
6486 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 int status;
6488
6489 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006490 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 state = ctx->state;
6492
6493 if (request->fl_start < 0 || request->fl_end < 0)
6494 return -EINVAL;
6495
Trond Myklebustd9531262009-07-21 19:22:38 -04006496 if (IS_GETLK(cmd)) {
6497 if (state != NULL)
6498 return nfs4_proc_getlk(state, F_GETLK, request);
6499 return 0;
6500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
6502 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6503 return -EINVAL;
6504
Trond Myklebustd9531262009-07-21 19:22:38 -04006505 if (request->fl_type == F_UNLCK) {
6506 if (state != NULL)
6507 return nfs4_proc_unlck(state, cmd, request);
6508 return 0;
6509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510
Trond Myklebustd9531262009-07-21 19:22:38 -04006511 if (state == NULL)
6512 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006513
6514 if ((request->fl_flags & FL_POSIX) &&
6515 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6516 return -ENOLCK;
6517
Trond Myklebust55725512012-04-18 12:48:35 -04006518 /*
6519 * Don't rely on the VFS having checked the file open mode,
6520 * since it won't do this for flock() locks.
6521 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006522 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006523 case F_RDLCK:
6524 if (!(filp->f_mode & FMODE_READ))
6525 return -EBADF;
6526 break;
6527 case F_WRLCK:
6528 if (!(filp->f_mode & FMODE_WRITE))
6529 return -EBADF;
6530 }
6531
Jeff Layton1ea67db2016-09-17 18:17:37 -04006532 status = nfs4_set_lock_state(state, request);
6533 if (status != 0)
6534 return status;
6535
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006536 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537}
6538
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006539int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006540{
6541 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006542 int err;
6543
6544 err = nfs4_set_lock_state(state, fl);
6545 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006546 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006547 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006548 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006549}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006550
Trond Myklebustcf470c32012-03-07 13:49:12 -05006551struct nfs_release_lockowner_data {
6552 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006553 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006554 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006555 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006556 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006557};
6558
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006559static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6560{
6561 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006562 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006563 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6564 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006565 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006566 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006567}
6568
6569static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6570{
6571 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006572 struct nfs_server *server = data->server;
6573
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006574 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006575
6576 switch (task->tk_status) {
6577 case 0:
6578 renew_lease(server, data->timestamp);
6579 break;
6580 case -NFS4ERR_STALE_CLIENTID:
6581 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006582 nfs4_schedule_lease_recovery(server->nfs_client);
6583 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006584 case -NFS4ERR_LEASE_MOVED:
6585 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006586 if (nfs4_async_handle_error(task, server,
6587 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006588 rpc_restart_call_prepare(task);
6589 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006590}
6591
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006592static void nfs4_release_lockowner_release(void *calldata)
6593{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006594 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006595 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006596 kfree(calldata);
6597}
6598
Trond Myklebust17280172012-03-11 13:11:00 -04006599static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006600 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6601 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006602 .rpc_release = nfs4_release_lockowner_release,
6603};
6604
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006605static void
6606nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006607{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006608 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006609 struct rpc_message msg = {
6610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6611 };
6612
6613 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006614 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006615
Trond Myklebustcf470c32012-03-07 13:49:12 -05006616 data = kmalloc(sizeof(*data), GFP_NOFS);
6617 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006618 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006619 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006620 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006621 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6622 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6623 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006624
Trond Myklebustcf470c32012-03-07 13:49:12 -05006625 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006626 msg.rpc_resp = &data->res;
6627 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006628 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006629}
6630
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006631#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6632
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006633static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006634 struct dentry *unused, struct inode *inode,
6635 const char *key, const void *buf,
6636 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006637{
Al Viro59301222016-05-27 10:19:30 -04006638 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006639}
6640
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006641static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006642 struct dentry *unused, struct inode *inode,
6643 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006644{
Al Virob2968212016-04-10 20:48:24 -04006645 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006646}
6647
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006648static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006649{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006650 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006651}
6652
David Quigleyc9bccef2013-05-22 12:50:45 -04006653#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006654
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006655static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006656 struct dentry *unused, struct inode *inode,
6657 const char *key, const void *buf,
6658 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006659{
6660 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006661 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006662
6663 return -EOPNOTSUPP;
6664}
6665
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006666static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006667 struct dentry *unused, struct inode *inode,
6668 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006669{
6670 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006671 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006672 return -EOPNOTSUPP;
6673}
6674
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006675static ssize_t
6676nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006677{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006678 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006679
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006680 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6681 len = security_inode_listsecurity(inode, list, list_len);
6682 if (list_len && len > list_len)
6683 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006684 }
6685 return len;
6686}
6687
6688static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6689 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006690 .get = nfs4_xattr_get_nfs4_label,
6691 .set = nfs4_xattr_set_nfs4_label,
6692};
David Quigleyc9bccef2013-05-22 12:50:45 -04006693
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006694#else
6695
6696static ssize_t
6697nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6698{
6699 return 0;
6700}
6701
6702#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006703
Andy Adamson533eb462011-06-13 18:25:56 -04006704/*
6705 * nfs_fhget will use either the mounted_on_fileid or the fileid
6706 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006707static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6708{
Andy Adamson533eb462011-06-13 18:25:56 -04006709 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6710 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6711 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006712 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006713 return;
6714
6715 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006716 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006717 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6718 fattr->nlink = 2;
6719}
6720
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006721static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6722 const struct qstr *name,
6723 struct nfs4_fs_locations *fs_locations,
6724 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006725{
6726 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006727 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006728 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006729 };
6730 struct nfs4_fs_locations_arg args = {
6731 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006732 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006733 .page = page,
6734 .bitmask = bitmask,
6735 };
Benny Halevy22958462009-04-01 09:22:02 -04006736 struct nfs4_fs_locations_res res = {
6737 .fs_locations = fs_locations,
6738 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006739 struct rpc_message msg = {
6740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6741 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006742 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006743 };
6744 int status;
6745
Harvey Harrison3110ff82008-05-02 13:42:44 -07006746 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006747
6748 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6749 * is not supported */
6750 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6751 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6752 else
6753 bitmask[0] |= FATTR4_WORD0_FILEID;
6754
Trond Myklebustc228fd32007-01-13 02:28:11 -05006755 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006756 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006757 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006758 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006759 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006760 return status;
6761}
6762
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006763int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6764 const struct qstr *name,
6765 struct nfs4_fs_locations *fs_locations,
6766 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006767{
6768 struct nfs4_exception exception = { };
6769 int err;
6770 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006771 err = _nfs4_proc_fs_locations(client, dir, name,
6772 fs_locations, page);
6773 trace_nfs4_get_fs_locations(dir, name, err);
6774 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006775 &exception);
6776 } while (exception.retry);
6777 return err;
6778}
6779
Chuck Leverb03d7352013-10-17 14:12:50 -04006780/*
6781 * This operation also signals the server that this client is
6782 * performing migration recovery. The server can stop returning
6783 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6784 * appended to this compound to identify the client ID which is
6785 * performing recovery.
6786 */
6787static int _nfs40_proc_get_locations(struct inode *inode,
6788 struct nfs4_fs_locations *locations,
6789 struct page *page, struct rpc_cred *cred)
6790{
6791 struct nfs_server *server = NFS_SERVER(inode);
6792 struct rpc_clnt *clnt = server->client;
6793 u32 bitmask[2] = {
6794 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6795 };
6796 struct nfs4_fs_locations_arg args = {
6797 .clientid = server->nfs_client->cl_clientid,
6798 .fh = NFS_FH(inode),
6799 .page = page,
6800 .bitmask = bitmask,
6801 .migration = 1, /* skip LOOKUP */
6802 .renew = 1, /* append RENEW */
6803 };
6804 struct nfs4_fs_locations_res res = {
6805 .fs_locations = locations,
6806 .migration = 1,
6807 .renew = 1,
6808 };
6809 struct rpc_message msg = {
6810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6811 .rpc_argp = &args,
6812 .rpc_resp = &res,
6813 .rpc_cred = cred,
6814 };
6815 unsigned long now = jiffies;
6816 int status;
6817
6818 nfs_fattr_init(&locations->fattr);
6819 locations->server = server;
6820 locations->nlocations = 0;
6821
6822 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6823 nfs4_set_sequence_privileged(&args.seq_args);
6824 status = nfs4_call_sync_sequence(clnt, server, &msg,
6825 &args.seq_args, &res.seq_res);
6826 if (status)
6827 return status;
6828
6829 renew_lease(server, now);
6830 return 0;
6831}
6832
6833#ifdef CONFIG_NFS_V4_1
6834
6835/*
6836 * This operation also signals the server that this client is
6837 * performing migration recovery. The server can stop asserting
6838 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6839 * performing this operation is identified in the SEQUENCE
6840 * operation in this compound.
6841 *
6842 * When the client supports GETATTR(fs_locations_info), it can
6843 * be plumbed in here.
6844 */
6845static int _nfs41_proc_get_locations(struct inode *inode,
6846 struct nfs4_fs_locations *locations,
6847 struct page *page, struct rpc_cred *cred)
6848{
6849 struct nfs_server *server = NFS_SERVER(inode);
6850 struct rpc_clnt *clnt = server->client;
6851 u32 bitmask[2] = {
6852 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6853 };
6854 struct nfs4_fs_locations_arg args = {
6855 .fh = NFS_FH(inode),
6856 .page = page,
6857 .bitmask = bitmask,
6858 .migration = 1, /* skip LOOKUP */
6859 };
6860 struct nfs4_fs_locations_res res = {
6861 .fs_locations = locations,
6862 .migration = 1,
6863 };
6864 struct rpc_message msg = {
6865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6866 .rpc_argp = &args,
6867 .rpc_resp = &res,
6868 .rpc_cred = cred,
6869 };
6870 int status;
6871
6872 nfs_fattr_init(&locations->fattr);
6873 locations->server = server;
6874 locations->nlocations = 0;
6875
6876 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6877 nfs4_set_sequence_privileged(&args.seq_args);
6878 status = nfs4_call_sync_sequence(clnt, server, &msg,
6879 &args.seq_args, &res.seq_res);
6880 if (status == NFS4_OK &&
6881 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6882 status = -NFS4ERR_LEASE_MOVED;
6883 return status;
6884}
6885
6886#endif /* CONFIG_NFS_V4_1 */
6887
6888/**
6889 * nfs4_proc_get_locations - discover locations for a migrated FSID
6890 * @inode: inode on FSID that is migrating
6891 * @locations: result of query
6892 * @page: buffer
6893 * @cred: credential to use for this operation
6894 *
6895 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6896 * operation failed, or a negative errno if a local error occurred.
6897 *
6898 * On success, "locations" is filled in, but if the server has
6899 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6900 * asserted.
6901 *
6902 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6903 * from this client that require migration recovery.
6904 */
6905int nfs4_proc_get_locations(struct inode *inode,
6906 struct nfs4_fs_locations *locations,
6907 struct page *page, struct rpc_cred *cred)
6908{
6909 struct nfs_server *server = NFS_SERVER(inode);
6910 struct nfs_client *clp = server->nfs_client;
6911 const struct nfs4_mig_recovery_ops *ops =
6912 clp->cl_mvops->mig_recovery_ops;
6913 struct nfs4_exception exception = { };
6914 int status;
6915
6916 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6917 (unsigned long long)server->fsid.major,
6918 (unsigned long long)server->fsid.minor,
6919 clp->cl_hostname);
6920 nfs_display_fhandle(NFS_FH(inode), __func__);
6921
6922 do {
6923 status = ops->get_locations(inode, locations, page, cred);
6924 if (status != -NFS4ERR_DELAY)
6925 break;
6926 nfs4_handle_exception(server, status, &exception);
6927 } while (exception.retry);
6928 return status;
6929}
6930
Chuck Lever44c99932013-10-17 14:13:30 -04006931/*
6932 * This operation also signals the server that this client is
6933 * performing "lease moved" recovery. The server can stop
6934 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6935 * is appended to this compound to identify the client ID which is
6936 * performing recovery.
6937 */
6938static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6939{
6940 struct nfs_server *server = NFS_SERVER(inode);
6941 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6942 struct rpc_clnt *clnt = server->client;
6943 struct nfs4_fsid_present_arg args = {
6944 .fh = NFS_FH(inode),
6945 .clientid = clp->cl_clientid,
6946 .renew = 1, /* append RENEW */
6947 };
6948 struct nfs4_fsid_present_res res = {
6949 .renew = 1,
6950 };
6951 struct rpc_message msg = {
6952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6953 .rpc_argp = &args,
6954 .rpc_resp = &res,
6955 .rpc_cred = cred,
6956 };
6957 unsigned long now = jiffies;
6958 int status;
6959
6960 res.fh = nfs_alloc_fhandle();
6961 if (res.fh == NULL)
6962 return -ENOMEM;
6963
6964 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6965 nfs4_set_sequence_privileged(&args.seq_args);
6966 status = nfs4_call_sync_sequence(clnt, server, &msg,
6967 &args.seq_args, &res.seq_res);
6968 nfs_free_fhandle(res.fh);
6969 if (status)
6970 return status;
6971
6972 do_renew_lease(clp, now);
6973 return 0;
6974}
6975
6976#ifdef CONFIG_NFS_V4_1
6977
6978/*
6979 * This operation also signals the server that this client is
6980 * performing "lease moved" recovery. The server can stop asserting
6981 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6982 * this operation is identified in the SEQUENCE operation in this
6983 * compound.
6984 */
6985static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6986{
6987 struct nfs_server *server = NFS_SERVER(inode);
6988 struct rpc_clnt *clnt = server->client;
6989 struct nfs4_fsid_present_arg args = {
6990 .fh = NFS_FH(inode),
6991 };
6992 struct nfs4_fsid_present_res res = {
6993 };
6994 struct rpc_message msg = {
6995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6996 .rpc_argp = &args,
6997 .rpc_resp = &res,
6998 .rpc_cred = cred,
6999 };
7000 int status;
7001
7002 res.fh = nfs_alloc_fhandle();
7003 if (res.fh == NULL)
7004 return -ENOMEM;
7005
7006 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7007 nfs4_set_sequence_privileged(&args.seq_args);
7008 status = nfs4_call_sync_sequence(clnt, server, &msg,
7009 &args.seq_args, &res.seq_res);
7010 nfs_free_fhandle(res.fh);
7011 if (status == NFS4_OK &&
7012 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7013 status = -NFS4ERR_LEASE_MOVED;
7014 return status;
7015}
7016
7017#endif /* CONFIG_NFS_V4_1 */
7018
7019/**
7020 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7021 * @inode: inode on FSID to check
7022 * @cred: credential to use for this operation
7023 *
7024 * Server indicates whether the FSID is present, moved, or not
7025 * recognized. This operation is necessary to clear a LEASE_MOVED
7026 * condition for this client ID.
7027 *
7028 * Returns NFS4_OK if the FSID is present on this server,
7029 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7030 * NFS4ERR code if some error occurred on the server, or a
7031 * negative errno if a local failure occurred.
7032 */
7033int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7034{
7035 struct nfs_server *server = NFS_SERVER(inode);
7036 struct nfs_client *clp = server->nfs_client;
7037 const struct nfs4_mig_recovery_ops *ops =
7038 clp->cl_mvops->mig_recovery_ops;
7039 struct nfs4_exception exception = { };
7040 int status;
7041
7042 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7043 (unsigned long long)server->fsid.major,
7044 (unsigned long long)server->fsid.minor,
7045 clp->cl_hostname);
7046 nfs_display_fhandle(NFS_FH(inode), __func__);
7047
7048 do {
7049 status = ops->fsid_present(inode, cred);
7050 if (status != -NFS4ERR_DELAY)
7051 break;
7052 nfs4_handle_exception(server, status, &exception);
7053 } while (exception.retry);
7054 return status;
7055}
7056
Andy Adamson5ec16a82013-08-08 10:57:55 -04007057/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007058 * If 'use_integrity' is true and the state managment nfs_client
7059 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7060 * and the machine credential as per RFC3530bis and RFC5661 Security
7061 * Considerations sections. Otherwise, just use the user cred with the
7062 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007063 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007064static 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 +00007065{
7066 int status;
7067 struct nfs4_secinfo_arg args = {
7068 .dir_fh = NFS_FH(dir),
7069 .name = name,
7070 };
7071 struct nfs4_secinfo_res res = {
7072 .flavors = flavors,
7073 };
7074 struct rpc_message msg = {
7075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7076 .rpc_argp = &args,
7077 .rpc_resp = &res,
7078 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007079 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007080 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007081
7082 if (use_integrity) {
7083 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007084 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7085 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007086 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007087
7088 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007089
7090 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7091 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7092
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007093 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7094 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007095 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007096
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007097 if (cred)
7098 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007099
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007100 return status;
7101}
7102
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007103int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7104 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007105{
7106 struct nfs4_exception exception = { };
7107 int err;
7108 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007109 err = -NFS4ERR_WRONGSEC;
7110
7111 /* try to use integrity protection with machine cred */
7112 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7113 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7114
7115 /*
7116 * if unable to use integrity protection, or SECINFO with
7117 * integrity protection returns NFS4ERR_WRONGSEC (which is
7118 * disallowed by spec, but exists in deployed servers) use
7119 * the current filesystem's rpc_client and the user cred.
7120 */
7121 if (err == -NFS4ERR_WRONGSEC)
7122 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7123
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007124 trace_nfs4_secinfo(dir, name, err);
7125 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007126 &exception);
7127 } while (exception.retry);
7128 return err;
7129}
7130
Andy Adamson557134a2009-04-01 09:21:53 -04007131#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007132/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007133 * Check the exchange flags returned by the server for invalid flags, having
7134 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7135 * DS flags set.
7136 */
7137static int nfs4_check_cl_exchange_flags(u32 flags)
7138{
7139 if (flags & ~EXCHGID4_FLAG_MASK_R)
7140 goto out_inval;
7141 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7142 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7143 goto out_inval;
7144 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7145 goto out_inval;
7146 return NFS_OK;
7147out_inval:
7148 return -NFS4ERR_INVAL;
7149}
7150
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007151static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007152nfs41_same_server_scope(struct nfs41_server_scope *a,
7153 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007154{
7155 if (a->server_scope_sz == b->server_scope_sz &&
7156 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7157 return true;
7158
7159 return false;
7160}
7161
Andy Adamson02a95de2016-02-05 16:08:37 -05007162static void
7163nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7164{
7165}
7166
7167static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7168 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7169};
7170
Andy Adamson357f54d2010-12-14 10:11:57 -05007171/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007172 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007173 *
7174 * The 4.1 client currently uses the same TCP connection for the
7175 * fore and backchannel.
7176 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007177static
7178int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7179 struct rpc_xprt *xprt,
7180 struct nfs_client *clp,
7181 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007182{
7183 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007184 struct nfs41_bind_conn_to_session_args args = {
7185 .client = clp,
7186 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7187 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007188 struct nfs41_bind_conn_to_session_res res;
7189 struct rpc_message msg = {
7190 .rpc_proc =
7191 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007192 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007193 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007194 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007195 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007196 struct rpc_task_setup task_setup_data = {
7197 .rpc_client = clnt,
7198 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007199 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007200 .rpc_message = &msg,
7201 .flags = RPC_TASK_TIMEOUT,
7202 };
7203 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007204
7205 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007206
Trond Myklebust71a097c2015-02-18 09:27:18 -08007207 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7208 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7209 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007210
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007211 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7212 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7213 args.dir = NFS4_CDFC4_FORE;
7214
7215 task = rpc_run_task(&task_setup_data);
7216 if (!IS_ERR(task)) {
7217 status = task->tk_status;
7218 rpc_put_task(task);
7219 } else
7220 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007221 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007222 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007223 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007224 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7225 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7226 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007227 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007228 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007229 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230 dprintk("NFS: %s: Unexpected direction from server\n",
7231 __func__);
7232 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007233 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007234 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007235 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007236 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7237 __func__);
7238 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007239 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007240 }
7241 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007242out:
7243 dprintk("<-- %s status= %d\n", __func__, status);
7244 return status;
7245}
7246
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007247struct rpc_bind_conn_calldata {
7248 struct nfs_client *clp;
7249 struct rpc_cred *cred;
7250};
7251
7252static int
7253nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7254 struct rpc_xprt *xprt,
7255 void *calldata)
7256{
7257 struct rpc_bind_conn_calldata *p = calldata;
7258
7259 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7260}
7261
7262int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7263{
7264 struct rpc_bind_conn_calldata data = {
7265 .clp = clp,
7266 .cred = cred,
7267 };
7268 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7269 nfs4_proc_bind_conn_to_session_callback, &data);
7270}
7271
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007272/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007273 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7274 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007275 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007276static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7277 .how = SP4_MACH_CRED,
7278 .enforce.u.words = {
7279 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7280 1 << (OP_EXCHANGE_ID - 32) |
7281 1 << (OP_CREATE_SESSION - 32) |
7282 1 << (OP_DESTROY_SESSION - 32) |
7283 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007284 },
7285 .allow.u.words = {
7286 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007287 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007288 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007289 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007290 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007291 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007292 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007293 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007294 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007295 1 << (OP_FREE_STATEID - 32) |
7296 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007297 }
7298};
7299
7300/*
7301 * Select the state protection mode for client `clp' given the server results
7302 * from exchange_id in `sp'.
7303 *
7304 * Returns 0 on success, negative errno otherwise.
7305 */
7306static int nfs4_sp4_select_mode(struct nfs_client *clp,
7307 struct nfs41_state_protection *sp)
7308{
7309 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7310 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7311 1 << (OP_EXCHANGE_ID - 32) |
7312 1 << (OP_CREATE_SESSION - 32) |
7313 1 << (OP_DESTROY_SESSION - 32) |
7314 1 << (OP_DESTROY_CLIENTID - 32)
7315 };
7316 unsigned int i;
7317
7318 if (sp->how == SP4_MACH_CRED) {
7319 /* Print state protect result */
7320 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7321 for (i = 0; i <= LAST_NFS4_OP; i++) {
7322 if (test_bit(i, sp->enforce.u.longs))
7323 dfprintk(MOUNT, " enforce op %d\n", i);
7324 if (test_bit(i, sp->allow.u.longs))
7325 dfprintk(MOUNT, " allow op %d\n", i);
7326 }
7327
7328 /* make sure nothing is on enforce list that isn't supported */
7329 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7330 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7331 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7332 return -EINVAL;
7333 }
7334 }
7335
7336 /*
7337 * Minimal mode - state operations are allowed to use machine
7338 * credential. Note this already happens by default, so the
7339 * client doesn't have to do anything more than the negotiation.
7340 *
7341 * NOTE: we don't care if EXCHANGE_ID is in the list -
7342 * we're already using the machine cred for exchange_id
7343 * and will never use a different cred.
7344 */
7345 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7346 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7347 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7348 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7349 dfprintk(MOUNT, "sp4_mach_cred:\n");
7350 dfprintk(MOUNT, " minimal mode enabled\n");
7351 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7352 } else {
7353 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7354 return -EINVAL;
7355 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007356
7357 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007358 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7359 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007360 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7361 dfprintk(MOUNT, " cleanup mode enabled\n");
7362 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7363 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007364
Andrew Elble99ade3c2015-12-02 09:39:51 -05007365 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7366 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7367 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7368 &clp->cl_sp4_flags);
7369 }
7370
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007371 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7372 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7373 dfprintk(MOUNT, " secinfo mode enabled\n");
7374 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7375 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007376
7377 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7378 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7379 dfprintk(MOUNT, " stateid mode enabled\n");
7380 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7381 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007382
7383 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7384 dfprintk(MOUNT, " write mode enabled\n");
7385 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7386 }
7387
7388 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7389 dfprintk(MOUNT, " commit mode enabled\n");
7390 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7391 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007392 }
7393
7394 return 0;
7395}
7396
Andy Adamson8d89bd72016-09-09 09:22:18 -04007397struct nfs41_exchange_id_data {
7398 struct nfs41_exchange_id_res res;
7399 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007400 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007401 int rpc_status;
7402};
7403
7404static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007405{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007406 struct nfs41_exchange_id_data *cdata =
7407 (struct nfs41_exchange_id_data *)data;
7408 struct nfs_client *clp = cdata->args.client;
7409 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007410
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007411 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007412
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007413 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007414 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007415
7416 if (cdata->xprt && status == 0) {
7417 status = nfs4_detect_session_trunking(clp, &cdata->res,
7418 cdata->xprt);
7419 goto out;
7420 }
7421
Andy Adamson8d89bd72016-09-09 09:22:18 -04007422 if (status == 0)
7423 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007424
Chuck Lever177313f2012-05-21 22:44:58 -04007425 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007426 clp->cl_clientid = cdata->res.clientid;
7427 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007428 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007429 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007430 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007431 &clp->cl_session->session_state);
7432 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007433 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007434
Chuck Leveracdeb692012-05-21 22:46:16 -04007435 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007436 clp->cl_serverowner = cdata->res.server_owner;
7437 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007438
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007439 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007440 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007441 clp->cl_implid = cdata->res.impl_id;
7442 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007443
Chuck Lever177313f2012-05-21 22:44:58 -04007444 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007445 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007447 dprintk("%s: server_scope mismatch detected\n",
7448 __func__);
7449 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007450 kfree(clp->cl_serverscope);
7451 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007452 }
7453
Chuck Lever177313f2012-05-21 22:44:58 -04007454 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007455 clp->cl_serverscope = cdata->res.server_scope;
7456 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007457 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007458 /* Save the EXCHANGE_ID verifier session trunk tests */
7459 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7460 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007461 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007462out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007464 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007465}
7466
7467static void nfs4_exchange_id_release(void *data)
7468{
7469 struct nfs41_exchange_id_data *cdata =
7470 (struct nfs41_exchange_id_data *)data;
7471
7472 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007473 if (cdata->xprt) {
7474 xprt_put(cdata->xprt);
7475 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7476 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 kfree(cdata->res.impl_id);
7478 kfree(cdata->res.server_scope);
7479 kfree(cdata->res.server_owner);
7480 kfree(cdata);
7481}
7482
7483static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7484 .rpc_call_done = nfs4_exchange_id_done,
7485 .rpc_release = nfs4_exchange_id_release,
7486};
7487
Benny Halevy99fe60d2009-04-01 09:22:29 -04007488/*
7489 * _nfs4_proc_exchange_id()
7490 *
7491 * Wrapper for EXCHANGE_ID operation.
7492 */
7493static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007494 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007495{
7496 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007497 struct rpc_message msg = {
7498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499 .rpc_cred = cred,
7500 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007501 struct rpc_task_setup task_setup_data = {
7502 .rpc_client = clp->cl_rpcclient,
7503 .callback_ops = &nfs4_exchange_id_call_ops,
7504 .rpc_message = &msg,
7505 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7506 };
7507 struct nfs41_exchange_id_data *calldata;
7508 struct rpc_task *task;
7509 int status = -EIO;
7510
7511 if (!atomic_inc_not_zero(&clp->cl_count))
7512 goto out;
7513
7514 status = -ENOMEM;
7515 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7516 if (!calldata)
7517 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007518
Andy Adamsonad0849a2016-09-09 09:22:28 -04007519 if (!xprt)
7520 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007521
7522 status = nfs4_init_uniform_client_string(clp);
7523 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007524 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007525
7526 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7527 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7528 clp->cl_owner_id);
7529
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7531 GFP_NOFS);
7532 status = -ENOMEM;
7533 if (unlikely(calldata->res.server_owner == NULL))
7534 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007535
Andy Adamson8d89bd72016-09-09 09:22:18 -04007536 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007537 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007539 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007540
Andy Adamson8d89bd72016-09-09 09:22:18 -04007541 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7542 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007543 goto out_server_scope;
7544
7545 switch (sp4_how) {
7546 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007547 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007548 break;
7549
7550 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007551 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007552 break;
7553
7554 default:
7555 /* unsupported! */
7556 WARN_ON_ONCE(1);
7557 status = -EINVAL;
7558 goto out_impl_id;
7559 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007560 if (xprt) {
7561 calldata->xprt = xprt;
7562 task_setup_data.rpc_xprt = xprt;
7563 task_setup_data.flags =
7564 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7565 calldata->args.verifier = &clp->cl_confirm;
7566 } else {
7567 calldata->args.verifier = &verifier;
7568 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 calldata->args.client = clp;
7570#ifdef CONFIG_NFS_V4_1_MIGRATION
7571 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7572 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7573 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7574#else
7575 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7576 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7577#endif
7578 msg.rpc_argp = &calldata->args;
7579 msg.rpc_resp = &calldata->res;
7580 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007581
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582 task = rpc_run_task(&task_setup_data);
7583 if (IS_ERR(task)) {
7584 status = PTR_ERR(task);
7585 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007586 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007587
Andy Adamsonad0849a2016-09-09 09:22:28 -04007588 if (!xprt) {
7589 status = rpc_wait_for_completion_task(task);
7590 if (!status)
7591 status = calldata->rpc_status;
7592 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007593 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007594
Andy Adamson8d89bd72016-09-09 09:22:18 -04007595 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007596out:
Chuck Lever177313f2012-05-21 22:44:58 -04007597 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007598 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007599 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007600 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007601 clp->cl_implid->date.seconds,
7602 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007603 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007604 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007605
7606out_impl_id:
7607 kfree(calldata->res.impl_id);
7608out_server_scope:
7609 kfree(calldata->res.server_scope);
7610out_server_owner:
7611 kfree(calldata->res.server_owner);
7612out_calldata:
7613 kfree(calldata);
7614 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007615}
7616
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007617/*
7618 * nfs4_proc_exchange_id()
7619 *
7620 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7621 *
7622 * Since the clientid has expired, all compounds using sessions
7623 * associated with the stale clientid will be returning
7624 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7625 * be in some phase of session reset.
7626 *
7627 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7628 */
7629int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7630{
7631 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7632 int status;
7633
7634 /* try SP4_MACH_CRED if krb5i/p */
7635 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7636 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007637 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007638 if (!status)
7639 return 0;
7640 }
7641
7642 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007643 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007644}
7645
Andy Adamson04fa2c62016-09-09 09:22:29 -04007646/**
7647 * nfs4_test_session_trunk
7648 *
7649 * This is an add_xprt_test() test function called from
7650 * rpc_clnt_setup_test_and_add_xprt.
7651 *
7652 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7653 * and is dereferrenced in nfs4_exchange_id_release
7654 *
7655 * Upon success, add the new transport to the rpc_clnt
7656 *
7657 * @clnt: struct rpc_clnt to get new transport
7658 * @xprt: the rpc_xprt to test
7659 * @data: call data for _nfs4_proc_exchange_id.
7660 */
7661int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7662 void *data)
7663{
7664 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7665 u32 sp4_how;
7666
7667 dprintk("--> %s try %s\n", __func__,
7668 xprt->address_strings[RPC_DISPLAY_ADDR]);
7669
7670 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7671
7672 /* Test connection for session trunking. Async exchange_id call */
7673 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7674}
7675EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7676
Trond Myklebust66245532012-05-25 17:18:09 -04007677static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7678 struct rpc_cred *cred)
7679{
7680 struct rpc_message msg = {
7681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7682 .rpc_argp = clp,
7683 .rpc_cred = cred,
7684 };
7685 int status;
7686
7687 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007688 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007689 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007690 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007691 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7692 return status;
7693}
7694
7695static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7696 struct rpc_cred *cred)
7697{
7698 unsigned int loop;
7699 int ret;
7700
7701 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7702 ret = _nfs4_proc_destroy_clientid(clp, cred);
7703 switch (ret) {
7704 case -NFS4ERR_DELAY:
7705 case -NFS4ERR_CLIENTID_BUSY:
7706 ssleep(1);
7707 break;
7708 default:
7709 return ret;
7710 }
7711 }
7712 return 0;
7713}
7714
7715int nfs4_destroy_clientid(struct nfs_client *clp)
7716{
7717 struct rpc_cred *cred;
7718 int ret = 0;
7719
7720 if (clp->cl_mvops->minor_version < 1)
7721 goto out;
7722 if (clp->cl_exchange_flags == 0)
7723 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007724 if (clp->cl_preserve_clid)
7725 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007726 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007727 ret = nfs4_proc_destroy_clientid(clp, cred);
7728 if (cred)
7729 put_rpccred(cred);
7730 switch (ret) {
7731 case 0:
7732 case -NFS4ERR_STALE_CLIENTID:
7733 clp->cl_exchange_flags = 0;
7734 }
7735out:
7736 return ret;
7737}
7738
Andy Adamson2050f0c2009-04-01 09:22:30 -04007739struct nfs4_get_lease_time_data {
7740 struct nfs4_get_lease_time_args *args;
7741 struct nfs4_get_lease_time_res *res;
7742 struct nfs_client *clp;
7743};
7744
7745static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7746 void *calldata)
7747{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007748 struct nfs4_get_lease_time_data *data =
7749 (struct nfs4_get_lease_time_data *)calldata;
7750
7751 dprintk("--> %s\n", __func__);
7752 /* just setup sequence, do not trigger session recovery
7753 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007754 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007755 &data->args->la_seq_args,
7756 &data->res->lr_seq_res,
7757 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007758 dprintk("<-- %s\n", __func__);
7759}
7760
7761/*
7762 * Called from nfs4_state_manager thread for session setup, so don't recover
7763 * from sequence operation or clientid errors.
7764 */
7765static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7766{
7767 struct nfs4_get_lease_time_data *data =
7768 (struct nfs4_get_lease_time_data *)calldata;
7769
7770 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007771 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7772 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007773 switch (task->tk_status) {
7774 case -NFS4ERR_DELAY:
7775 case -NFS4ERR_GRACE:
7776 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7777 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7778 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007779 /* fall through */
7780 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007781 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007782 return;
7783 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007784 dprintk("<-- %s\n", __func__);
7785}
7786
Trond Myklebust17280172012-03-11 13:11:00 -04007787static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007788 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7789 .rpc_call_done = nfs4_get_lease_time_done,
7790};
7791
7792int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7793{
7794 struct rpc_task *task;
7795 struct nfs4_get_lease_time_args args;
7796 struct nfs4_get_lease_time_res res = {
7797 .lr_fsinfo = fsinfo,
7798 };
7799 struct nfs4_get_lease_time_data data = {
7800 .args = &args,
7801 .res = &res,
7802 .clp = clp,
7803 };
7804 struct rpc_message msg = {
7805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7806 .rpc_argp = &args,
7807 .rpc_resp = &res,
7808 };
7809 struct rpc_task_setup task_setup = {
7810 .rpc_client = clp->cl_rpcclient,
7811 .rpc_message = &msg,
7812 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007813 .callback_data = &data,
7814 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007815 };
7816 int status;
7817
Chuck Levera9c92d62013-08-09 12:48:18 -04007818 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007819 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007820 dprintk("--> %s\n", __func__);
7821 task = rpc_run_task(&task_setup);
7822
7823 if (IS_ERR(task))
7824 status = PTR_ERR(task);
7825 else {
7826 status = task->tk_status;
7827 rpc_put_task(task);
7828 }
7829 dprintk("<-- %s return %d\n", __func__, status);
7830
7831 return status;
7832}
7833
Andy Adamsonfc931582009-04-01 09:22:31 -04007834/*
7835 * Initialize the values to be used by the client in CREATE_SESSION
7836 * If nfs4_init_session set the fore channel request and response sizes,
7837 * use them.
7838 *
7839 * Set the back channel max_resp_sz_cached to zero to force the client to
7840 * always set csa_cachethis to FALSE because the current implementation
7841 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7842 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007843static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7844 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007845{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007846 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007847 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007848
Andy Adamson18aad3d2013-06-26 12:21:49 -04007849 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7850 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7851
Andy Adamsonfc931582009-04-01 09:22:31 -04007852 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007853 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7854 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007855 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007856 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007857
7858 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007859 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007860 __func__,
7861 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007862 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007863
7864 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007865 args->bc_attrs.max_rqst_sz = max_bc_payload;
7866 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007867 args->bc_attrs.max_resp_sz_cached = 0;
7868 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007869 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007870
7871 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7872 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7873 __func__,
7874 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7875 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7876 args->bc_attrs.max_reqs);
7877}
7878
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7880 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007881{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007882 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007883 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007885 if (rcvd->max_resp_sz > sent->max_resp_sz)
7886 return -EINVAL;
7887 /*
7888 * Our requested max_ops is the minimum we need; we're not
7889 * prepared to break up compounds into smaller pieces than that.
7890 * So, no point even trying to continue if the server won't
7891 * cooperate:
7892 */
7893 if (rcvd->max_ops < sent->max_ops)
7894 return -EINVAL;
7895 if (rcvd->max_reqs == 0)
7896 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007897 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7898 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007899 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007900}
7901
Trond Myklebust79969dd2015-02-18 11:30:18 -08007902static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7903 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007904{
7905 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007906 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007907
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007908 if (!(res->flags & SESSION4_BACK_CHAN))
7909 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007910 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7911 return -EINVAL;
7912 if (rcvd->max_resp_sz < sent->max_resp_sz)
7913 return -EINVAL;
7914 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7915 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007916 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007917 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007918 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007919 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007920out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007921 return 0;
7922}
Andy Adamson8d353012009-04-01 09:22:32 -04007923
Andy Adamson8d353012009-04-01 09:22:32 -04007924static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007925 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007926{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007927 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007928
Trond Myklebust79969dd2015-02-18 11:30:18 -08007929 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007930 if (ret)
7931 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007932 return nfs4_verify_back_channel_attrs(args, res);
7933}
7934
7935static void nfs4_update_session(struct nfs4_session *session,
7936 struct nfs41_create_session_res *res)
7937{
7938 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007939 /* Mark client id and session as being confirmed */
7940 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7941 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007942 session->flags = res->flags;
7943 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007944 if (res->flags & SESSION4_BACK_CHAN)
7945 memcpy(&session->bc_attrs, &res->bc_attrs,
7946 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007947}
7948
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007949static int _nfs4_proc_create_session(struct nfs_client *clp,
7950 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007951{
7952 struct nfs4_session *session = clp->cl_session;
7953 struct nfs41_create_session_args args = {
7954 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007955 .clientid = clp->cl_clientid,
7956 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007957 .cb_program = NFS4_CALLBACK,
7958 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007959 struct nfs41_create_session_res res;
7960
Andy Adamsonfc931582009-04-01 09:22:31 -04007961 struct rpc_message msg = {
7962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7963 .rpc_argp = &args,
7964 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007965 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007966 };
7967 int status;
7968
Chuck Lever6b26cc82016-05-02 14:40:40 -04007969 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007970 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007971
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007972 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007973 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007974
Trond Myklebustb519d402016-09-11 14:50:01 -04007975 switch (status) {
7976 case -NFS4ERR_STALE_CLIENTID:
7977 case -NFS4ERR_DELAY:
7978 case -ETIMEDOUT:
7979 case -EACCES:
7980 case -EAGAIN:
7981 goto out;
7982 };
7983
7984 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007985 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007986 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007987 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007988 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007989 if (status)
7990 goto out;
7991 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007992 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007993out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 return status;
7995}
7996
7997/*
7998 * Issues a CREATE_SESSION operation to the server.
7999 * It is the responsibility of the caller to verify the session is
8000 * expired before calling this routine.
8001 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008002int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008003{
8004 int status;
8005 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008006 struct nfs4_session *session = clp->cl_session;
8007
8008 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8009
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008010 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008011 if (status)
8012 goto out;
8013
Andy Adamsonaacd5532011-11-09 13:58:21 -05008014 /* Init or reset the session slot tables */
8015 status = nfs4_setup_session_slot_tables(session);
8016 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008017 if (status)
8018 goto out;
8019
8020 ptr = (unsigned *)&session->sess_id.data[0];
8021 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8022 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008023out:
8024 dprintk("<-- %s\n", __func__);
8025 return status;
8026}
8027
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008028/*
8029 * Issue the over-the-wire RPC DESTROY_SESSION.
8030 * The caller must serialize access to this routine.
8031 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008032int nfs4_proc_destroy_session(struct nfs4_session *session,
8033 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008034{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008035 struct rpc_message msg = {
8036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8037 .rpc_argp = session,
8038 .rpc_cred = cred,
8039 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008040 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008041
8042 dprintk("--> nfs4_proc_destroy_session\n");
8043
8044 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008045 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8046 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008047
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008048 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008049 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008050
8051 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008052 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008053 "Session has been destroyed regardless...\n", status);
8054
8055 dprintk("<-- nfs4_proc_destroy_session\n");
8056 return status;
8057}
8058
Trond Myklebust7b38c362012-05-23 13:23:31 -04008059/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008060 * Renew the cl_session lease.
8061 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008062struct nfs4_sequence_data {
8063 struct nfs_client *clp;
8064 struct nfs4_sequence_args args;
8065 struct nfs4_sequence_res res;
8066};
8067
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008068static void nfs41_sequence_release(void *data)
8069{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008070 struct nfs4_sequence_data *calldata = data;
8071 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008072
Alexandros Batsakis71358402010-02-05 03:45:05 -08008073 if (atomic_read(&clp->cl_count) > 1)
8074 nfs4_schedule_state_renewal(clp);
8075 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008076 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008077}
8078
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008079static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8080{
8081 switch(task->tk_status) {
8082 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008083 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8084 return -EAGAIN;
8085 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008086 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008087 }
8088 return 0;
8089}
8090
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008091static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008092{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008093 struct nfs4_sequence_data *calldata = data;
8094 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008095
Trond Myklebust14516c32010-07-31 14:29:06 -04008096 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8097 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008098
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008099 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100 if (task->tk_status < 0) {
8101 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008102 if (atomic_read(&clp->cl_count) == 1)
8103 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008104
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008105 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8106 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008107 return;
8108 }
8109 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008110 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008111out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112 dprintk("<-- %s\n", __func__);
8113}
8114
8115static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8116{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008117 struct nfs4_sequence_data *calldata = data;
8118 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008119 struct nfs4_sequence_args *args;
8120 struct nfs4_sequence_res *res;
8121
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008122 args = task->tk_msg.rpc_argp;
8123 res = task->tk_msg.rpc_resp;
8124
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008125 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008126}
8127
8128static const struct rpc_call_ops nfs41_sequence_ops = {
8129 .rpc_call_done = nfs41_sequence_call_done,
8130 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008131 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008132};
8133
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008134static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8135 struct rpc_cred *cred,
8136 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008137{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008138 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008139 struct rpc_message msg = {
8140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8141 .rpc_cred = cred,
8142 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008143 struct rpc_task_setup task_setup_data = {
8144 .rpc_client = clp->cl_rpcclient,
8145 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008146 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008147 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008148 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008149
Alexandros Batsakis71358402010-02-05 03:45:05 -08008150 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008151 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008152 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008153 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008154 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008155 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008156 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008157 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008158 if (is_privileged)
8159 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008160 msg.rpc_argp = &calldata->args;
8161 msg.rpc_resp = &calldata->res;
8162 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008163 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008164
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008165 return rpc_run_task(&task_setup_data);
8166}
8167
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008168static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008169{
8170 struct rpc_task *task;
8171 int ret = 0;
8172
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008173 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008174 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008175 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008176 if (IS_ERR(task))
8177 ret = PTR_ERR(task);
8178 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008179 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008180 dprintk("<-- %s status=%d\n", __func__, ret);
8181 return ret;
8182}
8183
8184static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8185{
8186 struct rpc_task *task;
8187 int ret;
8188
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008189 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008190 if (IS_ERR(task)) {
8191 ret = PTR_ERR(task);
8192 goto out;
8193 }
8194 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008195 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008196 ret = task->tk_status;
8197 rpc_put_task(task);
8198out:
8199 dprintk("<-- %s status=%d\n", __func__, ret);
8200 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008201}
8202
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008203struct nfs4_reclaim_complete_data {
8204 struct nfs_client *clp;
8205 struct nfs41_reclaim_complete_args arg;
8206 struct nfs41_reclaim_complete_res res;
8207};
8208
8209static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8210{
8211 struct nfs4_reclaim_complete_data *calldata = data;
8212
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008213 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008214 &calldata->arg.seq_args,
8215 &calldata->res.seq_res,
8216 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008217}
8218
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008219static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8220{
8221 switch(task->tk_status) {
8222 case 0:
8223 case -NFS4ERR_COMPLETE_ALREADY:
8224 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8225 break;
8226 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008227 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008228 /* fall through */
8229 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008230 return -EAGAIN;
8231 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008232 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008233 }
8234 return 0;
8235}
8236
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008237static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8238{
8239 struct nfs4_reclaim_complete_data *calldata = data;
8240 struct nfs_client *clp = calldata->clp;
8241 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8242
8243 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008244 if (!nfs41_sequence_done(task, res))
8245 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008246
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008247 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008248 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8249 rpc_restart_call_prepare(task);
8250 return;
8251 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252 dprintk("<-- %s\n", __func__);
8253}
8254
8255static void nfs4_free_reclaim_complete_data(void *data)
8256{
8257 struct nfs4_reclaim_complete_data *calldata = data;
8258
8259 kfree(calldata);
8260}
8261
8262static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8263 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8264 .rpc_call_done = nfs4_reclaim_complete_done,
8265 .rpc_release = nfs4_free_reclaim_complete_data,
8266};
8267
8268/*
8269 * Issue a global reclaim complete.
8270 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008271static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8272 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008273{
8274 struct nfs4_reclaim_complete_data *calldata;
8275 struct rpc_task *task;
8276 struct rpc_message msg = {
8277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008278 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008279 };
8280 struct rpc_task_setup task_setup_data = {
8281 .rpc_client = clp->cl_rpcclient,
8282 .rpc_message = &msg,
8283 .callback_ops = &nfs4_reclaim_complete_call_ops,
8284 .flags = RPC_TASK_ASYNC,
8285 };
8286 int status = -ENOMEM;
8287
8288 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008289 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008290 if (calldata == NULL)
8291 goto out;
8292 calldata->clp = clp;
8293 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008294
Chuck Levera9c92d62013-08-09 12:48:18 -04008295 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008296 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008297 msg.rpc_argp = &calldata->arg;
8298 msg.rpc_resp = &calldata->res;
8299 task_setup_data.callback_data = calldata;
8300 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008301 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008302 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008303 goto out;
8304 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008305 status = nfs4_wait_for_completion_rpc_task(task);
8306 if (status == 0)
8307 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008308 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008309 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008310out:
8311 dprintk("<-- %s status=%d\n", __func__, status);
8312 return status;
8313}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008314
8315static void
8316nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8317{
8318 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008319 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008320
8321 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008322 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008323 &lgp->res.seq_res, task);
8324 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008325}
8326
8327static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8328{
8329 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008330
8331 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008332 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008333 dprintk("<-- %s\n", __func__);
8334}
8335
8336static int
8337nfs4_layoutget_handle_exception(struct rpc_task *task,
8338 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8339{
Trond Myklebustee314c22012-10-01 17:25:48 -07008340 struct inode *inode = lgp->args.inode;
8341 struct nfs_server *server = NFS_SERVER(inode);
8342 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008343 int nfs4err = task->tk_status;
8344 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008345 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008346
Boaz Harroshed7e5422014-01-22 20:34:54 +02008347 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008348
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008349 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008350 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008351 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008352
8353 /*
8354 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8355 * on the file. set tk_status to -ENODATA to tell upper layer to
8356 * retry go inband.
8357 */
8358 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008359 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008360 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008361 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008362 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8363 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8364 */
8365 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008366 status = -EOVERFLOW;
8367 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008368 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008369 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008370 * (or clients) writing to the same RAID stripe except when
8371 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008372 *
8373 * Treat it like we would RECALLCONFLICT -- we retry for a little
8374 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008375 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008376 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008377 if (lgp->args.minlength == 0) {
8378 status = -EOVERFLOW;
8379 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008380 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008381 status = -EBUSY;
8382 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008383 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008384 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008385 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008386 case -NFS4ERR_DELEG_REVOKED:
8387 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008388 case -NFS4ERR_EXPIRED:
8389 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008390 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008391 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008392 lo = NFS_I(inode)->layout;
8393 /* If the open stateid was bad, then recover it. */
8394 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8395 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008396 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008397 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008398 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008399 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008400 break;
8401 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008402
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008403 /*
8404 * Mark the bad layout state as invalid, then retry
8405 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008406 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008407 spin_unlock(&inode->i_lock);
8408 pnfs_free_lseg_list(&head);
8409 status = -EAGAIN;
8410 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008411 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008412
Trond Myklebust8ac09252017-01-23 22:44:12 -05008413 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008414 err = nfs4_handle_exception(server, nfs4err, exception);
8415 if (!status) {
8416 if (exception->retry)
8417 status = -EAGAIN;
8418 else
8419 status = err;
8420 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008421out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008422 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008423 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008424}
8425
Idan Kedar85541162012-08-02 11:47:10 +03008426static size_t max_response_pages(struct nfs_server *server)
8427{
8428 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8429 return nfs_page_array_len(0, max_resp_sz);
8430}
8431
8432static void nfs4_free_pages(struct page **pages, size_t size)
8433{
8434 int i;
8435
8436 if (!pages)
8437 return;
8438
8439 for (i = 0; i < size; i++) {
8440 if (!pages[i])
8441 break;
8442 __free_page(pages[i]);
8443 }
8444 kfree(pages);
8445}
8446
8447static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8448{
8449 struct page **pages;
8450 int i;
8451
8452 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8453 if (!pages) {
8454 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8455 return NULL;
8456 }
8457
8458 for (i = 0; i < size; i++) {
8459 pages[i] = alloc_page(gfp_flags);
8460 if (!pages[i]) {
8461 dprintk("%s: failed to allocate page\n", __func__);
8462 nfs4_free_pages(pages, size);
8463 return NULL;
8464 }
8465 }
8466
8467 return pages;
8468}
8469
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008470static void nfs4_layoutget_release(void *calldata)
8471{
8472 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008473 struct inode *inode = lgp->args.inode;
8474 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008475 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008476
8477 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008478 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008479 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480 put_nfs_open_context(lgp->args.ctx);
8481 kfree(calldata);
8482 dprintk("<-- %s\n", __func__);
8483}
8484
8485static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8486 .rpc_call_prepare = nfs4_layoutget_prepare,
8487 .rpc_call_done = nfs4_layoutget_done,
8488 .rpc_release = nfs4_layoutget_release,
8489};
8490
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008491struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008492nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008493{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008494 struct inode *inode = lgp->args.inode;
8495 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008496 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008497 struct rpc_task *task;
8498 struct rpc_message msg = {
8499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8500 .rpc_argp = &lgp->args,
8501 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008502 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008503 };
8504 struct rpc_task_setup task_setup_data = {
8505 .rpc_client = server->client,
8506 .rpc_message = &msg,
8507 .callback_ops = &nfs4_layoutget_call_ops,
8508 .callback_data = lgp,
8509 .flags = RPC_TASK_ASYNC,
8510 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008511 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008512 struct nfs4_exception exception = {
8513 .inode = inode,
8514 .timeout = *timeout,
8515 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008516 int status = 0;
8517
8518 dprintk("--> %s\n", __func__);
8519
Peng Tao4bd5a982014-11-17 11:05:17 +08008520 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8521 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8522
Idan Kedar85541162012-08-02 11:47:10 +03008523 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8524 if (!lgp->args.layout.pages) {
8525 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008526 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008527 }
8528 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8529
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008530 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008531 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008532 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008533
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008534 task = rpc_run_task(&task_setup_data);
8535 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008536 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008537 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008538 if (status == 0) {
8539 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8540 *timeout = exception.timeout;
8541 }
8542
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008543 trace_nfs4_layoutget(lgp->args.ctx,
8544 &lgp->args.range,
8545 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008546 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008547 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008548
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008549 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8550 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008551 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008552 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008553 rpc_put_task(task);
8554 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008555 if (status)
8556 return ERR_PTR(status);
8557 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008558}
8559
Benny Halevycbe82602011-05-22 19:52:37 +03008560static void
8561nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8562{
8563 struct nfs4_layoutreturn *lrp = calldata;
8564
8565 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008566 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008567 &lrp->args.seq_args,
8568 &lrp->res.seq_res,
8569 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008570}
8571
8572static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8573{
8574 struct nfs4_layoutreturn *lrp = calldata;
8575 struct nfs_server *server;
8576
8577 dprintk("--> %s\n", __func__);
8578
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008579 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008580 return;
8581
8582 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008583 switch (task->tk_status) {
8584 default:
8585 task->tk_status = 0;
8586 case 0:
8587 break;
8588 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008589 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008590 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008591 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008592 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008593 return;
8594 }
Benny Halevycbe82602011-05-22 19:52:37 +03008595 dprintk("<-- %s\n", __func__);
8596}
8597
8598static void nfs4_layoutreturn_release(void *calldata)
8599{
8600 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008601 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008602
8603 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008604 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008605 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008606 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008607 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8608 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008609 pnfs_put_layout_hdr(lrp->args.layout);
8610 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008611 kfree(calldata);
8612 dprintk("<-- %s\n", __func__);
8613}
8614
8615static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8616 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8617 .rpc_call_done = nfs4_layoutreturn_done,
8618 .rpc_release = nfs4_layoutreturn_release,
8619};
8620
Peng Tao6c166052014-11-17 09:30:40 +08008621int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008622{
8623 struct rpc_task *task;
8624 struct rpc_message msg = {
8625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8626 .rpc_argp = &lrp->args,
8627 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008628 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008629 };
8630 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008631 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008632 .rpc_message = &msg,
8633 .callback_ops = &nfs4_layoutreturn_call_ops,
8634 .callback_data = lrp,
8635 };
Peng Tao6c166052014-11-17 09:30:40 +08008636 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008637
Andrew Elble99ade3c2015-12-02 09:39:51 -05008638 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8639 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8640 &task_setup_data.rpc_client, &msg);
8641
Benny Halevycbe82602011-05-22 19:52:37 +03008642 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008643 if (!sync) {
8644 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8645 if (!lrp->inode) {
8646 nfs4_layoutreturn_release(lrp);
8647 return -EAGAIN;
8648 }
8649 task_setup_data.flags |= RPC_TASK_ASYNC;
8650 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008651 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008652 task = rpc_run_task(&task_setup_data);
8653 if (IS_ERR(task))
8654 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008655 if (sync)
8656 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008657 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008658 dprintk("<-- %s status=%d\n", __func__, status);
8659 rpc_put_task(task);
8660 return status;
8661}
8662
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008664_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8665 struct pnfs_device *pdev,
8666 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008667{
8668 struct nfs4_getdeviceinfo_args args = {
8669 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008670 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8671 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008672 };
8673 struct nfs4_getdeviceinfo_res res = {
8674 .pdev = pdev,
8675 };
8676 struct rpc_message msg = {
8677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8678 .rpc_argp = &args,
8679 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008680 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008681 };
8682 int status;
8683
8684 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008685 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008686 if (res.notification & ~args.notify_types)
8687 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008688 if (res.notification != args.notify_types)
8689 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008690
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008691 dprintk("<-- %s status=%d\n", __func__, status);
8692
8693 return status;
8694}
8695
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008696int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8697 struct pnfs_device *pdev,
8698 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008699{
8700 struct nfs4_exception exception = { };
8701 int err;
8702
8703 do {
8704 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008705 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008706 &exception);
8707 } while (exception.retry);
8708 return err;
8709}
8710EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8711
Andy Adamson863a3c62011-03-23 13:27:54 +00008712static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8713{
8714 struct nfs4_layoutcommit_data *data = calldata;
8715 struct nfs_server *server = NFS_SERVER(data->args.inode);
8716
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008717 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008718 &data->args.seq_args,
8719 &data->res.seq_res,
8720 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008721}
8722
8723static void
8724nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8725{
8726 struct nfs4_layoutcommit_data *data = calldata;
8727 struct nfs_server *server = NFS_SERVER(data->args.inode);
8728
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008729 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008730 return;
8731
8732 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008733 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8734 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8735 case -NFS4ERR_BADLAYOUT: /* no layout */
8736 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008737 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008738 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008739 break;
8740 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008741 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008742 rpc_restart_call_prepare(task);
8743 return;
8744 }
8745 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008746}
8747
8748static void nfs4_layoutcommit_release(void *calldata)
8749{
8750 struct nfs4_layoutcommit_data *data = calldata;
8751
Andy Adamsondb29c082011-07-30 20:52:38 -04008752 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008753 nfs_post_op_update_inode_force_wcc(data->args.inode,
8754 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008755 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008756 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008757 kfree(data);
8758}
8759
8760static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8761 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8762 .rpc_call_done = nfs4_layoutcommit_done,
8763 .rpc_release = nfs4_layoutcommit_release,
8764};
8765
8766int
Andy Adamsonef311532011-03-12 02:58:10 -05008767nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008768{
8769 struct rpc_message msg = {
8770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8771 .rpc_argp = &data->args,
8772 .rpc_resp = &data->res,
8773 .rpc_cred = data->cred,
8774 };
8775 struct rpc_task_setup task_setup_data = {
8776 .task = &data->task,
8777 .rpc_client = NFS_CLIENT(data->args.inode),
8778 .rpc_message = &msg,
8779 .callback_ops = &nfs4_layoutcommit_ops,
8780 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008781 };
8782 struct rpc_task *task;
8783 int status = 0;
8784
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008785 dprintk("NFS: initiating layoutcommit call. sync %d "
8786 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008787 data->args.lastbytewritten,
8788 data->args.inode->i_ino);
8789
Trond Myklebust472e2592015-02-05 16:50:30 -05008790 if (!sync) {
8791 data->inode = nfs_igrab_and_active(data->args.inode);
8792 if (data->inode == NULL) {
8793 nfs4_layoutcommit_release(data);
8794 return -EAGAIN;
8795 }
8796 task_setup_data.flags = RPC_TASK_ASYNC;
8797 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008798 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008799 task = rpc_run_task(&task_setup_data);
8800 if (IS_ERR(task))
8801 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008802 if (sync)
8803 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008804 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008805 dprintk("%s: status %d\n", __func__, status);
8806 rpc_put_task(task);
8807 return status;
8808}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008809
Andy Adamson97431202013-08-08 10:57:56 -04008810/**
8811 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8812 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8813 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008814static int
8815_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008816 struct nfs_fsinfo *info,
8817 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008818{
8819 struct nfs41_secinfo_no_name_args args = {
8820 .style = SECINFO_STYLE_CURRENT_FH,
8821 };
8822 struct nfs4_secinfo_res res = {
8823 .flavors = flavors,
8824 };
8825 struct rpc_message msg = {
8826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8827 .rpc_argp = &args,
8828 .rpc_resp = &res,
8829 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008830 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008831 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008832 int status;
8833
8834 if (use_integrity) {
8835 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008836 cred = nfs4_get_clid_cred(server->nfs_client);
8837 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008838 }
8839
8840 dprintk("--> %s\n", __func__);
8841 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8842 &res.seq_res, 0);
8843 dprintk("<-- %s status=%d\n", __func__, status);
8844
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008845 if (cred)
8846 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008847
8848 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008849}
8850
8851static int
8852nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8853 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8854{
8855 struct nfs4_exception exception = { };
8856 int err;
8857 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008858 /* first try using integrity protection */
8859 err = -NFS4ERR_WRONGSEC;
8860
8861 /* try to use integrity protection with machine cred */
8862 if (_nfs4_is_integrity_protected(server->nfs_client))
8863 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8864 flavors, true);
8865
8866 /*
8867 * if unable to use integrity protection, or SECINFO with
8868 * integrity protection returns NFS4ERR_WRONGSEC (which is
8869 * disallowed by spec, but exists in deployed servers) use
8870 * the current filesystem's rpc_client and the user cred.
8871 */
8872 if (err == -NFS4ERR_WRONGSEC)
8873 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8874 flavors, false);
8875
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008876 switch (err) {
8877 case 0:
8878 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008879 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008880 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008881 default:
8882 err = nfs4_handle_exception(server, err, &exception);
8883 }
8884 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008885out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008886 return err;
8887}
8888
8889static int
8890nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8891 struct nfs_fsinfo *info)
8892{
8893 int err;
8894 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008895 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008896 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008897 struct nfs4_secinfo4 *secinfo;
8898 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008899
8900 page = alloc_page(GFP_KERNEL);
8901 if (!page) {
8902 err = -ENOMEM;
8903 goto out;
8904 }
8905
8906 flavors = page_address(page);
8907 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8908
8909 /*
8910 * Fall back on "guess and check" method if
8911 * the server doesn't support SECINFO_NO_NAME
8912 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008913 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008914 err = nfs4_find_root_sec(server, fhandle, info);
8915 goto out_freepage;
8916 }
8917 if (err)
8918 goto out_freepage;
8919
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008920 for (i = 0; i < flavors->num_flavors; i++) {
8921 secinfo = &flavors->flavors[i];
8922
8923 switch (secinfo->flavor) {
8924 case RPC_AUTH_NULL:
8925 case RPC_AUTH_UNIX:
8926 case RPC_AUTH_GSS:
8927 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8928 &secinfo->flavor_info);
8929 break;
8930 default:
8931 flavor = RPC_AUTH_MAXFLAVOR;
8932 break;
8933 }
8934
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008935 if (!nfs_auth_info_match(&server->auth_info, flavor))
8936 flavor = RPC_AUTH_MAXFLAVOR;
8937
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008938 if (flavor != RPC_AUTH_MAXFLAVOR) {
8939 err = nfs4_lookup_root_sec(server, fhandle,
8940 info, flavor);
8941 if (!err)
8942 break;
8943 }
8944 }
8945
8946 if (flavor == RPC_AUTH_MAXFLAVOR)
8947 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008948
8949out_freepage:
8950 put_page(page);
8951 if (err == -EACCES)
8952 return -EPERM;
8953out:
8954 return err;
8955}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008956
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008957static int _nfs41_test_stateid(struct nfs_server *server,
8958 nfs4_stateid *stateid,
8959 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008960{
8961 int status;
8962 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008963 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008964 };
8965 struct nfs41_test_stateid_res res;
8966 struct rpc_message msg = {
8967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8968 .rpc_argp = &args,
8969 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008970 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008971 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008972 struct rpc_clnt *rpc_client = server->client;
8973
8974 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8975 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008976
Chuck Lever38527b12012-07-11 16:30:23 -04008977 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008978 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008979 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008980 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008981 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008982 if (status != NFS_OK) {
8983 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008984 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008985 }
8986 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008987 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008988}
8989
Trond Myklebust43912bb2016-09-22 13:38:56 -04008990static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8991 int err, struct nfs4_exception *exception)
8992{
8993 exception->retry = 0;
8994 switch(err) {
8995 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008996 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008997 nfs4_handle_exception(server, err, exception);
8998 break;
8999 case -NFS4ERR_BADSESSION:
9000 case -NFS4ERR_BADSLOT:
9001 case -NFS4ERR_BAD_HIGH_SLOT:
9002 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9003 case -NFS4ERR_DEADSESSION:
9004 nfs4_do_handle_exception(server, err, exception);
9005 }
9006}
9007
Chuck Lever38527b12012-07-11 16:30:23 -04009008/**
9009 * nfs41_test_stateid - perform a TEST_STATEID operation
9010 *
9011 * @server: server / transport on which to perform the operation
9012 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009013 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009014 *
9015 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9016 * Otherwise a negative NFS4ERR value is returned if the operation
9017 * failed or the state ID is not currently valid.
9018 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009019static int nfs41_test_stateid(struct nfs_server *server,
9020 nfs4_stateid *stateid,
9021 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009022{
9023 struct nfs4_exception exception = { };
9024 int err;
9025 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009026 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009027 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009028 } while (exception.retry);
9029 return err;
9030}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009031
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009032struct nfs_free_stateid_data {
9033 struct nfs_server *server;
9034 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009035 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009036};
9037
9038static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9039{
9040 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009041 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009042 &data->args.seq_args,
9043 &data->res.seq_res,
9044 task);
9045}
9046
9047static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9048{
9049 struct nfs_free_stateid_data *data = calldata;
9050
9051 nfs41_sequence_done(task, &data->res.seq_res);
9052
9053 switch (task->tk_status) {
9054 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009055 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009056 rpc_restart_call_prepare(task);
9057 }
9058}
9059
9060static void nfs41_free_stateid_release(void *calldata)
9061{
9062 kfree(calldata);
9063}
9064
Trond Myklebust17f26b12013-08-21 15:48:42 -04009065static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009066 .rpc_call_prepare = nfs41_free_stateid_prepare,
9067 .rpc_call_done = nfs41_free_stateid_done,
9068 .rpc_release = nfs41_free_stateid_release,
9069};
9070
9071static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009072 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009073 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009074 bool privileged)
9075{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009076 struct rpc_message msg = {
9077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009078 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009079 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009080 struct rpc_task_setup task_setup = {
9081 .rpc_client = server->client,
9082 .rpc_message = &msg,
9083 .callback_ops = &nfs41_free_stateid_ops,
9084 .flags = RPC_TASK_ASYNC,
9085 };
9086 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009087
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009088 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9089 &task_setup.rpc_client, &msg);
9090
Chuck Lever38527b12012-07-11 16:30:23 -04009091 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009092 data = kmalloc(sizeof(*data), GFP_NOFS);
9093 if (!data)
9094 return ERR_PTR(-ENOMEM);
9095 data->server = server;
9096 nfs4_stateid_copy(&data->args.stateid, stateid);
9097
9098 task_setup.callback_data = data;
9099
9100 msg.rpc_argp = &data->args;
9101 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009102 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009103 if (privileged)
9104 nfs4_set_sequence_privileged(&data->args.seq_args);
9105
9106 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009107}
9108
Chuck Lever38527b12012-07-11 16:30:23 -04009109/**
9110 * nfs41_free_stateid - perform a FREE_STATEID operation
9111 *
9112 * @server: server / transport on which to perform the operation
9113 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009114 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009115 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009116 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009117 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009118 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009119static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009120 const nfs4_stateid *stateid,
9121 struct rpc_cred *cred,
9122 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009123{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009124 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009125
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009126 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009127 if (IS_ERR(task))
9128 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009129 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009130 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009131}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009132
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009133static void
9134nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009135{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009136 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009137
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009138 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009139 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009140}
9141
Trond Myklebust36281ca2012-03-04 18:13:56 -05009142static bool nfs41_match_stateid(const nfs4_stateid *s1,
9143 const nfs4_stateid *s2)
9144{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009145 if (s1->type != s2->type)
9146 return false;
9147
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009148 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009149 return false;
9150
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009151 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009152 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009153 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009154 return true;
9155
9156 return false;
9157}
9158
Andy Adamson557134a2009-04-01 09:21:53 -04009159#endif /* CONFIG_NFS_V4_1 */
9160
Trond Myklebust36281ca2012-03-04 18:13:56 -05009161static bool nfs4_match_stateid(const nfs4_stateid *s1,
9162 const nfs4_stateid *s2)
9163{
Trond Myklebustf597c532012-03-04 18:13:56 -05009164 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009165}
9166
9167
Trond Myklebust17280172012-03-11 13:11:00 -04009168static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009169 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009170 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171 .recover_open = nfs4_open_reclaim,
9172 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009173 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009174 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175};
9176
Andy Adamson591d71c2009-04-01 09:22:47 -04009177#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009178static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009179 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9180 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9181 .recover_open = nfs4_open_reclaim,
9182 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009183 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009184 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009185 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009186};
9187#endif /* CONFIG_NFS_V4_1 */
9188
Trond Myklebust17280172012-03-11 13:11:00 -04009189static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009190 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009191 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009192 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009194 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195};
9196
Andy Adamson591d71c2009-04-01 09:22:47 -04009197#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009198static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009199 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9200 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009201 .recover_open = nfs41_open_expired,
9202 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009203 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009204};
9205#endif /* CONFIG_NFS_V4_1 */
9206
Trond Myklebust17280172012-03-11 13:11:00 -04009207static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009208 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009209 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009210 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009211};
9212
9213#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009214static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009215 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009216 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009217 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009218};
9219#endif
9220
Chuck Leverec011fe2013-10-17 14:12:39 -04009221static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009222 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009223 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009224};
9225
9226#if defined(CONFIG_NFS_V4_1)
9227static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009228 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009229 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009230};
9231#endif /* CONFIG_NFS_V4_1 */
9232
Trond Myklebust97dc1352010-06-16 09:52:26 -04009233static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9234 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009235 .init_caps = NFS_CAP_READDIRPLUS
9236 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009237 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009238 .init_client = nfs40_init_client,
9239 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009240 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009241 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009242 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009243 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009244 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009245 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009246 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9247 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9248 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009249 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009250};
9251
9252#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009253static struct nfs_seqid *
9254nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9255{
9256 return NULL;
9257}
9258
Trond Myklebust97dc1352010-06-16 09:52:26 -04009259static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9260 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009261 .init_caps = NFS_CAP_READDIRPLUS
9262 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009263 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009264 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009265 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009266 .init_client = nfs41_init_client,
9267 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009268 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009269 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009270 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009271 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009272 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009273 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009274 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009275 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9276 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9277 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009278 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009279};
9280#endif
9281
Steve Dickson42c2c422013-05-22 12:50:38 -04009282#if defined(CONFIG_NFS_V4_2)
9283static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9284 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009285 .init_caps = NFS_CAP_READDIRPLUS
9286 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009287 | NFS_CAP_POSIX_LOCK
9288 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009289 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009290 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009291 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009292 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009293 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009294 | NFS_CAP_LAYOUTSTATS
9295 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009296 .init_client = nfs41_init_client,
9297 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009298 .match_stateid = nfs41_match_stateid,
9299 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009300 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009301 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009302 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009303 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009304 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009305 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9306 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9307 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009308 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009309};
9310#endif
9311
Trond Myklebust97dc1352010-06-16 09:52:26 -04009312const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9313 [0] = &nfs_v4_0_minor_ops,
9314#if defined(CONFIG_NFS_V4_1)
9315 [1] = &nfs_v4_1_minor_ops,
9316#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009317#if defined(CONFIG_NFS_V4_2)
9318 [2] = &nfs_v4_2_minor_ops,
9319#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009320};
9321
Trond Myklebust13997822016-07-24 17:10:52 -04009322static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009323{
9324 ssize_t error, error2;
9325
9326 error = generic_listxattr(dentry, list, size);
9327 if (error < 0)
9328 return error;
9329 if (list) {
9330 list += error;
9331 size -= error;
9332 }
9333
9334 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9335 if (error2 < 0)
9336 return error2;
9337 return error + error2;
9338}
9339
Trond Myklebust17f26b12013-08-21 15:48:42 -04009340static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009341 .create = nfs_create,
9342 .lookup = nfs_lookup,
9343 .atomic_open = nfs_atomic_open,
9344 .link = nfs_link,
9345 .unlink = nfs_unlink,
9346 .symlink = nfs_symlink,
9347 .mkdir = nfs_mkdir,
9348 .rmdir = nfs_rmdir,
9349 .mknod = nfs_mknod,
9350 .rename = nfs_rename,
9351 .permission = nfs_permission,
9352 .getattr = nfs_getattr,
9353 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009354 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009355};
9356
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009357static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009358 .permission = nfs_permission,
9359 .getattr = nfs_getattr,
9360 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009361 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009362};
9363
David Howells509de812006-08-22 20:06:11 -04009364const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .version = 4, /* protocol version */
9366 .dentry_ops = &nfs4_dentry_operations,
9367 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009368 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009369 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009371 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009372 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 .getattr = nfs4_proc_getattr,
9374 .setattr = nfs4_proc_setattr,
9375 .lookup = nfs4_proc_lookup,
9376 .access = nfs4_proc_access,
9377 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378 .create = nfs4_proc_create,
9379 .remove = nfs4_proc_remove,
9380 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009381 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009383 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009384 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009385 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 .link = nfs4_proc_link,
9387 .symlink = nfs4_proc_symlink,
9388 .mkdir = nfs4_proc_mkdir,
9389 .rmdir = nfs4_proc_remove,
9390 .readdir = nfs4_proc_readdir,
9391 .mknod = nfs4_proc_mknod,
9392 .statfs = nfs4_proc_statfs,
9393 .fsinfo = nfs4_proc_fsinfo,
9394 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009395 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009397 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009399 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009401 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009403 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009404 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009406 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009407 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009408 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009409 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009410 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009411 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009412 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009413 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009414 .create_server = nfs4_create_server,
9415 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416};
9417
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009418static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009419 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009420 .list = nfs4_xattr_list_nfs4_acl,
9421 .get = nfs4_xattr_get_nfs4_acl,
9422 .set = nfs4_xattr_set_nfs4_acl,
9423};
9424
9425const struct xattr_handler *nfs4_xattr_handlers[] = {
9426 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009427#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9428 &nfs4_xattr_nfs4_label_handler,
9429#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009430 NULL
9431};
9432
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433/*
9434 * Local variables:
9435 * c-basic-offset: 8
9436 * End:
9437 */