blob: 700ed1fc1075997f7010a4d69faca383478c49b4 [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
Peng Taocb04ad22014-06-11 05:24:15 +0800625int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400629{
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 struct nfs4_slot *slot;
631
632 /* slot already allocated? */
633 if (res->sr_slot != NULL)
634 goto out_start;
635
636 spin_lock(&tbl->slot_tbl_lock);
637 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
638 goto out_sleep;
639
640 slot = nfs4_alloc_slot(tbl);
641 if (IS_ERR(slot)) {
642 if (slot == ERR_PTR(-ENOMEM))
643 task->tk_timeout = HZ >> 2;
644 goto out_sleep;
645 }
646 spin_unlock(&tbl->slot_tbl_lock);
647
Trond Myklebust0a014a42016-09-22 13:38:51 -0400648 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400649 args->sa_slot = slot;
650 res->sr_slot = slot;
651
652out_start:
653 rpc_call_start(task);
654 return 0;
655
656out_sleep:
657 if (args->sa_privileged)
658 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
659 NULL, RPC_PRIORITY_PRIVILEGED);
660 else
661 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
662 spin_unlock(&tbl->slot_tbl_lock);
663 return -EAGAIN;
664}
Peng Taocb04ad22014-06-11 05:24:15 +0800665EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400666
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400667static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400668{
669 struct nfs4_slot *slot = res->sr_slot;
670 struct nfs4_slot_table *tbl;
671
Chuck Lever3bd23842013-08-09 12:49:19 -0400672 tbl = slot->table;
673 spin_lock(&tbl->slot_tbl_lock);
674 if (!nfs41_wake_and_assign_slot(tbl, slot))
675 nfs4_free_slot(tbl, slot);
676 spin_unlock(&tbl->slot_tbl_lock);
677
678 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400679}
680
681static int nfs40_sequence_done(struct rpc_task *task,
682 struct nfs4_sequence_res *res)
683{
684 if (res->sr_slot != NULL)
685 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400686 return 1;
687}
688
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689#if defined(CONFIG_NFS_V4_1)
690
Trond Myklebustd185a332010-06-16 09:52:25 -0400691static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400692{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500693 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400694 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500695 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500696 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400697
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500698 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500699 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500700
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400701 /* Bump the slot sequence number */
702 if (slot->seq_done)
703 slot->seq_nr++;
704 slot->seq_done = 0;
705
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500706 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500707 /* Be nice to the server: try to ensure that the last transmitted
708 * value for highest_user_slotid <= target_highest_slotid
709 */
710 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
711 send_new_highest_used_slotid = true;
712
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500713 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500714 send_new_highest_used_slotid = false;
715 goto out_unlock;
716 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500717 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500718
719 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
720 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500721out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500722 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400723 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500724 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400725 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400726 if (waitqueue_active(&tbl->slot_waitq))
727 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400728}
729
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400730static int nfs41_sequence_process(struct rpc_task *task,
731 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400732{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500733 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500734 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500736 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500737 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400738
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 if (slot == NULL)
740 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400741 /* don't increment the sequence number if the task wasn't sent */
742 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743 goto out;
744
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500745 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500746
Trond Myklebustac20d162012-12-15 15:36:07 -0500747 if (slot->interrupted) {
748 slot->interrupted = 0;
749 interrupted = true;
750 }
751
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400752 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500753 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400754 switch (res->sr_status) {
755 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400756 /* If previous op on slot was interrupted and we reused
757 * the seq# and got a reply from the cache, then retry
758 */
759 if (task->tk_status == -EREMOTEIO && interrupted) {
760 ++slot->seq_nr;
761 goto retry_nowait;
762 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400763 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400764 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500765 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500766 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500767 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400768 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
769 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500770 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500772 case 1:
773 /*
774 * sr_status remains 1 if an RPC level error occurred.
775 * The server may or may not have processed the sequence
776 * operation..
777 * Mark the slot as having hosted an interrupted RPC call.
778 */
779 slot->interrupted = 1;
780 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400781 case -NFS4ERR_DELAY:
782 /* The server detected a resend of the RPC call and
783 * returned NFS4ERR_DELAY as per Section 2.10.6.2
784 * of RFC5661.
785 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500786 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400787 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500788 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500789 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400790 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500791 case -NFS4ERR_BADSLOT:
792 /*
793 * The slot id we used was probably retired. Try again
794 * using a different slot id.
795 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500796 goto retry_nowait;
797 case -NFS4ERR_SEQ_MISORDERED:
798 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500799 * Was the last operation on this sequence interrupted?
800 * If so, retry after bumping the sequence number.
801 */
802 if (interrupted) {
803 ++slot->seq_nr;
804 goto retry_nowait;
805 }
806 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500807 * Could this slot have been previously retired?
808 * If so, then the server may be expecting seq_nr = 1!
809 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500810 if (slot->seq_nr != 1) {
811 slot->seq_nr = 1;
812 goto retry_nowait;
813 }
814 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500815 case -NFS4ERR_SEQ_FALSE_RETRY:
816 ++slot->seq_nr;
817 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500818 case -NFS4ERR_DEADSESSION:
819 case -NFS4ERR_BADSESSION:
820 nfs4_schedule_session_recovery(session, res->sr_status);
821 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400822 default:
823 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400824 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400825 }
826out:
827 /* The session may be reset by one of the error handlers. */
828 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500829out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500830 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500831retry_nowait:
832 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400833 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500834 task->tk_status = 0;
835 ret = 0;
836 }
837 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400838out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400839 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 goto out;
841 rpc_delay(task, NFS4_POLL_RETRY_MAX);
842 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400843}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400844
845int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
846{
847 if (!nfs41_sequence_process(task, res))
848 return 0;
849 if (res->sr_slot != NULL)
850 nfs41_sequence_free_slot(res);
851 return 1;
852
853}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500854EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400855
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400856static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
857{
858 if (res->sr_slot == NULL)
859 return 1;
860 if (res->sr_slot->table->session != NULL)
861 return nfs41_sequence_process(task, res);
862 return nfs40_sequence_done(task, res);
863}
864
865static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
866{
867 if (res->sr_slot != NULL) {
868 if (res->sr_slot->table->session != NULL)
869 nfs41_sequence_free_slot(res);
870 else
871 nfs40_sequence_free_slot(res);
872 }
873}
874
Peng Tao2c4b1312014-06-11 05:24:16 +0800875int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400876{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500877 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400878 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400879 if (!res->sr_slot->table->session)
880 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400881 return nfs41_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);
Trond Myklebustdf896452010-06-16 09:52:26 -0400884
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000885int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400886 struct nfs4_sequence_args *args,
887 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888 struct rpc_task *task)
889{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400890 struct nfs4_slot *slot;
891 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400892
893 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400895 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400896 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400898 tbl = &session->fc_slot_table;
899
Trond Myklebust69d206b2012-11-22 13:21:02 -0500900 task->tk_timeout = 0;
901
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400902 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400903 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400904 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500905 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400907 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400908 }
909
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500910 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500911 if (IS_ERR(slot)) {
912 /* If out of memory, try again in 1/4 second */
913 if (slot == ERR_PTR(-ENOMEM))
914 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400915 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400916 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400917 }
918 spin_unlock(&tbl->slot_tbl_lock);
919
Trond Myklebust0a014a42016-09-22 13:38:51 -0400920 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500921 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400922
Chuck Levere8d92382013-08-09 12:47:51 -0400923 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500924 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400925
Benny Halevydfb4f3092010-09-24 09:17:01 -0400926 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500927 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400928 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400929 /*
930 * sr_status is only set in decode_sequence, and so will remain
931 * set to 1 if an rpc level failure occurs.
932 */
933 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400934 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400935out_success:
936 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400938out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400939 /* Privileged tasks are queued with top priority */
940 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400941 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
942 NULL, RPC_PRIORITY_PRIVILEGED);
943 else
944 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400945 spin_unlock(&tbl->slot_tbl_lock);
946 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000948EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400949
Chuck Lever5a580e02013-08-09 12:48:09 -0400950static int nfs4_setup_sequence(const struct nfs_server *server,
951 struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400954{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400955 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400956 int ret = 0;
957
Chuck Lever3bd23842013-08-09 12:49:19 -0400958 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800959 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
960 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400961
Chuck Levere8d92382013-08-09 12:47:51 -0400962 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400963 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400964 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400965
Trond Myklebust9d12b212012-01-17 22:04:25 -0500966 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400967
Andy Adamsonce5039c2009-04-01 09:22:13 -0400968 dprintk("<-- %s status=%d\n", __func__, ret);
969 return ret;
970}
971
Andy Adamsonce5039c2009-04-01 09:22:13 -0400972static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
973{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400974 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400975 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400976
Trond Myklebust035168ab2010-06-16 09:52:26 -0400977 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
978
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400979 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400980}
981
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
983{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400984 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985
Trond Myklebust14516c32010-07-31 14:29:06 -0400986 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400987}
988
Trond Myklebust17280172012-03-11 13:11:00 -0400989static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400990 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400991 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400992};
993
Chuck Lever3bd23842013-08-09 12:49:19 -0400994#else /* !CONFIG_NFS_V4_1 */
995
Chuck Lever5a580e02013-08-09 12:48:09 -0400996static int nfs4_setup_sequence(const struct nfs_server *server,
997 struct nfs4_sequence_args *args,
998 struct nfs4_sequence_res *res,
999 struct rpc_task *task)
1000{
Peng Taocb04ad22014-06-11 05:24:15 +08001001 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1002 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001003}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001004
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001005static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1006{
1007 return nfs40_sequence_done(task, res);
1008}
1009
1010static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1011{
1012 if (res->sr_slot != NULL)
1013 nfs40_sequence_free_slot(res);
1014}
1015
Peng Tao2c4b1312014-06-11 05:24:16 +08001016int nfs4_sequence_done(struct rpc_task *task,
1017 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001018{
Chuck Lever3bd23842013-08-09 12:49:19 -04001019 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001020}
Peng Tao2c4b1312014-06-11 05:24:16 +08001021EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001022
1023#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001024
Chuck Lever9915ea72013-08-09 12:48:27 -04001025static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1026{
1027 struct nfs4_call_sync_data *data = calldata;
1028 nfs4_setup_sequence(data->seq_server,
1029 data->seq_args, data->seq_res, task);
1030}
1031
1032static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1033{
1034 struct nfs4_call_sync_data *data = calldata;
1035 nfs4_sequence_done(task, data->seq_res);
1036}
1037
1038static const struct rpc_call_ops nfs40_call_sync_ops = {
1039 .rpc_call_prepare = nfs40_call_sync_prepare,
1040 .rpc_call_done = nfs40_call_sync_done,
1041};
1042
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001043static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001044 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001046 struct nfs4_sequence_args *args,
1047 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001048{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001049 int ret;
1050 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 struct nfs_client *clp = server->nfs_client;
1052 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001053 .seq_server = server,
1054 .seq_args = args,
1055 .seq_res = res,
1056 };
1057 struct rpc_task_setup task_setup = {
1058 .rpc_client = clnt,
1059 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001060 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001061 .callback_data = &data
1062 };
1063
1064 task = rpc_run_task(&task_setup);
1065 if (IS_ERR(task))
1066 ret = PTR_ERR(task);
1067 else {
1068 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001069 rpc_put_task(task);
1070 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001071 return ret;
1072}
1073
Bryan Schumaker7c513052011-03-24 17:12:24 +00001074int nfs4_call_sync(struct rpc_clnt *clnt,
1075 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001076 struct rpc_message *msg,
1077 struct nfs4_sequence_args *args,
1078 struct nfs4_sequence_res *res,
1079 int cache_reply)
1080{
Chuck Levera9c92d62013-08-09 12:48:18 -04001081 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001082 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1086{
1087 struct nfs_inode *nfsi = NFS_I(dir);
1088
1089 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001090 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001091 if (cinfo->atomic && cinfo->before == dir->i_version) {
1092 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1093 nfsi->attrtimeo_timestamp = jiffies;
1094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001096 if (cinfo->before != dir->i_version)
1097 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1098 NFS_INO_INVALID_ACL;
1099 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001100 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001101 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001102 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 spin_unlock(&dir->i_lock);
1104}
1105
1106struct nfs4_opendata {
1107 struct kref kref;
1108 struct nfs_openargs o_arg;
1109 struct nfs_openres o_res;
1110 struct nfs_open_confirmargs c_arg;
1111 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001112 struct nfs4_string owner_name;
1113 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001114 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001116 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001118 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct nfs4_state_owner *owner;
1120 struct nfs4_state *state;
1121 struct iattr attrs;
1122 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001124 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001125 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001126 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int cancelled;
1128};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001129
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001130static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1131 int err, struct nfs4_exception *exception)
1132{
1133 if (err != -EINVAL)
1134 return false;
1135 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1136 return false;
1137 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1138 exception->retry = 1;
1139 return true;
1140}
1141
Trond Myklebust6ae37332015-01-30 14:21:14 -05001142static u32
1143nfs4_map_atomic_open_share(struct nfs_server *server,
1144 fmode_t fmode, int openflags)
1145{
1146 u32 res = 0;
1147
1148 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1149 case FMODE_READ:
1150 res = NFS4_SHARE_ACCESS_READ;
1151 break;
1152 case FMODE_WRITE:
1153 res = NFS4_SHARE_ACCESS_WRITE;
1154 break;
1155 case FMODE_READ|FMODE_WRITE:
1156 res = NFS4_SHARE_ACCESS_BOTH;
1157 }
1158 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1159 goto out;
1160 /* Want no delegation if we're using O_DIRECT */
1161 if (openflags & O_DIRECT)
1162 res |= NFS4_SHARE_WANT_NO_DELEG;
1163out:
1164 return res;
1165}
1166
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001167static enum open_claim_type4
1168nfs4_map_atomic_open_claim(struct nfs_server *server,
1169 enum open_claim_type4 claim)
1170{
1171 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1172 return claim;
1173 switch (claim) {
1174 default:
1175 return claim;
1176 case NFS4_OPEN_CLAIM_FH:
1177 return NFS4_OPEN_CLAIM_NULL;
1178 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1179 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1180 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1182 }
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186{
1187 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001188 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_res.seqid = p->o_arg.seqid;
1190 p->c_res.seqid = p->c_arg.seqid;
1191 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001192 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001194 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195}
1196
Al Viro82a2c1b2011-06-22 18:30:55 -04001197static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001199 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001200 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001201 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001202 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001203{
Al Viro82a2c1b2011-06-22 18:30:55 -04001204 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001205 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001207 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 struct nfs4_opendata *p;
1209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 if (p == NULL)
1212 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001213
1214 p->f_label = nfs4_label_alloc(server, gfp_mask);
1215 if (IS_ERR(p->f_label))
1216 goto err_free_p;
1217
Kinglong Meea49c2692015-07-27 15:31:38 +08001218 p->a_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->a_label))
1220 goto err_free_f;
1221
Trond Myklebust63f5f792015-01-23 19:19:25 -05001222 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1223 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001224 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001225 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001226 nfs_sb_active(dentry->d_sb);
1227 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 p->dir = parent;
1229 p->owner = sp;
1230 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 p->o_arg.open_flags = flags;
1232 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001233 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001234 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001235 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1236 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001237 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1238 * will return permission denied for all bits until close */
1239 if (!(flags & O_EXCL)) {
1240 /* ask server to check for all possible rights as results
1241 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001242 switch (p->o_arg.claim) {
1243 default:
1244 break;
1245 case NFS4_OPEN_CLAIM_NULL:
1246 case NFS4_OPEN_CLAIM_FH:
1247 p->o_arg.access = NFS4_ACCESS_READ |
1248 NFS4_ACCESS_MODIFY |
1249 NFS4_ACCESS_EXTEND |
1250 NFS4_ACCESS_EXECUTE;
1251 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001252 }
David Howells7539bba2006-08-22 20:06:09 -04001253 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001254 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1255 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001256 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001258 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001259 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001260 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001261 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001262 case NFS4_OPEN_CLAIM_NULL:
1263 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1264 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1265 p->o_arg.fh = NFS_FH(dir);
1266 break;
1267 case NFS4_OPEN_CLAIM_PREVIOUS:
1268 case NFS4_OPEN_CLAIM_FH:
1269 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1270 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001271 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001272 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001273 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001274 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001275
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 p->o_arg.u.attrs = &p->attrs;
1277 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001278
1279 verf[0] = jiffies;
1280 verf[1] = current->pid;
1281 memcpy(p->o_arg.u.verifier.data, verf,
1282 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 p->c_arg.fh = &p->o_res.fh;
1285 p->c_arg.stateid = &p->o_res.stateid;
1286 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001288 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001290
1291err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001292 nfs4_label_free(p->a_label);
1293err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001294 nfs4_label_free(p->f_label);
1295err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296 kfree(p);
1297err:
1298 dput(parent);
1299 return NULL;
1300}
1301
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001302static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001303{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 struct nfs4_opendata *p = container_of(kref,
1305 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001306 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307
1308 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001309 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001310 if (p->state != NULL)
1311 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001313
Kinglong Meea49c2692015-07-27 15:31:38 +08001314 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001315 nfs4_label_free(p->f_label);
1316
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001317 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001318 dput(p->dentry);
1319 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001320 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001321 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001322 kfree(p);
1323}
1324
1325static void nfs4_opendata_put(struct nfs4_opendata *p)
1326{
1327 if (p != NULL)
1328 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001329}
1330
Trond Myklebust06f814a2006-01-03 09:55:07 +01001331static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1332{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001333 int ret;
1334
Trond Myklebust06f814a2006-01-03 09:55:07 +01001335 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001336 return ret;
1337}
1338
Trond Myklebust24311f82015-09-20 10:50:17 -04001339static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1340 fmode_t fmode)
1341{
1342 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1343 case FMODE_READ|FMODE_WRITE:
1344 return state->n_rdwr != 0;
1345 case FMODE_WRITE:
1346 return state->n_wronly != 0;
1347 case FMODE_READ:
1348 return state->n_rdonly != 0;
1349 }
1350 WARN_ON_ONCE(1);
1351 return false;
1352}
1353
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001355{
1356 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357
Trond Myklebust536e43d2012-01-17 22:04:26 -05001358 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001359 goto out;
1360 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001362 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1363 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001364 break;
1365 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001366 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1367 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001368 break;
1369 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001370 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1371 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001372 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001373out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001374 return ret;
1375}
1376
Trond Myklebust2a606182015-08-19 22:30:00 -05001377static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1378 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001380 if (delegation == NULL)
1381 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001383 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001384 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1385 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001386 switch (claim) {
1387 case NFS4_OPEN_CLAIM_NULL:
1388 case NFS4_OPEN_CLAIM_FH:
1389 break;
1390 case NFS4_OPEN_CLAIM_PREVIOUS:
1391 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1392 break;
1393 default:
1394 return 0;
1395 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001396 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001397 return 1;
1398}
1399
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001400static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001401{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001402 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001403 case FMODE_WRITE:
1404 state->n_wronly++;
1405 break;
1406 case FMODE_READ:
1407 state->n_rdonly++;
1408 break;
1409 case FMODE_READ|FMODE_WRITE:
1410 state->n_rdwr++;
1411 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001412 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001413}
1414
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001415#ifdef CONFIG_NFS_V4_1
1416static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1417{
1418 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1419 return true;
1420 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1421 return true;
1422 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1423 return true;
1424 return false;
1425}
1426#endif /* CONFIG_NFS_V4_1 */
1427
Trond Myklebust4f14c192014-02-12 19:15:06 -05001428static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001429{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001430 struct nfs_client *clp = state->owner->so_server->nfs_client;
1431 bool need_recover = false;
1432
1433 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1434 need_recover = true;
1435 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1436 need_recover = true;
1437 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1438 need_recover = true;
1439 if (need_recover)
1440 nfs4_state_mark_reclaim_nograce(clp, state);
1441}
1442
Trond Myklebuste999e802014-02-10 18:20:47 -05001443static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001444 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001445{
1446 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1447 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001448 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001449 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001450 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001451 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001452 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001453 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1454 return true;
1455 return false;
1456}
1457
Trond Myklebustf95549c2015-01-23 18:06:09 -05001458static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1459{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001460 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1461 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001462 if (state->n_wronly)
1463 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1464 if (state->n_rdonly)
1465 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 if (state->n_rdwr)
1467 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001468 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001469}
1470
Trond Myklebust226056c2014-02-11 10:41:07 -05001471static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1472 nfs4_stateid *stateid, fmode_t fmode)
1473{
1474 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1475 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1476 case FMODE_WRITE:
1477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1478 break;
1479 case FMODE_READ:
1480 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1481 break;
1482 case 0:
1483 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1484 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1485 clear_bit(NFS_OPEN_STATE, &state->flags);
1486 }
1487 if (stateid == NULL)
1488 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001489 /* Handle OPEN+OPEN_DOWNGRADE races */
1490 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1491 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001492 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001493 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001494 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001495 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001496 nfs4_stateid_copy(&state->stateid, stateid);
1497 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001498}
1499
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001500static void nfs_clear_open_stateid(struct nfs4_state *state,
1501 nfs4_stateid *arg_stateid,
1502 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001503{
1504 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001505 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1506 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1507 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001508 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1510 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001511}
1512
Trond Myklebust1393d962016-09-22 13:39:13 -04001513static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1514 const nfs4_stateid *stateid, fmode_t fmode,
1515 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001516{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001517 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001518 case FMODE_READ:
1519 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1520 break;
1521 case FMODE_WRITE:
1522 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1523 break;
1524 case FMODE_READ|FMODE_WRITE:
1525 set_bit(NFS_O_RDWR_STATE, &state->flags);
1526 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001527 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001528 return;
1529 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1530 nfs4_stateid_copy(&state->stateid, stateid);
1531 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001532}
1533
Trond Myklebust1393d962016-09-22 13:39:13 -04001534static void __update_open_stateid(struct nfs4_state *state,
1535 const nfs4_stateid *open_stateid,
1536 const nfs4_stateid *deleg_stateid,
1537 fmode_t fmode,
1538 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001540 /*
1541 * Protect the call to nfs4_state_set_mode_locked and
1542 * serialise the stateid update
1543 */
Andrew Elble361cad32015-12-02 09:20:57 -05001544 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001545 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001546 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001547 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001548 set_bit(NFS_DELEGATED_STATE, &state->flags);
1549 }
1550 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001551 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001552 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001553 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001554 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Trond Myklebust1393d962016-09-22 13:39:13 -04001557static int update_open_stateid(struct nfs4_state *state,
1558 const nfs4_stateid *open_stateid,
1559 const nfs4_stateid *delegation,
1560 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001561{
Trond Myklebust1393d962016-09-22 13:39:13 -04001562 struct nfs_server *server = NFS_SERVER(state->inode);
1563 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001564 struct nfs_inode *nfsi = NFS_I(state->inode);
1565 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001566 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001567 int ret = 0;
1568
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001569 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001570
1571 rcu_read_lock();
1572 deleg_cur = rcu_dereference(nfsi->delegation);
1573 if (deleg_cur == NULL)
1574 goto no_delegation;
1575
1576 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001577 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001578 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001579 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001580 goto no_delegation_unlock;
1581
1582 if (delegation == NULL)
1583 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001584 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001585 goto no_delegation_unlock;
1586
Trond Myklebustb7391f42008-12-23 15:21:52 -05001587 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001588 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1589 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001590 ret = 1;
1591no_delegation_unlock:
1592 spin_unlock(&deleg_cur->lock);
1593no_delegation:
1594 rcu_read_unlock();
1595
1596 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001597 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001598 ret = 1;
1599 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001600 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001601 nfs4_schedule_state_manager(clp);
1602 if (freeme.type != 0)
1603 nfs4_test_and_free_stateid(server, &freeme,
1604 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001605
1606 return ret;
1607}
1608
Trond Myklebust39071e62015-01-24 15:07:56 -05001609static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1610 const nfs4_stateid *stateid)
1611{
1612 struct nfs4_state *state = lsp->ls_state;
1613 bool ret = false;
1614
1615 spin_lock(&state->state_lock);
1616 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1617 goto out_noupdate;
1618 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1619 goto out_noupdate;
1620 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1621 ret = true;
1622out_noupdate:
1623 spin_unlock(&state->state_lock);
1624 return ret;
1625}
Trond Myklebust34310432008-12-23 15:21:38 -05001626
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001627static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628{
1629 struct nfs_delegation *delegation;
1630
1631 rcu_read_lock();
1632 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001633 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 rcu_read_unlock();
1635 return;
1636 }
1637 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001638 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639}
1640
Trond Myklebust6ee41262007-07-08 14:11:36 -04001641static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642{
1643 struct nfs4_state *state = opendata->state;
1644 struct nfs_inode *nfsi = NFS_I(state->inode);
1645 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001646 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001648 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001649 nfs4_stateid stateid;
1650 int ret = -EAGAIN;
1651
Trond Myklebustaac00a82007-07-05 19:02:21 -04001652 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001653 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001654 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001655 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001656 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001657 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001658 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001659 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001660 rcu_read_lock();
1661 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001662 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001663 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001664 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001665 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001666 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001667 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001669 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001670 if (!opendata->is_recover) {
1671 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1672 if (ret != 0)
1673 goto out;
1674 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001676
1677 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001678 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001679 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001680 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681out:
1682 return ERR_PTR(ret);
1683out_return_state:
1684 atomic_inc(&state->count);
1685 return state;
1686}
1687
Andy Adamsone23008e2012-10-02 21:07:32 -04001688static void
1689nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1690{
1691 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1692 struct nfs_delegation *delegation;
1693 int delegation_flags = 0;
1694
1695 rcu_read_lock();
1696 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1697 if (delegation)
1698 delegation_flags = delegation->flags;
1699 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001700 switch (data->o_arg.claim) {
1701 default:
1702 break;
1703 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1704 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1706 "returning a delegation for "
1707 "OPEN(CLAIM_DELEGATE_CUR)\n",
1708 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001709 return;
1710 }
1711 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001712 nfs_inode_set_delegation(state->inode,
1713 data->owner->so_cred,
1714 &data->o_res);
1715 else
1716 nfs_inode_reclaim_delegation(state->inode,
1717 data->owner->so_cred,
1718 &data->o_res);
1719}
1720
1721/*
1722 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1723 * and update the nfs4_state.
1724 */
1725static struct nfs4_state *
1726_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1727{
1728 struct inode *inode = data->state->inode;
1729 struct nfs4_state *state = data->state;
1730 int ret;
1731
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001732 if (!data->rpc_done) {
1733 if (data->rpc_status) {
1734 ret = data->rpc_status;
1735 goto err;
1736 }
1737 /* cached opens have already been processed */
1738 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001739 }
1740
Andy Adamsone23008e2012-10-02 21:07:32 -04001741 ret = nfs_refresh_inode(inode, &data->f_attr);
1742 if (ret)
1743 goto err;
1744
1745 if (data->o_res.delegation_type != 0)
1746 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001747update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001748 update_open_stateid(state, &data->o_res.stateid, NULL,
1749 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001750 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001751
1752 return state;
1753err:
1754 return ERR_PTR(ret);
1755
1756}
1757
1758static struct nfs4_state *
1759_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001760{
1761 struct inode *inode;
1762 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001763 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764
Trond Myklebustaac00a82007-07-05 19:02:21 -04001765 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001766 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001767 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001768 goto out;
1769 }
1770
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001771 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001773 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001774 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001775 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001776 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001777 goto err;
1778 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 state = nfs4_get_open_state(inode, data->owner);
1780 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001781 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001782 if (data->o_res.delegation_type != 0)
1783 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001784 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001785 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001787out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001788 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001789 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001790err_put_inode:
1791 iput(inode);
1792err:
1793 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001794}
1795
Andy Adamsone23008e2012-10-02 21:07:32 -04001796static struct nfs4_state *
1797nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1798{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001799 struct nfs4_state *ret;
1800
Andy Adamsone23008e2012-10-02 21:07:32 -04001801 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001802 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1803 else
1804 ret = _nfs4_opendata_to_nfs4_state(data);
1805 nfs4_sequence_free_slot(&data->o_res.seq_res);
1806 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001807}
1808
Trond Myklebust864472e2006-01-03 09:55:15 +01001809static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1810{
1811 struct nfs_inode *nfsi = NFS_I(state->inode);
1812 struct nfs_open_context *ctx;
1813
1814 spin_lock(&state->inode->i_lock);
1815 list_for_each_entry(ctx, &nfsi->open_files, list) {
1816 if (ctx->state != state)
1817 continue;
1818 get_nfs_open_context(ctx);
1819 spin_unlock(&state->inode->i_lock);
1820 return ctx;
1821 }
1822 spin_unlock(&state->inode->i_lock);
1823 return ERR_PTR(-ENOENT);
1824}
1825
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001826static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1827 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001828{
1829 struct nfs4_opendata *opendata;
1830
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001831 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001832 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001833 if (opendata == NULL)
1834 return ERR_PTR(-ENOMEM);
1835 opendata->state = state;
1836 atomic_inc(&state->count);
1837 return opendata;
1838}
1839
Trond Myklebust24311f82015-09-20 10:50:17 -04001840static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1841 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001842{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001843 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001844 int ret;
1845
Trond Myklebust24311f82015-09-20 10:50:17 -04001846 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001847 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001848 opendata->o_arg.open_flags = 0;
1849 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001850 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1851 NFS_SB(opendata->dentry->d_sb),
1852 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001853 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1854 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1855 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001856 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001857 if (ret != 0)
1858 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001859 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001860 if (IS_ERR(newstate))
1861 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001862 if (newstate != opendata->state)
1863 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001864 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001865 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001866}
1867
1868static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1869{
Trond Myklebust864472e2006-01-03 09:55:15 +01001870 int ret;
1871
Trond Myklebust4f14c192014-02-12 19:15:06 -05001872 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1873 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1874 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1875 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001876 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001877 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001878 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001879 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001880 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1881 if (ret != 0)
1882 return ret;
1883 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1884 if (ret != 0)
1885 return ret;
1886 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1887 if (ret != 0)
1888 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001889 /*
1890 * We may have performed cached opens for all three recoveries.
1891 * Check if we need to update the current stateid.
1892 */
1893 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001894 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001895 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001896 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001897 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001898 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001899 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001900 return 0;
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
1904 * OPEN_RECLAIM:
1905 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001907static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001909 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001910 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001911 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 int status;
1913
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001914 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1915 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001916 if (IS_ERR(opendata))
1917 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001918 rcu_read_lock();
1919 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001920 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001921 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001922 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001923 opendata->o_arg.u.delegation_type = delegation_type;
1924 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001925 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return status;
1927}
1928
Trond Myklebust539cd032007-06-05 11:46:42 -04001929static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 struct nfs_server *server = NFS_SERVER(state->inode);
1932 struct nfs4_exception exception = { };
1933 int err;
1934 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001935 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001936 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001937 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1938 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001939 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001940 break;
1941 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 } while (exception.retry);
1943 return err;
1944}
1945
Trond Myklebust864472e2006-01-03 09:55:15 +01001946static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1947{
1948 struct nfs_open_context *ctx;
1949 int ret;
1950
1951 ctx = nfs4_state_find_open_context(state);
1952 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001953 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001954 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001955 put_nfs_open_context(ctx);
1956 return ret;
1957}
1958
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001959static 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 -07001960{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001961 switch (err) {
1962 default:
1963 printk(KERN_ERR "NFS: %s: unhandled error "
1964 "%d.\n", __func__, err);
1965 case 0:
1966 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001967 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001968 case -ESTALE:
1969 break;
1970 case -NFS4ERR_BADSESSION:
1971 case -NFS4ERR_BADSLOT:
1972 case -NFS4ERR_BAD_HIGH_SLOT:
1973 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1974 case -NFS4ERR_DEADSESSION:
1975 set_bit(NFS_DELEGATED_STATE, &state->flags);
1976 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1977 return -EAGAIN;
1978 case -NFS4ERR_STALE_CLIENTID:
1979 case -NFS4ERR_STALE_STATEID:
1980 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001981 /* Don't recall a delegation if it was lost */
1982 nfs4_schedule_lease_recovery(server->nfs_client);
1983 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001984 case -NFS4ERR_MOVED:
1985 nfs4_schedule_migration_recovery(server);
1986 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001987 case -NFS4ERR_LEASE_MOVED:
1988 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1989 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001990 case -NFS4ERR_DELEG_REVOKED:
1991 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001992 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001993 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001994 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001995 nfs_inode_find_state_and_recover(state->inode,
1996 stateid);
1997 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001998 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001999 case -NFS4ERR_DELAY:
2000 case -NFS4ERR_GRACE:
2001 set_bit(NFS_DELEGATED_STATE, &state->flags);
2002 ssleep(1);
2003 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002004 case -ENOMEM:
2005 case -NFS4ERR_DENIED:
2006 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2007 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return err;
2010}
2011
Trond Myklebust24311f82015-09-20 10:50:17 -04002012int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2013 struct nfs4_state *state, const nfs4_stateid *stateid,
2014 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002015{
2016 struct nfs_server *server = NFS_SERVER(state->inode);
2017 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002018 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002019
2020 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2021 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2022 if (IS_ERR(opendata))
2023 return PTR_ERR(opendata);
2024 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002025 write_seqlock(&state->seqlock);
2026 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2027 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002028 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2029 switch (type & (FMODE_READ|FMODE_WRITE)) {
2030 case FMODE_READ|FMODE_WRITE:
2031 case FMODE_WRITE:
2032 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2033 if (err)
2034 break;
2035 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2036 if (err)
2037 break;
2038 case FMODE_READ:
2039 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2040 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002041 nfs4_opendata_put(opendata);
2042 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2043}
2044
Chuck Leverbe05c862013-08-09 12:49:47 -04002045static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2046{
2047 struct nfs4_opendata *data = calldata;
2048
Peng Taocb04ad22014-06-11 05:24:15 +08002049 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2050 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002051}
2052
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002053static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2054{
2055 struct nfs4_opendata *data = calldata;
2056
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002057 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002058
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002059 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002060 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002061 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002062 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002063 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002064 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002065 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002066}
2067
2068static void nfs4_open_confirm_release(void *calldata)
2069{
2070 struct nfs4_opendata *data = calldata;
2071 struct nfs4_state *state = NULL;
2072
2073 /* If this request hasn't been cancelled, do nothing */
2074 if (data->cancelled == 0)
2075 goto out_free;
2076 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002077 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002078 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002079 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002080 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002081 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002082out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002083 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002084}
2085
2086static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002087 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002088 .rpc_call_done = nfs4_open_confirm_done,
2089 .rpc_release = nfs4_open_confirm_release,
2090};
2091
2092/*
2093 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2094 */
2095static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2096{
David Howells2b0143b2015-03-17 22:25:59 +00002097 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002098 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002099 struct rpc_message msg = {
2100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2101 .rpc_argp = &data->c_arg,
2102 .rpc_resp = &data->c_res,
2103 .rpc_cred = data->owner->so_cred,
2104 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002105 struct rpc_task_setup task_setup_data = {
2106 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002107 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002108 .callback_ops = &nfs4_open_confirm_ops,
2109 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002110 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002111 .flags = RPC_TASK_ASYNC,
2112 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int status;
2114
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002115 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002116 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002117 data->rpc_done = 0;
2118 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002119 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002120 if (data->is_recover)
2121 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002122 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002123 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002124 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002125 status = nfs4_wait_for_completion_rpc_task(task);
2126 if (status != 0) {
2127 data->cancelled = 1;
2128 smp_wmb();
2129 } else
2130 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002131 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return status;
2133}
2134
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002135static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002137 struct nfs4_opendata *data = calldata;
2138 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002139 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002140 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002141
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002142 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002143 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002144 /*
2145 * Check if we still need to send an OPEN call, or if we can use
2146 * a delegation instead.
2147 */
2148 if (data->state != NULL) {
2149 struct nfs_delegation *delegation;
2150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002151 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002152 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002153 rcu_read_lock();
2154 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002155 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002156 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002157 rcu_read_unlock();
2158 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002159 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002160 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002161 switch (claim) {
2162 default:
2163 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002164 case NFS4_OPEN_CLAIM_PREVIOUS:
2165 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2166 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002167 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002168 case NFS4_OPEN_CLAIM_FH:
2169 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002170 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2171 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002172 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002173 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002174 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002175 &data->o_res.seq_res,
2176 task) != 0)
2177 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002178
2179 /* Set the create mode (note dependency on the session type) */
2180 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2181 if (data->o_arg.open_flags & O_EXCL) {
2182 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2183 if (nfs4_has_persistent_session(clp))
2184 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2185 else if (clp->cl_mvops->minor_version > 0)
2186 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2187 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002188 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002189unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002190 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002191 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002192out_no_action:
2193 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002194out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002195 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002196}
2197
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198static void nfs4_open_done(struct rpc_task *task, void *calldata)
2199{
2200 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002203
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002204 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002205 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002206
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002208 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2209 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210 case S_IFREG:
2211 break;
2212 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002213 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002214 break;
2215 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002216 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002217 break;
2218 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002219 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002220 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002221 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002222 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002223 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2224 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002225 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002226 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002227}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002228
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002229static void nfs4_open_release(void *calldata)
2230{
2231 struct nfs4_opendata *data = calldata;
2232 struct nfs4_state *state = NULL;
2233
2234 /* If this request hasn't been cancelled, do nothing */
2235 if (data->cancelled == 0)
2236 goto out_free;
2237 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002238 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239 goto out_free;
2240 /* In case we need an open_confirm, no cleanup! */
2241 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2242 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002243 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002244 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002245 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002246out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002247 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248}
2249
2250static const struct rpc_call_ops nfs4_open_ops = {
2251 .rpc_call_prepare = nfs4_open_prepare,
2252 .rpc_call_done = nfs4_open_done,
2253 .rpc_release = nfs4_open_release,
2254};
2255
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002256static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002257{
David Howells2b0143b2015-03-17 22:25:59 +00002258 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002259 struct nfs_server *server = NFS_SERVER(dir);
2260 struct nfs_openargs *o_arg = &data->o_arg;
2261 struct nfs_openres *o_res = &data->o_res;
2262 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2265 .rpc_argp = o_arg,
2266 .rpc_resp = o_res,
2267 .rpc_cred = data->owner->so_cred,
2268 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002269 struct rpc_task_setup task_setup_data = {
2270 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002271 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002272 .callback_ops = &nfs4_open_ops,
2273 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002274 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002275 .flags = RPC_TASK_ASYNC,
2276 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277 int status;
2278
Chuck Levera9c92d62013-08-09 12:48:18 -04002279 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002280 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002281 data->rpc_done = 0;
2282 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002283 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002284 data->is_recover = 0;
2285 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002286 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002287 data->is_recover = 1;
2288 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002289 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002290 if (IS_ERR(task))
2291 return PTR_ERR(task);
2292 status = nfs4_wait_for_completion_rpc_task(task);
2293 if (status != 0) {
2294 data->cancelled = 1;
2295 smp_wmb();
2296 } else
2297 status = data->rpc_status;
2298 rpc_put_task(task);
2299
2300 return status;
2301}
2302
2303static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2304{
David Howells2b0143b2015-03-17 22:25:59 +00002305 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002306 struct nfs_openres *o_res = &data->o_res;
2307 int status;
2308
2309 status = nfs4_run_open_task(data, 1);
2310 if (status != 0 || !data->rpc_done)
2311 return status;
2312
Trond Myklebust6926afd2012-01-07 13:22:46 -05002313 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2314
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002315 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2316 status = _nfs4_proc_open_confirm(data);
2317 if (status != 0)
2318 return status;
2319 }
2320
2321 return status;
2322}
2323
Trond Myklebustf3792d62014-07-10 08:54:32 -04002324/*
2325 * Additional permission checks in order to distinguish between an
2326 * open for read, and an open for execute. This works around the
2327 * fact that NFSv4 OPEN treats read and execute permissions as being
2328 * the same.
2329 * Note that in the non-execute case, we want to turn off permission
2330 * checking if we just created a new file (POSIX open() semantics).
2331 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002332static int nfs4_opendata_access(struct rpc_cred *cred,
2333 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002334 struct nfs4_state *state, fmode_t fmode,
2335 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002336{
2337 struct nfs_access_entry cache;
2338 u32 mask;
2339
2340 /* access call failed or for some reason the server doesn't
2341 * support any access modes -- defer access call until later */
2342 if (opendata->o_res.access_supported == 0)
2343 return 0;
2344
2345 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002346 /*
2347 * Use openflags to check for exec, because fmode won't
2348 * always have FMODE_EXEC set when file open for exec.
2349 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002350 if (openflags & __FMODE_EXEC) {
2351 /* ONLY check for exec rights */
2352 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002353 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002354 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002355
2356 cache.cred = cred;
2357 cache.jiffies = jiffies;
2358 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2359 nfs_access_add_cache(state->inode, &cache);
2360
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002361 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002362 return 0;
2363
2364 /* even though OPEN succeeded, access is denied. Close the file */
2365 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002366 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002367}
2368
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002369/*
2370 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2371 */
2372static int _nfs4_proc_open(struct nfs4_opendata *data)
2373{
David Howells2b0143b2015-03-17 22:25:59 +00002374 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002375 struct nfs_server *server = NFS_SERVER(dir);
2376 struct nfs_openargs *o_arg = &data->o_arg;
2377 struct nfs_openres *o_res = &data->o_res;
2378 int status;
2379
2380 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002381 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002382 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002383 if (status != 0) {
2384 if (status == -NFS4ERR_BADNAME &&
2385 !(o_arg->open_flags & O_CREAT))
2386 return -ENOENT;
2387 return status;
2388 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002389
Trond Myklebust6926afd2012-01-07 13:22:46 -05002390 nfs_fattr_map_and_free_names(server, &data->f_attr);
2391
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002392 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002393 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002394 if (o_arg->open_flags & O_EXCL)
2395 data->file_created = 1;
2396 else if (o_res->cinfo.before != o_res->cinfo.after)
2397 data->file_created = 1;
2398 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002399 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2400 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002402 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002404 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002407 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Andy Adamsond83217c2011-03-01 01:34:17 +00002411static int nfs4_recover_expired_lease(struct nfs_server *server)
2412{
2413 return nfs4_client_recover_expired_lease(server->nfs_client);
2414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416/*
2417 * OPEN_EXPIRED:
2418 * reclaim state on the server after a network partition.
2419 * Assumes caller holds the appropriate lock
2420 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002421static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002423 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002424 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002426 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002427 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002428 if (IS_ERR(opendata))
2429 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002430 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002431 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002432 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002433 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002434 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002437static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002438{
Trond Myklebust539cd032007-06-05 11:46:42 -04002439 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002440 struct nfs4_exception exception = { };
2441 int err;
2442
2443 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002444 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002445 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002446 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2447 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002448 switch (err) {
2449 default:
2450 goto out;
2451 case -NFS4ERR_GRACE:
2452 case -NFS4ERR_DELAY:
2453 nfs4_handle_exception(server, err, &exception);
2454 err = 0;
2455 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002456 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002457out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002458 return err;
2459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002464 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Trond Myklebust864472e2006-01-03 09:55:15 +01002466 ctx = nfs4_state_find_open_context(state);
2467 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002468 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002469 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002470 put_nfs_open_context(ctx);
2471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
Trond Myklebust41020b62016-09-22 13:38:58 -04002474static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2475 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002476{
Trond Myklebust41020b62016-09-22 13:38:58 -04002477 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002478 write_seqlock(&state->seqlock);
2479 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2480 write_sequnlock(&state->seqlock);
2481 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2482}
2483
2484static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2485{
2486 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002487 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002488}
2489
2490static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2491{
2492 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2493 nfs40_clear_delegation_stateid(state);
2494 return nfs4_open_expired(sp, state);
2495}
2496
Trond Myklebust45870d62016-09-22 13:38:59 -04002497static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2498 nfs4_stateid *stateid,
2499 struct rpc_cred *cred)
2500{
2501 return -NFS4ERR_BAD_STATEID;
2502}
2503
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002504#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002505static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2506 nfs4_stateid *stateid,
2507 struct rpc_cred *cred)
2508{
2509 int status;
2510
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002511 switch (stateid->type) {
2512 default:
2513 break;
2514 case NFS4_INVALID_STATEID_TYPE:
2515 case NFS4_SPECIAL_STATEID_TYPE:
2516 return -NFS4ERR_BAD_STATEID;
2517 case NFS4_REVOKED_STATEID_TYPE:
2518 goto out_free;
2519 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002520
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002521 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002522 switch (status) {
2523 case -NFS4ERR_EXPIRED:
2524 case -NFS4ERR_ADMIN_REVOKED:
2525 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002526 break;
2527 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002528 return status;
2529 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002530out_free:
2531 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002532 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002533 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002534}
2535
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002536static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002537{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002538 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002539 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002540 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002541 struct rpc_cred *cred;
2542 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002543
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002544 /* Get the delegation credential for use by test/free_stateid */
2545 rcu_read_lock();
2546 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002547 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002548 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002549 return;
2550 }
2551
2552 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002553 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2554 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002555 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002556 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002557 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002558
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002559 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2560 rcu_read_unlock();
2561 return;
2562 }
2563
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002564 cred = get_rpccred(delegation->cred);
2565 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002566 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002567 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002568 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002569 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002570
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002571 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002572}
2573
2574/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002575 * nfs41_check_expired_locks - possibly free a lock stateid
2576 *
2577 * @state: NFSv4 state for an inode
2578 *
2579 * Returns NFS_OK if recovery for this stateid is now finished.
2580 * Otherwise a negative NFS4ERR value is returned.
2581 */
2582static int nfs41_check_expired_locks(struct nfs4_state *state)
2583{
2584 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002585 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002586 struct nfs_server *server = NFS_SERVER(state->inode);
2587
2588 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2589 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002590
2591 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002592 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2593 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2594 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2595
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002596 atomic_inc(&lsp->ls_count);
2597 spin_unlock(&state->state_lock);
2598
2599 nfs4_put_lock_state(prev);
2600 prev = lsp;
2601
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002602 status = nfs41_test_and_free_expired_stateid(server,
2603 &lsp->ls_stateid,
2604 cred);
2605 trace_nfs4_test_lock_stateid(state, lsp, status);
2606 if (status == -NFS4ERR_EXPIRED ||
2607 status == -NFS4ERR_BAD_STATEID) {
2608 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002609 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002610 if (!recover_lost_locks)
2611 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2612 } else if (status != NFS_OK) {
2613 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002614 nfs4_put_lock_state(prev);
2615 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002616 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002617 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002618 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002619 }
2620 spin_unlock(&state->state_lock);
2621 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002622out:
2623 return ret;
2624}
2625
2626/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002627 * nfs41_check_open_stateid - possibly free an open stateid
2628 *
2629 * @state: NFSv4 state for an inode
2630 *
2631 * Returns NFS_OK if recovery for this stateid is now finished.
2632 * Otherwise a negative NFS4ERR value is returned.
2633 */
2634static int nfs41_check_open_stateid(struct nfs4_state *state)
2635{
2636 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002637 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002638 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002639 int status;
2640
Trond Myklebustb134fc42016-09-22 13:39:16 -04002641 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002642 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2643 if (nfs4_have_delegation(state->inode, state->state))
2644 return NFS_OK;
2645 return -NFS4ERR_OPENMODE;
2646 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002647 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002648 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002649 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002650 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002651 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002652 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2653 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2654 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002655 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002656 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002657 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002658 if (status != NFS_OK)
2659 return status;
2660 if (nfs_open_stateid_recover_openmode(state))
2661 return -NFS4ERR_OPENMODE;
2662 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002663}
2664
2665static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2666{
Chuck Levereb64cf92012-07-11 16:30:05 -04002667 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002668
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002669 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002670 status = nfs41_check_expired_locks(state);
2671 if (status != NFS_OK)
2672 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002673 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002674 if (status != NFS_OK)
2675 status = nfs4_open_expired(sp, state);
2676 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002677}
2678#endif
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002681 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2682 * fields corresponding to attributes that were used to store the verifier.
2683 * Make sure we clobber those fields in the later setattr call
2684 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002685static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2686 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002687{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002688 const u32 *attrset = opendata->o_res.attrset;
2689
2690 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002691 !(sattr->ia_valid & ATTR_ATIME_SET))
2692 sattr->ia_valid |= ATTR_ATIME;
2693
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002694 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002695 !(sattr->ia_valid & ATTR_MTIME_SET))
2696 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002697
2698 /* Except MODE, it seems harmless of setting twice. */
2699 if ((attrset[1] & FATTR4_WORD1_MODE))
2700 sattr->ia_valid &= ~ATTR_MODE;
2701
2702 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2703 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002704}
2705
Trond Myklebustc21443c2013-02-07 14:26:21 -05002706static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2707 fmode_t fmode,
2708 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002709 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002710{
2711 struct nfs4_state_owner *sp = opendata->owner;
2712 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002713 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002714 struct nfs4_state *state;
2715 unsigned int seq;
2716 int ret;
2717
2718 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2719
2720 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002721 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002722 goto out;
2723
2724 state = nfs4_opendata_to_nfs4_state(opendata);
2725 ret = PTR_ERR(state);
2726 if (IS_ERR(state))
2727 goto out;
2728 if (server->caps & NFS_CAP_POSIX_LOCK)
2729 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002730 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2731 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002732
Trond Myklebust275bb302013-05-29 13:11:28 -04002733 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002734 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002735 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002736 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002737 alias = d_exact_alias(dentry, state->inode);
2738 if (!alias)
2739 alias = d_splice_alias(igrab(state->inode), dentry);
2740 /* d_splice_alias() can't fail here - it's a non-directory */
2741 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002742 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002743 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002744 }
2745 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002746 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002747 }
2748
Trond Myklebustc21443c2013-02-07 14:26:21 -05002749 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2750 if (ret != 0)
2751 goto out;
2752
Trond Myklebust3efb9722013-05-29 13:17:04 -04002753 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002754 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002755 nfs_inode_attach_open_context(ctx);
2756 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2757 nfs4_schedule_stateid_recovery(server, state);
2758 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002759out:
2760 return ret;
2761}
2762
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002763/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002764 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 */
Andy Adamson82be4172012-05-23 05:02:35 -04002766static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002767 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002768 int flags,
2769 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002770 struct nfs4_label *label,
2771 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 struct nfs4_state_owner *sp;
2774 struct nfs4_state *state = NULL;
2775 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002776 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002777 struct dentry *dentry = ctx->dentry;
2778 struct rpc_cred *cred = ctx->cred;
2779 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2780 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002781 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002782 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002783 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002787 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2788 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2790 goto out_err;
2791 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002792 status = nfs4_recover_expired_lease(server);
2793 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002794 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002795 if (d_really_is_positive(dentry))
2796 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002797 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002798 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002799 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002800 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002801 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002802 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002803 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
David Quigley14c43f72013-05-22 12:50:43 -04002805 if (label) {
2806 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2807 if (IS_ERR(olabel)) {
2808 status = PTR_ERR(olabel);
2809 goto err_opendata_put;
2810 }
2811 }
2812
Trond Myklebuste911b812014-03-26 13:24:37 -07002813 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2814 if (!opendata->f_attr.mdsthreshold) {
2815 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2816 if (!opendata->f_attr.mdsthreshold)
2817 goto err_free_label;
2818 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002819 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002820 }
David Howells2b0143b2015-03-17 22:25:59 +00002821 if (d_really_is_positive(dentry))
2822 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002823
Trond Myklebust3efb9722013-05-29 13:17:04 -04002824 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002825 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002826 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002827 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002828
NeilBrownefcbc042015-07-30 13:00:56 +10002829 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002830 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002831 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002832 /*
2833 * send create attributes which was not set by open
2834 * with an extra setattr.
2835 */
2836 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2837 nfs_fattr_init(opendata->o_res.f_attr);
2838 status = nfs4_do_setattr(state->inode, cred,
2839 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002840 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002841 if (status == 0) {
2842 nfs_setattr_update_inode(state->inode, sattr,
2843 opendata->o_res.f_attr);
2844 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2845 }
David Quigley1775fd32013-05-22 12:50:42 -04002846 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002847 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002848 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002849 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002850
Trond Myklebuste911b812014-03-26 13:24:37 -07002851 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002852 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002853 opendata->f_attr.mdsthreshold = NULL;
2854 }
Andy Adamson82be4172012-05-23 05:02:35 -04002855
David Quigley14c43f72013-05-22 12:50:43 -04002856 nfs4_label_free(olabel);
2857
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002858 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002861err_free_label:
2862 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002863err_opendata_put:
2864 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002865err_put_state_owner:
2866 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return status;
2869}
2870
2871
Andy Adamson82be4172012-05-23 05:02:35 -04002872static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002873 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002874 int flags,
2875 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002876 struct nfs4_label *label,
2877 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002879 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct nfs4_exception exception = { };
2881 struct nfs4_state *res;
2882 int status;
2883
2884 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002885 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002886 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002887 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (status == 0)
2889 break;
2890 /* NOTE: BAD_SEQID means the server and client disagree about the
2891 * book-keeping w.r.t. state-changing operations
2892 * (OPEN/CLOSE/LOCK/LOCKU...)
2893 * It is actually a sign of a bug on the client or on the server.
2894 *
2895 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002896 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 * have unhashed the old state_owner for us, and that we can
2898 * therefore safely retry using a new one. We should still warn
2899 * the user though...
2900 */
2901 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002902 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002903 " returned a bad sequence-id error!\n",
2904 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 exception.retry = 1;
2906 continue;
2907 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002908 /*
2909 * BAD_STATEID on OPEN means that the server cancelled our
2910 * state before it received the OPEN_CONFIRM.
2911 * Recover by retrying the request as per the discussion
2912 * on Page 181 of RFC3530.
2913 */
2914 if (status == -NFS4ERR_BAD_STATEID) {
2915 exception.retry = 1;
2916 continue;
2917 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002918 if (status == -EAGAIN) {
2919 /* We must have found a delegation */
2920 exception.retry = 1;
2921 continue;
2922 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002923 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2924 continue;
2925 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 status, &exception));
2927 } while (exception.retry);
2928 return res;
2929}
2930
Trond Myklebust8487c472016-06-26 08:44:35 -04002931static int _nfs4_do_setattr(struct inode *inode,
2932 struct nfs_setattrargs *arg,
2933 struct nfs_setattrres *res,
2934 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002935 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002936{
2937 struct nfs_server *server = NFS_SERVER(inode);
2938 struct rpc_message msg = {
2939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2940 .rpc_argp = arg,
2941 .rpc_resp = res,
2942 .rpc_cred = cred,
2943 };
2944 struct rpc_cred *delegation_cred = NULL;
2945 unsigned long timestamp = jiffies;
2946 fmode_t fmode;
2947 bool truncate;
2948 int status;
2949
2950 nfs_fattr_init(res->fattr);
2951
2952 /* Servers should only apply open mode checks for file size changes */
2953 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2954 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2955
2956 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2957 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002958 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002959 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002960 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002961 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002962 l_ctx = nfs_get_lock_context(ctx);
2963 if (IS_ERR(l_ctx))
2964 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002965 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2966 &arg->stateid, &delegation_cred);
2967 nfs_put_lock_context(l_ctx);
2968 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002969 return -EBADF;
2970 } else
2971 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2972 if (delegation_cred)
2973 msg.rpc_cred = delegation_cred;
2974
2975 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2976
2977 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002978 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002979 renew_lease(server, timestamp);
2980 trace_nfs4_setattr(inode, &arg->stateid, status);
2981 return status;
2982}
2983
2984static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2985 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002986 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002987 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002989 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002990 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002992 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 .iap = sattr,
2994 .server = server,
2995 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002996 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
2998 struct nfs_setattrres res = {
2999 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003000 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 .server = server,
3002 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003003 struct nfs4_exception exception = {
3004 .state = state,
3005 .inode = inode,
3006 .stateid = &arg.stateid,
3007 };
3008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
David Quigleyaa9c2662013-05-22 12:50:44 -04003010 arg.bitmask = nfs4_bitmask(server, ilabel);
3011 if (ilabel)
3012 arg.bitmask = nfs4_bitmask(server, olabel);
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003015 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003016 switch (err) {
3017 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003018 if (!(sattr->ia_valid & ATTR_SIZE)) {
3019 pr_warn_once("NFSv4: server %s is incorrectly "
3020 "applying open mode checks to "
3021 "a SETATTR that is not "
3022 "changing file size.\n",
3023 server->nfs_client->cl_hostname);
3024 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003025 if (state && !(state->state & FMODE_WRITE)) {
3026 err = -EBADF;
3027 if (sattr->ia_valid & ATTR_OPEN)
3028 err = -EACCES;
3029 goto out;
3030 }
3031 }
3032 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003034out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return err;
3036}
3037
Peng Tao500d7012015-09-22 11:35:22 +08003038static bool
3039nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3040{
3041 if (inode == NULL || !nfs_have_layout(inode))
3042 return false;
3043
3044 return pnfs_wait_on_layoutreturn(inode, task);
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047struct nfs4_closedata {
3048 struct inode *inode;
3049 struct nfs4_state *state;
3050 struct nfs_closeargs arg;
3051 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003052 struct {
3053 struct nfs4_layoutreturn_args arg;
3054 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003055 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003056 u32 roc_barrier;
3057 bool roc;
3058 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003059 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003060 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061};
3062
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003063static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003064{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003065 struct nfs4_closedata *calldata = data;
3066 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003067 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003068
Trond Myklebustcf805162016-11-15 14:56:07 -05003069 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003070 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3071 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003072 nfs4_put_open_state(calldata->state);
3073 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003074 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003075 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003076 kfree(calldata);
3077}
3078
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003079static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003081 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003084 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Chuck Levera3ca5652012-03-01 17:00:40 -05003086 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003087 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3088 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003089 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003090
3091 /* Handle Layoutreturn errors */
3092 if (calldata->arg.lr_args && task->tk_status != 0) {
3093 switch (calldata->res.lr_ret) {
3094 default:
3095 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3096 break;
3097 case 0:
3098 calldata->arg.lr_args = NULL;
3099 calldata->res.lr_res = NULL;
3100 break;
3101 case -NFS4ERR_ADMIN_REVOKED:
3102 case -NFS4ERR_DELEG_REVOKED:
3103 case -NFS4ERR_EXPIRED:
3104 case -NFS4ERR_BAD_STATEID:
3105 case -NFS4ERR_OLD_STATEID:
3106 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3107 case -NFS4ERR_WRONG_CRED:
3108 calldata->arg.lr_args = NULL;
3109 calldata->res.lr_res = NULL;
3110 calldata->res.lr_ret = 0;
3111 rpc_restart_call_prepare(task);
3112 return;
3113 }
3114 }
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 /* hmm. we are done with the inode, and in the process of freeing
3117 * the state_owner. we keep this around to process errors
3118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 switch (task->tk_status) {
3120 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003121 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003122 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003123 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003124 case -NFS4ERR_ACCESS:
3125 if (calldata->arg.bitmask != NULL) {
3126 calldata->arg.bitmask = NULL;
3127 calldata->res.fattr = NULL;
3128 task->tk_status = 0;
3129 rpc_restart_call_prepare(task);
3130 goto out_release;
3131
3132 }
3133 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003134 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003136 case -NFS4ERR_EXPIRED:
3137 nfs4_free_revoked_stateid(server,
3138 &calldata->arg.stateid,
3139 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003140 case -NFS4ERR_OLD_STATEID:
3141 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003142 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003143 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003144 rpc_restart_call_prepare(task);
3145 goto out_release;
3146 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003147 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003150 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003151 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003152 goto out_release;
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003155 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3156 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003157out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003158 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003159 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003160 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003163static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003165 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003166 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003167 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003168 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003169 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003170
Chuck Levera3ca5652012-03-01 17:00:40 -05003171 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003173 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003174
Trond Myklebust88069f72009-12-08 08:33:16 -05003175 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003176 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003177 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3178 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3179 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003180 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003181 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003182 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003183 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003184 if (state->n_rdonly == 0)
3185 call_close |= is_rdonly;
3186 else if (is_rdonly)
3187 calldata->arg.fmode |= FMODE_READ;
3188 if (state->n_wronly == 0)
3189 call_close |= is_wronly;
3190 else if (is_wronly)
3191 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003192 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3193 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003194 } else if (is_rdwr)
3195 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3196
Trond Myklebust5cc78612016-11-14 11:19:56 -05003197 if (!nfs4_valid_open_stateid(state) ||
3198 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003199 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003200 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003201
3202 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003203 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003204 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003205 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003206
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003207 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003208 nfs_release_seqid(calldata->arg.seqid);
3209 goto out_wait;
3210 }
3211
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003212 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003213 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003214
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003215 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003216 /* Close-to-open cache consistency revalidation */
3217 if (!nfs4_have_delegation(inode, FMODE_READ))
3218 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3219 else
3220 calldata->arg.bitmask = NULL;
3221 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003222
Trond Myklebust6ae37332015-01-30 14:21:14 -05003223 calldata->arg.share_access =
3224 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3225 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003226
Trond Myklebustd8d84982016-12-19 12:14:44 -05003227 if (calldata->res.fattr == NULL)
3228 calldata->arg.bitmask = NULL;
3229 else if (calldata->arg.bitmask == NULL)
3230 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003231 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003232 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003233 &calldata->arg.seq_args,
3234 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003235 task) != 0)
3236 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003237 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003238 return;
3239out_no_action:
3240 task->tk_action = NULL;
3241out_wait:
3242 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003245static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003246 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003247 .rpc_call_done = nfs4_close_done,
3248 .rpc_release = nfs4_free_closedata,
3249};
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251/*
3252 * It is possible for data to be read/written from a mem-mapped file
3253 * after the sys_close call (which hits the vfs layer as a flush).
3254 * This means that we can't safely call nfsv4 close on a file until
3255 * the inode is cleared. This in turn means that we are not good
3256 * NFSv4 citizens - we do not indicate to the server to update the file's
3257 * share state even when we are done with one of the three share
3258 * stateid's in the inode.
3259 *
3260 * NOTE: Caller must be holding the sp->so_owner semaphore!
3261 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003262int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003264 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003265 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003267 struct nfs4_state_owner *sp = state->owner;
3268 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003269 struct rpc_message msg = {
3270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3271 .rpc_cred = state->owner->so_cred,
3272 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003273 struct rpc_task_setup task_setup_data = {
3274 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003275 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003276 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003277 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003278 .flags = RPC_TASK_ASYNC,
3279 };
Trond Myklebust95121352005-10-18 14:20:12 -07003280 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003282 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3283 &task_setup_data.rpc_client, &msg);
3284
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003285 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003287 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003288 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003289 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003291 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003293 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3294 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003295 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003296 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003297 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003298 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003299 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003300 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003301 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003302 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003303 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003304 calldata->lr.roc = pnfs_roc(state->inode,
3305 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3306 if (calldata->lr.roc) {
3307 calldata->arg.lr_args = &calldata->lr.arg;
3308 calldata->res.lr_res = &calldata->lr.res;
3309 }
Al Viro643168c2011-06-22 18:20:23 -04003310 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003311
Trond Myklebust1174dd12010-12-21 10:52:24 -05003312 msg.rpc_argp = &calldata->arg;
3313 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003314 task_setup_data.callback_data = calldata;
3315 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003316 if (IS_ERR(task))
3317 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003318 status = 0;
3319 if (wait)
3320 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003321 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003322 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003323out_free_calldata:
3324 kfree(calldata);
3325out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003326 nfs4_put_open_state(state);
3327 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003328 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
Trond Myklebust2b484292010-09-17 10:56:51 -04003331static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003332nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3333 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003336 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3337
3338 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Trond Myklebust565277f2007-10-15 18:17:53 -04003340 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003341 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003342
3343 nfs4_label_release_security(label);
3344
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003345 if (IS_ERR(state))
3346 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003347 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
Trond Myklebust1185a552009-12-03 15:54:02 -05003350static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003351{
3352 if (ctx->state == NULL)
3353 return;
3354 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003355 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003356 else
Al Viro643168c2011-06-22 18:20:23 -04003357 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Trond Myklebustb944dba2013-11-04 15:20:20 -05003360#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3361#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003362#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3365{
Kinglong Mee8c612822015-08-26 21:12:58 +08003366 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003367 struct nfs4_server_caps_arg args = {
3368 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003369 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003370 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 struct nfs4_server_caps_res res = {};
3372 struct rpc_message msg = {
3373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003374 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 .rpc_resp = &res,
3376 };
3377 int status;
3378
Kinglong Mee8c612822015-08-26 21:12:58 +08003379 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3380 FATTR4_WORD0_FH_EXPIRE_TYPE |
3381 FATTR4_WORD0_LINK_SUPPORT |
3382 FATTR4_WORD0_SYMLINK_SUPPORT |
3383 FATTR4_WORD0_ACLSUPPORT;
3384 if (minorversion)
3385 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3386
Bryan Schumaker7c513052011-03-24 17:12:24 +00003387 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003389 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003390 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003391 case 0:
3392 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3393 res.attr_bitmask[2] = 0;
3394 break;
3395 case 1:
3396 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3397 break;
3398 case 2:
3399 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003402 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3403 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3404 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3405 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003406 NFS_CAP_CTIME|NFS_CAP_MTIME|
3407 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003408 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3409 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 server->caps |= NFS_CAP_ACLS;
3411 if (res.has_links != 0)
3412 server->caps |= NFS_CAP_HARDLINKS;
3413 if (res.has_symlinks != 0)
3414 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003415 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3416 server->caps |= NFS_CAP_FILEID;
3417 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3418 server->caps |= NFS_CAP_MODE;
3419 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3420 server->caps |= NFS_CAP_NLINK;
3421 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3422 server->caps |= NFS_CAP_OWNER;
3423 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3424 server->caps |= NFS_CAP_OWNER_GROUP;
3425 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3426 server->caps |= NFS_CAP_ATIME;
3427 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3428 server->caps |= NFS_CAP_CTIME;
3429 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3430 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003431#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3432 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3433 server->caps |= NFS_CAP_SECURITY_LABEL;
3434#endif
3435 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3436 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003437 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003438
Trond Myklebusta65318b2009-03-11 14:10:28 -04003439 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3440 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3441 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003442 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003443 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3444 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003446 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 return status;
3450}
3451
Trond Myklebust55a97592006-06-09 09:34:19 -04003452int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
3454 struct nfs4_exception exception = { };
3455 int err;
3456 do {
3457 err = nfs4_handle_exception(server,
3458 _nfs4_server_capabilities(server, fhandle),
3459 &exception);
3460 } while (exception.retry);
3461 return err;
3462}
3463
3464static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3465 struct nfs_fsinfo *info)
3466{
David Quigleyaa9c2662013-05-22 12:50:44 -04003467 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003469 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 };
3471 struct nfs4_lookup_res res = {
3472 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003473 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 .fh = fhandle,
3475 };
3476 struct rpc_message msg = {
3477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3478 .rpc_argp = &args,
3479 .rpc_resp = &res,
3480 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003481
David Quigleyaa9c2662013-05-22 12:50:44 -04003482 bitmask[0] = nfs4_fattr_bitmap[0];
3483 bitmask[1] = nfs4_fattr_bitmap[1];
3484 /*
3485 * Process the label in the upcoming getfattr
3486 */
3487 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3488
Trond Myklebust0e574af2005-10-27 22:12:38 -04003489 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003490 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
3493static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3494 struct nfs_fsinfo *info)
3495{
3496 struct nfs4_exception exception = { };
3497 int err;
3498 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003499 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003500 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003501 switch (err) {
3502 case 0:
3503 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003504 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003505 default:
3506 err = nfs4_handle_exception(server, err, &exception);
3507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return err;
3511}
3512
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003513static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3514 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3515{
Trond Myklebustc2190662013-08-26 19:23:04 -04003516 struct rpc_auth_create_args auth_args = {
3517 .pseudoflavor = flavor,
3518 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003519 struct rpc_auth *auth;
3520 int ret;
3521
Trond Myklebustc2190662013-08-26 19:23:04 -04003522 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003523 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003524 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003525 goto out;
3526 }
3527 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003528out:
3529 return ret;
3530}
3531
Chuck Lever9a744ba2013-03-16 15:56:02 -04003532/*
3533 * Retry pseudoroot lookup with various security flavors. We do this when:
3534 *
3535 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3536 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3537 *
3538 * Returns zero on success, or a negative NFS4ERR value, or a
3539 * negative errno value.
3540 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003541static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003542 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003544 /* Per 3530bis 15.33.5 */
3545 static const rpc_authflavor_t flav_array[] = {
3546 RPC_AUTH_GSS_KRB5P,
3547 RPC_AUTH_GSS_KRB5I,
3548 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003549 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003550 RPC_AUTH_NULL,
3551 };
3552 int status = -EPERM;
3553 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003555 if (server->auth_info.flavor_len > 0) {
3556 /* try each flavor specified by user */
3557 for (i = 0; i < server->auth_info.flavor_len; i++) {
3558 status = nfs4_lookup_root_sec(server, fhandle, info,
3559 server->auth_info.flavors[i]);
3560 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3561 continue;
3562 break;
3563 }
3564 } else {
3565 /* no flavors specified by user, try default list */
3566 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3567 status = nfs4_lookup_root_sec(server, fhandle, info,
3568 flav_array[i]);
3569 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3570 continue;
3571 break;
3572 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003573 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003574
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003575 /*
3576 * -EACCESS could mean that the user doesn't have correct permissions
3577 * to access the mount. It could also mean that we tried to mount
3578 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3579 * existing mount programs don't handle -EACCES very well so it should
3580 * be mapped to -EPERM instead.
3581 */
3582 if (status == -EACCES)
3583 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003584 return status;
3585}
3586
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003587/**
3588 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3589 * @server: initialized nfs_server handle
3590 * @fhandle: we fill in the pseudo-fs root file handle
3591 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003592 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003593 *
3594 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003595 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003596int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003597 struct nfs_fsinfo *info,
3598 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003599{
Andre Przywarac7757072015-04-23 17:17:40 +01003600 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003601
Andre Przywarac7757072015-04-23 17:17:40 +01003602 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003603 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003604
3605 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003606 status = server->nfs_client->cl_mvops->find_root_sec(server,
3607 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (status == 0)
3610 status = nfs4_server_capabilities(server, fhandle);
3611 if (status == 0)
3612 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003613
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003614 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Bryan Schumakerbae36242012-05-10 15:07:31 -04003617static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3618 struct nfs_fsinfo *info)
3619{
3620 int error;
3621 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003622 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003623
3624 error = nfs4_server_capabilities(server, mntfh);
3625 if (error < 0) {
3626 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3627 return error;
3628 }
3629
David Quigley14c43f72013-05-22 12:50:43 -04003630 label = nfs4_label_alloc(server, GFP_KERNEL);
3631 if (IS_ERR(label))
3632 return PTR_ERR(label);
3633
David Quigley1775fd32013-05-22 12:50:42 -04003634 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003635 if (error < 0) {
3636 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003637 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003638 }
3639
3640 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3641 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3642 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3643
David Quigley14c43f72013-05-22 12:50:43 -04003644err_free_label:
3645 nfs4_label_free(label);
3646
Bryan Schumakerbae36242012-05-10 15:07:31 -04003647 return error;
3648}
3649
Manoj Naik6b97fd32006-06-09 09:34:29 -04003650/*
3651 * Get locations and (maybe) other attributes of a referral.
3652 * Note that we'll actually follow the referral later when
3653 * we detect fsid mismatch in inode revalidation
3654 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003655static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3656 const struct qstr *name, struct nfs_fattr *fattr,
3657 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003658{
3659 int status = -ENOMEM;
3660 struct page *page = NULL;
3661 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003662
3663 page = alloc_page(GFP_KERNEL);
3664 if (page == NULL)
3665 goto out;
3666 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3667 if (locations == NULL)
3668 goto out;
3669
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003670 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003671 if (status != 0)
3672 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003673
3674 /*
3675 * If the fsid didn't change, this is a migration event, not a
3676 * referral. Cause us to drop into the exception handler, which
3677 * will kick off migration recovery.
3678 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003679 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003680 dprintk("%s: server did not return a different fsid for"
3681 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003682 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003683 goto out;
3684 }
Andy Adamson533eb462011-06-13 18:25:56 -04003685 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3686 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003687
Andy Adamson533eb462011-06-13 18:25:56 -04003688 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003689 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003690 memset(fhandle, 0, sizeof(struct nfs_fh));
3691out:
3692 if (page)
3693 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003694 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003695 return status;
3696}
3697
David Quigley1775fd32013-05-22 12:50:42 -04003698static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3699 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 struct nfs4_getattr_arg args = {
3702 .fh = fhandle,
3703 .bitmask = server->attr_bitmask,
3704 };
3705 struct nfs4_getattr_res res = {
3706 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003707 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 .server = server,
3709 };
3710 struct rpc_message msg = {
3711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3712 .rpc_argp = &args,
3713 .rpc_resp = &res,
3714 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003715
3716 args.bitmask = nfs4_bitmask(server, label);
3717
Trond Myklebust0e574af2005-10-27 22:12:38 -04003718 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003719 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
David Quigley1775fd32013-05-22 12:50:42 -04003722static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3723 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 struct nfs4_exception exception = { };
3726 int err;
3727 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003728 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3729 trace_nfs4_getattr(server, fhandle, fattr, err);
3730 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 &exception);
3732 } while (exception.retry);
3733 return err;
3734}
3735
3736/*
3737 * The file is not closed if it is opened due to the a request to change
3738 * the size of the file. The open call will not be needed once the
3739 * VFS layer lookup-intents are implemented.
3740 *
3741 * Close is called when the inode is destroyed.
3742 * If we haven't opened the file for O_WRONLY, we
3743 * need to in the size_change case to obtain a stateid.
3744 *
3745 * Got race?
3746 * Because OPEN is always done by name in nfsv4, it is
3747 * possible that we opened a different file by the same
3748 * name. We can recognize this race condition, but we
3749 * can't do anything about it besides returning an error.
3750 *
3751 * This will be fixed with VFS changes (lookup-intent).
3752 */
3753static int
3754nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3755 struct iattr *sattr)
3756{
David Howells2b0143b2015-03-17 22:25:59 +00003757 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003758 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003759 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003760 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 int status;
3762
Peng Tao88ac8152014-09-12 11:04:10 +08003763 if (pnfs_ld_layoutret_on_setattr(inode) &&
3764 sattr->ia_valid & ATTR_SIZE &&
3765 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003766 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003767
Trond Myklebust0e574af2005-10-27 22:12:38 -04003768 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Andy Adamson26699402012-05-30 16:12:24 -04003770 /* Deal with open(O_TRUNC) */
3771 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003772 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003773
3774 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003775 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003776 return 0;
3777
Trond Myklebustd5308382005-11-04 15:33:38 -05003778 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003779 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003780
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003781 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003782 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003783 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003784 }
3785
David Quigley14c43f72013-05-22 12:50:43 -04003786 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3787 if (IS_ERR(label))
3788 return PTR_ERR(label);
3789
NeilBrown29b59f92016-10-13 15:26:47 +11003790 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003791 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003792 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003793 nfs_setsecurity(inode, fattr, label);
3794 }
David Quigley14c43f72013-05-22 12:50:43 -04003795 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 return status;
3797}
3798
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003799static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3800 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003801 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003802{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003803 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003804 int status;
3805 struct nfs4_lookup_arg args = {
3806 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003807 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003808 .name = name,
3809 };
3810 struct nfs4_lookup_res res = {
3811 .server = server,
3812 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003813 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003814 .fh = fhandle,
3815 };
3816 struct rpc_message msg = {
3817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3818 .rpc_argp = &args,
3819 .rpc_resp = &res,
3820 };
3821
David Quigleyaa9c2662013-05-22 12:50:44 -04003822 args.bitmask = nfs4_bitmask(server, label);
3823
David Howells2b3de442006-08-22 20:06:09 -04003824 nfs_fattr_init(fattr);
3825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003827 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 dprintk("NFS reply lookup: %d\n", status);
3829 return status;
3830}
3831
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003832static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003833{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003834 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003835 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003836 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3837 fattr->nlink = 2;
3838}
3839
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003840static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003841 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003842 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003843{
3844 struct nfs4_exception exception = { };
3845 struct rpc_clnt *client = *clnt;
3846 int err;
3847 do {
David Quigley1775fd32013-05-22 12:50:42 -04003848 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003849 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003850 switch (err) {
3851 case -NFS4ERR_BADNAME:
3852 err = -ENOENT;
3853 goto out;
3854 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003855 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003856 if (err == -NFS4ERR_MOVED)
3857 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003858 goto out;
3859 case -NFS4ERR_WRONGSEC:
3860 err = -EPERM;
3861 if (client != *clnt)
3862 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003863 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003864 if (IS_ERR(client))
3865 return PTR_ERR(client);
3866
3867 exception.retry = 1;
3868 break;
3869 default:
3870 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3871 }
3872 } while (exception.retry);
3873
3874out:
3875 if (err == 0)
3876 *clnt = client;
3877 else if (client != *clnt)
3878 rpc_shutdown_client(client);
3879
3880 return err;
3881}
3882
Al Virobeffb8f2016-07-20 16:34:42 -04003883static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003884 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3885 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003887 int status;
3888 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003889
David Quigley1775fd32013-05-22 12:50:42 -04003890 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003891 if (client != NFS_CLIENT(dir)) {
3892 rpc_shutdown_client(client);
3893 nfs_fixup_secinfo_attributes(fattr);
3894 }
3895 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003898struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003899nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003900 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3901{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003902 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003903 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003904
David Quigley1775fd32013-05-22 12:50:42 -04003905 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003906 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003907 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003908 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003909}
3910
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3912{
Trond Myklebust76b32992007-08-10 17:45:11 -04003913 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 struct nfs4_accessargs args = {
3915 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003916 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003918 struct nfs4_accessres res = {
3919 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003920 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 struct rpc_message msg = {
3922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3923 .rpc_argp = &args,
3924 .rpc_resp = &res,
3925 .rpc_cred = entry->cred,
3926 };
3927 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 /*
3931 * Determine which access bits we want to ask for...
3932 */
3933 if (mode & MAY_READ)
3934 args.access |= NFS4_ACCESS_READ;
3935 if (S_ISDIR(inode->i_mode)) {
3936 if (mode & MAY_WRITE)
3937 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3938 if (mode & MAY_EXEC)
3939 args.access |= NFS4_ACCESS_LOOKUP;
3940 } else {
3941 if (mode & MAY_WRITE)
3942 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3943 if (mode & MAY_EXEC)
3944 args.access |= NFS4_ACCESS_EXECUTE;
3945 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003946
3947 res.fattr = nfs_alloc_fattr();
3948 if (res.fattr == NULL)
3949 return -ENOMEM;
3950
Bryan Schumaker7c513052011-03-24 17:12:24 +00003951 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003953 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003954 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003956 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 return status;
3958}
3959
3960static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3961{
3962 struct nfs4_exception exception = { };
3963 int err;
3964 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003965 err = _nfs4_proc_access(inode, entry);
3966 trace_nfs4_access(inode, err);
3967 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 &exception);
3969 } while (exception.retry);
3970 return err;
3971}
3972
3973/*
3974 * TODO: For the time being, we don't try to get any attributes
3975 * along with any of the zero-copy operations READ, READDIR,
3976 * READLINK, WRITE.
3977 *
3978 * In the case of the first three, we want to put the GETATTR
3979 * after the read-type operation -- this is because it is hard
3980 * to predict the length of a GETATTR response in v4, and thus
3981 * align the READ data correctly. This means that the GETATTR
3982 * may end up partially falling into the page cache, and we should
3983 * shift it into the 'tail' of the xdr_buf before processing.
3984 * To do this efficiently, we need to know the total length
3985 * of data received, which doesn't seem to be available outside
3986 * of the RPC layer.
3987 *
3988 * In the case of WRITE, we also want to put the GETATTR after
3989 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003990 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 *
3992 * Both of these changes to the XDR layer would in fact be quite
3993 * minor, but I decided to leave them for a subsequent patch.
3994 */
3995static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3996 unsigned int pgbase, unsigned int pglen)
3997{
3998 struct nfs4_readlink args = {
3999 .fh = NFS_FH(inode),
4000 .pgbase = pgbase,
4001 .pglen = pglen,
4002 .pages = &page,
4003 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004004 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 struct rpc_message msg = {
4006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4007 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004008 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 };
4010
Bryan Schumaker7c513052011-03-24 17:12:24 +00004011 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 -07004012}
4013
4014static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4015 unsigned int pgbase, unsigned int pglen)
4016{
4017 struct nfs4_exception exception = { };
4018 int err;
4019 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004020 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4021 trace_nfs4_readlink(inode, err);
4022 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 &exception);
4024 } while (exception.retry);
4025 return err;
4026}
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004029 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031static int
4032nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004033 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004035 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004036 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004037 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 int status = 0;
4040
NeilBrown532d4de2016-10-13 15:26:47 +11004041 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004042 if (IS_ERR(ctx))
4043 return PTR_ERR(ctx);
4044
David Quigleyaa9c2662013-05-22 12:50:44 -04004045 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4046
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004047 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4048 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004049 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 if (IS_ERR(state)) {
4051 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004052 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004055 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004056 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return status;
4058}
4059
Al Virobeffb8f2016-07-20 16:34:42 -04004060static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Trond Myklebust16e42952005-10-27 22:12:44 -04004062 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004063 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004065 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004067 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004068 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4072 .rpc_argp = &args,
4073 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004075 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004076
Bryan Schumaker7c513052011-03-24 17:12:24 +00004077 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004078 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004079 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return status;
4081}
4082
Al Virobeffb8f2016-07-20 16:34:42 -04004083static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 struct nfs4_exception exception = { };
4086 int err;
4087 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004088 err = _nfs4_proc_remove(dir, name);
4089 trace_nfs4_remove(dir, name, err);
4090 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 &exception);
4092 } while (exception.retry);
4093 return err;
4094}
4095
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004096static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004098 struct nfs_server *server = NFS_SERVER(dir);
4099 struct nfs_removeargs *args = msg->rpc_argp;
4100 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004102 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004104 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004105
4106 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107}
4108
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004109static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4110{
Al Viro884be172016-04-28 23:56:31 -04004111 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004112 &data->args.seq_args,
4113 &data->res.seq_res,
4114 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004117static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004119 struct nfs_unlinkdata *data = task->tk_calldata;
4120 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004121
Trond Myklebust14516c32010-07-31 14:29:06 -04004122 if (!nfs4_sequence_done(task, &res->seq_res))
4123 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004124 if (nfs4_async_handle_error(task, res->server, NULL,
4125 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004126 return 0;
4127 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004128 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129}
4130
Jeff Laytond3d41522010-09-17 17:31:57 -04004131static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4132{
4133 struct nfs_server *server = NFS_SERVER(dir);
4134 struct nfs_renameargs *arg = msg->rpc_argp;
4135 struct nfs_renameres *res = msg->rpc_resp;
4136
4137 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004138 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004139 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004140}
4141
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004142static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4143{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004144 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4145 &data->args.seq_args,
4146 &data->res.seq_res,
4147 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004148}
4149
4150static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4151 struct inode *new_dir)
4152{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004153 struct nfs_renamedata *data = task->tk_calldata;
4154 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004155
4156 if (!nfs4_sequence_done(task, &res->seq_res))
4157 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004158 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004159 return 0;
4160
4161 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004162 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004163 return 1;
4164}
4165
Al Virobeffb8f2016-07-20 16:34:42 -04004166static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004168 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 struct nfs4_link_arg arg = {
4170 .fh = NFS_FH(inode),
4171 .dir_fh = NFS_FH(dir),
4172 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004173 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004175 struct nfs4_link_res res = {
4176 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004177 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 struct rpc_message msg = {
4180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4181 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004182 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004184 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Trond Myklebust136f2622010-04-16 16:22:49 -04004186 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004187 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004188 goto out;
4189
David Quigley14c43f72013-05-22 12:50:43 -04004190 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4191 if (IS_ERR(res.label)) {
4192 status = PTR_ERR(res.label);
4193 goto out;
4194 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004195 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004196
Bryan Schumaker7c513052011-03-24 17:12:24 +00004197 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004198 if (!status) {
4199 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004200 status = nfs_post_op_update_inode(inode, res.fattr);
4201 if (!status)
4202 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004203 }
David Quigley14c43f72013-05-22 12:50:43 -04004204
4205
4206 nfs4_label_free(res.label);
4207
Trond Myklebust136f2622010-04-16 16:22:49 -04004208out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004209 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 return status;
4211}
4212
Al Virobeffb8f2016-07-20 16:34:42 -04004213static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 struct nfs4_exception exception = { };
4216 int err;
4217 do {
4218 err = nfs4_handle_exception(NFS_SERVER(inode),
4219 _nfs4_proc_link(inode, dir, name),
4220 &exception);
4221 } while (exception.retry);
4222 return err;
4223}
4224
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004225struct nfs4_createdata {
4226 struct rpc_message msg;
4227 struct nfs4_create_arg arg;
4228 struct nfs4_create_res res;
4229 struct nfs_fh fh;
4230 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004231 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232};
4233
4234static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004235 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236{
4237 struct nfs4_createdata *data;
4238
4239 data = kzalloc(sizeof(*data), GFP_KERNEL);
4240 if (data != NULL) {
4241 struct nfs_server *server = NFS_SERVER(dir);
4242
David Quigley14c43f72013-05-22 12:50:43 -04004243 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4244 if (IS_ERR(data->label))
4245 goto out_free;
4246
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004247 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4248 data->msg.rpc_argp = &data->arg;
4249 data->msg.rpc_resp = &data->res;
4250 data->arg.dir_fh = NFS_FH(dir);
4251 data->arg.server = server;
4252 data->arg.name = name;
4253 data->arg.attrs = sattr;
4254 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004255 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004256 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 data->res.server = server;
4258 data->res.fh = &data->fh;
4259 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004260 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004261 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 }
4263 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004264out_free:
4265 kfree(data);
4266 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267}
4268
4269static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4270{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004271 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004272 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004273 if (status == 0) {
4274 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004275 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004276 }
4277 return status;
4278}
4279
4280static void nfs4_free_createdata(struct nfs4_createdata *data)
4281{
David Quigley14c43f72013-05-22 12:50:43 -04004282 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004283 kfree(data);
4284}
4285
Chuck Lever4f390c12006-08-22 20:06:22 -04004286static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004287 struct page *page, unsigned int len, struct iattr *sattr,
4288 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004290 struct nfs4_createdata *data;
4291 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Chuck Lever94a6d752006-08-22 20:06:23 -04004293 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004294 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004295
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004296 status = -ENOMEM;
4297 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4298 if (data == NULL)
4299 goto out;
4300
4301 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4302 data->arg.u.symlink.pages = &page;
4303 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004304 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004306 status = nfs4_do_create(dir, dentry, data);
4307
4308 nfs4_free_createdata(data);
4309out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return status;
4311}
4312
Chuck Lever4f390c12006-08-22 20:06:22 -04004313static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004314 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
4316 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004317 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004319
4320 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004323 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4324 trace_nfs4_symlink(dir, &dentry->d_name, err);
4325 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 &exception);
4327 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004328
4329 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return err;
4331}
4332
4333static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004334 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004336 struct nfs4_createdata *data;
4337 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4340 if (data == NULL)
4341 goto out;
4342
David Quigley1775fd32013-05-22 12:50:42 -04004343 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344 status = nfs4_do_create(dir, dentry, data);
4345
4346 nfs4_free_createdata(data);
4347out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return status;
4349}
4350
4351static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4352 struct iattr *sattr)
4353{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004354 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004356 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004358
David Quigleyaa9c2662013-05-22 12:50:44 -04004359 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4360
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004361 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4362 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004364 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4365 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4366 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 &exception);
4368 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004369 nfs4_label_release_security(label);
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return err;
4372}
4373
4374static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004375 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
David Howells2b0143b2015-03-17 22:25:59 +00004377 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 struct nfs4_readdir_arg args = {
4379 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004380 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 .pgbase = 0,
4382 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004383 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004384 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 };
4386 struct nfs4_readdir_res res;
4387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4389 .rpc_argp = &args,
4390 .rpc_resp = &res,
4391 .rpc_cred = cred,
4392 };
4393 int status;
4394
Al Viro6de14722013-09-16 10:53:17 -04004395 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4396 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004397 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004398 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004400 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 -05004401 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004402 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004403 status += args.pgbase;
4404 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004405
4406 nfs_invalidate_atime(dir);
4407
Harvey Harrison3110ff82008-05-02 13:42:44 -07004408 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 return status;
4410}
4411
4412static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004413 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
4415 struct nfs4_exception exception = { };
4416 int err;
4417 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004418 err = _nfs4_proc_readdir(dentry, cred, cookie,
4419 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004420 trace_nfs4_readdir(d_inode(dentry), err);
4421 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 &exception);
4423 } while (exception.retry);
4424 return err;
4425}
4426
4427static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004428 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004430 struct nfs4_createdata *data;
4431 int mode = sattr->ia_mode;
4432 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004434 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4435 if (data == NULL)
4436 goto out;
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004439 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004441 data->arg.ftype = NF4BLK;
4442 data->arg.u.device.specdata1 = MAJOR(rdev);
4443 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
4445 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004446 data->arg.ftype = NF4CHR;
4447 data->arg.u.device.specdata1 = MAJOR(rdev);
4448 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004449 } else if (!S_ISSOCK(mode)) {
4450 status = -EINVAL;
4451 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 }
David Quigley1775fd32013-05-22 12:50:42 -04004453
David Quigleyaa9c2662013-05-22 12:50:44 -04004454 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004455 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004456out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 nfs4_free_createdata(data);
4458out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 return status;
4460}
4461
4462static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4463 struct iattr *sattr, dev_t rdev)
4464{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004465 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004467 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004469
David Quigleyaa9c2662013-05-22 12:50:44 -04004470 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4471
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004472 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4473 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004475 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4476 trace_nfs4_mknod(dir, &dentry->d_name, err);
4477 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 &exception);
4479 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004480
4481 nfs4_label_release_security(label);
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return err;
4484}
4485
4486static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4487 struct nfs_fsstat *fsstat)
4488{
4489 struct nfs4_statfs_arg args = {
4490 .fh = fhandle,
4491 .bitmask = server->attr_bitmask,
4492 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004493 struct nfs4_statfs_res res = {
4494 .fsstat = fsstat,
4495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4498 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004499 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 };
4501
Trond Myklebust0e574af2005-10-27 22:12:38 -04004502 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004503 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
4506static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4507{
4508 struct nfs4_exception exception = { };
4509 int err;
4510 do {
4511 err = nfs4_handle_exception(server,
4512 _nfs4_proc_statfs(server, fhandle, fsstat),
4513 &exception);
4514 } while (exception.retry);
4515 return err;
4516}
4517
4518static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4519 struct nfs_fsinfo *fsinfo)
4520{
4521 struct nfs4_fsinfo_arg args = {
4522 .fh = fhandle,
4523 .bitmask = server->attr_bitmask,
4524 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004525 struct nfs4_fsinfo_res res = {
4526 .fsinfo = fsinfo,
4527 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 struct rpc_message msg = {
4529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4530 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004531 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 };
4533
Bryan Schumaker7c513052011-03-24 17:12:24 +00004534 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
4537static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4538{
4539 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004540 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 int err;
4542
4543 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004544 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004545 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004546 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004547 nfs4_set_lease_period(server->nfs_client,
4548 fsinfo->lease_time * HZ,
4549 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004550 break;
4551 }
4552 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 } while (exception.retry);
4554 return err;
4555}
4556
4557static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4558{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004559 int error;
4560
Trond Myklebust0e574af2005-10-27 22:12:38 -04004561 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004562 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004563 if (error == 0) {
4564 /* block layout checks this! */
4565 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004566 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004567 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004568
4569 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
4571
4572static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4573 struct nfs_pathconf *pathconf)
4574{
4575 struct nfs4_pathconf_arg args = {
4576 .fh = fhandle,
4577 .bitmask = server->attr_bitmask,
4578 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004579 struct nfs4_pathconf_res res = {
4580 .pathconf = pathconf,
4581 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 struct rpc_message msg = {
4583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4584 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004585 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 };
4587
4588 /* None of the pathconf attributes are mandatory to implement */
4589 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4590 memset(pathconf, 0, sizeof(*pathconf));
4591 return 0;
4592 }
4593
Trond Myklebust0e574af2005-10-27 22:12:38 -04004594 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004595 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
4598static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4599 struct nfs_pathconf *pathconf)
4600{
4601 struct nfs4_exception exception = { };
4602 int err;
4603
4604 do {
4605 err = nfs4_handle_exception(server,
4606 _nfs4_proc_pathconf(server, fhandle, pathconf),
4607 &exception);
4608 } while (exception.retry);
4609 return err;
4610}
4611
Trond Myklebust5521abf2013-03-16 20:54:34 -04004612int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004613 const struct nfs_open_context *ctx,
4614 const struct nfs_lock_context *l_ctx,
4615 fmode_t fmode)
4616{
NeilBrown17393472016-10-13 15:26:47 +11004617 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004618}
4619EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4620
Trond Myklebust5521abf2013-03-16 20:54:34 -04004621static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4622 const struct nfs_open_context *ctx,
4623 const struct nfs_lock_context *l_ctx,
4624 fmode_t fmode)
4625{
4626 nfs4_stateid current_stateid;
4627
Trond Myklebuste1253be2014-03-05 08:44:23 -05004628 /* If the current stateid represents a lost lock, then exit */
4629 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4630 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004631 return nfs4_stateid_match(stateid, &current_stateid);
4632}
4633
4634static bool nfs4_error_stateid_expired(int err)
4635{
4636 switch (err) {
4637 case -NFS4ERR_DELEG_REVOKED:
4638 case -NFS4ERR_ADMIN_REVOKED:
4639 case -NFS4ERR_BAD_STATEID:
4640 case -NFS4ERR_STALE_STATEID:
4641 case -NFS4ERR_OLD_STATEID:
4642 case -NFS4ERR_OPENMODE:
4643 case -NFS4ERR_EXPIRED:
4644 return true;
4645 }
4646 return false;
4647}
4648
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004651 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004654 if (task->tk_status < 0) {
4655 struct nfs4_exception exception = {
4656 .inode = hdr->inode,
4657 .state = hdr->args.context->state,
4658 .stateid = &hdr->args.stateid,
4659 };
4660 task->tk_status = nfs4_async_handle_exception(task,
4661 server, task->tk_status, &exception);
4662 if (exception.retry) {
4663 rpc_restart_call_prepare(task);
4664 return -EAGAIN;
4665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004669 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671}
4672
Trond Myklebust5521abf2013-03-16 20:54:34 -04004673static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004674 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004675{
4676
4677 if (!nfs4_error_stateid_expired(task->tk_status) ||
4678 nfs4_stateid_is_current(&args->stateid,
4679 args->context,
4680 args->lock_context,
4681 FMODE_READ))
4682 return false;
4683 rpc_restart_call_prepare(task);
4684 return true;
4685}
4686
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004688{
4689
4690 dprintk("--> %s\n", __func__);
4691
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004693 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004695 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004696 if (task->tk_status > 0)
4697 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4699 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004700}
4701
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4703 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004706 if (!hdr->pgio_done_cb)
4707 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004708 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4713 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004714{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4716 &hdr->args.seq_args,
4717 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004718 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004719 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004720 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4721 hdr->args.lock_context,
4722 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004723 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004725 return -EIO;
4726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729static int nfs4_write_done_cb(struct rpc_task *task,
4730 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004732 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004733
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004735 if (task->tk_status < 0) {
4736 struct nfs4_exception exception = {
4737 .inode = hdr->inode,
4738 .state = hdr->args.context->state,
4739 .stateid = &hdr->args.stateid,
4740 };
4741 task->tk_status = nfs4_async_handle_exception(task,
4742 NFS_SERVER(inode), task->tk_status,
4743 &exception);
4744 if (exception.retry) {
4745 rpc_restart_call_prepare(task);
4746 return -EAGAIN;
4747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004749 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004750 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004751 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004752 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
Trond Myklebust5521abf2013-03-16 20:54:34 -04004756static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004757 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004758{
4759
4760 if (!nfs4_error_stateid_expired(task->tk_status) ||
4761 nfs4_stateid_is_current(&args->stateid,
4762 args->context,
4763 args->lock_context,
4764 FMODE_WRITE))
4765 return false;
4766 rpc_restart_call_prepare(task);
4767 return true;
4768}
4769
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004770static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004771{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004772 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004773 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004774 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004775 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004776 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4777 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004778}
4779
Trond Myklebust5a37f852012-04-28 14:55:16 -04004780static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004781bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004782{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004783 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004784 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004785 return false;
4786 /* Otherwise, request attributes if and only if we don't hold
4787 * a delegation
4788 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004789 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004790}
Fred Isamana69aef12011-03-03 15:13:47 +00004791
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004792static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4793 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004795 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004796
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004797 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4798 hdr->args.bitmask = NULL;
4799 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004800 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004801 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004802
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004803 if (!hdr->pgio_done_cb)
4804 hdr->pgio_done_cb = nfs4_write_done_cb;
4805 hdr->res.server = server;
4806 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004808 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004809 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Fred Isaman0b7c0152012-04-20 14:47:39 -04004812static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4813{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004814 nfs4_setup_sequence(NFS_SERVER(data->inode),
4815 &data->args.seq_args,
4816 &data->res.seq_res,
4817 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
Fred Isaman0b7c0152012-04-20 14:47:39 -04004820static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004823
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004824 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004825 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4826 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004827 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004828 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831}
4832
Fred Isaman0b7c0152012-04-20 14:47:39 -04004833static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004834{
4835 if (!nfs4_sequence_done(task, &data->res.seq_res))
4836 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004837 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004838}
4839
Fred Isaman0b7c0152012-04-20 14:47:39 -04004840static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004842 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004843
Fred Isaman0b7c0152012-04-20 14:47:39 -04004844 if (data->commit_done_cb == NULL)
4845 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004846 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004847 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004848 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004851struct nfs4_renewdata {
4852 struct nfs_client *client;
4853 unsigned long timestamp;
4854};
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856/*
4857 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4858 * standalone procedure for queueing an asynchronous RENEW.
4859 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004860static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004861{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004862 struct nfs4_renewdata *data = calldata;
4863 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004864
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004865 if (atomic_read(&clp->cl_count) > 1)
4866 nfs4_schedule_state_renewal(clp);
4867 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004868 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004869}
4870
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004871static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004873 struct nfs4_renewdata *data = calldata;
4874 struct nfs_client *clp = data->client;
4875 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004877 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004878 switch (task->tk_status) {
4879 case 0:
4880 break;
4881 case -NFS4ERR_LEASE_MOVED:
4882 nfs4_schedule_lease_moved_recovery(clp);
4883 break;
4884 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004885 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004886 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4887 return;
4888 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004889 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004890 return;
4891 }
4892 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004894 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
4896
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004897static const struct rpc_call_ops nfs4_renew_ops = {
4898 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004899 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004900};
4901
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004902static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
4904 struct rpc_message msg = {
4905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4906 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004907 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004909 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004911 if (renew_flags == 0)
4912 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004913 if (!atomic_inc_not_zero(&clp->cl_count))
4914 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004915 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004916 if (data == NULL)
4917 return -ENOMEM;
4918 data->client = clp;
4919 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004920 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004921 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
4923
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004924static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
4926 struct rpc_message msg = {
4927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4928 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004929 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 };
4931 unsigned long now = jiffies;
4932 int status;
4933
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004934 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 if (status < 0)
4936 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004937 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 return 0;
4939}
4940
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004941static inline int nfs4_server_supports_acls(struct nfs_server *server)
4942{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004943 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004944}
4945
Trond Myklebust21f498c2012-08-24 10:59:25 -04004946/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4947 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004948 * the stack.
4949 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004950#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004951
Neil Hormane9e3d722011-03-04 19:26:03 -05004952static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004953 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004954{
4955 struct page *newpage, **spages;
4956 int rc = 0;
4957 size_t len;
4958 spages = pages;
4959
4960 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004961 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004962 newpage = alloc_page(GFP_KERNEL);
4963
4964 if (newpage == NULL)
4965 goto unwind;
4966 memcpy(page_address(newpage), buf, len);
4967 buf += len;
4968 buflen -= len;
4969 *pages++ = newpage;
4970 rc++;
4971 } while (buflen != 0);
4972
4973 return rc;
4974
4975unwind:
4976 for(; rc > 0; rc--)
4977 __free_page(spages[rc-1]);
4978 return -ENOMEM;
4979}
4980
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004981struct nfs4_cached_acl {
4982 int cached;
4983 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004984 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004985};
4986
4987static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004988{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004989 struct nfs_inode *nfsi = NFS_I(inode);
4990
4991 spin_lock(&inode->i_lock);
4992 kfree(nfsi->nfs4_acl);
4993 nfsi->nfs4_acl = acl;
4994 spin_unlock(&inode->i_lock);
4995}
4996
4997static void nfs4_zap_acl_attr(struct inode *inode)
4998{
4999 nfs4_set_cached_acl(inode, NULL);
5000}
5001
5002static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5003{
5004 struct nfs_inode *nfsi = NFS_I(inode);
5005 struct nfs4_cached_acl *acl;
5006 int ret = -ENOENT;
5007
5008 spin_lock(&inode->i_lock);
5009 acl = nfsi->nfs4_acl;
5010 if (acl == NULL)
5011 goto out;
5012 if (buf == NULL) /* user is just asking for length */
5013 goto out_len;
5014 if (acl->cached == 0)
5015 goto out;
5016 ret = -ERANGE; /* see getxattr(2) man page */
5017 if (acl->len > buflen)
5018 goto out;
5019 memcpy(buf, acl->data, acl->len);
5020out_len:
5021 ret = acl->len;
5022out:
5023 spin_unlock(&inode->i_lock);
5024 return ret;
5025}
5026
Sachin Prabhu5794d212012-04-17 14:36:40 +01005027static 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 +00005028{
5029 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005030 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005031
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005032 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005033 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005034 if (acl == NULL)
5035 goto out;
5036 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005037 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005038 } else {
5039 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5040 if (acl == NULL)
5041 goto out;
5042 acl->cached = 0;
5043 }
5044 acl->len = acl_len;
5045out:
5046 nfs4_set_cached_acl(inode, acl);
5047}
5048
Andy Adamsonbf118a32011-12-07 11:55:27 -05005049/*
5050 * The getxattr API returns the required buffer length when called with a
5051 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5052 * the required buf. On a NULL buf, we send a page of data to the server
5053 * guessing that the ACL request can be serviced by a page. If so, we cache
5054 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5055 * the cache. If not so, we throw away the page, and cache the required
5056 * length. The next getxattr call will then produce another round trip to
5057 * the server, this time with the input buf of the required size.
5058 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005059static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005060{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005061 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005062 struct nfs_getaclargs args = {
5063 .fh = NFS_FH(inode),
5064 .acl_pages = pages,
5065 .acl_len = buflen,
5066 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005067 struct nfs_getaclres res = {
5068 .acl_len = buflen,
5069 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005070 struct rpc_message msg = {
5071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5072 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005073 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005074 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005075 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5076 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005077
Andy Adamsonbf118a32011-12-07 11:55:27 -05005078 /* As long as we're doing a round trip to the server anyway,
5079 * let's be prepared for a page of acl data. */
5080 if (npages == 0)
5081 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005082 if (npages > ARRAY_SIZE(pages))
5083 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005084
Andy Adamsonbf118a32011-12-07 11:55:27 -05005085 for (i = 0; i < npages; i++) {
5086 pages[i] = alloc_page(GFP_KERNEL);
5087 if (!pages[i])
5088 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005090
5091 /* for decoding across pages */
5092 res.acl_scratch = alloc_page(GFP_KERNEL);
5093 if (!res.acl_scratch)
5094 goto out_free;
5095
Andy Adamsonbf118a32011-12-07 11:55:27 -05005096 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005097
Peng Taode040be2012-01-10 22:42:47 +08005098 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005099 __func__, buf, buflen, npages, args.acl_len);
5100 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5101 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102 if (ret)
5103 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005104
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005105 /* Handle the case where the passed-in buffer is too short */
5106 if (res.acl_flags & NFS4_ACL_TRUNC) {
5107 /* Did the user only issue a request for the acl length? */
5108 if (buf == NULL)
5109 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005110 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005111 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005112 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005113 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005114 if (buf) {
5115 if (res.acl_len > buflen) {
5116 ret = -ERANGE;
5117 goto out_free;
5118 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005119 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005120 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005121out_ok:
5122 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005123out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005124 for (i = 0; i < npages; i++)
5125 if (pages[i])
5126 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005127 if (res.acl_scratch)
5128 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005129 return ret;
5130}
5131
Trond Myklebust16b42892006-08-24 12:27:15 -04005132static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5133{
5134 struct nfs4_exception exception = { };
5135 ssize_t ret;
5136 do {
5137 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005138 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005139 if (ret >= 0)
5140 break;
5141 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5142 } while (exception.retry);
5143 return ret;
5144}
5145
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005146static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5147{
5148 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005149 int ret;
5150
5151 if (!nfs4_server_supports_acls(server))
5152 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005153 ret = nfs_revalidate_inode(server, inode);
5154 if (ret < 0)
5155 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005156 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5157 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005158 ret = nfs4_read_cached_acl(inode, buf, buflen);
5159 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005160 /* -ENOENT is returned if there is no ACL or if there is an ACL
5161 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005162 return ret;
5163 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005164}
5165
Trond Myklebust16b42892006-08-24 12:27:15 -04005166static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005167{
5168 struct nfs_server *server = NFS_SERVER(inode);
5169 struct page *pages[NFS4ACL_MAXPAGES];
5170 struct nfs_setaclargs arg = {
5171 .fh = NFS_FH(inode),
5172 .acl_pages = pages,
5173 .acl_len = buflen,
5174 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005175 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005176 struct rpc_message msg = {
5177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5178 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005179 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005180 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005181 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005182 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005183
5184 if (!nfs4_server_supports_acls(server))
5185 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005186 if (npages > ARRAY_SIZE(pages))
5187 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005188 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005189 if (i < 0)
5190 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005191 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005192 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005193
5194 /*
5195 * Free each page after tx, so the only ref left is
5196 * held by the network stack
5197 */
5198 for (; i > 0; i--)
5199 put_page(pages[i-1]);
5200
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005201 /*
5202 * Acl update can result in inode attribute update.
5203 * so mark the attribute cache invalid.
5204 */
5205 spin_lock(&inode->i_lock);
5206 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5207 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005208 nfs_access_zap_cache(inode);
5209 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005210 return ret;
5211}
5212
Trond Myklebust16b42892006-08-24 12:27:15 -04005213static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5214{
5215 struct nfs4_exception exception = { };
5216 int err;
5217 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005218 err = __nfs4_proc_set_acl(inode, buf, buflen);
5219 trace_nfs4_set_acl(inode, err);
5220 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005221 &exception);
5222 } while (exception.retry);
5223 return err;
5224}
5225
David Quigleyaa9c2662013-05-22 12:50:44 -04005226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5227static int _nfs4_get_security_label(struct inode *inode, void *buf,
5228 size_t buflen)
5229{
5230 struct nfs_server *server = NFS_SERVER(inode);
5231 struct nfs_fattr fattr;
5232 struct nfs4_label label = {0, 0, buflen, buf};
5233
5234 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005235 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005236 .fh = NFS_FH(inode),
5237 .bitmask = bitmask,
5238 };
5239 struct nfs4_getattr_res res = {
5240 .fattr = &fattr,
5241 .label = &label,
5242 .server = server,
5243 };
5244 struct rpc_message msg = {
5245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005246 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005247 .rpc_resp = &res,
5248 };
5249 int ret;
5250
5251 nfs_fattr_init(&fattr);
5252
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005253 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005254 if (ret)
5255 return ret;
5256 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5257 return -ENOENT;
5258 if (buflen < label.len)
5259 return -ERANGE;
5260 return 0;
5261}
5262
5263static int nfs4_get_security_label(struct inode *inode, void *buf,
5264 size_t buflen)
5265{
5266 struct nfs4_exception exception = { };
5267 int err;
5268
5269 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5270 return -EOPNOTSUPP;
5271
5272 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005273 err = _nfs4_get_security_label(inode, buf, buflen);
5274 trace_nfs4_get_security_label(inode, err);
5275 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005276 &exception);
5277 } while (exception.retry);
5278 return err;
5279}
5280
5281static int _nfs4_do_set_security_label(struct inode *inode,
5282 struct nfs4_label *ilabel,
5283 struct nfs_fattr *fattr,
5284 struct nfs4_label *olabel)
5285{
5286
5287 struct iattr sattr = {0};
5288 struct nfs_server *server = NFS_SERVER(inode);
5289 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005290 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005291 .fh = NFS_FH(inode),
5292 .iap = &sattr,
5293 .server = server,
5294 .bitmask = bitmask,
5295 .label = ilabel,
5296 };
5297 struct nfs_setattrres res = {
5298 .fattr = fattr,
5299 .label = olabel,
5300 .server = server,
5301 };
5302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005304 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005305 .rpc_resp = &res,
5306 };
5307 int status;
5308
Jeff Layton12207f62013-11-01 10:49:32 -04005309 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005310
Jeff Layton12207f62013-11-01 10:49:32 -04005311 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005312 if (status)
5313 dprintk("%s failed: %d\n", __func__, status);
5314
5315 return status;
5316}
5317
5318static int nfs4_do_set_security_label(struct inode *inode,
5319 struct nfs4_label *ilabel,
5320 struct nfs_fattr *fattr,
5321 struct nfs4_label *olabel)
5322{
5323 struct nfs4_exception exception = { };
5324 int err;
5325
5326 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005327 err = _nfs4_do_set_security_label(inode, ilabel,
5328 fattr, olabel);
5329 trace_nfs4_set_security_label(inode, err);
5330 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005331 &exception);
5332 } while (exception.retry);
5333 return err;
5334}
5335
5336static int
Al Viro59301222016-05-27 10:19:30 -04005337nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005338{
5339 struct nfs4_label ilabel, *olabel = NULL;
5340 struct nfs_fattr fattr;
5341 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005342 int status;
5343
5344 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5345 return -EOPNOTSUPP;
5346
5347 nfs_fattr_init(&fattr);
5348
5349 ilabel.pi = 0;
5350 ilabel.lfs = 0;
5351 ilabel.label = (char *)buf;
5352 ilabel.len = buflen;
5353
5354 cred = rpc_lookup_cred();
5355 if (IS_ERR(cred))
5356 return PTR_ERR(cred);
5357
5358 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5359 if (IS_ERR(olabel)) {
5360 status = -PTR_ERR(olabel);
5361 goto out;
5362 }
5363
5364 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5365 if (status == 0)
5366 nfs_setsecurity(inode, &fattr, olabel);
5367
5368 nfs4_label_free(olabel);
5369out:
5370 put_rpccred(cred);
5371 return status;
5372}
5373#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5374
5375
Chuck Leverf0920752012-05-21 22:45:41 -04005376static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5377 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005378{
5379 __be32 verf[2];
5380
Chuck Lever2c820d92012-05-21 22:45:33 -04005381 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5382 /* An impossible timestamp guarantees this value
5383 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005384 verf[0] = cpu_to_be32(U32_MAX);
5385 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005386 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005387 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005388 u64 ns = ktime_to_ns(nn->boot_time);
5389
5390 verf[0] = cpu_to_be32(ns >> 32);
5391 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005392 }
Chuck Levercd937102012-03-02 17:14:31 -05005393 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5394}
5395
Jeff Laytona3192682015-06-09 19:43:59 -04005396static int
5397nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005398{
Jeff Laytona3192682015-06-09 19:43:59 -04005399 size_t len;
5400 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005401
Trond Myklebustceb3a162015-01-03 15:16:04 -05005402 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005403 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005404
Jeff Laytona3192682015-06-09 19:43:59 -04005405 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005406 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005407 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5408 1 +
5409 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5410 1;
5411 rcu_read_unlock();
5412
5413 if (len > NFS4_OPAQUE_LIMIT + 1)
5414 return -EINVAL;
5415
5416 /*
5417 * Since this string is allocated at mount time, and held until the
5418 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5419 * about a memory-reclaim deadlock.
5420 */
5421 str = kmalloc(len, GFP_KERNEL);
5422 if (!str)
5423 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005424
Chuck Levere984a552012-09-14 17:24:21 -04005425 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005426 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005427 clp->cl_ipaddr,
5428 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5429 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005430 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005431
Jeff Laytona3192682015-06-09 19:43:59 -04005432 clp->cl_owner_id = str;
5433 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005434}
5435
Jeff Layton873e3852015-06-09 19:44:00 -04005436static int
5437nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005438{
Jeff Layton873e3852015-06-09 19:44:00 -04005439 size_t len;
5440 char *str;
5441
5442 len = 10 + 10 + 1 + 10 + 1 +
5443 strlen(nfs4_client_id_uniquifier) + 1 +
5444 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5445
5446 if (len > NFS4_OPAQUE_LIMIT + 1)
5447 return -EINVAL;
5448
5449 /*
5450 * Since this string is allocated at mount time, and held until the
5451 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5452 * about a memory-reclaim deadlock.
5453 */
5454 str = kmalloc(len, GFP_KERNEL);
5455 if (!str)
5456 return -ENOMEM;
5457
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005458 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005459 clp->rpc_ops->version, clp->cl_minorversion,
5460 nfs4_client_id_uniquifier,
5461 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005462 clp->cl_owner_id = str;
5463 return 0;
5464}
5465
5466static int
5467nfs4_init_uniform_client_string(struct nfs_client *clp)
5468{
Jeff Layton873e3852015-06-09 19:44:00 -04005469 size_t len;
5470 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005471
5472 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005473 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005474
5475 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005476 return nfs4_init_uniquifier_client_string(clp);
5477
5478 len = 10 + 10 + 1 + 10 + 1 +
5479 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5480
5481 if (len > NFS4_OPAQUE_LIMIT + 1)
5482 return -EINVAL;
5483
5484 /*
5485 * Since this string is allocated at mount time, and held until the
5486 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5487 * about a memory-reclaim deadlock.
5488 */
5489 str = kmalloc(len, GFP_KERNEL);
5490 if (!str)
5491 return -ENOMEM;
5492
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005493 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005494 clp->rpc_ops->version, clp->cl_minorversion,
5495 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005496 clp->cl_owner_id = str;
5497 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005498}
5499
Chuck Lever706cb8d2014-03-12 12:51:47 -04005500/*
5501 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5502 * services. Advertise one based on the address family of the
5503 * clientaddr.
5504 */
5505static unsigned int
5506nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5507{
5508 if (strchr(clp->cl_ipaddr, ':') != NULL)
5509 return scnprintf(buf, len, "tcp6");
5510 else
5511 return scnprintf(buf, len, "tcp");
5512}
5513
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005514static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5515{
5516 struct nfs4_setclientid *sc = calldata;
5517
5518 if (task->tk_status == 0)
5519 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5520}
5521
5522static const struct rpc_call_ops nfs4_setclientid_ops = {
5523 .rpc_call_done = nfs4_setclientid_done,
5524};
5525
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005526/**
5527 * nfs4_proc_setclientid - Negotiate client ID
5528 * @clp: state data structure
5529 * @program: RPC program for NFSv4 callback service
5530 * @port: IP port number for NFS4 callback service
5531 * @cred: RPC credential to use for this call
5532 * @res: where to place the result
5533 *
5534 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5535 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005536int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5537 unsigned short port, struct rpc_cred *cred,
5538 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
5540 nfs4_verifier sc_verifier;
5541 struct nfs4_setclientid setclientid = {
5542 .sc_verifier = &sc_verifier,
5543 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005544 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 };
5546 struct rpc_message msg = {
5547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5548 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005549 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005550 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005552 struct rpc_task *task;
5553 struct rpc_task_setup task_setup_data = {
5554 .rpc_client = clp->cl_rpcclient,
5555 .rpc_message = &msg,
5556 .callback_ops = &nfs4_setclientid_ops,
5557 .callback_data = &setclientid,
5558 .flags = RPC_TASK_TIMEOUT,
5559 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005560 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Chuck Leverde734832012-07-11 16:30:50 -04005562 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005563 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005564
5565 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5566 status = nfs4_init_uniform_client_string(clp);
5567 else
Jeff Laytona3192682015-06-09 19:43:59 -04005568 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005569
5570 if (status)
5571 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005572
Chuck Leverde734832012-07-11 16:30:50 -04005573 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005574 setclientid.sc_netid_len =
5575 nfs4_init_callback_netid(clp,
5576 setclientid.sc_netid,
5577 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005578 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005579 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 clp->cl_ipaddr, port >> 8, port & 255);
5581
Jeff Layton3a6bb732015-06-09 19:43:57 -04005582 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005583 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005584 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005585 task = rpc_run_task(&task_setup_data);
5586 if (IS_ERR(task)) {
5587 status = PTR_ERR(task);
5588 goto out;
5589 }
5590 status = task->tk_status;
5591 if (setclientid.sc_cred) {
5592 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5593 put_rpccred(setclientid.sc_cred);
5594 }
5595 rpc_put_task(task);
5596out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005597 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005598 dprintk("NFS reply setclientid: %d\n", status);
5599 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600}
5601
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005602/**
5603 * nfs4_proc_setclientid_confirm - Confirm client ID
5604 * @clp: state data structure
5605 * @res: result of a previous SETCLIENTID
5606 * @cred: RPC credential to use for this call
5607 *
5608 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5609 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005610int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005611 struct nfs4_setclientid_res *arg,
5612 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 struct rpc_message msg = {
5615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005616 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005617 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 int status;
5620
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005621 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5622 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5623 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005624 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005625 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005626 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return status;
5628}
5629
Trond Myklebustfe650402006-01-03 09:55:18 +01005630struct nfs4_delegreturndata {
5631 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005632 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005633 struct nfs_fh fh;
5634 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005635 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005636 struct {
5637 struct nfs4_layoutreturn_args arg;
5638 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005639 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005640 u32 roc_barrier;
5641 bool roc;
5642 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005643 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005644 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005645 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005646};
5647
Trond Myklebustfe650402006-01-03 09:55:18 +01005648static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5649{
5650 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005651
Trond Myklebust14516c32010-07-31 14:29:06 -04005652 if (!nfs4_sequence_done(task, &data->res.seq_res))
5653 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005654
Trond Myklebustca8acf82013-08-13 10:36:56 -04005655 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005656
5657 /* Handle Layoutreturn errors */
5658 if (data->args.lr_args && task->tk_status != 0) {
5659 switch(data->res.lr_ret) {
5660 default:
5661 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5662 break;
5663 case 0:
5664 data->args.lr_args = NULL;
5665 data->res.lr_res = NULL;
5666 break;
5667 case -NFS4ERR_ADMIN_REVOKED:
5668 case -NFS4ERR_DELEG_REVOKED:
5669 case -NFS4ERR_EXPIRED:
5670 case -NFS4ERR_BAD_STATEID:
5671 case -NFS4ERR_OLD_STATEID:
5672 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5673 case -NFS4ERR_WRONG_CRED:
5674 data->args.lr_args = NULL;
5675 data->res.lr_res = NULL;
5676 data->res.lr_ret = 0;
5677 rpc_restart_call_prepare(task);
5678 return;
5679 }
5680 }
5681
Ricardo Labiaga79708862009-12-07 09:23:21 -05005682 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005683 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005684 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005685 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005686 case -NFS4ERR_ADMIN_REVOKED:
5687 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005688 case -NFS4ERR_EXPIRED:
5689 nfs4_free_revoked_stateid(data->res.server,
5690 data->args.stateid,
5691 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005692 case -NFS4ERR_BAD_STATEID:
5693 case -NFS4ERR_OLD_STATEID:
5694 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005695 task->tk_status = 0;
5696 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005697 case -NFS4ERR_ACCESS:
5698 if (data->args.bitmask) {
5699 data->args.bitmask = NULL;
5700 data->res.fattr = NULL;
5701 task->tk_status = 0;
5702 rpc_restart_call_prepare(task);
5703 return;
5704 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005705 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005706 if (nfs4_async_handle_error(task, data->res.server,
5707 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005708 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005709 return;
5710 }
5711 }
5712 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005713}
5714
5715static void nfs4_delegreturn_release(void *calldata)
5716{
Peng Tao039b7562014-07-03 13:05:02 +08005717 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005718 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005719
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005720 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005721 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005722 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5723 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005724 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005725 nfs_iput_and_deactive(inode);
5726 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005727 kfree(calldata);
5728}
5729
Andy Adamson938e1012009-04-01 09:22:28 -04005730static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5731{
5732 struct nfs4_delegreturndata *d_data;
5733
5734 d_data = (struct nfs4_delegreturndata *)data;
5735
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005736 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005737 return;
5738
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005739 nfs4_setup_sequence(d_data->res.server,
5740 &d_data->args.seq_args,
5741 &d_data->res.seq_res,
5742 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005743}
Andy Adamson938e1012009-04-01 09:22:28 -04005744
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005745static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005746 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005747 .rpc_call_done = nfs4_delegreturn_done,
5748 .rpc_release = nfs4_delegreturn_release,
5749};
5750
Trond Myklebuste6f81072008-01-24 18:14:34 -05005751static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005752{
5753 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005754 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005755 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005756 struct rpc_message msg = {
5757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5758 .rpc_cred = cred,
5759 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005760 struct rpc_task_setup task_setup_data = {
5761 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005762 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005763 .callback_ops = &nfs4_delegreturn_ops,
5764 .flags = RPC_TASK_ASYNC,
5765 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005766 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005767
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005768 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005769 if (data == NULL)
5770 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005771 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005772
5773 nfs4_state_protect(server->nfs_client,
5774 NFS_SP4_MACH_CRED_CLEANUP,
5775 &task_setup_data.rpc_client, &msg);
5776
Trond Myklebustfe650402006-01-03 09:55:18 +01005777 data->args.fhandle = &data->fh;
5778 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005779 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005780 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005781 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005782 data->res.fattr = &data->fattr;
5783 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005784 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005785 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005786 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005787 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005788 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005789 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005790 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005791 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005792 if (data->lr.roc) {
5793 data->args.lr_args = &data->lr.arg;
5794 data->res.lr_res = &data->lr.res;
5795 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005796 } else if (data->lr.roc) {
5797 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5798 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005799 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005800
Trond Myklebustc970aa82007-07-14 15:39:59 -04005801 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005802 msg.rpc_argp = &data->args;
5803 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005804 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005805 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005806 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005807 if (!issync)
5808 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005809 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005810 if (status != 0)
5811 goto out;
5812 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005813out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005814 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005815 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816}
5817
Trond Myklebuste6f81072008-01-24 18:14:34 -05005818int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
5820 struct nfs_server *server = NFS_SERVER(inode);
5821 struct nfs4_exception exception = { };
5822 int err;
5823 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005824 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005825 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 switch (err) {
5827 case -NFS4ERR_STALE_STATEID:
5828 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 case 0:
5830 return 0;
5831 }
5832 err = nfs4_handle_exception(server, err, &exception);
5833 } while (exception.retry);
5834 return err;
5835}
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5838{
5839 struct inode *inode = state->inode;
5840 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005841 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005842 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005844 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005846 struct nfs_lockt_res res = {
5847 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 };
5849 struct rpc_message msg = {
5850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5851 .rpc_argp = &arg,
5852 .rpc_resp = &res,
5853 .rpc_cred = state->owner->so_cred,
5854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 struct nfs4_lock_state *lsp;
5856 int status;
5857
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005858 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005859 status = nfs4_set_lock_state(state, request);
5860 if (status != 0)
5861 goto out;
5862 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005863 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005864 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005865 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005866 switch (status) {
5867 case 0:
5868 request->fl_type = F_UNLCK;
5869 break;
5870 case -NFS4ERR_DENIED:
5871 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005873 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005874 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005875out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return status;
5877}
5878
5879static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5880{
5881 struct nfs4_exception exception = { };
5882 int err;
5883
5884 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005885 err = _nfs4_proc_getlk(state, cmd, request);
5886 trace_nfs4_get_lock(request, state, cmd, err);
5887 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 &exception);
5889 } while (exception.retry);
5890 return err;
5891}
5892
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005893struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005894 struct nfs_locku_args arg;
5895 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005896 struct nfs4_lock_state *lsp;
5897 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005898 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005899 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005900 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005901};
5902
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005903static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5904 struct nfs_open_context *ctx,
5905 struct nfs4_lock_state *lsp,
5906 struct nfs_seqid *seqid)
5907{
5908 struct nfs4_unlockdata *p;
5909 struct inode *inode = lsp->ls_state->inode;
5910
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005911 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005912 if (p == NULL)
5913 return NULL;
5914 p->arg.fh = NFS_FH(inode);
5915 p->arg.fl = &p->fl;
5916 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005917 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005918 p->lsp = lsp;
5919 atomic_inc(&lsp->ls_count);
5920 /* Ensure we don't close file until we're done freeing locks! */
5921 p->ctx = get_nfs_open_context(ctx);
5922 memcpy(&p->fl, fl, sizeof(p->fl));
5923 p->server = NFS_SERVER(inode);
5924 return p;
5925}
5926
Trond Myklebust06f814a2006-01-03 09:55:07 +01005927static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005928{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005929 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005930 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005931 nfs4_put_lock_state(calldata->lsp);
5932 put_nfs_open_context(calldata->ctx);
5933 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005934}
5935
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005936static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005937{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005938 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939
Trond Myklebust14516c32010-07-31 14:29:06 -04005940 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5941 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005942 switch (task->tk_status) {
5943 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005944 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005945 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005946 if (nfs4_update_lock_stateid(calldata->lsp,
5947 &calldata->res.stateid))
5948 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005949 case -NFS4ERR_ADMIN_REVOKED:
5950 case -NFS4ERR_EXPIRED:
5951 nfs4_free_revoked_stateid(calldata->server,
5952 &calldata->arg.stateid,
5953 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005954 case -NFS4ERR_BAD_STATEID:
5955 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005956 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005957 if (!nfs4_stateid_match(&calldata->arg.stateid,
5958 &calldata->lsp->ls_stateid))
5959 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005960 break;
5961 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005962 if (nfs4_async_handle_error(task, calldata->server,
5963 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005964 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005965 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005966 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005967}
5968
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005969static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005970{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005971 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005973 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005974 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005975 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005976 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005977 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005978 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005979 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005980 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005981 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005982 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005983 &calldata->res.seq_res,
5984 task) != 0)
5985 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005986 return;
5987out_no_action:
5988 task->tk_action = NULL;
5989out_wait:
5990 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991}
5992
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005993static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005994 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005995 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005996 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005997};
5998
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6000 struct nfs_open_context *ctx,
6001 struct nfs4_lock_state *lsp,
6002 struct nfs_seqid *seqid)
6003{
6004 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006005 struct rpc_message msg = {
6006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6007 .rpc_cred = ctx->cred,
6008 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006009 struct rpc_task_setup task_setup_data = {
6010 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006011 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006012 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006013 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006014 .flags = RPC_TASK_ASYNC,
6015 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006016
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006017 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6018 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6019
Frank Filz137d6ac2007-07-09 15:32:29 -07006020 /* Ensure this is an unlock - when canceling a lock, the
6021 * canceled lock is passed in, and it won't be an unlock.
6022 */
6023 fl->fl_type = F_UNLCK;
6024
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6026 if (data == NULL) {
6027 nfs_free_seqid(seqid);
6028 return ERR_PTR(-ENOMEM);
6029 }
6030
Chuck Levera9c92d62013-08-09 12:48:18 -04006031 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006032 msg.rpc_argp = &data->arg;
6033 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006034 task_setup_data.callback_data = data;
6035 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006036}
6037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6039{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006040 struct inode *inode = state->inode;
6041 struct nfs4_state_owner *sp = state->owner;
6042 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006043 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006044 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006045 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006046 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006047 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006048 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
Trond Myklebust9b073572006-06-29 16:38:34 -04006050 status = nfs4_set_lock_state(state, request);
6051 /* Unlock _before_ we do the RPC call */
6052 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006053 /* Exclude nfs_delegation_claim_locks() */
6054 mutex_lock(&sp->so_delegreturn_mutex);
6055 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006056 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006057 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006058 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006059 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006060 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006061 }
6062 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006063 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006064 if (status != 0)
6065 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006066 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006067 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006068 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6069 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006070 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6071 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006072 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006073 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006074 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006075 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006076 status = PTR_ERR(task);
6077 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006078 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006079 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006080 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006081out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006082 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006083 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006084 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085}
6086
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087struct nfs4_lockdata {
6088 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006089 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090 struct nfs4_lock_state *lsp;
6091 struct nfs_open_context *ctx;
6092 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006093 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094 int rpc_status;
6095 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006096 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097};
6098
6099static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006100 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6101 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006102{
6103 struct nfs4_lockdata *p;
6104 struct inode *inode = lsp->ls_state->inode;
6105 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006106 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006107
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006108 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006109 if (p == NULL)
6110 return NULL;
6111
6112 p->arg.fh = NFS_FH(inode);
6113 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006114 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006115 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006116 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006117 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6118 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006119 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006120 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006121 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006122 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006123 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006124 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006125 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006126 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006127 atomic_inc(&lsp->ls_count);
6128 p->ctx = get_nfs_open_context(ctx);
6129 memcpy(&p->fl, fl, sizeof(p->fl));
6130 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006131out_free_seqid:
6132 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006133out_free:
6134 kfree(p);
6135 return NULL;
6136}
6137
6138static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6139{
6140 struct nfs4_lockdata *data = calldata;
6141 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Harvey Harrison3110ff82008-05-02 13:42:44 -07006143 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006144 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006145 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006146 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006147 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006148 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006149 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006150 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006151 nfs4_stateid_copy(&data->arg.open_stateid,
6152 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006154 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006155 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006156 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006157 nfs4_stateid_copy(&data->arg.lock_stateid,
6158 &data->lsp->ls_stateid);
6159 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006160 if (!nfs4_valid_open_stateid(state)) {
6161 data->rpc_status = -EBADF;
6162 task->tk_action = NULL;
6163 goto out_release_open_seqid;
6164 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006165 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006166 if (nfs4_setup_sequence(data->server,
6167 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006168 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006169 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006170 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006171out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006172 nfs_release_seqid(data->arg.open_seqid);
6173out_release_lock_seqid:
6174 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006175out_wait:
6176 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006177 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006178}
6179
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006180static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6181{
6182 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006183 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184
Harvey Harrison3110ff82008-05-02 13:42:44 -07006185 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006186
Trond Myklebust14516c32010-07-31 14:29:06 -04006187 if (!nfs4_sequence_done(task, &data->res.seq_res))
6188 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006189
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006191 switch (task->tk_status) {
6192 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006193 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006194 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006195 if (data->arg.new_lock) {
6196 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006197 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006198 rpc_restart_call_prepare(task);
6199 break;
6200 }
6201 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006202 if (data->arg.new_lock_owner != 0) {
6203 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6204 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6205 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6206 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6207 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006208 break;
6209 case -NFS4ERR_BAD_STATEID:
6210 case -NFS4ERR_OLD_STATEID:
6211 case -NFS4ERR_STALE_STATEID:
6212 case -NFS4ERR_EXPIRED:
6213 if (data->arg.new_lock_owner != 0) {
6214 if (!nfs4_stateid_match(&data->arg.open_stateid,
6215 &lsp->ls_state->open_stateid))
6216 rpc_restart_call_prepare(task);
6217 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6218 &lsp->ls_stateid))
6219 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006220 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006221 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222}
6223
6224static void nfs4_lock_release(void *calldata)
6225{
6226 struct nfs4_lockdata *data = calldata;
6227
Harvey Harrison3110ff82008-05-02 13:42:44 -07006228 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006229 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230 if (data->cancelled != 0) {
6231 struct rpc_task *task;
6232 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6233 data->arg.lock_seqid);
6234 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006235 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006236 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006237 } else
6238 nfs_free_seqid(data->arg.lock_seqid);
6239 nfs4_put_lock_state(data->lsp);
6240 put_nfs_open_context(data->ctx);
6241 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006242 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006243}
6244
6245static const struct rpc_call_ops nfs4_lock_ops = {
6246 .rpc_call_prepare = nfs4_lock_prepare,
6247 .rpc_call_done = nfs4_lock_done,
6248 .rpc_release = nfs4_lock_release,
6249};
6250
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006251static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6252{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006253 switch (error) {
6254 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006255 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006256 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006257 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006258 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006259 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006260 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006261 break;
6262 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006263 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006264 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006265 };
6266}
6267
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006268static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006269{
6270 struct nfs4_lockdata *data;
6271 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006272 struct rpc_message msg = {
6273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6274 .rpc_cred = state->owner->so_cred,
6275 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006276 struct rpc_task_setup task_setup_data = {
6277 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006278 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006279 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006280 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006281 .flags = RPC_TASK_ASYNC,
6282 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006283 int ret;
6284
Harvey Harrison3110ff82008-05-02 13:42:44 -07006285 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006286 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006287 fl->fl_u.nfs4_fl.owner,
6288 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006289 if (data == NULL)
6290 return -ENOMEM;
6291 if (IS_SETLKW(cmd))
6292 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006293 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006294 msg.rpc_argp = &data->arg;
6295 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006296 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006297 if (recovery_type > NFS_LOCK_NEW) {
6298 if (recovery_type == NFS_LOCK_RECLAIM)
6299 data->arg.reclaim = NFS_LOCK_RECLAIM;
6300 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006301 } else
6302 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006303 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006304 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006305 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006306 ret = nfs4_wait_for_completion_rpc_task(task);
6307 if (ret == 0) {
6308 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006309 if (ret)
6310 nfs4_handle_setlk_error(data->server, data->lsp,
6311 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006312 } else
6313 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006314 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006315 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006316 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318}
6319
6320static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6321{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006322 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006323 struct nfs4_exception exception = {
6324 .inode = state->inode,
6325 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006326 int err;
6327
6328 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006329 /* Cache the lock if possible... */
6330 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6331 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006332 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006333 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006334 break;
6335 nfs4_handle_exception(server, err, &exception);
6336 } while (exception.retry);
6337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338}
6339
6340static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6341{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006342 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006343 struct nfs4_exception exception = {
6344 .inode = state->inode,
6345 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006346 int err;
6347
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006348 err = nfs4_set_lock_state(state, request);
6349 if (err != 0)
6350 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006351 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006352 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6353 return 0;
6354 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006355 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006356 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6357 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006358 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006359 switch (err) {
6360 default:
6361 goto out;
6362 case -NFS4ERR_GRACE:
6363 case -NFS4ERR_DELAY:
6364 nfs4_handle_exception(server, err, &exception);
6365 err = 0;
6366 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006367 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006368out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370}
6371
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006372#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006373static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6374{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006375 struct nfs4_lock_state *lsp;
6376 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006377
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006378 status = nfs4_set_lock_state(state, request);
6379 if (status != 0)
6380 return status;
6381 lsp = request->fl_u.nfs4_fl.owner;
6382 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6383 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6384 return 0;
6385 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006386 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006387}
6388#endif
6389
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6391{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006392 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006393 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006394 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006395 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Trond Myklebust01c3b862006-06-29 16:38:39 -04006397 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006398 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006399 if (status < 0)
6400 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006401 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006402 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006403 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006404 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006405 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006406 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006407 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006408 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006409 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006410 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006411 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006412 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006413 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006414 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006415out:
6416 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 return status;
6418}
6419
6420static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6421{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006422 struct nfs4_exception exception = {
6423 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006424 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 int err;
6427
6428 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006429 err = _nfs4_proc_setlk(state, cmd, request);
6430 if (err == -NFS4ERR_DENIED)
6431 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006433 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 } while (exception.retry);
6435 return err;
6436}
6437
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006438#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6439#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6440
6441static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006442nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6443 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006444{
6445 int status = -ERESTARTSYS;
6446 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6447
6448 while(!signalled()) {
6449 status = nfs4_proc_setlk(state, cmd, request);
6450 if ((status != -EAGAIN) || IS_SETLK(cmd))
6451 break;
6452 freezable_schedule_timeout_interruptible(timeout);
6453 timeout *= 2;
6454 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6455 status = -ERESTARTSYS;
6456 }
6457 return status;
6458}
6459
Jeff Laytona1d617d82016-09-17 18:17:39 -04006460#ifdef CONFIG_NFS_V4_1
6461struct nfs4_lock_waiter {
6462 struct task_struct *task;
6463 struct inode *inode;
6464 struct nfs_lowner *owner;
6465 bool notified;
6466};
6467
6468static int
6469nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6470{
6471 int ret;
6472 struct cb_notify_lock_args *cbnl = key;
6473 struct nfs4_lock_waiter *waiter = wait->private;
6474 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6475 *wowner = waiter->owner;
6476
6477 /* Only wake if the callback was for the same owner */
6478 if (lowner->clientid != wowner->clientid ||
6479 lowner->id != wowner->id ||
6480 lowner->s_dev != wowner->s_dev)
6481 return 0;
6482
6483 /* Make sure it's for the right inode */
6484 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6485 return 0;
6486
6487 waiter->notified = true;
6488
6489 /* override "private" so we can use default_wake_function */
6490 wait->private = waiter->task;
6491 ret = autoremove_wake_function(wait, mode, flags, key);
6492 wait->private = waiter;
6493 return ret;
6494}
6495
6496static int
6497nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6498{
6499 int status = -ERESTARTSYS;
6500 unsigned long flags;
6501 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6502 struct nfs_server *server = NFS_SERVER(state->inode);
6503 struct nfs_client *clp = server->nfs_client;
6504 wait_queue_head_t *q = &clp->cl_lock_waitq;
6505 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6506 .id = lsp->ls_seqid.owner_id,
6507 .s_dev = server->s_dev };
6508 struct nfs4_lock_waiter waiter = { .task = current,
6509 .inode = state->inode,
6510 .owner = &owner,
6511 .notified = false };
6512 wait_queue_t wait;
6513
6514 /* Don't bother with waitqueue if we don't expect a callback */
6515 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6516 return nfs4_retry_setlk_simple(state, cmd, request);
6517
6518 init_wait(&wait);
6519 wait.private = &waiter;
6520 wait.func = nfs4_wake_lock_waiter;
6521 add_wait_queue(q, &wait);
6522
6523 while(!signalled()) {
6524 status = nfs4_proc_setlk(state, cmd, request);
6525 if ((status != -EAGAIN) || IS_SETLK(cmd))
6526 break;
6527
6528 status = -ERESTARTSYS;
6529 spin_lock_irqsave(&q->lock, flags);
6530 if (waiter.notified) {
6531 spin_unlock_irqrestore(&q->lock, flags);
6532 continue;
6533 }
6534 set_current_state(TASK_INTERRUPTIBLE);
6535 spin_unlock_irqrestore(&q->lock, flags);
6536
6537 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6538 }
6539
6540 finish_wait(q, &wait);
6541 return status;
6542}
6543#else /* !CONFIG_NFS_V4_1 */
6544static inline int
6545nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6546{
6547 return nfs4_retry_setlk_simple(state, cmd, request);
6548}
6549#endif
6550
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551static int
6552nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6553{
6554 struct nfs_open_context *ctx;
6555 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 int status;
6557
6558 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006559 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 state = ctx->state;
6561
6562 if (request->fl_start < 0 || request->fl_end < 0)
6563 return -EINVAL;
6564
Trond Myklebustd9531262009-07-21 19:22:38 -04006565 if (IS_GETLK(cmd)) {
6566 if (state != NULL)
6567 return nfs4_proc_getlk(state, F_GETLK, request);
6568 return 0;
6569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
6571 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6572 return -EINVAL;
6573
Trond Myklebustd9531262009-07-21 19:22:38 -04006574 if (request->fl_type == F_UNLCK) {
6575 if (state != NULL)
6576 return nfs4_proc_unlck(state, cmd, request);
6577 return 0;
6578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
Trond Myklebustd9531262009-07-21 19:22:38 -04006580 if (state == NULL)
6581 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006582
6583 if ((request->fl_flags & FL_POSIX) &&
6584 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6585 return -ENOLCK;
6586
Trond Myklebust55725512012-04-18 12:48:35 -04006587 /*
6588 * Don't rely on the VFS having checked the file open mode,
6589 * since it won't do this for flock() locks.
6590 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006591 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006592 case F_RDLCK:
6593 if (!(filp->f_mode & FMODE_READ))
6594 return -EBADF;
6595 break;
6596 case F_WRLCK:
6597 if (!(filp->f_mode & FMODE_WRITE))
6598 return -EBADF;
6599 }
6600
Jeff Layton1ea67db2016-09-17 18:17:37 -04006601 status = nfs4_set_lock_state(state, request);
6602 if (status != 0)
6603 return status;
6604
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006605 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606}
6607
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006608int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006609{
6610 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006611 int err;
6612
6613 err = nfs4_set_lock_state(state, fl);
6614 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006615 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006616 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006617 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006618}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006619
Trond Myklebustcf470c32012-03-07 13:49:12 -05006620struct nfs_release_lockowner_data {
6621 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006622 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006623 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006624 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006625 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006626};
6627
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006628static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6629{
6630 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006631 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006632 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6633 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006634 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006635 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006636}
6637
6638static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6639{
6640 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006641 struct nfs_server *server = data->server;
6642
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006643 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006644
6645 switch (task->tk_status) {
6646 case 0:
6647 renew_lease(server, data->timestamp);
6648 break;
6649 case -NFS4ERR_STALE_CLIENTID:
6650 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006651 nfs4_schedule_lease_recovery(server->nfs_client);
6652 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006653 case -NFS4ERR_LEASE_MOVED:
6654 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006655 if (nfs4_async_handle_error(task, server,
6656 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006657 rpc_restart_call_prepare(task);
6658 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006659}
6660
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006661static void nfs4_release_lockowner_release(void *calldata)
6662{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006663 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006664 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006665 kfree(calldata);
6666}
6667
Trond Myklebust17280172012-03-11 13:11:00 -04006668static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006669 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6670 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006671 .rpc_release = nfs4_release_lockowner_release,
6672};
6673
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006674static void
6675nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006676{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006677 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006678 struct rpc_message msg = {
6679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6680 };
6681
6682 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006683 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006684
Trond Myklebustcf470c32012-03-07 13:49:12 -05006685 data = kmalloc(sizeof(*data), GFP_NOFS);
6686 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006687 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006688 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006689 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006690 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6691 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6692 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006693
Trond Myklebustcf470c32012-03-07 13:49:12 -05006694 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006695 msg.rpc_resp = &data->res;
6696 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006697 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006698}
6699
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006700#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6701
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006702static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006703 struct dentry *unused, struct inode *inode,
6704 const char *key, const void *buf,
6705 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006706{
Al Viro59301222016-05-27 10:19:30 -04006707 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006708}
6709
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006710static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006711 struct dentry *unused, struct inode *inode,
6712 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006713{
Al Virob2968212016-04-10 20:48:24 -04006714 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006715}
6716
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006717static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006718{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006719 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006720}
6721
David Quigleyc9bccef2013-05-22 12:50:45 -04006722#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006723
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006724static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006725 struct dentry *unused, struct inode *inode,
6726 const char *key, const void *buf,
6727 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006728{
6729 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006730 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006731
6732 return -EOPNOTSUPP;
6733}
6734
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006735static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006736 struct dentry *unused, struct inode *inode,
6737 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006738{
6739 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006740 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006741 return -EOPNOTSUPP;
6742}
6743
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006744static ssize_t
6745nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006746{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006747 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006748
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006749 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6750 len = security_inode_listsecurity(inode, list, list_len);
6751 if (list_len && len > list_len)
6752 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006753 }
6754 return len;
6755}
6756
6757static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6758 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006759 .get = nfs4_xattr_get_nfs4_label,
6760 .set = nfs4_xattr_set_nfs4_label,
6761};
David Quigleyc9bccef2013-05-22 12:50:45 -04006762
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006763#else
6764
6765static ssize_t
6766nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6767{
6768 return 0;
6769}
6770
6771#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006772
Andy Adamson533eb462011-06-13 18:25:56 -04006773/*
6774 * nfs_fhget will use either the mounted_on_fileid or the fileid
6775 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006776static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6777{
Andy Adamson533eb462011-06-13 18:25:56 -04006778 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6779 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6780 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006781 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006782 return;
6783
6784 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006785 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006786 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6787 fattr->nlink = 2;
6788}
6789
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006790static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6791 const struct qstr *name,
6792 struct nfs4_fs_locations *fs_locations,
6793 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006794{
6795 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006796 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006797 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006798 };
6799 struct nfs4_fs_locations_arg args = {
6800 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006801 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006802 .page = page,
6803 .bitmask = bitmask,
6804 };
Benny Halevy22958462009-04-01 09:22:02 -04006805 struct nfs4_fs_locations_res res = {
6806 .fs_locations = fs_locations,
6807 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006808 struct rpc_message msg = {
6809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6810 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006811 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006812 };
6813 int status;
6814
Harvey Harrison3110ff82008-05-02 13:42:44 -07006815 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006816
6817 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6818 * is not supported */
6819 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6820 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6821 else
6822 bitmask[0] |= FATTR4_WORD0_FILEID;
6823
Trond Myklebustc228fd32007-01-13 02:28:11 -05006824 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006825 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006826 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006827 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006828 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006829 return status;
6830}
6831
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006832int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6833 const struct qstr *name,
6834 struct nfs4_fs_locations *fs_locations,
6835 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006836{
6837 struct nfs4_exception exception = { };
6838 int err;
6839 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006840 err = _nfs4_proc_fs_locations(client, dir, name,
6841 fs_locations, page);
6842 trace_nfs4_get_fs_locations(dir, name, err);
6843 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006844 &exception);
6845 } while (exception.retry);
6846 return err;
6847}
6848
Chuck Leverb03d7352013-10-17 14:12:50 -04006849/*
6850 * This operation also signals the server that this client is
6851 * performing migration recovery. The server can stop returning
6852 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6853 * appended to this compound to identify the client ID which is
6854 * performing recovery.
6855 */
6856static int _nfs40_proc_get_locations(struct inode *inode,
6857 struct nfs4_fs_locations *locations,
6858 struct page *page, struct rpc_cred *cred)
6859{
6860 struct nfs_server *server = NFS_SERVER(inode);
6861 struct rpc_clnt *clnt = server->client;
6862 u32 bitmask[2] = {
6863 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6864 };
6865 struct nfs4_fs_locations_arg args = {
6866 .clientid = server->nfs_client->cl_clientid,
6867 .fh = NFS_FH(inode),
6868 .page = page,
6869 .bitmask = bitmask,
6870 .migration = 1, /* skip LOOKUP */
6871 .renew = 1, /* append RENEW */
6872 };
6873 struct nfs4_fs_locations_res res = {
6874 .fs_locations = locations,
6875 .migration = 1,
6876 .renew = 1,
6877 };
6878 struct rpc_message msg = {
6879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6880 .rpc_argp = &args,
6881 .rpc_resp = &res,
6882 .rpc_cred = cred,
6883 };
6884 unsigned long now = jiffies;
6885 int status;
6886
6887 nfs_fattr_init(&locations->fattr);
6888 locations->server = server;
6889 locations->nlocations = 0;
6890
6891 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6892 nfs4_set_sequence_privileged(&args.seq_args);
6893 status = nfs4_call_sync_sequence(clnt, server, &msg,
6894 &args.seq_args, &res.seq_res);
6895 if (status)
6896 return status;
6897
6898 renew_lease(server, now);
6899 return 0;
6900}
6901
6902#ifdef CONFIG_NFS_V4_1
6903
6904/*
6905 * This operation also signals the server that this client is
6906 * performing migration recovery. The server can stop asserting
6907 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6908 * performing this operation is identified in the SEQUENCE
6909 * operation in this compound.
6910 *
6911 * When the client supports GETATTR(fs_locations_info), it can
6912 * be plumbed in here.
6913 */
6914static int _nfs41_proc_get_locations(struct inode *inode,
6915 struct nfs4_fs_locations *locations,
6916 struct page *page, struct rpc_cred *cred)
6917{
6918 struct nfs_server *server = NFS_SERVER(inode);
6919 struct rpc_clnt *clnt = server->client;
6920 u32 bitmask[2] = {
6921 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6922 };
6923 struct nfs4_fs_locations_arg args = {
6924 .fh = NFS_FH(inode),
6925 .page = page,
6926 .bitmask = bitmask,
6927 .migration = 1, /* skip LOOKUP */
6928 };
6929 struct nfs4_fs_locations_res res = {
6930 .fs_locations = locations,
6931 .migration = 1,
6932 };
6933 struct rpc_message msg = {
6934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6935 .rpc_argp = &args,
6936 .rpc_resp = &res,
6937 .rpc_cred = cred,
6938 };
6939 int status;
6940
6941 nfs_fattr_init(&locations->fattr);
6942 locations->server = server;
6943 locations->nlocations = 0;
6944
6945 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6946 nfs4_set_sequence_privileged(&args.seq_args);
6947 status = nfs4_call_sync_sequence(clnt, server, &msg,
6948 &args.seq_args, &res.seq_res);
6949 if (status == NFS4_OK &&
6950 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6951 status = -NFS4ERR_LEASE_MOVED;
6952 return status;
6953}
6954
6955#endif /* CONFIG_NFS_V4_1 */
6956
6957/**
6958 * nfs4_proc_get_locations - discover locations for a migrated FSID
6959 * @inode: inode on FSID that is migrating
6960 * @locations: result of query
6961 * @page: buffer
6962 * @cred: credential to use for this operation
6963 *
6964 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6965 * operation failed, or a negative errno if a local error occurred.
6966 *
6967 * On success, "locations" is filled in, but if the server has
6968 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6969 * asserted.
6970 *
6971 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6972 * from this client that require migration recovery.
6973 */
6974int nfs4_proc_get_locations(struct inode *inode,
6975 struct nfs4_fs_locations *locations,
6976 struct page *page, struct rpc_cred *cred)
6977{
6978 struct nfs_server *server = NFS_SERVER(inode);
6979 struct nfs_client *clp = server->nfs_client;
6980 const struct nfs4_mig_recovery_ops *ops =
6981 clp->cl_mvops->mig_recovery_ops;
6982 struct nfs4_exception exception = { };
6983 int status;
6984
6985 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6986 (unsigned long long)server->fsid.major,
6987 (unsigned long long)server->fsid.minor,
6988 clp->cl_hostname);
6989 nfs_display_fhandle(NFS_FH(inode), __func__);
6990
6991 do {
6992 status = ops->get_locations(inode, locations, page, cred);
6993 if (status != -NFS4ERR_DELAY)
6994 break;
6995 nfs4_handle_exception(server, status, &exception);
6996 } while (exception.retry);
6997 return status;
6998}
6999
Chuck Lever44c99932013-10-17 14:13:30 -04007000/*
7001 * This operation also signals the server that this client is
7002 * performing "lease moved" recovery. The server can stop
7003 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7004 * is appended to this compound to identify the client ID which is
7005 * performing recovery.
7006 */
7007static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7008{
7009 struct nfs_server *server = NFS_SERVER(inode);
7010 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7011 struct rpc_clnt *clnt = server->client;
7012 struct nfs4_fsid_present_arg args = {
7013 .fh = NFS_FH(inode),
7014 .clientid = clp->cl_clientid,
7015 .renew = 1, /* append RENEW */
7016 };
7017 struct nfs4_fsid_present_res res = {
7018 .renew = 1,
7019 };
7020 struct rpc_message msg = {
7021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7022 .rpc_argp = &args,
7023 .rpc_resp = &res,
7024 .rpc_cred = cred,
7025 };
7026 unsigned long now = jiffies;
7027 int status;
7028
7029 res.fh = nfs_alloc_fhandle();
7030 if (res.fh == NULL)
7031 return -ENOMEM;
7032
7033 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7034 nfs4_set_sequence_privileged(&args.seq_args);
7035 status = nfs4_call_sync_sequence(clnt, server, &msg,
7036 &args.seq_args, &res.seq_res);
7037 nfs_free_fhandle(res.fh);
7038 if (status)
7039 return status;
7040
7041 do_renew_lease(clp, now);
7042 return 0;
7043}
7044
7045#ifdef CONFIG_NFS_V4_1
7046
7047/*
7048 * This operation also signals the server that this client is
7049 * performing "lease moved" recovery. The server can stop asserting
7050 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7051 * this operation is identified in the SEQUENCE operation in this
7052 * compound.
7053 */
7054static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7055{
7056 struct nfs_server *server = NFS_SERVER(inode);
7057 struct rpc_clnt *clnt = server->client;
7058 struct nfs4_fsid_present_arg args = {
7059 .fh = NFS_FH(inode),
7060 };
7061 struct nfs4_fsid_present_res res = {
7062 };
7063 struct rpc_message msg = {
7064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7065 .rpc_argp = &args,
7066 .rpc_resp = &res,
7067 .rpc_cred = cred,
7068 };
7069 int status;
7070
7071 res.fh = nfs_alloc_fhandle();
7072 if (res.fh == NULL)
7073 return -ENOMEM;
7074
7075 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7076 nfs4_set_sequence_privileged(&args.seq_args);
7077 status = nfs4_call_sync_sequence(clnt, server, &msg,
7078 &args.seq_args, &res.seq_res);
7079 nfs_free_fhandle(res.fh);
7080 if (status == NFS4_OK &&
7081 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7082 status = -NFS4ERR_LEASE_MOVED;
7083 return status;
7084}
7085
7086#endif /* CONFIG_NFS_V4_1 */
7087
7088/**
7089 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7090 * @inode: inode on FSID to check
7091 * @cred: credential to use for this operation
7092 *
7093 * Server indicates whether the FSID is present, moved, or not
7094 * recognized. This operation is necessary to clear a LEASE_MOVED
7095 * condition for this client ID.
7096 *
7097 * Returns NFS4_OK if the FSID is present on this server,
7098 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7099 * NFS4ERR code if some error occurred on the server, or a
7100 * negative errno if a local failure occurred.
7101 */
7102int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7103{
7104 struct nfs_server *server = NFS_SERVER(inode);
7105 struct nfs_client *clp = server->nfs_client;
7106 const struct nfs4_mig_recovery_ops *ops =
7107 clp->cl_mvops->mig_recovery_ops;
7108 struct nfs4_exception exception = { };
7109 int status;
7110
7111 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7112 (unsigned long long)server->fsid.major,
7113 (unsigned long long)server->fsid.minor,
7114 clp->cl_hostname);
7115 nfs_display_fhandle(NFS_FH(inode), __func__);
7116
7117 do {
7118 status = ops->fsid_present(inode, cred);
7119 if (status != -NFS4ERR_DELAY)
7120 break;
7121 nfs4_handle_exception(server, status, &exception);
7122 } while (exception.retry);
7123 return status;
7124}
7125
Andy Adamson5ec16a82013-08-08 10:57:55 -04007126/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007127 * If 'use_integrity' is true and the state managment nfs_client
7128 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7129 * and the machine credential as per RFC3530bis and RFC5661 Security
7130 * Considerations sections. Otherwise, just use the user cred with the
7131 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007132 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007133static 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 +00007134{
7135 int status;
7136 struct nfs4_secinfo_arg args = {
7137 .dir_fh = NFS_FH(dir),
7138 .name = name,
7139 };
7140 struct nfs4_secinfo_res res = {
7141 .flavors = flavors,
7142 };
7143 struct rpc_message msg = {
7144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7145 .rpc_argp = &args,
7146 .rpc_resp = &res,
7147 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007148 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007149 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007150
7151 if (use_integrity) {
7152 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007153 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7154 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007155 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007156
7157 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007158
7159 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7160 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7161
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007162 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7163 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007164 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007165
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007166 if (cred)
7167 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007168
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007169 return status;
7170}
7171
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007172int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7173 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007174{
7175 struct nfs4_exception exception = { };
7176 int err;
7177 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007178 err = -NFS4ERR_WRONGSEC;
7179
7180 /* try to use integrity protection with machine cred */
7181 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7182 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7183
7184 /*
7185 * if unable to use integrity protection, or SECINFO with
7186 * integrity protection returns NFS4ERR_WRONGSEC (which is
7187 * disallowed by spec, but exists in deployed servers) use
7188 * the current filesystem's rpc_client and the user cred.
7189 */
7190 if (err == -NFS4ERR_WRONGSEC)
7191 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7192
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007193 trace_nfs4_secinfo(dir, name, err);
7194 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007195 &exception);
7196 } while (exception.retry);
7197 return err;
7198}
7199
Andy Adamson557134a2009-04-01 09:21:53 -04007200#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007201/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007202 * Check the exchange flags returned by the server for invalid flags, having
7203 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7204 * DS flags set.
7205 */
7206static int nfs4_check_cl_exchange_flags(u32 flags)
7207{
7208 if (flags & ~EXCHGID4_FLAG_MASK_R)
7209 goto out_inval;
7210 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7211 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7212 goto out_inval;
7213 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7214 goto out_inval;
7215 return NFS_OK;
7216out_inval:
7217 return -NFS4ERR_INVAL;
7218}
7219
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007220static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007221nfs41_same_server_scope(struct nfs41_server_scope *a,
7222 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007223{
7224 if (a->server_scope_sz == b->server_scope_sz &&
7225 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7226 return true;
7227
7228 return false;
7229}
7230
Andy Adamson02a95de2016-02-05 16:08:37 -05007231static void
7232nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7233{
7234}
7235
7236static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7237 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7238};
7239
Andy Adamson357f54d2010-12-14 10:11:57 -05007240/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007241 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007242 *
7243 * The 4.1 client currently uses the same TCP connection for the
7244 * fore and backchannel.
7245 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007246static
7247int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7248 struct rpc_xprt *xprt,
7249 struct nfs_client *clp,
7250 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007251{
7252 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007253 struct nfs41_bind_conn_to_session_args args = {
7254 .client = clp,
7255 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7256 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007257 struct nfs41_bind_conn_to_session_res res;
7258 struct rpc_message msg = {
7259 .rpc_proc =
7260 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007261 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007262 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007263 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007264 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007265 struct rpc_task_setup task_setup_data = {
7266 .rpc_client = clnt,
7267 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007268 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007269 .rpc_message = &msg,
7270 .flags = RPC_TASK_TIMEOUT,
7271 };
7272 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007273
7274 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007275
Trond Myklebust71a097c2015-02-18 09:27:18 -08007276 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7277 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7278 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007279
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007280 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7281 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7282 args.dir = NFS4_CDFC4_FORE;
7283
7284 task = rpc_run_task(&task_setup_data);
7285 if (!IS_ERR(task)) {
7286 status = task->tk_status;
7287 rpc_put_task(task);
7288 } else
7289 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007290 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007291 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007292 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007293 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7294 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7295 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007296 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007297 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007298 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007299 dprintk("NFS: %s: Unexpected direction from server\n",
7300 __func__);
7301 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007302 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007303 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007304 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007305 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7306 __func__);
7307 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007308 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007309 }
7310 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007311out:
7312 dprintk("<-- %s status= %d\n", __func__, status);
7313 return status;
7314}
7315
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007316struct rpc_bind_conn_calldata {
7317 struct nfs_client *clp;
7318 struct rpc_cred *cred;
7319};
7320
7321static int
7322nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7323 struct rpc_xprt *xprt,
7324 void *calldata)
7325{
7326 struct rpc_bind_conn_calldata *p = calldata;
7327
7328 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7329}
7330
7331int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7332{
7333 struct rpc_bind_conn_calldata data = {
7334 .clp = clp,
7335 .cred = cred,
7336 };
7337 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7338 nfs4_proc_bind_conn_to_session_callback, &data);
7339}
7340
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007341/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007342 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7343 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007344 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007345static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7346 .how = SP4_MACH_CRED,
7347 .enforce.u.words = {
7348 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7349 1 << (OP_EXCHANGE_ID - 32) |
7350 1 << (OP_CREATE_SESSION - 32) |
7351 1 << (OP_DESTROY_SESSION - 32) |
7352 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007353 },
7354 .allow.u.words = {
7355 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007356 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007357 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007358 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007359 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007360 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007361 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007362 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007363 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007364 1 << (OP_FREE_STATEID - 32) |
7365 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007366 }
7367};
7368
7369/*
7370 * Select the state protection mode for client `clp' given the server results
7371 * from exchange_id in `sp'.
7372 *
7373 * Returns 0 on success, negative errno otherwise.
7374 */
7375static int nfs4_sp4_select_mode(struct nfs_client *clp,
7376 struct nfs41_state_protection *sp)
7377{
7378 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7379 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7380 1 << (OP_EXCHANGE_ID - 32) |
7381 1 << (OP_CREATE_SESSION - 32) |
7382 1 << (OP_DESTROY_SESSION - 32) |
7383 1 << (OP_DESTROY_CLIENTID - 32)
7384 };
7385 unsigned int i;
7386
7387 if (sp->how == SP4_MACH_CRED) {
7388 /* Print state protect result */
7389 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7390 for (i = 0; i <= LAST_NFS4_OP; i++) {
7391 if (test_bit(i, sp->enforce.u.longs))
7392 dfprintk(MOUNT, " enforce op %d\n", i);
7393 if (test_bit(i, sp->allow.u.longs))
7394 dfprintk(MOUNT, " allow op %d\n", i);
7395 }
7396
7397 /* make sure nothing is on enforce list that isn't supported */
7398 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7399 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7400 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7401 return -EINVAL;
7402 }
7403 }
7404
7405 /*
7406 * Minimal mode - state operations are allowed to use machine
7407 * credential. Note this already happens by default, so the
7408 * client doesn't have to do anything more than the negotiation.
7409 *
7410 * NOTE: we don't care if EXCHANGE_ID is in the list -
7411 * we're already using the machine cred for exchange_id
7412 * and will never use a different cred.
7413 */
7414 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7415 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7416 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7417 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7418 dfprintk(MOUNT, "sp4_mach_cred:\n");
7419 dfprintk(MOUNT, " minimal mode enabled\n");
7420 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7421 } else {
7422 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7423 return -EINVAL;
7424 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007425
7426 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007427 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7428 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007429 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7430 dfprintk(MOUNT, " cleanup mode enabled\n");
7431 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7432 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007433
Andrew Elble99ade3c2015-12-02 09:39:51 -05007434 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7435 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7436 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7437 &clp->cl_sp4_flags);
7438 }
7439
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007440 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7441 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7442 dfprintk(MOUNT, " secinfo mode enabled\n");
7443 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7444 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007445
7446 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7447 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7448 dfprintk(MOUNT, " stateid mode enabled\n");
7449 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7450 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007451
7452 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7453 dfprintk(MOUNT, " write mode enabled\n");
7454 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7455 }
7456
7457 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7458 dfprintk(MOUNT, " commit mode enabled\n");
7459 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7460 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007461 }
7462
7463 return 0;
7464}
7465
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466struct nfs41_exchange_id_data {
7467 struct nfs41_exchange_id_res res;
7468 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007469 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007470 int rpc_status;
7471};
7472
7473static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 struct nfs41_exchange_id_data *cdata =
7476 (struct nfs41_exchange_id_data *)data;
7477 struct nfs_client *clp = cdata->args.client;
7478 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007480 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007481
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007482 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007484
7485 if (cdata->xprt && status == 0) {
7486 status = nfs4_detect_session_trunking(clp, &cdata->res,
7487 cdata->xprt);
7488 goto out;
7489 }
7490
Andy Adamson8d89bd72016-09-09 09:22:18 -04007491 if (status == 0)
7492 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007493
Chuck Lever177313f2012-05-21 22:44:58 -04007494 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 clp->cl_clientid = cdata->res.clientid;
7496 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007497 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007499 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 &clp->cl_session->session_state);
7501 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007502 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007503
Chuck Leveracdeb692012-05-21 22:46:16 -04007504 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 clp->cl_serverowner = cdata->res.server_owner;
7506 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007507
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007508 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007509 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 clp->cl_implid = cdata->res.impl_id;
7511 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007512
Chuck Lever177313f2012-05-21 22:44:58 -04007513 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007514 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007516 dprintk("%s: server_scope mismatch detected\n",
7517 __func__);
7518 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007519 kfree(clp->cl_serverscope);
7520 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007521 }
7522
Chuck Lever177313f2012-05-21 22:44:58 -04007523 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007524 clp->cl_serverscope = cdata->res.server_scope;
7525 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007526 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007527 /* Save the EXCHANGE_ID verifier session trunk tests */
7528 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7529 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007531out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007533 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534}
7535
7536static void nfs4_exchange_id_release(void *data)
7537{
7538 struct nfs41_exchange_id_data *cdata =
7539 (struct nfs41_exchange_id_data *)data;
7540
7541 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007542 if (cdata->xprt) {
7543 xprt_put(cdata->xprt);
7544 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7545 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007546 kfree(cdata->res.impl_id);
7547 kfree(cdata->res.server_scope);
7548 kfree(cdata->res.server_owner);
7549 kfree(cdata);
7550}
7551
7552static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7553 .rpc_call_done = nfs4_exchange_id_done,
7554 .rpc_release = nfs4_exchange_id_release,
7555};
7556
Benny Halevy99fe60d2009-04-01 09:22:29 -04007557/*
7558 * _nfs4_proc_exchange_id()
7559 *
7560 * Wrapper for EXCHANGE_ID operation.
7561 */
7562static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007563 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007564{
7565 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007566 struct rpc_message msg = {
7567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007568 .rpc_cred = cred,
7569 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007570 struct rpc_task_setup task_setup_data = {
7571 .rpc_client = clp->cl_rpcclient,
7572 .callback_ops = &nfs4_exchange_id_call_ops,
7573 .rpc_message = &msg,
7574 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7575 };
7576 struct nfs41_exchange_id_data *calldata;
7577 struct rpc_task *task;
7578 int status = -EIO;
7579
7580 if (!atomic_inc_not_zero(&clp->cl_count))
7581 goto out;
7582
7583 status = -ENOMEM;
7584 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7585 if (!calldata)
7586 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007587
Andy Adamsonad0849a2016-09-09 09:22:28 -04007588 if (!xprt)
7589 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007590
7591 status = nfs4_init_uniform_client_string(clp);
7592 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007593 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007594
7595 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7596 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7597 clp->cl_owner_id);
7598
Andy Adamson8d89bd72016-09-09 09:22:18 -04007599 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7600 GFP_NOFS);
7601 status = -ENOMEM;
7602 if (unlikely(calldata->res.server_owner == NULL))
7603 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007604
Andy Adamson8d89bd72016-09-09 09:22:18 -04007605 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007606 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007607 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007608 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007609
Andy Adamson8d89bd72016-09-09 09:22:18 -04007610 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7611 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007612 goto out_server_scope;
7613
7614 switch (sp4_how) {
7615 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007616 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007617 break;
7618
7619 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007620 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007621 break;
7622
7623 default:
7624 /* unsupported! */
7625 WARN_ON_ONCE(1);
7626 status = -EINVAL;
7627 goto out_impl_id;
7628 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007629 if (xprt) {
7630 calldata->xprt = xprt;
7631 task_setup_data.rpc_xprt = xprt;
7632 task_setup_data.flags =
7633 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7634 calldata->args.verifier = &clp->cl_confirm;
7635 } else {
7636 calldata->args.verifier = &verifier;
7637 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007638 calldata->args.client = clp;
7639#ifdef CONFIG_NFS_V4_1_MIGRATION
7640 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7641 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7642 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7643#else
7644 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7645 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7646#endif
7647 msg.rpc_argp = &calldata->args;
7648 msg.rpc_resp = &calldata->res;
7649 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007650
Andy Adamson8d89bd72016-09-09 09:22:18 -04007651 task = rpc_run_task(&task_setup_data);
7652 if (IS_ERR(task)) {
7653 status = PTR_ERR(task);
7654 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007655 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007656
Andy Adamsonad0849a2016-09-09 09:22:28 -04007657 if (!xprt) {
7658 status = rpc_wait_for_completion_task(task);
7659 if (!status)
7660 status = calldata->rpc_status;
7661 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007662 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007663
Andy Adamson8d89bd72016-09-09 09:22:18 -04007664 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007665out:
Chuck Lever177313f2012-05-21 22:44:58 -04007666 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007667 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007668 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007669 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007670 clp->cl_implid->date.seconds,
7671 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007672 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007673 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007674
7675out_impl_id:
7676 kfree(calldata->res.impl_id);
7677out_server_scope:
7678 kfree(calldata->res.server_scope);
7679out_server_owner:
7680 kfree(calldata->res.server_owner);
7681out_calldata:
7682 kfree(calldata);
7683 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007684}
7685
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007686/*
7687 * nfs4_proc_exchange_id()
7688 *
7689 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7690 *
7691 * Since the clientid has expired, all compounds using sessions
7692 * associated with the stale clientid will be returning
7693 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7694 * be in some phase of session reset.
7695 *
7696 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7697 */
7698int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7699{
7700 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7701 int status;
7702
7703 /* try SP4_MACH_CRED if krb5i/p */
7704 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7705 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007706 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007707 if (!status)
7708 return 0;
7709 }
7710
7711 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007712 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007713}
7714
Andy Adamson04fa2c62016-09-09 09:22:29 -04007715/**
7716 * nfs4_test_session_trunk
7717 *
7718 * This is an add_xprt_test() test function called from
7719 * rpc_clnt_setup_test_and_add_xprt.
7720 *
7721 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7722 * and is dereferrenced in nfs4_exchange_id_release
7723 *
7724 * Upon success, add the new transport to the rpc_clnt
7725 *
7726 * @clnt: struct rpc_clnt to get new transport
7727 * @xprt: the rpc_xprt to test
7728 * @data: call data for _nfs4_proc_exchange_id.
7729 */
7730int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7731 void *data)
7732{
7733 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7734 u32 sp4_how;
7735
7736 dprintk("--> %s try %s\n", __func__,
7737 xprt->address_strings[RPC_DISPLAY_ADDR]);
7738
7739 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7740
7741 /* Test connection for session trunking. Async exchange_id call */
7742 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7743}
7744EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7745
Trond Myklebust66245532012-05-25 17:18:09 -04007746static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7747 struct rpc_cred *cred)
7748{
7749 struct rpc_message msg = {
7750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7751 .rpc_argp = clp,
7752 .rpc_cred = cred,
7753 };
7754 int status;
7755
7756 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007757 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007758 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007759 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007760 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7761 return status;
7762}
7763
7764static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7765 struct rpc_cred *cred)
7766{
7767 unsigned int loop;
7768 int ret;
7769
7770 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7771 ret = _nfs4_proc_destroy_clientid(clp, cred);
7772 switch (ret) {
7773 case -NFS4ERR_DELAY:
7774 case -NFS4ERR_CLIENTID_BUSY:
7775 ssleep(1);
7776 break;
7777 default:
7778 return ret;
7779 }
7780 }
7781 return 0;
7782}
7783
7784int nfs4_destroy_clientid(struct nfs_client *clp)
7785{
7786 struct rpc_cred *cred;
7787 int ret = 0;
7788
7789 if (clp->cl_mvops->minor_version < 1)
7790 goto out;
7791 if (clp->cl_exchange_flags == 0)
7792 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007793 if (clp->cl_preserve_clid)
7794 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007795 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007796 ret = nfs4_proc_destroy_clientid(clp, cred);
7797 if (cred)
7798 put_rpccred(cred);
7799 switch (ret) {
7800 case 0:
7801 case -NFS4ERR_STALE_CLIENTID:
7802 clp->cl_exchange_flags = 0;
7803 }
7804out:
7805 return ret;
7806}
7807
Andy Adamson2050f0c2009-04-01 09:22:30 -04007808struct nfs4_get_lease_time_data {
7809 struct nfs4_get_lease_time_args *args;
7810 struct nfs4_get_lease_time_res *res;
7811 struct nfs_client *clp;
7812};
7813
7814static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7815 void *calldata)
7816{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007817 struct nfs4_get_lease_time_data *data =
7818 (struct nfs4_get_lease_time_data *)calldata;
7819
7820 dprintk("--> %s\n", __func__);
7821 /* just setup sequence, do not trigger session recovery
7822 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007823 nfs41_setup_sequence(data->clp->cl_session,
7824 &data->args->la_seq_args,
7825 &data->res->lr_seq_res,
7826 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007827 dprintk("<-- %s\n", __func__);
7828}
7829
7830/*
7831 * Called from nfs4_state_manager thread for session setup, so don't recover
7832 * from sequence operation or clientid errors.
7833 */
7834static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7835{
7836 struct nfs4_get_lease_time_data *data =
7837 (struct nfs4_get_lease_time_data *)calldata;
7838
7839 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007840 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7841 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007842 switch (task->tk_status) {
7843 case -NFS4ERR_DELAY:
7844 case -NFS4ERR_GRACE:
7845 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7846 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7847 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007848 /* fall through */
7849 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007850 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007851 return;
7852 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007853 dprintk("<-- %s\n", __func__);
7854}
7855
Trond Myklebust17280172012-03-11 13:11:00 -04007856static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007857 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7858 .rpc_call_done = nfs4_get_lease_time_done,
7859};
7860
7861int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7862{
7863 struct rpc_task *task;
7864 struct nfs4_get_lease_time_args args;
7865 struct nfs4_get_lease_time_res res = {
7866 .lr_fsinfo = fsinfo,
7867 };
7868 struct nfs4_get_lease_time_data data = {
7869 .args = &args,
7870 .res = &res,
7871 .clp = clp,
7872 };
7873 struct rpc_message msg = {
7874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7875 .rpc_argp = &args,
7876 .rpc_resp = &res,
7877 };
7878 struct rpc_task_setup task_setup = {
7879 .rpc_client = clp->cl_rpcclient,
7880 .rpc_message = &msg,
7881 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007882 .callback_data = &data,
7883 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007884 };
7885 int status;
7886
Chuck Levera9c92d62013-08-09 12:48:18 -04007887 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007888 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007889 dprintk("--> %s\n", __func__);
7890 task = rpc_run_task(&task_setup);
7891
7892 if (IS_ERR(task))
7893 status = PTR_ERR(task);
7894 else {
7895 status = task->tk_status;
7896 rpc_put_task(task);
7897 }
7898 dprintk("<-- %s return %d\n", __func__, status);
7899
7900 return status;
7901}
7902
Andy Adamsonfc931582009-04-01 09:22:31 -04007903/*
7904 * Initialize the values to be used by the client in CREATE_SESSION
7905 * If nfs4_init_session set the fore channel request and response sizes,
7906 * use them.
7907 *
7908 * Set the back channel max_resp_sz_cached to zero to force the client to
7909 * always set csa_cachethis to FALSE because the current implementation
7910 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7911 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007912static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7913 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007914{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007915 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007916 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007917
Andy Adamson18aad3d2013-06-26 12:21:49 -04007918 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7919 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7920
Andy Adamsonfc931582009-04-01 09:22:31 -04007921 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007922 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7923 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007924 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007925 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007926
7927 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007928 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 __func__,
7930 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007931 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007932
7933 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007934 args->bc_attrs.max_rqst_sz = max_bc_payload;
7935 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007936 args->bc_attrs.max_resp_sz_cached = 0;
7937 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007938 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007939
7940 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7941 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7942 __func__,
7943 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7944 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7945 args->bc_attrs.max_reqs);
7946}
7947
Trond Myklebust79969dd2015-02-18 11:30:18 -08007948static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7949 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007950{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007951 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007952 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007953
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007954 if (rcvd->max_resp_sz > sent->max_resp_sz)
7955 return -EINVAL;
7956 /*
7957 * Our requested max_ops is the minimum we need; we're not
7958 * prepared to break up compounds into smaller pieces than that.
7959 * So, no point even trying to continue if the server won't
7960 * cooperate:
7961 */
7962 if (rcvd->max_ops < sent->max_ops)
7963 return -EINVAL;
7964 if (rcvd->max_reqs == 0)
7965 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007966 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7967 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007968 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007969}
7970
Trond Myklebust79969dd2015-02-18 11:30:18 -08007971static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7972 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007973{
7974 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007975 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007976
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007977 if (!(res->flags & SESSION4_BACK_CHAN))
7978 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007979 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7980 return -EINVAL;
7981 if (rcvd->max_resp_sz < sent->max_resp_sz)
7982 return -EINVAL;
7983 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7984 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007985 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007986 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007987 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007988 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007989out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007990 return 0;
7991}
Andy Adamson8d353012009-04-01 09:22:32 -04007992
Andy Adamson8d353012009-04-01 09:22:32 -04007993static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007994 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007995{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007996 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007997
Trond Myklebust79969dd2015-02-18 11:30:18 -08007998 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007999 if (ret)
8000 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008001 return nfs4_verify_back_channel_attrs(args, res);
8002}
8003
8004static void nfs4_update_session(struct nfs4_session *session,
8005 struct nfs41_create_session_res *res)
8006{
8007 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008008 /* Mark client id and session as being confirmed */
8009 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8010 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008011 session->flags = res->flags;
8012 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008013 if (res->flags & SESSION4_BACK_CHAN)
8014 memcpy(&session->bc_attrs, &res->bc_attrs,
8015 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008016}
8017
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008018static int _nfs4_proc_create_session(struct nfs_client *clp,
8019 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008020{
8021 struct nfs4_session *session = clp->cl_session;
8022 struct nfs41_create_session_args args = {
8023 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008024 .clientid = clp->cl_clientid,
8025 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008026 .cb_program = NFS4_CALLBACK,
8027 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008028 struct nfs41_create_session_res res;
8029
Andy Adamsonfc931582009-04-01 09:22:31 -04008030 struct rpc_message msg = {
8031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8032 .rpc_argp = &args,
8033 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008034 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008035 };
8036 int status;
8037
Chuck Lever6b26cc82016-05-02 14:40:40 -04008038 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008039 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008040
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008041 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008042 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008043
Trond Myklebustb519d402016-09-11 14:50:01 -04008044 switch (status) {
8045 case -NFS4ERR_STALE_CLIENTID:
8046 case -NFS4ERR_DELAY:
8047 case -ETIMEDOUT:
8048 case -EACCES:
8049 case -EAGAIN:
8050 goto out;
8051 };
8052
8053 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008054 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008055 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008056 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008057 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008058 if (status)
8059 goto out;
8060 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008061 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008062out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008063 return status;
8064}
8065
8066/*
8067 * Issues a CREATE_SESSION operation to the server.
8068 * It is the responsibility of the caller to verify the session is
8069 * expired before calling this routine.
8070 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008071int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008072{
8073 int status;
8074 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008075 struct nfs4_session *session = clp->cl_session;
8076
8077 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8078
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008079 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008080 if (status)
8081 goto out;
8082
Andy Adamsonaacd5532011-11-09 13:58:21 -05008083 /* Init or reset the session slot tables */
8084 status = nfs4_setup_session_slot_tables(session);
8085 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008086 if (status)
8087 goto out;
8088
8089 ptr = (unsigned *)&session->sess_id.data[0];
8090 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8091 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008092out:
8093 dprintk("<-- %s\n", __func__);
8094 return status;
8095}
8096
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008097/*
8098 * Issue the over-the-wire RPC DESTROY_SESSION.
8099 * The caller must serialize access to this routine.
8100 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008101int nfs4_proc_destroy_session(struct nfs4_session *session,
8102 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008103{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008104 struct rpc_message msg = {
8105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8106 .rpc_argp = session,
8107 .rpc_cred = cred,
8108 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008109 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008110
8111 dprintk("--> nfs4_proc_destroy_session\n");
8112
8113 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008114 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8115 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008116
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008117 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008118 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008119
8120 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008121 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008122 "Session has been destroyed regardless...\n", status);
8123
8124 dprintk("<-- nfs4_proc_destroy_session\n");
8125 return status;
8126}
8127
Trond Myklebust7b38c362012-05-23 13:23:31 -04008128/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008129 * Renew the cl_session lease.
8130 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008131struct nfs4_sequence_data {
8132 struct nfs_client *clp;
8133 struct nfs4_sequence_args args;
8134 struct nfs4_sequence_res res;
8135};
8136
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008137static void nfs41_sequence_release(void *data)
8138{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008139 struct nfs4_sequence_data *calldata = data;
8140 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008141
Alexandros Batsakis71358402010-02-05 03:45:05 -08008142 if (atomic_read(&clp->cl_count) > 1)
8143 nfs4_schedule_state_renewal(clp);
8144 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008145 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008146}
8147
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008148static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8149{
8150 switch(task->tk_status) {
8151 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008152 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8153 return -EAGAIN;
8154 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008155 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008156 }
8157 return 0;
8158}
8159
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008160static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008161{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008162 struct nfs4_sequence_data *calldata = data;
8163 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008164
Trond Myklebust14516c32010-07-31 14:29:06 -04008165 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8166 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008167
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008168 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008169 if (task->tk_status < 0) {
8170 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008171 if (atomic_read(&clp->cl_count) == 1)
8172 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008173
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008174 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8175 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008176 return;
8177 }
8178 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008179 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008180out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008181 dprintk("<-- %s\n", __func__);
8182}
8183
8184static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8185{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008186 struct nfs4_sequence_data *calldata = data;
8187 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008188 struct nfs4_sequence_args *args;
8189 struct nfs4_sequence_res *res;
8190
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008191 args = task->tk_msg.rpc_argp;
8192 res = task->tk_msg.rpc_resp;
8193
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008194 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008195}
8196
8197static const struct rpc_call_ops nfs41_sequence_ops = {
8198 .rpc_call_done = nfs41_sequence_call_done,
8199 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008200 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008201};
8202
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008203static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8204 struct rpc_cred *cred,
8205 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008206{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008207 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008208 struct rpc_message msg = {
8209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8210 .rpc_cred = cred,
8211 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008212 struct rpc_task_setup task_setup_data = {
8213 .rpc_client = clp->cl_rpcclient,
8214 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008215 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008216 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008217 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008218
Alexandros Batsakis71358402010-02-05 03:45:05 -08008219 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008220 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008221 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008222 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008223 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008224 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008225 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008226 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008227 if (is_privileged)
8228 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008229 msg.rpc_argp = &calldata->args;
8230 msg.rpc_resp = &calldata->res;
8231 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008232 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008233
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008234 return rpc_run_task(&task_setup_data);
8235}
8236
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008237static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008238{
8239 struct rpc_task *task;
8240 int ret = 0;
8241
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008242 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008243 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008244 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008245 if (IS_ERR(task))
8246 ret = PTR_ERR(task);
8247 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008248 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008249 dprintk("<-- %s status=%d\n", __func__, ret);
8250 return ret;
8251}
8252
8253static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8254{
8255 struct rpc_task *task;
8256 int ret;
8257
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008258 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008259 if (IS_ERR(task)) {
8260 ret = PTR_ERR(task);
8261 goto out;
8262 }
8263 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008264 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008265 ret = task->tk_status;
8266 rpc_put_task(task);
8267out:
8268 dprintk("<-- %s status=%d\n", __func__, ret);
8269 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008270}
8271
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008272struct nfs4_reclaim_complete_data {
8273 struct nfs_client *clp;
8274 struct nfs41_reclaim_complete_args arg;
8275 struct nfs41_reclaim_complete_res res;
8276};
8277
8278static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8279{
8280 struct nfs4_reclaim_complete_data *calldata = data;
8281
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008282 nfs41_setup_sequence(calldata->clp->cl_session,
8283 &calldata->arg.seq_args,
8284 &calldata->res.seq_res,
8285 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008286}
8287
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008288static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8289{
8290 switch(task->tk_status) {
8291 case 0:
8292 case -NFS4ERR_COMPLETE_ALREADY:
8293 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8294 break;
8295 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008296 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008297 /* fall through */
8298 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008299 return -EAGAIN;
8300 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008301 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008302 }
8303 return 0;
8304}
8305
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008306static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8307{
8308 struct nfs4_reclaim_complete_data *calldata = data;
8309 struct nfs_client *clp = calldata->clp;
8310 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8311
8312 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008313 if (!nfs41_sequence_done(task, res))
8314 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008315
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008316 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008317 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8318 rpc_restart_call_prepare(task);
8319 return;
8320 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008321 dprintk("<-- %s\n", __func__);
8322}
8323
8324static void nfs4_free_reclaim_complete_data(void *data)
8325{
8326 struct nfs4_reclaim_complete_data *calldata = data;
8327
8328 kfree(calldata);
8329}
8330
8331static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8332 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8333 .rpc_call_done = nfs4_reclaim_complete_done,
8334 .rpc_release = nfs4_free_reclaim_complete_data,
8335};
8336
8337/*
8338 * Issue a global reclaim complete.
8339 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008340static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8341 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008342{
8343 struct nfs4_reclaim_complete_data *calldata;
8344 struct rpc_task *task;
8345 struct rpc_message msg = {
8346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008347 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008348 };
8349 struct rpc_task_setup task_setup_data = {
8350 .rpc_client = clp->cl_rpcclient,
8351 .rpc_message = &msg,
8352 .callback_ops = &nfs4_reclaim_complete_call_ops,
8353 .flags = RPC_TASK_ASYNC,
8354 };
8355 int status = -ENOMEM;
8356
8357 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008358 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008359 if (calldata == NULL)
8360 goto out;
8361 calldata->clp = clp;
8362 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008363
Chuck Levera9c92d62013-08-09 12:48:18 -04008364 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008365 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008366 msg.rpc_argp = &calldata->arg;
8367 msg.rpc_resp = &calldata->res;
8368 task_setup_data.callback_data = calldata;
8369 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008370 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008371 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008372 goto out;
8373 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008374 status = nfs4_wait_for_completion_rpc_task(task);
8375 if (status == 0)
8376 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008377 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008378 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008379out:
8380 dprintk("<-- %s status=%d\n", __func__, status);
8381 return status;
8382}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008383
8384static void
8385nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8386{
8387 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008388 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008389 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008390
8391 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008392 nfs41_setup_sequence(session, &lgp->args.seq_args,
8393 &lgp->res.seq_res, task);
8394 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008395}
8396
8397static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8398{
8399 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008400
8401 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008402 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008403 dprintk("<-- %s\n", __func__);
8404}
8405
8406static int
8407nfs4_layoutget_handle_exception(struct rpc_task *task,
8408 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8409{
Trond Myklebustee314c22012-10-01 17:25:48 -07008410 struct inode *inode = lgp->args.inode;
8411 struct nfs_server *server = NFS_SERVER(inode);
8412 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008413 int nfs4err = task->tk_status;
8414 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008415 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008416
Boaz Harroshed7e5422014-01-22 20:34:54 +02008417 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008418
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008419 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008420 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008421 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008422
8423 /*
8424 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8425 * on the file. set tk_status to -ENODATA to tell upper layer to
8426 * retry go inband.
8427 */
8428 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008429 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008430 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008431 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008432 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8433 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8434 */
8435 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008436 status = -EOVERFLOW;
8437 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008438 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008439 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008440 * (or clients) writing to the same RAID stripe except when
8441 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008442 *
8443 * Treat it like we would RECALLCONFLICT -- we retry for a little
8444 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008445 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008446 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008447 if (lgp->args.minlength == 0) {
8448 status = -EOVERFLOW;
8449 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008450 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008451 status = -EBUSY;
8452 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008453 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008454 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008455 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008456 case -NFS4ERR_DELEG_REVOKED:
8457 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008458 case -NFS4ERR_EXPIRED:
8459 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008460 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008461 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008462 lo = NFS_I(inode)->layout;
8463 /* If the open stateid was bad, then recover it. */
8464 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8465 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008466 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008467 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008468 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008469 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008470 break;
8471 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008472
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008473 /*
8474 * Mark the bad layout state as invalid, then retry
8475 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008476 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008477 spin_unlock(&inode->i_lock);
8478 pnfs_free_lseg_list(&head);
8479 status = -EAGAIN;
8480 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008481 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008482
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008483 err = nfs4_handle_exception(server, nfs4err, exception);
8484 if (!status) {
8485 if (exception->retry)
8486 status = -EAGAIN;
8487 else
8488 status = err;
8489 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008490out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008491 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008492 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008493}
8494
Idan Kedar85541162012-08-02 11:47:10 +03008495static size_t max_response_pages(struct nfs_server *server)
8496{
8497 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8498 return nfs_page_array_len(0, max_resp_sz);
8499}
8500
8501static void nfs4_free_pages(struct page **pages, size_t size)
8502{
8503 int i;
8504
8505 if (!pages)
8506 return;
8507
8508 for (i = 0; i < size; i++) {
8509 if (!pages[i])
8510 break;
8511 __free_page(pages[i]);
8512 }
8513 kfree(pages);
8514}
8515
8516static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8517{
8518 struct page **pages;
8519 int i;
8520
8521 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8522 if (!pages) {
8523 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8524 return NULL;
8525 }
8526
8527 for (i = 0; i < size; i++) {
8528 pages[i] = alloc_page(gfp_flags);
8529 if (!pages[i]) {
8530 dprintk("%s: failed to allocate page\n", __func__);
8531 nfs4_free_pages(pages, size);
8532 return NULL;
8533 }
8534 }
8535
8536 return pages;
8537}
8538
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539static void nfs4_layoutget_release(void *calldata)
8540{
8541 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008542 struct inode *inode = lgp->args.inode;
8543 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008544 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008545
8546 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008547 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008548 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008549 put_nfs_open_context(lgp->args.ctx);
8550 kfree(calldata);
8551 dprintk("<-- %s\n", __func__);
8552}
8553
8554static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8555 .rpc_call_prepare = nfs4_layoutget_prepare,
8556 .rpc_call_done = nfs4_layoutget_done,
8557 .rpc_release = nfs4_layoutget_release,
8558};
8559
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008560struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008561nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008562{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008563 struct inode *inode = lgp->args.inode;
8564 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008565 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008566 struct rpc_task *task;
8567 struct rpc_message msg = {
8568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8569 .rpc_argp = &lgp->args,
8570 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008571 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008572 };
8573 struct rpc_task_setup task_setup_data = {
8574 .rpc_client = server->client,
8575 .rpc_message = &msg,
8576 .callback_ops = &nfs4_layoutget_call_ops,
8577 .callback_data = lgp,
8578 .flags = RPC_TASK_ASYNC,
8579 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008580 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008581 struct nfs4_exception exception = {
8582 .inode = inode,
8583 .timeout = *timeout,
8584 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008585 int status = 0;
8586
8587 dprintk("--> %s\n", __func__);
8588
Peng Tao4bd5a982014-11-17 11:05:17 +08008589 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8590 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8591
Idan Kedar85541162012-08-02 11:47:10 +03008592 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8593 if (!lgp->args.layout.pages) {
8594 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008595 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008596 }
8597 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8598
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008599 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008600 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008601 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008602
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008603 task = rpc_run_task(&task_setup_data);
8604 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008605 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008606 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008607 if (status == 0) {
8608 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8609 *timeout = exception.timeout;
8610 }
8611
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008612 trace_nfs4_layoutget(lgp->args.ctx,
8613 &lgp->args.range,
8614 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008615 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008616 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008617
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008618 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8619 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008620 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008621 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008622 rpc_put_task(task);
8623 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008624 if (status)
8625 return ERR_PTR(status);
8626 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008627}
8628
Benny Halevycbe82602011-05-22 19:52:37 +03008629static void
8630nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8631{
8632 struct nfs4_layoutreturn *lrp = calldata;
8633
8634 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008635 nfs41_setup_sequence(lrp->clp->cl_session,
8636 &lrp->args.seq_args,
8637 &lrp->res.seq_res,
8638 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008639}
8640
8641static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8642{
8643 struct nfs4_layoutreturn *lrp = calldata;
8644 struct nfs_server *server;
8645
8646 dprintk("--> %s\n", __func__);
8647
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008648 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008649 return;
8650
8651 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008652 switch (task->tk_status) {
8653 default:
8654 task->tk_status = 0;
8655 case 0:
8656 break;
8657 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008658 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008659 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008660 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008661 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008662 return;
8663 }
Benny Halevycbe82602011-05-22 19:52:37 +03008664 dprintk("<-- %s\n", __func__);
8665}
8666
8667static void nfs4_layoutreturn_release(void *calldata)
8668{
8669 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008670 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008671
8672 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008673 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008674 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008675 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008676 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8677 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008678 pnfs_put_layout_hdr(lrp->args.layout);
8679 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008680 kfree(calldata);
8681 dprintk("<-- %s\n", __func__);
8682}
8683
8684static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8685 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8686 .rpc_call_done = nfs4_layoutreturn_done,
8687 .rpc_release = nfs4_layoutreturn_release,
8688};
8689
Peng Tao6c166052014-11-17 09:30:40 +08008690int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008691{
8692 struct rpc_task *task;
8693 struct rpc_message msg = {
8694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8695 .rpc_argp = &lrp->args,
8696 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008697 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008698 };
8699 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008700 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008701 .rpc_message = &msg,
8702 .callback_ops = &nfs4_layoutreturn_call_ops,
8703 .callback_data = lrp,
8704 };
Peng Tao6c166052014-11-17 09:30:40 +08008705 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008706
Andrew Elble99ade3c2015-12-02 09:39:51 -05008707 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8708 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8709 &task_setup_data.rpc_client, &msg);
8710
Benny Halevycbe82602011-05-22 19:52:37 +03008711 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008712 if (!sync) {
8713 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8714 if (!lrp->inode) {
8715 nfs4_layoutreturn_release(lrp);
8716 return -EAGAIN;
8717 }
8718 task_setup_data.flags |= RPC_TASK_ASYNC;
8719 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008720 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008721 task = rpc_run_task(&task_setup_data);
8722 if (IS_ERR(task))
8723 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008724 if (sync)
8725 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008726 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008727 dprintk("<-- %s status=%d\n", __func__, status);
8728 rpc_put_task(task);
8729 return status;
8730}
8731
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008732static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008733_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8734 struct pnfs_device *pdev,
8735 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008736{
8737 struct nfs4_getdeviceinfo_args args = {
8738 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008739 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8740 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008741 };
8742 struct nfs4_getdeviceinfo_res res = {
8743 .pdev = pdev,
8744 };
8745 struct rpc_message msg = {
8746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8747 .rpc_argp = &args,
8748 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008749 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008750 };
8751 int status;
8752
8753 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008754 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008755 if (res.notification & ~args.notify_types)
8756 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008757 if (res.notification != args.notify_types)
8758 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008759
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008760 dprintk("<-- %s status=%d\n", __func__, status);
8761
8762 return status;
8763}
8764
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008765int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8766 struct pnfs_device *pdev,
8767 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008768{
8769 struct nfs4_exception exception = { };
8770 int err;
8771
8772 do {
8773 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008774 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008775 &exception);
8776 } while (exception.retry);
8777 return err;
8778}
8779EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8780
Andy Adamson863a3c62011-03-23 13:27:54 +00008781static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8782{
8783 struct nfs4_layoutcommit_data *data = calldata;
8784 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008785 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008786
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008787 nfs41_setup_sequence(session,
8788 &data->args.seq_args,
8789 &data->res.seq_res,
8790 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008791}
8792
8793static void
8794nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8795{
8796 struct nfs4_layoutcommit_data *data = calldata;
8797 struct nfs_server *server = NFS_SERVER(data->args.inode);
8798
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008799 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008800 return;
8801
8802 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008803 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8804 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8805 case -NFS4ERR_BADLAYOUT: /* no layout */
8806 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008807 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008808 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008809 break;
8810 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008811 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008812 rpc_restart_call_prepare(task);
8813 return;
8814 }
8815 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008816}
8817
8818static void nfs4_layoutcommit_release(void *calldata)
8819{
8820 struct nfs4_layoutcommit_data *data = calldata;
8821
Andy Adamsondb29c082011-07-30 20:52:38 -04008822 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008823 nfs_post_op_update_inode_force_wcc(data->args.inode,
8824 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008825 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008826 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008827 kfree(data);
8828}
8829
8830static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8831 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8832 .rpc_call_done = nfs4_layoutcommit_done,
8833 .rpc_release = nfs4_layoutcommit_release,
8834};
8835
8836int
Andy Adamsonef311532011-03-12 02:58:10 -05008837nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008838{
8839 struct rpc_message msg = {
8840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8841 .rpc_argp = &data->args,
8842 .rpc_resp = &data->res,
8843 .rpc_cred = data->cred,
8844 };
8845 struct rpc_task_setup task_setup_data = {
8846 .task = &data->task,
8847 .rpc_client = NFS_CLIENT(data->args.inode),
8848 .rpc_message = &msg,
8849 .callback_ops = &nfs4_layoutcommit_ops,
8850 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008851 };
8852 struct rpc_task *task;
8853 int status = 0;
8854
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008855 dprintk("NFS: initiating layoutcommit call. sync %d "
8856 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008857 data->args.lastbytewritten,
8858 data->args.inode->i_ino);
8859
Trond Myklebust472e2592015-02-05 16:50:30 -05008860 if (!sync) {
8861 data->inode = nfs_igrab_and_active(data->args.inode);
8862 if (data->inode == NULL) {
8863 nfs4_layoutcommit_release(data);
8864 return -EAGAIN;
8865 }
8866 task_setup_data.flags = RPC_TASK_ASYNC;
8867 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008868 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008869 task = rpc_run_task(&task_setup_data);
8870 if (IS_ERR(task))
8871 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008872 if (sync)
8873 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008874 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008875 dprintk("%s: status %d\n", __func__, status);
8876 rpc_put_task(task);
8877 return status;
8878}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008879
Andy Adamson97431202013-08-08 10:57:56 -04008880/**
8881 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8882 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8883 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008884static int
8885_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008886 struct nfs_fsinfo *info,
8887 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888{
8889 struct nfs41_secinfo_no_name_args args = {
8890 .style = SECINFO_STYLE_CURRENT_FH,
8891 };
8892 struct nfs4_secinfo_res res = {
8893 .flavors = flavors,
8894 };
8895 struct rpc_message msg = {
8896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8897 .rpc_argp = &args,
8898 .rpc_resp = &res,
8899 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008900 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008901 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008902 int status;
8903
8904 if (use_integrity) {
8905 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008906 cred = nfs4_get_clid_cred(server->nfs_client);
8907 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008908 }
8909
8910 dprintk("--> %s\n", __func__);
8911 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8912 &res.seq_res, 0);
8913 dprintk("<-- %s status=%d\n", __func__, status);
8914
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008915 if (cred)
8916 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008917
8918 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008919}
8920
8921static int
8922nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8923 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8924{
8925 struct nfs4_exception exception = { };
8926 int err;
8927 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008928 /* first try using integrity protection */
8929 err = -NFS4ERR_WRONGSEC;
8930
8931 /* try to use integrity protection with machine cred */
8932 if (_nfs4_is_integrity_protected(server->nfs_client))
8933 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8934 flavors, true);
8935
8936 /*
8937 * if unable to use integrity protection, or SECINFO with
8938 * integrity protection returns NFS4ERR_WRONGSEC (which is
8939 * disallowed by spec, but exists in deployed servers) use
8940 * the current filesystem's rpc_client and the user cred.
8941 */
8942 if (err == -NFS4ERR_WRONGSEC)
8943 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8944 flavors, false);
8945
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008946 switch (err) {
8947 case 0:
8948 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008949 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008950 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008951 default:
8952 err = nfs4_handle_exception(server, err, &exception);
8953 }
8954 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008955out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008956 return err;
8957}
8958
8959static int
8960nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8961 struct nfs_fsinfo *info)
8962{
8963 int err;
8964 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008965 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008966 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008967 struct nfs4_secinfo4 *secinfo;
8968 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008969
8970 page = alloc_page(GFP_KERNEL);
8971 if (!page) {
8972 err = -ENOMEM;
8973 goto out;
8974 }
8975
8976 flavors = page_address(page);
8977 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8978
8979 /*
8980 * Fall back on "guess and check" method if
8981 * the server doesn't support SECINFO_NO_NAME
8982 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008983 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008984 err = nfs4_find_root_sec(server, fhandle, info);
8985 goto out_freepage;
8986 }
8987 if (err)
8988 goto out_freepage;
8989
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008990 for (i = 0; i < flavors->num_flavors; i++) {
8991 secinfo = &flavors->flavors[i];
8992
8993 switch (secinfo->flavor) {
8994 case RPC_AUTH_NULL:
8995 case RPC_AUTH_UNIX:
8996 case RPC_AUTH_GSS:
8997 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8998 &secinfo->flavor_info);
8999 break;
9000 default:
9001 flavor = RPC_AUTH_MAXFLAVOR;
9002 break;
9003 }
9004
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009005 if (!nfs_auth_info_match(&server->auth_info, flavor))
9006 flavor = RPC_AUTH_MAXFLAVOR;
9007
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009008 if (flavor != RPC_AUTH_MAXFLAVOR) {
9009 err = nfs4_lookup_root_sec(server, fhandle,
9010 info, flavor);
9011 if (!err)
9012 break;
9013 }
9014 }
9015
9016 if (flavor == RPC_AUTH_MAXFLAVOR)
9017 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009018
9019out_freepage:
9020 put_page(page);
9021 if (err == -EACCES)
9022 return -EPERM;
9023out:
9024 return err;
9025}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009026
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009027static int _nfs41_test_stateid(struct nfs_server *server,
9028 nfs4_stateid *stateid,
9029 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009030{
9031 int status;
9032 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009033 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009034 };
9035 struct nfs41_test_stateid_res res;
9036 struct rpc_message msg = {
9037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9038 .rpc_argp = &args,
9039 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009040 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009041 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009042 struct rpc_clnt *rpc_client = server->client;
9043
9044 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9045 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009046
Chuck Lever38527b12012-07-11 16:30:23 -04009047 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009048 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009049 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009050 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009051 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009052 if (status != NFS_OK) {
9053 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009054 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009055 }
9056 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009057 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009058}
9059
Trond Myklebust43912bb2016-09-22 13:38:56 -04009060static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9061 int err, struct nfs4_exception *exception)
9062{
9063 exception->retry = 0;
9064 switch(err) {
9065 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009066 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009067 nfs4_handle_exception(server, err, exception);
9068 break;
9069 case -NFS4ERR_BADSESSION:
9070 case -NFS4ERR_BADSLOT:
9071 case -NFS4ERR_BAD_HIGH_SLOT:
9072 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9073 case -NFS4ERR_DEADSESSION:
9074 nfs4_do_handle_exception(server, err, exception);
9075 }
9076}
9077
Chuck Lever38527b12012-07-11 16:30:23 -04009078/**
9079 * nfs41_test_stateid - perform a TEST_STATEID operation
9080 *
9081 * @server: server / transport on which to perform the operation
9082 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009083 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009084 *
9085 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9086 * Otherwise a negative NFS4ERR value is returned if the operation
9087 * failed or the state ID is not currently valid.
9088 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009089static int nfs41_test_stateid(struct nfs_server *server,
9090 nfs4_stateid *stateid,
9091 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009092{
9093 struct nfs4_exception exception = { };
9094 int err;
9095 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009096 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009097 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009098 } while (exception.retry);
9099 return err;
9100}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009101
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009102struct nfs_free_stateid_data {
9103 struct nfs_server *server;
9104 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009105 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009106};
9107
9108static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9109{
9110 struct nfs_free_stateid_data *data = calldata;
9111 nfs41_setup_sequence(nfs4_get_session(data->server),
9112 &data->args.seq_args,
9113 &data->res.seq_res,
9114 task);
9115}
9116
9117static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9118{
9119 struct nfs_free_stateid_data *data = calldata;
9120
9121 nfs41_sequence_done(task, &data->res.seq_res);
9122
9123 switch (task->tk_status) {
9124 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009125 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009126 rpc_restart_call_prepare(task);
9127 }
9128}
9129
9130static void nfs41_free_stateid_release(void *calldata)
9131{
9132 kfree(calldata);
9133}
9134
Trond Myklebust17f26b12013-08-21 15:48:42 -04009135static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009136 .rpc_call_prepare = nfs41_free_stateid_prepare,
9137 .rpc_call_done = nfs41_free_stateid_done,
9138 .rpc_release = nfs41_free_stateid_release,
9139};
9140
9141static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009142 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009143 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009144 bool privileged)
9145{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009146 struct rpc_message msg = {
9147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009148 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009149 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009150 struct rpc_task_setup task_setup = {
9151 .rpc_client = server->client,
9152 .rpc_message = &msg,
9153 .callback_ops = &nfs41_free_stateid_ops,
9154 .flags = RPC_TASK_ASYNC,
9155 };
9156 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009157
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009158 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9159 &task_setup.rpc_client, &msg);
9160
Chuck Lever38527b12012-07-11 16:30:23 -04009161 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009162 data = kmalloc(sizeof(*data), GFP_NOFS);
9163 if (!data)
9164 return ERR_PTR(-ENOMEM);
9165 data->server = server;
9166 nfs4_stateid_copy(&data->args.stateid, stateid);
9167
9168 task_setup.callback_data = data;
9169
9170 msg.rpc_argp = &data->args;
9171 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009172 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009173 if (privileged)
9174 nfs4_set_sequence_privileged(&data->args.seq_args);
9175
9176 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009177}
9178
Chuck Lever38527b12012-07-11 16:30:23 -04009179/**
9180 * nfs41_free_stateid - perform a FREE_STATEID operation
9181 *
9182 * @server: server / transport on which to perform the operation
9183 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009184 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009185 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009186 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009187 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009188 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009189static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009190 const nfs4_stateid *stateid,
9191 struct rpc_cred *cred,
9192 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009193{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009194 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009195
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009196 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009197 if (IS_ERR(task))
9198 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009199 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009200 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009201}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009202
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009203static void
9204nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009205{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009206 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009207
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009208 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009209 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009210}
9211
Trond Myklebust36281ca2012-03-04 18:13:56 -05009212static bool nfs41_match_stateid(const nfs4_stateid *s1,
9213 const nfs4_stateid *s2)
9214{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009215 if (s1->type != s2->type)
9216 return false;
9217
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009218 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009219 return false;
9220
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009221 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009222 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009223 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009224 return true;
9225
9226 return false;
9227}
9228
Andy Adamson557134a2009-04-01 09:21:53 -04009229#endif /* CONFIG_NFS_V4_1 */
9230
Trond Myklebust36281ca2012-03-04 18:13:56 -05009231static bool nfs4_match_stateid(const nfs4_stateid *s1,
9232 const nfs4_stateid *s2)
9233{
Trond Myklebustf597c532012-03-04 18:13:56 -05009234 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009235}
9236
9237
Trond Myklebust17280172012-03-11 13:11:00 -04009238static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009239 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009240 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241 .recover_open = nfs4_open_reclaim,
9242 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009243 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009244 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245};
9246
Andy Adamson591d71c2009-04-01 09:22:47 -04009247#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009248static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009249 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9250 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9251 .recover_open = nfs4_open_reclaim,
9252 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009253 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009254 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009255 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009256};
9257#endif /* CONFIG_NFS_V4_1 */
9258
Trond Myklebust17280172012-03-11 13:11:00 -04009259static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009260 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009261 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009262 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009264 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265};
9266
Andy Adamson591d71c2009-04-01 09:22:47 -04009267#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009268static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009269 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9270 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009271 .recover_open = nfs41_open_expired,
9272 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009273 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009274};
9275#endif /* CONFIG_NFS_V4_1 */
9276
Trond Myklebust17280172012-03-11 13:11:00 -04009277static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009278 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009279 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009280 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009281};
9282
9283#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009284static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009285 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009286 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009287 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009288};
9289#endif
9290
Chuck Leverec011fe2013-10-17 14:12:39 -04009291static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009292 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009293 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009294};
9295
9296#if defined(CONFIG_NFS_V4_1)
9297static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009298 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009299 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009300};
9301#endif /* CONFIG_NFS_V4_1 */
9302
Trond Myklebust97dc1352010-06-16 09:52:26 -04009303static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9304 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009305 .init_caps = NFS_CAP_READDIRPLUS
9306 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009307 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009308 .init_client = nfs40_init_client,
9309 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009310 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009311 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009312 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009313 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009314 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009315 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009316 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9317 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9318 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009319 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009320};
9321
9322#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009323static struct nfs_seqid *
9324nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9325{
9326 return NULL;
9327}
9328
Trond Myklebust97dc1352010-06-16 09:52:26 -04009329static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9330 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009331 .init_caps = NFS_CAP_READDIRPLUS
9332 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009333 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009334 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009335 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009336 .init_client = nfs41_init_client,
9337 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009338 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009339 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009340 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009341 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009342 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009343 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009344 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009345 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9346 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9347 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009348 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009349};
9350#endif
9351
Steve Dickson42c2c422013-05-22 12:50:38 -04009352#if defined(CONFIG_NFS_V4_2)
9353static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9354 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009355 .init_caps = NFS_CAP_READDIRPLUS
9356 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009357 | NFS_CAP_POSIX_LOCK
9358 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009359 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009360 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009361 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009362 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009363 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009364 | NFS_CAP_LAYOUTSTATS
9365 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009366 .init_client = nfs41_init_client,
9367 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009368 .match_stateid = nfs41_match_stateid,
9369 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009370 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009371 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009372 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009373 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009374 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009375 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9376 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9377 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009378 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009379};
9380#endif
9381
Trond Myklebust97dc1352010-06-16 09:52:26 -04009382const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9383 [0] = &nfs_v4_0_minor_ops,
9384#if defined(CONFIG_NFS_V4_1)
9385 [1] = &nfs_v4_1_minor_ops,
9386#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009387#if defined(CONFIG_NFS_V4_2)
9388 [2] = &nfs_v4_2_minor_ops,
9389#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009390};
9391
Trond Myklebust13997822016-07-24 17:10:52 -04009392static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009393{
9394 ssize_t error, error2;
9395
9396 error = generic_listxattr(dentry, list, size);
9397 if (error < 0)
9398 return error;
9399 if (list) {
9400 list += error;
9401 size -= error;
9402 }
9403
9404 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9405 if (error2 < 0)
9406 return error2;
9407 return error + error2;
9408}
9409
Trond Myklebust17f26b12013-08-21 15:48:42 -04009410static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009411 .create = nfs_create,
9412 .lookup = nfs_lookup,
9413 .atomic_open = nfs_atomic_open,
9414 .link = nfs_link,
9415 .unlink = nfs_unlink,
9416 .symlink = nfs_symlink,
9417 .mkdir = nfs_mkdir,
9418 .rmdir = nfs_rmdir,
9419 .mknod = nfs_mknod,
9420 .rename = nfs_rename,
9421 .permission = nfs_permission,
9422 .getattr = nfs_getattr,
9423 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009424 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009425};
9426
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009427static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009428 .permission = nfs_permission,
9429 .getattr = nfs_getattr,
9430 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009431 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009432};
9433
David Howells509de812006-08-22 20:06:11 -04009434const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 .version = 4, /* protocol version */
9436 .dentry_ops = &nfs4_dentry_operations,
9437 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009438 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009439 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009441 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009442 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 .getattr = nfs4_proc_getattr,
9444 .setattr = nfs4_proc_setattr,
9445 .lookup = nfs4_proc_lookup,
9446 .access = nfs4_proc_access,
9447 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 .create = nfs4_proc_create,
9449 .remove = nfs4_proc_remove,
9450 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009451 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009453 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009454 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009455 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 .link = nfs4_proc_link,
9457 .symlink = nfs4_proc_symlink,
9458 .mkdir = nfs4_proc_mkdir,
9459 .rmdir = nfs4_proc_remove,
9460 .readdir = nfs4_proc_readdir,
9461 .mknod = nfs4_proc_mknod,
9462 .statfs = nfs4_proc_statfs,
9463 .fsinfo = nfs4_proc_fsinfo,
9464 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009465 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009467 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009469 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009471 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009473 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009474 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009476 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009477 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009478 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009479 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009480 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009481 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009482 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009483 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009484 .create_server = nfs4_create_server,
9485 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486};
9487
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009488static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009489 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009490 .list = nfs4_xattr_list_nfs4_acl,
9491 .get = nfs4_xattr_get_nfs4_acl,
9492 .set = nfs4_xattr_set_nfs4_acl,
9493};
9494
9495const struct xattr_handler *nfs4_xattr_handlers[] = {
9496 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009497#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9498 &nfs4_xattr_nfs4_label_handler,
9499#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009500 NULL
9501};
9502
Linus Torvalds1da177e2005-04-16 15:20:36 -07009503/*
9504 * Local variables:
9505 * c-basic-offset: 8
9506 * End:
9507 */