blob: 6a35204affa47bb796d368c0a21c4e2b443f17ba [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 Myklebust5bc2afc2013-09-23 18:01:28 -04002393 if (o_arg->open_flags & O_EXCL)
2394 data->file_created = 1;
2395 else if (o_res->cinfo.before != o_res->cinfo.after)
2396 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002397 if (data->file_created || dir->i_version != o_res->cinfo.after)
2398 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002399 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002400 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2401 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002403 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002405 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002408 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Andy Adamsond83217c2011-03-01 01:34:17 +00002412static int nfs4_recover_expired_lease(struct nfs_server *server)
2413{
2414 return nfs4_client_recover_expired_lease(server->nfs_client);
2415}
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/*
2418 * OPEN_EXPIRED:
2419 * reclaim state on the server after a network partition.
2420 * Assumes caller holds the appropriate lock
2421 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002422static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002424 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002425 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002427 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002428 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002429 if (IS_ERR(opendata))
2430 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002431 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002432 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002433 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002434 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002435 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002438static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002439{
Trond Myklebust539cd032007-06-05 11:46:42 -04002440 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002441 struct nfs4_exception exception = { };
2442 int err;
2443
2444 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002445 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002446 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002447 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2448 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002449 switch (err) {
2450 default:
2451 goto out;
2452 case -NFS4ERR_GRACE:
2453 case -NFS4ERR_DELAY:
2454 nfs4_handle_exception(server, err, &exception);
2455 err = 0;
2456 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002457 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002458out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002459 return err;
2460}
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002465 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Trond Myklebust864472e2006-01-03 09:55:15 +01002467 ctx = nfs4_state_find_open_context(state);
2468 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002469 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002470 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002471 put_nfs_open_context(ctx);
2472 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Trond Myklebust41020b62016-09-22 13:38:58 -04002475static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2476 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002477{
Trond Myklebust41020b62016-09-22 13:38:58 -04002478 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002479 write_seqlock(&state->seqlock);
2480 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2481 write_sequnlock(&state->seqlock);
2482 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2483}
2484
2485static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2486{
2487 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002488 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002489}
2490
2491static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2492{
2493 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2494 nfs40_clear_delegation_stateid(state);
2495 return nfs4_open_expired(sp, state);
2496}
2497
Trond Myklebust45870d62016-09-22 13:38:59 -04002498static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2499 nfs4_stateid *stateid,
2500 struct rpc_cred *cred)
2501{
2502 return -NFS4ERR_BAD_STATEID;
2503}
2504
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002505#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002506static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2507 nfs4_stateid *stateid,
2508 struct rpc_cred *cred)
2509{
2510 int status;
2511
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002512 switch (stateid->type) {
2513 default:
2514 break;
2515 case NFS4_INVALID_STATEID_TYPE:
2516 case NFS4_SPECIAL_STATEID_TYPE:
2517 return -NFS4ERR_BAD_STATEID;
2518 case NFS4_REVOKED_STATEID_TYPE:
2519 goto out_free;
2520 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002521
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002522 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002523 switch (status) {
2524 case -NFS4ERR_EXPIRED:
2525 case -NFS4ERR_ADMIN_REVOKED:
2526 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002527 break;
2528 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002529 return status;
2530 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002531out_free:
2532 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002533 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002534 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002535}
2536
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002537static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002538{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002539 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002541 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 struct rpc_cred *cred;
2543 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002544
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002545 /* Get the delegation credential for use by test/free_stateid */
2546 rcu_read_lock();
2547 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002548 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002549 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002550 return;
2551 }
2552
2553 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002554 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2555 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002556 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002557 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002558 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002559
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002560 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2561 rcu_read_unlock();
2562 return;
2563 }
2564
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002565 cred = get_rpccred(delegation->cred);
2566 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002567 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002568 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002569 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002570 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002571
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002572 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002573}
2574
2575/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002576 * nfs41_check_expired_locks - possibly free a lock stateid
2577 *
2578 * @state: NFSv4 state for an inode
2579 *
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2582 */
2583static int nfs41_check_expired_locks(struct nfs4_state *state)
2584{
2585 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002586 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002587 struct nfs_server *server = NFS_SERVER(state->inode);
2588
2589 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2590 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002591
2592 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002593 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2594 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2595 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2596
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002597 atomic_inc(&lsp->ls_count);
2598 spin_unlock(&state->state_lock);
2599
2600 nfs4_put_lock_state(prev);
2601 prev = lsp;
2602
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002603 status = nfs41_test_and_free_expired_stateid(server,
2604 &lsp->ls_stateid,
2605 cred);
2606 trace_nfs4_test_lock_stateid(state, lsp, status);
2607 if (status == -NFS4ERR_EXPIRED ||
2608 status == -NFS4ERR_BAD_STATEID) {
2609 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002610 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002611 if (!recover_lost_locks)
2612 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2613 } else if (status != NFS_OK) {
2614 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002615 nfs4_put_lock_state(prev);
2616 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002617 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002618 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002619 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002620 }
2621 spin_unlock(&state->state_lock);
2622 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002623out:
2624 return ret;
2625}
2626
2627/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628 * nfs41_check_open_stateid - possibly free an open stateid
2629 *
2630 * @state: NFSv4 state for an inode
2631 *
2632 * Returns NFS_OK if recovery for this stateid is now finished.
2633 * Otherwise a negative NFS4ERR value is returned.
2634 */
2635static int nfs41_check_open_stateid(struct nfs4_state *state)
2636{
2637 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002638 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002639 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002640 int status;
2641
Trond Myklebustb134fc42016-09-22 13:39:16 -04002642 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002643 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2644 if (nfs4_have_delegation(state->inode, state->state))
2645 return NFS_OK;
2646 return -NFS4ERR_OPENMODE;
2647 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002648 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002649 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002650 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002651 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002652 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002653 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2654 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2655 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002656 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002657 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002658 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002659 if (status != NFS_OK)
2660 return status;
2661 if (nfs_open_stateid_recover_openmode(state))
2662 return -NFS4ERR_OPENMODE;
2663 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002664}
2665
2666static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2667{
Chuck Levereb64cf92012-07-11 16:30:05 -04002668 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002669
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002670 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002671 status = nfs41_check_expired_locks(state);
2672 if (status != NFS_OK)
2673 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002674 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002675 if (status != NFS_OK)
2676 status = nfs4_open_expired(sp, state);
2677 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002678}
2679#endif
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002682 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2683 * fields corresponding to attributes that were used to store the verifier.
2684 * Make sure we clobber those fields in the later setattr call
2685 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2687 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002688{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002689 const u32 *attrset = opendata->o_res.attrset;
2690
2691 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002692 !(sattr->ia_valid & ATTR_ATIME_SET))
2693 sattr->ia_valid |= ATTR_ATIME;
2694
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002695 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002696 !(sattr->ia_valid & ATTR_MTIME_SET))
2697 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002698
2699 /* Except MODE, it seems harmless of setting twice. */
2700 if ((attrset[1] & FATTR4_WORD1_MODE))
2701 sattr->ia_valid &= ~ATTR_MODE;
2702
2703 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2704 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002705}
2706
Trond Myklebustc21443c2013-02-07 14:26:21 -05002707static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2708 fmode_t fmode,
2709 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002710 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002711{
2712 struct nfs4_state_owner *sp = opendata->owner;
2713 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002714 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 struct nfs4_state *state;
2716 unsigned int seq;
2717 int ret;
2718
2719 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2720
2721 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002722 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002723 goto out;
2724
2725 state = nfs4_opendata_to_nfs4_state(opendata);
2726 ret = PTR_ERR(state);
2727 if (IS_ERR(state))
2728 goto out;
2729 if (server->caps & NFS_CAP_POSIX_LOCK)
2730 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002731 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2732 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002733
Trond Myklebust275bb302013-05-29 13:11:28 -04002734 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002735 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002736 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002737 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002738 alias = d_exact_alias(dentry, state->inode);
2739 if (!alias)
2740 alias = d_splice_alias(igrab(state->inode), dentry);
2741 /* d_splice_alias() can't fail here - it's a non-directory */
2742 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002743 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002744 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002745 }
2746 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002747 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002748 }
2749
Trond Myklebustc21443c2013-02-07 14:26:21 -05002750 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2751 if (ret != 0)
2752 goto out;
2753
Trond Myklebust3efb9722013-05-29 13:17:04 -04002754 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002755 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002756 nfs_inode_attach_open_context(ctx);
2757 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2758 nfs4_schedule_stateid_recovery(server, state);
2759 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002760out:
2761 return ret;
2762}
2763
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002764/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002765 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 */
Andy Adamson82be4172012-05-23 05:02:35 -04002767static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002768 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002769 int flags,
2770 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002771 struct nfs4_label *label,
2772 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct nfs4_state_owner *sp;
2775 struct nfs4_state *state = NULL;
2776 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002777 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002778 struct dentry *dentry = ctx->dentry;
2779 struct rpc_cred *cred = ctx->cred;
2780 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2781 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002782 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002783 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002784 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002788 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2789 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2791 goto out_err;
2792 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002793 status = nfs4_recover_expired_lease(server);
2794 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002795 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002796 if (d_really_is_positive(dentry))
2797 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002798 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002799 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002800 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002801 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002802 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002803 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002804 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
David Quigley14c43f72013-05-22 12:50:43 -04002806 if (label) {
2807 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2808 if (IS_ERR(olabel)) {
2809 status = PTR_ERR(olabel);
2810 goto err_opendata_put;
2811 }
2812 }
2813
Trond Myklebuste911b812014-03-26 13:24:37 -07002814 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2815 if (!opendata->f_attr.mdsthreshold) {
2816 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2817 if (!opendata->f_attr.mdsthreshold)
2818 goto err_free_label;
2819 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002820 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002821 }
David Howells2b0143b2015-03-17 22:25:59 +00002822 if (d_really_is_positive(dentry))
2823 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002824
Trond Myklebust3efb9722013-05-29 13:17:04 -04002825 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002826 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002827 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002828 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002829
NeilBrownefcbc042015-07-30 13:00:56 +10002830 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002831 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002832 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002833 /*
2834 * send create attributes which was not set by open
2835 * with an extra setattr.
2836 */
2837 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2838 nfs_fattr_init(opendata->o_res.f_attr);
2839 status = nfs4_do_setattr(state->inode, cred,
2840 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002841 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002842 if (status == 0) {
2843 nfs_setattr_update_inode(state->inode, sattr,
2844 opendata->o_res.f_attr);
2845 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2846 }
David Quigley1775fd32013-05-22 12:50:42 -04002847 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002848 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002849 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002850 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002851
Trond Myklebuste911b812014-03-26 13:24:37 -07002852 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002853 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002854 opendata->f_attr.mdsthreshold = NULL;
2855 }
Andy Adamson82be4172012-05-23 05:02:35 -04002856
David Quigley14c43f72013-05-22 12:50:43 -04002857 nfs4_label_free(olabel);
2858
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002859 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002862err_free_label:
2863 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002864err_opendata_put:
2865 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002866err_put_state_owner:
2867 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return status;
2870}
2871
2872
Andy Adamson82be4172012-05-23 05:02:35 -04002873static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002874 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002875 int flags,
2876 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002877 struct nfs4_label *label,
2878 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002880 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct nfs4_exception exception = { };
2882 struct nfs4_state *res;
2883 int status;
2884
2885 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002886 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002887 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002888 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (status == 0)
2890 break;
2891 /* NOTE: BAD_SEQID means the server and client disagree about the
2892 * book-keeping w.r.t. state-changing operations
2893 * (OPEN/CLOSE/LOCK/LOCKU...)
2894 * It is actually a sign of a bug on the client or on the server.
2895 *
2896 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002897 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * have unhashed the old state_owner for us, and that we can
2899 * therefore safely retry using a new one. We should still warn
2900 * the user though...
2901 */
2902 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002903 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002904 " returned a bad sequence-id error!\n",
2905 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 exception.retry = 1;
2907 continue;
2908 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002909 /*
2910 * BAD_STATEID on OPEN means that the server cancelled our
2911 * state before it received the OPEN_CONFIRM.
2912 * Recover by retrying the request as per the discussion
2913 * on Page 181 of RFC3530.
2914 */
2915 if (status == -NFS4ERR_BAD_STATEID) {
2916 exception.retry = 1;
2917 continue;
2918 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002919 if (status == -EAGAIN) {
2920 /* We must have found a delegation */
2921 exception.retry = 1;
2922 continue;
2923 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002924 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2925 continue;
2926 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 status, &exception));
2928 } while (exception.retry);
2929 return res;
2930}
2931
Trond Myklebust8487c472016-06-26 08:44:35 -04002932static int _nfs4_do_setattr(struct inode *inode,
2933 struct nfs_setattrargs *arg,
2934 struct nfs_setattrres *res,
2935 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002936 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002937{
2938 struct nfs_server *server = NFS_SERVER(inode);
2939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2941 .rpc_argp = arg,
2942 .rpc_resp = res,
2943 .rpc_cred = cred,
2944 };
2945 struct rpc_cred *delegation_cred = NULL;
2946 unsigned long timestamp = jiffies;
2947 fmode_t fmode;
2948 bool truncate;
2949 int status;
2950
2951 nfs_fattr_init(res->fattr);
2952
2953 /* Servers should only apply open mode checks for file size changes */
2954 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2955 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2956
2957 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2958 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002959 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002960 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002961 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002962 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002963 l_ctx = nfs_get_lock_context(ctx);
2964 if (IS_ERR(l_ctx))
2965 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002966 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2967 &arg->stateid, &delegation_cred);
2968 nfs_put_lock_context(l_ctx);
2969 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002970 return -EBADF;
2971 } else
2972 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2973 if (delegation_cred)
2974 msg.rpc_cred = delegation_cred;
2975
2976 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2977
2978 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002979 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002980 renew_lease(server, timestamp);
2981 trace_nfs4_setattr(inode, &arg->stateid, status);
2982 return status;
2983}
2984
2985static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2986 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002987 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002988 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002990 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002991 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002993 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .iap = sattr,
2995 .server = server,
2996 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002997 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
2999 struct nfs_setattrres res = {
3000 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003001 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .server = server,
3003 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003004 struct nfs4_exception exception = {
3005 .state = state,
3006 .inode = inode,
3007 .stateid = &arg.stateid,
3008 };
3009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
David Quigleyaa9c2662013-05-22 12:50:44 -04003011 arg.bitmask = nfs4_bitmask(server, ilabel);
3012 if (ilabel)
3013 arg.bitmask = nfs4_bitmask(server, olabel);
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003016 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003017 switch (err) {
3018 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003019 if (!(sattr->ia_valid & ATTR_SIZE)) {
3020 pr_warn_once("NFSv4: server %s is incorrectly "
3021 "applying open mode checks to "
3022 "a SETATTR that is not "
3023 "changing file size.\n",
3024 server->nfs_client->cl_hostname);
3025 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003026 if (state && !(state->state & FMODE_WRITE)) {
3027 err = -EBADF;
3028 if (sattr->ia_valid & ATTR_OPEN)
3029 err = -EACCES;
3030 goto out;
3031 }
3032 }
3033 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return err;
3037}
3038
Peng Tao500d7012015-09-22 11:35:22 +08003039static bool
3040nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3041{
3042 if (inode == NULL || !nfs_have_layout(inode))
3043 return false;
3044
3045 return pnfs_wait_on_layoutreturn(inode, task);
3046}
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048struct nfs4_closedata {
3049 struct inode *inode;
3050 struct nfs4_state *state;
3051 struct nfs_closeargs arg;
3052 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003053 struct {
3054 struct nfs4_layoutreturn_args arg;
3055 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003056 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003057 u32 roc_barrier;
3058 bool roc;
3059 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003060 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003061 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062};
3063
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003064static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003065{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003066 struct nfs4_closedata *calldata = data;
3067 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003068 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003069
Trond Myklebustcf805162016-11-15 14:56:07 -05003070 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003071 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3072 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003073 nfs4_put_open_state(calldata->state);
3074 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003075 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003076 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003077 kfree(calldata);
3078}
3079
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003080static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003082 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003085 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Chuck Levera3ca5652012-03-01 17:00:40 -05003087 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003088 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3089 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003090 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003091
3092 /* Handle Layoutreturn errors */
3093 if (calldata->arg.lr_args && task->tk_status != 0) {
3094 switch (calldata->res.lr_ret) {
3095 default:
3096 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3097 break;
3098 case 0:
3099 calldata->arg.lr_args = NULL;
3100 calldata->res.lr_res = NULL;
3101 break;
3102 case -NFS4ERR_ADMIN_REVOKED:
3103 case -NFS4ERR_DELEG_REVOKED:
3104 case -NFS4ERR_EXPIRED:
3105 case -NFS4ERR_BAD_STATEID:
3106 case -NFS4ERR_OLD_STATEID:
3107 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3108 case -NFS4ERR_WRONG_CRED:
3109 calldata->arg.lr_args = NULL;
3110 calldata->res.lr_res = NULL;
3111 calldata->res.lr_ret = 0;
3112 rpc_restart_call_prepare(task);
3113 return;
3114 }
3115 }
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* hmm. we are done with the inode, and in the process of freeing
3118 * the state_owner. we keep this around to process errors
3119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 switch (task->tk_status) {
3121 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003122 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003123 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003124 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003125 case -NFS4ERR_ACCESS:
3126 if (calldata->arg.bitmask != NULL) {
3127 calldata->arg.bitmask = NULL;
3128 calldata->res.fattr = NULL;
3129 task->tk_status = 0;
3130 rpc_restart_call_prepare(task);
3131 goto out_release;
3132
3133 }
3134 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003135 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003137 case -NFS4ERR_EXPIRED:
3138 nfs4_free_revoked_stateid(server,
3139 &calldata->arg.stateid,
3140 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003141 case -NFS4ERR_OLD_STATEID:
3142 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003143 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003144 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003145 rpc_restart_call_prepare(task);
3146 goto out_release;
3147 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003148 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003149 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003151 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003152 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003153 goto out_release;
3154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003156 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3157 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003158out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003159 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003160 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003161 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003164static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003166 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003167 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003168 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003169 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003170 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003171
Chuck Levera3ca5652012-03-01 17:00:40 -05003172 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003173 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003174 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003175
Trond Myklebust88069f72009-12-08 08:33:16 -05003176 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003177 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003178 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3179 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3180 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003181 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003182 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003183 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003184 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003185 if (state->n_rdonly == 0)
3186 call_close |= is_rdonly;
3187 else if (is_rdonly)
3188 calldata->arg.fmode |= FMODE_READ;
3189 if (state->n_wronly == 0)
3190 call_close |= is_wronly;
3191 else if (is_wronly)
3192 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003193 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3194 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003195 } else if (is_rdwr)
3196 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3197
Trond Myklebust5cc78612016-11-14 11:19:56 -05003198 if (!nfs4_valid_open_stateid(state) ||
3199 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003200 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003201 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003202
3203 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003204 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003205 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003206 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003207
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003208 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003209 nfs_release_seqid(calldata->arg.seqid);
3210 goto out_wait;
3211 }
3212
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003213 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003214 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003215
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003216 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003217 /* Close-to-open cache consistency revalidation */
3218 if (!nfs4_have_delegation(inode, FMODE_READ))
3219 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3220 else
3221 calldata->arg.bitmask = NULL;
3222 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003223
Trond Myklebust6ae37332015-01-30 14:21:14 -05003224 calldata->arg.share_access =
3225 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3226 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003227
Trond Myklebustd8d84982016-12-19 12:14:44 -05003228 if (calldata->res.fattr == NULL)
3229 calldata->arg.bitmask = NULL;
3230 else if (calldata->arg.bitmask == NULL)
3231 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003232 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003233 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003234 &calldata->arg.seq_args,
3235 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003236 task) != 0)
3237 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003238 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003239 return;
3240out_no_action:
3241 task->tk_action = NULL;
3242out_wait:
3243 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003246static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003247 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003248 .rpc_call_done = nfs4_close_done,
3249 .rpc_release = nfs4_free_closedata,
3250};
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252/*
3253 * It is possible for data to be read/written from a mem-mapped file
3254 * after the sys_close call (which hits the vfs layer as a flush).
3255 * This means that we can't safely call nfsv4 close on a file until
3256 * the inode is cleared. This in turn means that we are not good
3257 * NFSv4 citizens - we do not indicate to the server to update the file's
3258 * share state even when we are done with one of the three share
3259 * stateid's in the inode.
3260 *
3261 * NOTE: Caller must be holding the sp->so_owner semaphore!
3262 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003263int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003265 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003266 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003268 struct nfs4_state_owner *sp = state->owner;
3269 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003270 struct rpc_message msg = {
3271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3272 .rpc_cred = state->owner->so_cred,
3273 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003274 struct rpc_task_setup task_setup_data = {
3275 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003276 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003277 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003278 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003279 .flags = RPC_TASK_ASYNC,
3280 };
Trond Myklebust95121352005-10-18 14:20:12 -07003281 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003283 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3284 &task_setup_data.rpc_client, &msg);
3285
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003286 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003288 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003289 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003290 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003292 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003294 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3295 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003296 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003297 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003298 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003299 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003300 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003301 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003302 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003303 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003304 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003305 calldata->lr.roc = pnfs_roc(state->inode,
3306 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3307 if (calldata->lr.roc) {
3308 calldata->arg.lr_args = &calldata->lr.arg;
3309 calldata->res.lr_res = &calldata->lr.res;
3310 }
Al Viro643168c2011-06-22 18:20:23 -04003311 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003312
Trond Myklebust1174dd12010-12-21 10:52:24 -05003313 msg.rpc_argp = &calldata->arg;
3314 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003315 task_setup_data.callback_data = calldata;
3316 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003317 if (IS_ERR(task))
3318 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003319 status = 0;
3320 if (wait)
3321 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003322 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003323 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003324out_free_calldata:
3325 kfree(calldata);
3326out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003327 nfs4_put_open_state(state);
3328 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003329 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Trond Myklebust2b484292010-09-17 10:56:51 -04003332static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003333nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3334 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003337 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3338
3339 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Trond Myklebust565277f2007-10-15 18:17:53 -04003341 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003342 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003343
3344 nfs4_label_release_security(label);
3345
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003346 if (IS_ERR(state))
3347 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003348 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
3350
Trond Myklebust1185a552009-12-03 15:54:02 -05003351static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003352{
3353 if (ctx->state == NULL)
3354 return;
3355 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003356 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003357 else
Al Viro643168c2011-06-22 18:20:23 -04003358 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003359}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Trond Myklebustb944dba2013-11-04 15:20:20 -05003361#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3362#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003363#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3366{
Kinglong Mee8c612822015-08-26 21:12:58 +08003367 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003368 struct nfs4_server_caps_arg args = {
3369 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003370 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003371 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 struct nfs4_server_caps_res res = {};
3373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003375 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 .rpc_resp = &res,
3377 };
3378 int status;
3379
Kinglong Mee8c612822015-08-26 21:12:58 +08003380 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3381 FATTR4_WORD0_FH_EXPIRE_TYPE |
3382 FATTR4_WORD0_LINK_SUPPORT |
3383 FATTR4_WORD0_SYMLINK_SUPPORT |
3384 FATTR4_WORD0_ACLSUPPORT;
3385 if (minorversion)
3386 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3387
Bryan Schumaker7c513052011-03-24 17:12:24 +00003388 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003390 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003391 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003392 case 0:
3393 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3394 res.attr_bitmask[2] = 0;
3395 break;
3396 case 1:
3397 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3398 break;
3399 case 2:
3400 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003403 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3404 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3405 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3406 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003407 NFS_CAP_CTIME|NFS_CAP_MTIME|
3408 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003409 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3410 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 server->caps |= NFS_CAP_ACLS;
3412 if (res.has_links != 0)
3413 server->caps |= NFS_CAP_HARDLINKS;
3414 if (res.has_symlinks != 0)
3415 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003416 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3417 server->caps |= NFS_CAP_FILEID;
3418 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3419 server->caps |= NFS_CAP_MODE;
3420 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3421 server->caps |= NFS_CAP_NLINK;
3422 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3423 server->caps |= NFS_CAP_OWNER;
3424 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3425 server->caps |= NFS_CAP_OWNER_GROUP;
3426 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3427 server->caps |= NFS_CAP_ATIME;
3428 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3429 server->caps |= NFS_CAP_CTIME;
3430 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3431 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003432#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3433 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3434 server->caps |= NFS_CAP_SECURITY_LABEL;
3435#endif
3436 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3437 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003438 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003439
Trond Myklebusta65318b2009-03-11 14:10:28 -04003440 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3441 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3442 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003443 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003444 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3445 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003447 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return status;
3451}
3452
Trond Myklebust55a97592006-06-09 09:34:19 -04003453int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
3455 struct nfs4_exception exception = { };
3456 int err;
3457 do {
3458 err = nfs4_handle_exception(server,
3459 _nfs4_server_capabilities(server, fhandle),
3460 &exception);
3461 } while (exception.retry);
3462 return err;
3463}
3464
3465static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3466 struct nfs_fsinfo *info)
3467{
David Quigleyaa9c2662013-05-22 12:50:44 -04003468 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003470 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 };
3472 struct nfs4_lookup_res res = {
3473 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003474 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 .fh = fhandle,
3476 };
3477 struct rpc_message msg = {
3478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3479 .rpc_argp = &args,
3480 .rpc_resp = &res,
3481 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003482
David Quigleyaa9c2662013-05-22 12:50:44 -04003483 bitmask[0] = nfs4_fattr_bitmap[0];
3484 bitmask[1] = nfs4_fattr_bitmap[1];
3485 /*
3486 * Process the label in the upcoming getfattr
3487 */
3488 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3489
Trond Myklebust0e574af2005-10-27 22:12:38 -04003490 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
3494static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3495 struct nfs_fsinfo *info)
3496{
3497 struct nfs4_exception exception = { };
3498 int err;
3499 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003500 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003501 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003502 switch (err) {
3503 case 0:
3504 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003505 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003506 default:
3507 err = nfs4_handle_exception(server, err, &exception);
3508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003510out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 return err;
3512}
3513
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003514static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3515 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3516{
Trond Myklebustc2190662013-08-26 19:23:04 -04003517 struct rpc_auth_create_args auth_args = {
3518 .pseudoflavor = flavor,
3519 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003520 struct rpc_auth *auth;
3521 int ret;
3522
Trond Myklebustc2190662013-08-26 19:23:04 -04003523 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003524 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003525 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003526 goto out;
3527 }
3528 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003529out:
3530 return ret;
3531}
3532
Chuck Lever9a744ba2013-03-16 15:56:02 -04003533/*
3534 * Retry pseudoroot lookup with various security flavors. We do this when:
3535 *
3536 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3537 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3538 *
3539 * Returns zero on success, or a negative NFS4ERR value, or a
3540 * negative errno value.
3541 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003542static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003543 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003545 /* Per 3530bis 15.33.5 */
3546 static const rpc_authflavor_t flav_array[] = {
3547 RPC_AUTH_GSS_KRB5P,
3548 RPC_AUTH_GSS_KRB5I,
3549 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003550 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003551 RPC_AUTH_NULL,
3552 };
3553 int status = -EPERM;
3554 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003556 if (server->auth_info.flavor_len > 0) {
3557 /* try each flavor specified by user */
3558 for (i = 0; i < server->auth_info.flavor_len; i++) {
3559 status = nfs4_lookup_root_sec(server, fhandle, info,
3560 server->auth_info.flavors[i]);
3561 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3562 continue;
3563 break;
3564 }
3565 } else {
3566 /* no flavors specified by user, try default list */
3567 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3568 status = nfs4_lookup_root_sec(server, fhandle, info,
3569 flav_array[i]);
3570 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3571 continue;
3572 break;
3573 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003574 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003575
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003576 /*
3577 * -EACCESS could mean that the user doesn't have correct permissions
3578 * to access the mount. It could also mean that we tried to mount
3579 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3580 * existing mount programs don't handle -EACCES very well so it should
3581 * be mapped to -EPERM instead.
3582 */
3583 if (status == -EACCES)
3584 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003585 return status;
3586}
3587
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003588/**
3589 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3590 * @server: initialized nfs_server handle
3591 * @fhandle: we fill in the pseudo-fs root file handle
3592 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003593 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003594 *
3595 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003596 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003597int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003598 struct nfs_fsinfo *info,
3599 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003600{
Andre Przywarac7757072015-04-23 17:17:40 +01003601 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003602
Andre Przywarac7757072015-04-23 17:17:40 +01003603 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003604 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003605
3606 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003607 status = server->nfs_client->cl_mvops->find_root_sec(server,
3608 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 if (status == 0)
3611 status = nfs4_server_capabilities(server, fhandle);
3612 if (status == 0)
3613 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003614
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003615 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
Bryan Schumakerbae36242012-05-10 15:07:31 -04003618static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3619 struct nfs_fsinfo *info)
3620{
3621 int error;
3622 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003623 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003624
3625 error = nfs4_server_capabilities(server, mntfh);
3626 if (error < 0) {
3627 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3628 return error;
3629 }
3630
David Quigley14c43f72013-05-22 12:50:43 -04003631 label = nfs4_label_alloc(server, GFP_KERNEL);
3632 if (IS_ERR(label))
3633 return PTR_ERR(label);
3634
David Quigley1775fd32013-05-22 12:50:42 -04003635 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003636 if (error < 0) {
3637 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003638 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003639 }
3640
3641 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3642 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3643 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3644
David Quigley14c43f72013-05-22 12:50:43 -04003645err_free_label:
3646 nfs4_label_free(label);
3647
Bryan Schumakerbae36242012-05-10 15:07:31 -04003648 return error;
3649}
3650
Manoj Naik6b97fd32006-06-09 09:34:29 -04003651/*
3652 * Get locations and (maybe) other attributes of a referral.
3653 * Note that we'll actually follow the referral later when
3654 * we detect fsid mismatch in inode revalidation
3655 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003656static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3657 const struct qstr *name, struct nfs_fattr *fattr,
3658 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003659{
3660 int status = -ENOMEM;
3661 struct page *page = NULL;
3662 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003663
3664 page = alloc_page(GFP_KERNEL);
3665 if (page == NULL)
3666 goto out;
3667 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3668 if (locations == NULL)
3669 goto out;
3670
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003671 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003672 if (status != 0)
3673 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003674
3675 /*
3676 * If the fsid didn't change, this is a migration event, not a
3677 * referral. Cause us to drop into the exception handler, which
3678 * will kick off migration recovery.
3679 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003680 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003681 dprintk("%s: server did not return a different fsid for"
3682 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003683 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003684 goto out;
3685 }
Andy Adamson533eb462011-06-13 18:25:56 -04003686 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3687 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003688
Andy Adamson533eb462011-06-13 18:25:56 -04003689 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003690 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003691 memset(fhandle, 0, sizeof(struct nfs_fh));
3692out:
3693 if (page)
3694 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003695 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003696 return status;
3697}
3698
David Quigley1775fd32013-05-22 12:50:42 -04003699static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3700 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
3702 struct nfs4_getattr_arg args = {
3703 .fh = fhandle,
3704 .bitmask = server->attr_bitmask,
3705 };
3706 struct nfs4_getattr_res res = {
3707 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003708 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 .server = server,
3710 };
3711 struct rpc_message msg = {
3712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3713 .rpc_argp = &args,
3714 .rpc_resp = &res,
3715 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003716
3717 args.bitmask = nfs4_bitmask(server, label);
3718
Trond Myklebust0e574af2005-10-27 22:12:38 -04003719 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003720 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
David Quigley1775fd32013-05-22 12:50:42 -04003723static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3724 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
3726 struct nfs4_exception exception = { };
3727 int err;
3728 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003729 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3730 trace_nfs4_getattr(server, fhandle, fattr, err);
3731 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 &exception);
3733 } while (exception.retry);
3734 return err;
3735}
3736
3737/*
3738 * The file is not closed if it is opened due to the a request to change
3739 * the size of the file. The open call will not be needed once the
3740 * VFS layer lookup-intents are implemented.
3741 *
3742 * Close is called when the inode is destroyed.
3743 * If we haven't opened the file for O_WRONLY, we
3744 * need to in the size_change case to obtain a stateid.
3745 *
3746 * Got race?
3747 * Because OPEN is always done by name in nfsv4, it is
3748 * possible that we opened a different file by the same
3749 * name. We can recognize this race condition, but we
3750 * can't do anything about it besides returning an error.
3751 *
3752 * This will be fixed with VFS changes (lookup-intent).
3753 */
3754static int
3755nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3756 struct iattr *sattr)
3757{
David Howells2b0143b2015-03-17 22:25:59 +00003758 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003759 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003760 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003761 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 int status;
3763
Peng Tao88ac8152014-09-12 11:04:10 +08003764 if (pnfs_ld_layoutret_on_setattr(inode) &&
3765 sattr->ia_valid & ATTR_SIZE &&
3766 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003767 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003768
Trond Myklebust0e574af2005-10-27 22:12:38 -04003769 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Andy Adamson26699402012-05-30 16:12:24 -04003771 /* Deal with open(O_TRUNC) */
3772 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003773 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003774
3775 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003776 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003777 return 0;
3778
Trond Myklebustd5308382005-11-04 15:33:38 -05003779 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003780 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003781
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003782 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003783 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003784 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003785 }
3786
David Quigley14c43f72013-05-22 12:50:43 -04003787 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3788 if (IS_ERR(label))
3789 return PTR_ERR(label);
3790
NeilBrown29b59f92016-10-13 15:26:47 +11003791 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003792 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003793 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003794 nfs_setsecurity(inode, fattr, label);
3795 }
David Quigley14c43f72013-05-22 12:50:43 -04003796 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return status;
3798}
3799
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003800static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3801 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003802 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003803{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003804 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003805 int status;
3806 struct nfs4_lookup_arg args = {
3807 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003808 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003809 .name = name,
3810 };
3811 struct nfs4_lookup_res res = {
3812 .server = server,
3813 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003814 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003815 .fh = fhandle,
3816 };
3817 struct rpc_message msg = {
3818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3819 .rpc_argp = &args,
3820 .rpc_resp = &res,
3821 };
3822
David Quigleyaa9c2662013-05-22 12:50:44 -04003823 args.bitmask = nfs4_bitmask(server, label);
3824
David Howells2b3de442006-08-22 20:06:09 -04003825 nfs_fattr_init(fattr);
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003828 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 dprintk("NFS reply lookup: %d\n", status);
3830 return status;
3831}
3832
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003833static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003834{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003835 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003836 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003837 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3838 fattr->nlink = 2;
3839}
3840
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003841static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003842 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003843 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003844{
3845 struct nfs4_exception exception = { };
3846 struct rpc_clnt *client = *clnt;
3847 int err;
3848 do {
David Quigley1775fd32013-05-22 12:50:42 -04003849 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003850 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003851 switch (err) {
3852 case -NFS4ERR_BADNAME:
3853 err = -ENOENT;
3854 goto out;
3855 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003856 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003857 if (err == -NFS4ERR_MOVED)
3858 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003859 goto out;
3860 case -NFS4ERR_WRONGSEC:
3861 err = -EPERM;
3862 if (client != *clnt)
3863 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003864 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003865 if (IS_ERR(client))
3866 return PTR_ERR(client);
3867
3868 exception.retry = 1;
3869 break;
3870 default:
3871 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3872 }
3873 } while (exception.retry);
3874
3875out:
3876 if (err == 0)
3877 *clnt = client;
3878 else if (client != *clnt)
3879 rpc_shutdown_client(client);
3880
3881 return err;
3882}
3883
Al Virobeffb8f2016-07-20 16:34:42 -04003884static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003885 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3886 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003888 int status;
3889 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003890
David Quigley1775fd32013-05-22 12:50:42 -04003891 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003892 if (client != NFS_CLIENT(dir)) {
3893 rpc_shutdown_client(client);
3894 nfs_fixup_secinfo_attributes(fattr);
3895 }
3896 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897}
3898
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003899struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003900nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003901 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3902{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003903 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003904 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003905
David Quigley1775fd32013-05-22 12:50:42 -04003906 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003907 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003908 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003909 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003910}
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3913{
Trond Myklebust76b32992007-08-10 17:45:11 -04003914 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct nfs4_accessargs args = {
3916 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003917 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003919 struct nfs4_accessres res = {
3920 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003921 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 struct rpc_message msg = {
3923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3924 .rpc_argp = &args,
3925 .rpc_resp = &res,
3926 .rpc_cred = entry->cred,
3927 };
3928 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003929 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 /*
3932 * Determine which access bits we want to ask for...
3933 */
3934 if (mode & MAY_READ)
3935 args.access |= NFS4_ACCESS_READ;
3936 if (S_ISDIR(inode->i_mode)) {
3937 if (mode & MAY_WRITE)
3938 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3939 if (mode & MAY_EXEC)
3940 args.access |= NFS4_ACCESS_LOOKUP;
3941 } else {
3942 if (mode & MAY_WRITE)
3943 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3944 if (mode & MAY_EXEC)
3945 args.access |= NFS4_ACCESS_EXECUTE;
3946 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003947
3948 res.fattr = nfs_alloc_fattr();
3949 if (res.fattr == NULL)
3950 return -ENOMEM;
3951
Bryan Schumaker7c513052011-03-24 17:12:24 +00003952 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003954 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003955 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003957 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return status;
3959}
3960
3961static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3962{
3963 struct nfs4_exception exception = { };
3964 int err;
3965 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003966 err = _nfs4_proc_access(inode, entry);
3967 trace_nfs4_access(inode, err);
3968 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 &exception);
3970 } while (exception.retry);
3971 return err;
3972}
3973
3974/*
3975 * TODO: For the time being, we don't try to get any attributes
3976 * along with any of the zero-copy operations READ, READDIR,
3977 * READLINK, WRITE.
3978 *
3979 * In the case of the first three, we want to put the GETATTR
3980 * after the read-type operation -- this is because it is hard
3981 * to predict the length of a GETATTR response in v4, and thus
3982 * align the READ data correctly. This means that the GETATTR
3983 * may end up partially falling into the page cache, and we should
3984 * shift it into the 'tail' of the xdr_buf before processing.
3985 * To do this efficiently, we need to know the total length
3986 * of data received, which doesn't seem to be available outside
3987 * of the RPC layer.
3988 *
3989 * In the case of WRITE, we also want to put the GETATTR after
3990 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003991 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 *
3993 * Both of these changes to the XDR layer would in fact be quite
3994 * minor, but I decided to leave them for a subsequent patch.
3995 */
3996static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3997 unsigned int pgbase, unsigned int pglen)
3998{
3999 struct nfs4_readlink args = {
4000 .fh = NFS_FH(inode),
4001 .pgbase = pgbase,
4002 .pglen = pglen,
4003 .pages = &page,
4004 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004005 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 struct rpc_message msg = {
4007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4008 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004009 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 };
4011
Bryan Schumaker7c513052011-03-24 17:12:24 +00004012 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 -07004013}
4014
4015static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4016 unsigned int pgbase, unsigned int pglen)
4017{
4018 struct nfs4_exception exception = { };
4019 int err;
4020 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004021 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4022 trace_nfs4_readlink(inode, err);
4023 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 &exception);
4025 } while (exception.retry);
4026 return err;
4027}
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004030 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032static int
4033nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004034 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004036 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004037 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004038 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 int status = 0;
4041
NeilBrown532d4de2016-10-13 15:26:47 +11004042 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004043 if (IS_ERR(ctx))
4044 return PTR_ERR(ctx);
4045
David Quigleyaa9c2662013-05-22 12:50:44 -04004046 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4047
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004048 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4049 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004050 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (IS_ERR(state)) {
4052 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004056 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004057 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return status;
4059}
4060
Al Virobeffb8f2016-07-20 16:34:42 -04004061static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Trond Myklebust16e42952005-10-27 22:12:44 -04004063 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004064 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004066 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004068 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004069 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004070 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4073 .rpc_argp = &args,
4074 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004076 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004077
Bryan Schumaker7c513052011-03-24 17:12:24 +00004078 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004079 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004080 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 return status;
4082}
4083
Al Virobeffb8f2016-07-20 16:34:42 -04004084static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
4086 struct nfs4_exception exception = { };
4087 int err;
4088 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004089 err = _nfs4_proc_remove(dir, name);
4090 trace_nfs4_remove(dir, name, err);
4091 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 &exception);
4093 } while (exception.retry);
4094 return err;
4095}
4096
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004097static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004099 struct nfs_server *server = NFS_SERVER(dir);
4100 struct nfs_removeargs *args = msg->rpc_argp;
4101 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004103 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004105 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004106
4107 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004110static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4111{
Al Viro884be172016-04-28 23:56:31 -04004112 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004113 &data->args.seq_args,
4114 &data->res.seq_res,
4115 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004118static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004120 struct nfs_unlinkdata *data = task->tk_calldata;
4121 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004122
Trond Myklebust14516c32010-07-31 14:29:06 -04004123 if (!nfs4_sequence_done(task, &res->seq_res))
4124 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004125 if (nfs4_async_handle_error(task, res->server, NULL,
4126 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004127 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004128 if (task->tk_status == 0)
4129 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004130 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Jeff Laytond3d41522010-09-17 17:31:57 -04004133static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4134{
4135 struct nfs_server *server = NFS_SERVER(dir);
4136 struct nfs_renameargs *arg = msg->rpc_argp;
4137 struct nfs_renameres *res = msg->rpc_resp;
4138
4139 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004140 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004141 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004142}
4143
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004144static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4145{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004146 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4147 &data->args.seq_args,
4148 &data->res.seq_res,
4149 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004150}
4151
4152static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4153 struct inode *new_dir)
4154{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004155 struct nfs_renamedata *data = task->tk_calldata;
4156 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004157
4158 if (!nfs4_sequence_done(task, &res->seq_res))
4159 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004160 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004161 return 0;
4162
Trond Myklebustc733c492017-01-11 12:32:26 -05004163 if (task->tk_status == 0) {
4164 update_changeattr(old_dir, &res->old_cinfo);
4165 if (new_dir != old_dir)
4166 update_changeattr(new_dir, &res->new_cinfo);
4167 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004168 return 1;
4169}
4170
Al Virobeffb8f2016-07-20 16:34:42 -04004171static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004173 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 struct nfs4_link_arg arg = {
4175 .fh = NFS_FH(inode),
4176 .dir_fh = NFS_FH(dir),
4177 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004178 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004180 struct nfs4_link_res res = {
4181 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004182 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4186 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004187 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004189 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Trond Myklebust136f2622010-04-16 16:22:49 -04004191 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004192 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004193 goto out;
4194
David Quigley14c43f72013-05-22 12:50:43 -04004195 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4196 if (IS_ERR(res.label)) {
4197 status = PTR_ERR(res.label);
4198 goto out;
4199 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004200 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004201
Bryan Schumaker7c513052011-03-24 17:12:24 +00004202 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004203 if (!status) {
4204 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004205 status = nfs_post_op_update_inode(inode, res.fattr);
4206 if (!status)
4207 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004208 }
David Quigley14c43f72013-05-22 12:50:43 -04004209
4210
4211 nfs4_label_free(res.label);
4212
Trond Myklebust136f2622010-04-16 16:22:49 -04004213out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004214 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return status;
4216}
4217
Al Virobeffb8f2016-07-20 16:34:42 -04004218static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
4220 struct nfs4_exception exception = { };
4221 int err;
4222 do {
4223 err = nfs4_handle_exception(NFS_SERVER(inode),
4224 _nfs4_proc_link(inode, dir, name),
4225 &exception);
4226 } while (exception.retry);
4227 return err;
4228}
4229
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230struct nfs4_createdata {
4231 struct rpc_message msg;
4232 struct nfs4_create_arg arg;
4233 struct nfs4_create_res res;
4234 struct nfs_fh fh;
4235 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004236 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237};
4238
4239static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004240 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241{
4242 struct nfs4_createdata *data;
4243
4244 data = kzalloc(sizeof(*data), GFP_KERNEL);
4245 if (data != NULL) {
4246 struct nfs_server *server = NFS_SERVER(dir);
4247
David Quigley14c43f72013-05-22 12:50:43 -04004248 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4249 if (IS_ERR(data->label))
4250 goto out_free;
4251
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4253 data->msg.rpc_argp = &data->arg;
4254 data->msg.rpc_resp = &data->res;
4255 data->arg.dir_fh = NFS_FH(dir);
4256 data->arg.server = server;
4257 data->arg.name = name;
4258 data->arg.attrs = sattr;
4259 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004260 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004261 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 data->res.server = server;
4263 data->res.fh = &data->fh;
4264 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004265 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004266 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267 }
4268 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004269out_free:
4270 kfree(data);
4271 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004272}
4273
4274static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4275{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004276 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004277 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004278 if (status == 0) {
4279 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004280 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004281 }
4282 return status;
4283}
4284
4285static void nfs4_free_createdata(struct nfs4_createdata *data)
4286{
David Quigley14c43f72013-05-22 12:50:43 -04004287 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004288 kfree(data);
4289}
4290
Chuck Lever4f390c12006-08-22 20:06:22 -04004291static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004292 struct page *page, unsigned int len, struct iattr *sattr,
4293 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004295 struct nfs4_createdata *data;
4296 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Chuck Lever94a6d752006-08-22 20:06:23 -04004298 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004299 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004300
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004301 status = -ENOMEM;
4302 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4303 if (data == NULL)
4304 goto out;
4305
4306 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4307 data->arg.u.symlink.pages = &page;
4308 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004309 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004311 status = nfs4_do_create(dir, dentry, data);
4312
4313 nfs4_free_createdata(data);
4314out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return status;
4316}
4317
Chuck Lever4f390c12006-08-22 20:06:22 -04004318static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004319 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
4321 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004322 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004324
4325 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004328 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4329 trace_nfs4_symlink(dir, &dentry->d_name, err);
4330 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 &exception);
4332 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004333
4334 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return err;
4336}
4337
4338static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004339 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004341 struct nfs4_createdata *data;
4342 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4345 if (data == NULL)
4346 goto out;
4347
David Quigley1775fd32013-05-22 12:50:42 -04004348 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349 status = nfs4_do_create(dir, dentry, data);
4350
4351 nfs4_free_createdata(data);
4352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return status;
4354}
4355
4356static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4357 struct iattr *sattr)
4358{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004359 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004361 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004363
David Quigleyaa9c2662013-05-22 12:50:44 -04004364 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4365
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004366 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4367 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004369 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4370 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4371 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 &exception);
4373 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004374 nfs4_label_release_security(label);
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return err;
4377}
4378
4379static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004380 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
David Howells2b0143b2015-03-17 22:25:59 +00004382 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 struct nfs4_readdir_arg args = {
4384 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004385 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 .pgbase = 0,
4387 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004388 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004389 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 };
4391 struct nfs4_readdir_res res;
4392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4394 .rpc_argp = &args,
4395 .rpc_resp = &res,
4396 .rpc_cred = cred,
4397 };
4398 int status;
4399
Al Viro6de14722013-09-16 10:53:17 -04004400 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4401 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004402 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004403 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004405 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 -05004406 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004407 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004408 status += args.pgbase;
4409 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004410
4411 nfs_invalidate_atime(dir);
4412
Harvey Harrison3110ff82008-05-02 13:42:44 -07004413 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return status;
4415}
4416
4417static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004418 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
4420 struct nfs4_exception exception = { };
4421 int err;
4422 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004423 err = _nfs4_proc_readdir(dentry, cred, cookie,
4424 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004425 trace_nfs4_readdir(d_inode(dentry), err);
4426 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 &exception);
4428 } while (exception.retry);
4429 return err;
4430}
4431
4432static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004433 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004435 struct nfs4_createdata *data;
4436 int mode = sattr->ia_mode;
4437 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004439 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4440 if (data == NULL)
4441 goto out;
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004444 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004446 data->arg.ftype = NF4BLK;
4447 data->arg.u.device.specdata1 = MAJOR(rdev);
4448 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
4450 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004451 data->arg.ftype = NF4CHR;
4452 data->arg.u.device.specdata1 = MAJOR(rdev);
4453 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004454 } else if (!S_ISSOCK(mode)) {
4455 status = -EINVAL;
4456 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
David Quigley1775fd32013-05-22 12:50:42 -04004458
David Quigleyaa9c2662013-05-22 12:50:44 -04004459 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004460 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004461out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004462 nfs4_free_createdata(data);
4463out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return status;
4465}
4466
4467static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4468 struct iattr *sattr, dev_t rdev)
4469{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004470 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004472 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004474
David Quigleyaa9c2662013-05-22 12:50:44 -04004475 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4476
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004477 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4478 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004480 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4481 trace_nfs4_mknod(dir, &dentry->d_name, err);
4482 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 &exception);
4484 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004485
4486 nfs4_label_release_security(label);
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return err;
4489}
4490
4491static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4492 struct nfs_fsstat *fsstat)
4493{
4494 struct nfs4_statfs_arg args = {
4495 .fh = fhandle,
4496 .bitmask = server->attr_bitmask,
4497 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004498 struct nfs4_statfs_res res = {
4499 .fsstat = fsstat,
4500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 struct rpc_message msg = {
4502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4503 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004504 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 };
4506
Trond Myklebust0e574af2005-10-27 22:12:38 -04004507 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509}
4510
4511static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4512{
4513 struct nfs4_exception exception = { };
4514 int err;
4515 do {
4516 err = nfs4_handle_exception(server,
4517 _nfs4_proc_statfs(server, fhandle, fsstat),
4518 &exception);
4519 } while (exception.retry);
4520 return err;
4521}
4522
4523static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4524 struct nfs_fsinfo *fsinfo)
4525{
4526 struct nfs4_fsinfo_arg args = {
4527 .fh = fhandle,
4528 .bitmask = server->attr_bitmask,
4529 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004530 struct nfs4_fsinfo_res res = {
4531 .fsinfo = fsinfo,
4532 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 struct rpc_message msg = {
4534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4535 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004536 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 };
4538
Bryan Schumaker7c513052011-03-24 17:12:24 +00004539 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540}
4541
4542static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4543{
4544 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004545 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 int err;
4547
4548 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004549 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004550 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004551 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004552 nfs4_set_lease_period(server->nfs_client,
4553 fsinfo->lease_time * HZ,
4554 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004555 break;
4556 }
4557 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 } while (exception.retry);
4559 return err;
4560}
4561
4562static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4563{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004564 int error;
4565
Trond Myklebust0e574af2005-10-27 22:12:38 -04004566 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004567 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004568 if (error == 0) {
4569 /* block layout checks this! */
4570 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004571 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004572 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004573
4574 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
4577static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4578 struct nfs_pathconf *pathconf)
4579{
4580 struct nfs4_pathconf_arg args = {
4581 .fh = fhandle,
4582 .bitmask = server->attr_bitmask,
4583 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004584 struct nfs4_pathconf_res res = {
4585 .pathconf = pathconf,
4586 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 struct rpc_message msg = {
4588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4589 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004590 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 };
4592
4593 /* None of the pathconf attributes are mandatory to implement */
4594 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4595 memset(pathconf, 0, sizeof(*pathconf));
4596 return 0;
4597 }
4598
Trond Myklebust0e574af2005-10-27 22:12:38 -04004599 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004600 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
4603static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4604 struct nfs_pathconf *pathconf)
4605{
4606 struct nfs4_exception exception = { };
4607 int err;
4608
4609 do {
4610 err = nfs4_handle_exception(server,
4611 _nfs4_proc_pathconf(server, fhandle, pathconf),
4612 &exception);
4613 } while (exception.retry);
4614 return err;
4615}
4616
Trond Myklebust5521abf2013-03-16 20:54:34 -04004617int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004618 const struct nfs_open_context *ctx,
4619 const struct nfs_lock_context *l_ctx,
4620 fmode_t fmode)
4621{
NeilBrown17393472016-10-13 15:26:47 +11004622 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004623}
4624EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4625
Trond Myklebust5521abf2013-03-16 20:54:34 -04004626static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4627 const struct nfs_open_context *ctx,
4628 const struct nfs_lock_context *l_ctx,
4629 fmode_t fmode)
4630{
4631 nfs4_stateid current_stateid;
4632
Trond Myklebuste1253be2014-03-05 08:44:23 -05004633 /* If the current stateid represents a lost lock, then exit */
4634 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4635 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004636 return nfs4_stateid_match(stateid, &current_stateid);
4637}
4638
4639static bool nfs4_error_stateid_expired(int err)
4640{
4641 switch (err) {
4642 case -NFS4ERR_DELEG_REVOKED:
4643 case -NFS4ERR_ADMIN_REVOKED:
4644 case -NFS4ERR_BAD_STATEID:
4645 case -NFS4ERR_STALE_STATEID:
4646 case -NFS4ERR_OLD_STATEID:
4647 case -NFS4ERR_OPENMODE:
4648 case -NFS4ERR_EXPIRED:
4649 return true;
4650 }
4651 return false;
4652}
4653
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004656 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004658 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004659 if (task->tk_status < 0) {
4660 struct nfs4_exception exception = {
4661 .inode = hdr->inode,
4662 .state = hdr->args.context->state,
4663 .stateid = &hdr->args.stateid,
4664 };
4665 task->tk_status = nfs4_async_handle_exception(task,
4666 server, task->tk_status, &exception);
4667 if (exception.retry) {
4668 rpc_restart_call_prepare(task);
4669 return -EAGAIN;
4670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
Trond Myklebust5521abf2013-03-16 20:54:34 -04004678static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004679 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004680{
4681
4682 if (!nfs4_error_stateid_expired(task->tk_status) ||
4683 nfs4_stateid_is_current(&args->stateid,
4684 args->context,
4685 args->lock_context,
4686 FMODE_READ))
4687 return false;
4688 rpc_restart_call_prepare(task);
4689 return true;
4690}
4691
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004693{
4694
4695 dprintk("--> %s\n", __func__);
4696
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004698 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004700 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004701 if (task->tk_status > 0)
4702 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4704 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004705}
4706
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4708 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004710 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004711 if (!hdr->pgio_done_cb)
4712 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004713 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004714 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004717static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4718 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004719{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004720 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4721 &hdr->args.seq_args,
4722 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004723 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004724 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004725 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4726 hdr->args.lock_context,
4727 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004728 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004730 return -EIO;
4731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734static int nfs4_write_done_cb(struct rpc_task *task,
4735 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004737 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004738
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004739 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004740 if (task->tk_status < 0) {
4741 struct nfs4_exception exception = {
4742 .inode = hdr->inode,
4743 .state = hdr->args.context->state,
4744 .stateid = &hdr->args.stateid,
4745 };
4746 task->tk_status = nfs4_async_handle_exception(task,
4747 NFS_SERVER(inode), task->tk_status,
4748 &exception);
4749 if (exception.retry) {
4750 rpc_restart_call_prepare(task);
4751 return -EAGAIN;
4752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004754 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004756 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004757 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
Trond Myklebust5521abf2013-03-16 20:54:34 -04004761static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004762 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004763{
4764
4765 if (!nfs4_error_stateid_expired(task->tk_status) ||
4766 nfs4_stateid_is_current(&args->stateid,
4767 args->context,
4768 args->lock_context,
4769 FMODE_WRITE))
4770 return false;
4771 rpc_restart_call_prepare(task);
4772 return true;
4773}
4774
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004775static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004776{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004777 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004778 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004779 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004780 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004781 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4782 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004783}
4784
Trond Myklebust5a37f852012-04-28 14:55:16 -04004785static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004786bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004787{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004788 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004789 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004790 return false;
4791 /* Otherwise, request attributes if and only if we don't hold
4792 * a delegation
4793 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004794 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004795}
Fred Isamana69aef12011-03-03 15:13:47 +00004796
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004797static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4798 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004800 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004801
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004802 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4803 hdr->args.bitmask = NULL;
4804 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004805 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004806 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004807
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808 if (!hdr->pgio_done_cb)
4809 hdr->pgio_done_cb = nfs4_write_done_cb;
4810 hdr->res.server = server;
4811 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004813 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004814 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
Fred Isaman0b7c0152012-04-20 14:47:39 -04004817static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4818{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004819 nfs4_setup_sequence(NFS_SERVER(data->inode),
4820 &data->args.seq_args,
4821 &data->res.seq_res,
4822 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Fred Isaman0b7c0152012-04-20 14:47:39 -04004825static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004828
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004829 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004830 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4831 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004832 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004833 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Fred Isaman0b7c0152012-04-20 14:47:39 -04004838static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004839{
4840 if (!nfs4_sequence_done(task, &data->res.seq_res))
4841 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004842 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004843}
4844
Fred Isaman0b7c0152012-04-20 14:47:39 -04004845static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004847 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004848
Fred Isaman0b7c0152012-04-20 14:47:39 -04004849 if (data->commit_done_cb == NULL)
4850 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004851 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004852 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004853 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004856struct nfs4_renewdata {
4857 struct nfs_client *client;
4858 unsigned long timestamp;
4859};
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861/*
4862 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4863 * standalone procedure for queueing an asynchronous RENEW.
4864 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004865static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004866{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004867 struct nfs4_renewdata *data = calldata;
4868 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004869
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004870 if (atomic_read(&clp->cl_count) > 1)
4871 nfs4_schedule_state_renewal(clp);
4872 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004873 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004874}
4875
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004876static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004878 struct nfs4_renewdata *data = calldata;
4879 struct nfs_client *clp = data->client;
4880 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004882 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004883 switch (task->tk_status) {
4884 case 0:
4885 break;
4886 case -NFS4ERR_LEASE_MOVED:
4887 nfs4_schedule_lease_moved_recovery(clp);
4888 break;
4889 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004890 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004891 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4892 return;
4893 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004894 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004895 return;
4896 }
4897 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004899 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004902static const struct rpc_call_ops nfs4_renew_ops = {
4903 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004904 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004905};
4906
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004907static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4911 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004912 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004914 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004916 if (renew_flags == 0)
4917 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004918 if (!atomic_inc_not_zero(&clp->cl_count))
4919 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004920 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004921 if (data == NULL)
4922 return -ENOMEM;
4923 data->client = clp;
4924 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004925 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004926 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004929static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
4931 struct rpc_message msg = {
4932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4933 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004934 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 };
4936 unsigned long now = jiffies;
4937 int status;
4938
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004939 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (status < 0)
4941 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004942 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 return 0;
4944}
4945
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004946static inline int nfs4_server_supports_acls(struct nfs_server *server)
4947{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004948 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004949}
4950
Trond Myklebust21f498c2012-08-24 10:59:25 -04004951/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4952 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004953 * the stack.
4954 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004955#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004956
Neil Hormane9e3d722011-03-04 19:26:03 -05004957static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004958 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004959{
4960 struct page *newpage, **spages;
4961 int rc = 0;
4962 size_t len;
4963 spages = pages;
4964
4965 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004966 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004967 newpage = alloc_page(GFP_KERNEL);
4968
4969 if (newpage == NULL)
4970 goto unwind;
4971 memcpy(page_address(newpage), buf, len);
4972 buf += len;
4973 buflen -= len;
4974 *pages++ = newpage;
4975 rc++;
4976 } while (buflen != 0);
4977
4978 return rc;
4979
4980unwind:
4981 for(; rc > 0; rc--)
4982 __free_page(spages[rc-1]);
4983 return -ENOMEM;
4984}
4985
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004986struct nfs4_cached_acl {
4987 int cached;
4988 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004989 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004990};
4991
4992static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004993{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004994 struct nfs_inode *nfsi = NFS_I(inode);
4995
4996 spin_lock(&inode->i_lock);
4997 kfree(nfsi->nfs4_acl);
4998 nfsi->nfs4_acl = acl;
4999 spin_unlock(&inode->i_lock);
5000}
5001
5002static void nfs4_zap_acl_attr(struct inode *inode)
5003{
5004 nfs4_set_cached_acl(inode, NULL);
5005}
5006
5007static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5008{
5009 struct nfs_inode *nfsi = NFS_I(inode);
5010 struct nfs4_cached_acl *acl;
5011 int ret = -ENOENT;
5012
5013 spin_lock(&inode->i_lock);
5014 acl = nfsi->nfs4_acl;
5015 if (acl == NULL)
5016 goto out;
5017 if (buf == NULL) /* user is just asking for length */
5018 goto out_len;
5019 if (acl->cached == 0)
5020 goto out;
5021 ret = -ERANGE; /* see getxattr(2) man page */
5022 if (acl->len > buflen)
5023 goto out;
5024 memcpy(buf, acl->data, acl->len);
5025out_len:
5026 ret = acl->len;
5027out:
5028 spin_unlock(&inode->i_lock);
5029 return ret;
5030}
5031
Sachin Prabhu5794d212012-04-17 14:36:40 +01005032static 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 +00005033{
5034 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005035 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005036
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005037 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005038 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005039 if (acl == NULL)
5040 goto out;
5041 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005042 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005043 } else {
5044 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5045 if (acl == NULL)
5046 goto out;
5047 acl->cached = 0;
5048 }
5049 acl->len = acl_len;
5050out:
5051 nfs4_set_cached_acl(inode, acl);
5052}
5053
Andy Adamsonbf118a32011-12-07 11:55:27 -05005054/*
5055 * The getxattr API returns the required buffer length when called with a
5056 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5057 * the required buf. On a NULL buf, we send a page of data to the server
5058 * guessing that the ACL request can be serviced by a page. If so, we cache
5059 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5060 * the cache. If not so, we throw away the page, and cache the required
5061 * length. The next getxattr call will then produce another round trip to
5062 * the server, this time with the input buf of the required size.
5063 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005064static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005065{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005066 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005067 struct nfs_getaclargs args = {
5068 .fh = NFS_FH(inode),
5069 .acl_pages = pages,
5070 .acl_len = buflen,
5071 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005072 struct nfs_getaclres res = {
5073 .acl_len = buflen,
5074 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5077 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005078 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005079 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005080 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5081 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005082
Andy Adamsonbf118a32011-12-07 11:55:27 -05005083 /* As long as we're doing a round trip to the server anyway,
5084 * let's be prepared for a page of acl data. */
5085 if (npages == 0)
5086 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005087 if (npages > ARRAY_SIZE(pages))
5088 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005089
Andy Adamsonbf118a32011-12-07 11:55:27 -05005090 for (i = 0; i < npages; i++) {
5091 pages[i] = alloc_page(GFP_KERNEL);
5092 if (!pages[i])
5093 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005095
5096 /* for decoding across pages */
5097 res.acl_scratch = alloc_page(GFP_KERNEL);
5098 if (!res.acl_scratch)
5099 goto out_free;
5100
Andy Adamsonbf118a32011-12-07 11:55:27 -05005101 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005102
Peng Taode040be2012-01-10 22:42:47 +08005103 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005104 __func__, buf, buflen, npages, args.acl_len);
5105 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5106 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005107 if (ret)
5108 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005109
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005110 /* Handle the case where the passed-in buffer is too short */
5111 if (res.acl_flags & NFS4_ACL_TRUNC) {
5112 /* Did the user only issue a request for the acl length? */
5113 if (buf == NULL)
5114 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005115 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005116 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005117 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005118 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005119 if (buf) {
5120 if (res.acl_len > buflen) {
5121 ret = -ERANGE;
5122 goto out_free;
5123 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005124 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005125 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005126out_ok:
5127 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005128out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005129 for (i = 0; i < npages; i++)
5130 if (pages[i])
5131 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005132 if (res.acl_scratch)
5133 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005134 return ret;
5135}
5136
Trond Myklebust16b42892006-08-24 12:27:15 -04005137static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5138{
5139 struct nfs4_exception exception = { };
5140 ssize_t ret;
5141 do {
5142 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005143 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005144 if (ret >= 0)
5145 break;
5146 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5147 } while (exception.retry);
5148 return ret;
5149}
5150
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005151static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5152{
5153 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005154 int ret;
5155
5156 if (!nfs4_server_supports_acls(server))
5157 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005158 ret = nfs_revalidate_inode(server, inode);
5159 if (ret < 0)
5160 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005161 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5162 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005163 ret = nfs4_read_cached_acl(inode, buf, buflen);
5164 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005165 /* -ENOENT is returned if there is no ACL or if there is an ACL
5166 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005167 return ret;
5168 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005169}
5170
Trond Myklebust16b42892006-08-24 12:27:15 -04005171static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005172{
5173 struct nfs_server *server = NFS_SERVER(inode);
5174 struct page *pages[NFS4ACL_MAXPAGES];
5175 struct nfs_setaclargs arg = {
5176 .fh = NFS_FH(inode),
5177 .acl_pages = pages,
5178 .acl_len = buflen,
5179 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005180 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005181 struct rpc_message msg = {
5182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5183 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005184 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005185 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005186 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005187 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005188
5189 if (!nfs4_server_supports_acls(server))
5190 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005191 if (npages > ARRAY_SIZE(pages))
5192 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005193 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005194 if (i < 0)
5195 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005196 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005197 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005198
5199 /*
5200 * Free each page after tx, so the only ref left is
5201 * held by the network stack
5202 */
5203 for (; i > 0; i--)
5204 put_page(pages[i-1]);
5205
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005206 /*
5207 * Acl update can result in inode attribute update.
5208 * so mark the attribute cache invalid.
5209 */
5210 spin_lock(&inode->i_lock);
5211 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5212 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005213 nfs_access_zap_cache(inode);
5214 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005215 return ret;
5216}
5217
Trond Myklebust16b42892006-08-24 12:27:15 -04005218static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5219{
5220 struct nfs4_exception exception = { };
5221 int err;
5222 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005223 err = __nfs4_proc_set_acl(inode, buf, buflen);
5224 trace_nfs4_set_acl(inode, err);
5225 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005226 &exception);
5227 } while (exception.retry);
5228 return err;
5229}
5230
David Quigleyaa9c2662013-05-22 12:50:44 -04005231#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5232static int _nfs4_get_security_label(struct inode *inode, void *buf,
5233 size_t buflen)
5234{
5235 struct nfs_server *server = NFS_SERVER(inode);
5236 struct nfs_fattr fattr;
5237 struct nfs4_label label = {0, 0, buflen, buf};
5238
5239 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005240 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005241 .fh = NFS_FH(inode),
5242 .bitmask = bitmask,
5243 };
5244 struct nfs4_getattr_res res = {
5245 .fattr = &fattr,
5246 .label = &label,
5247 .server = server,
5248 };
5249 struct rpc_message msg = {
5250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005251 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005252 .rpc_resp = &res,
5253 };
5254 int ret;
5255
5256 nfs_fattr_init(&fattr);
5257
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005258 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005259 if (ret)
5260 return ret;
5261 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5262 return -ENOENT;
5263 if (buflen < label.len)
5264 return -ERANGE;
5265 return 0;
5266}
5267
5268static int nfs4_get_security_label(struct inode *inode, void *buf,
5269 size_t buflen)
5270{
5271 struct nfs4_exception exception = { };
5272 int err;
5273
5274 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5275 return -EOPNOTSUPP;
5276
5277 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005278 err = _nfs4_get_security_label(inode, buf, buflen);
5279 trace_nfs4_get_security_label(inode, err);
5280 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005281 &exception);
5282 } while (exception.retry);
5283 return err;
5284}
5285
5286static int _nfs4_do_set_security_label(struct inode *inode,
5287 struct nfs4_label *ilabel,
5288 struct nfs_fattr *fattr,
5289 struct nfs4_label *olabel)
5290{
5291
5292 struct iattr sattr = {0};
5293 struct nfs_server *server = NFS_SERVER(inode);
5294 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005295 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005296 .fh = NFS_FH(inode),
5297 .iap = &sattr,
5298 .server = server,
5299 .bitmask = bitmask,
5300 .label = ilabel,
5301 };
5302 struct nfs_setattrres res = {
5303 .fattr = fattr,
5304 .label = olabel,
5305 .server = server,
5306 };
5307 struct rpc_message msg = {
5308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005309 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005310 .rpc_resp = &res,
5311 };
5312 int status;
5313
Jeff Layton12207f62013-11-01 10:49:32 -04005314 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005315
Jeff Layton12207f62013-11-01 10:49:32 -04005316 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005317 if (status)
5318 dprintk("%s failed: %d\n", __func__, status);
5319
5320 return status;
5321}
5322
5323static int nfs4_do_set_security_label(struct inode *inode,
5324 struct nfs4_label *ilabel,
5325 struct nfs_fattr *fattr,
5326 struct nfs4_label *olabel)
5327{
5328 struct nfs4_exception exception = { };
5329 int err;
5330
5331 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005332 err = _nfs4_do_set_security_label(inode, ilabel,
5333 fattr, olabel);
5334 trace_nfs4_set_security_label(inode, err);
5335 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005336 &exception);
5337 } while (exception.retry);
5338 return err;
5339}
5340
5341static int
Al Viro59301222016-05-27 10:19:30 -04005342nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005343{
5344 struct nfs4_label ilabel, *olabel = NULL;
5345 struct nfs_fattr fattr;
5346 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005347 int status;
5348
5349 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5350 return -EOPNOTSUPP;
5351
5352 nfs_fattr_init(&fattr);
5353
5354 ilabel.pi = 0;
5355 ilabel.lfs = 0;
5356 ilabel.label = (char *)buf;
5357 ilabel.len = buflen;
5358
5359 cred = rpc_lookup_cred();
5360 if (IS_ERR(cred))
5361 return PTR_ERR(cred);
5362
5363 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5364 if (IS_ERR(olabel)) {
5365 status = -PTR_ERR(olabel);
5366 goto out;
5367 }
5368
5369 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5370 if (status == 0)
5371 nfs_setsecurity(inode, &fattr, olabel);
5372
5373 nfs4_label_free(olabel);
5374out:
5375 put_rpccred(cred);
5376 return status;
5377}
5378#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5379
5380
Chuck Leverf0920752012-05-21 22:45:41 -04005381static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5382 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005383{
5384 __be32 verf[2];
5385
Chuck Lever2c820d92012-05-21 22:45:33 -04005386 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5387 /* An impossible timestamp guarantees this value
5388 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005389 verf[0] = cpu_to_be32(U32_MAX);
5390 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005391 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005392 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005393 u64 ns = ktime_to_ns(nn->boot_time);
5394
5395 verf[0] = cpu_to_be32(ns >> 32);
5396 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005397 }
Chuck Levercd937102012-03-02 17:14:31 -05005398 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5399}
5400
Jeff Laytona3192682015-06-09 19:43:59 -04005401static int
5402nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005403{
Jeff Laytona3192682015-06-09 19:43:59 -04005404 size_t len;
5405 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005406
Trond Myklebustceb3a162015-01-03 15:16:04 -05005407 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005408 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005409
Jeff Laytona3192682015-06-09 19:43:59 -04005410 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005411 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005412 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5413 1 +
5414 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5415 1;
5416 rcu_read_unlock();
5417
5418 if (len > NFS4_OPAQUE_LIMIT + 1)
5419 return -EINVAL;
5420
5421 /*
5422 * Since this string is allocated at mount time, and held until the
5423 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5424 * about a memory-reclaim deadlock.
5425 */
5426 str = kmalloc(len, GFP_KERNEL);
5427 if (!str)
5428 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005429
Chuck Levere984a552012-09-14 17:24:21 -04005430 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005431 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005432 clp->cl_ipaddr,
5433 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5434 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005435 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005436
Jeff Laytona3192682015-06-09 19:43:59 -04005437 clp->cl_owner_id = str;
5438 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005439}
5440
Jeff Layton873e3852015-06-09 19:44:00 -04005441static int
5442nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005443{
Jeff Layton873e3852015-06-09 19:44:00 -04005444 size_t len;
5445 char *str;
5446
5447 len = 10 + 10 + 1 + 10 + 1 +
5448 strlen(nfs4_client_id_uniquifier) + 1 +
5449 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5450
5451 if (len > NFS4_OPAQUE_LIMIT + 1)
5452 return -EINVAL;
5453
5454 /*
5455 * Since this string is allocated at mount time, and held until the
5456 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5457 * about a memory-reclaim deadlock.
5458 */
5459 str = kmalloc(len, GFP_KERNEL);
5460 if (!str)
5461 return -ENOMEM;
5462
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005463 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005464 clp->rpc_ops->version, clp->cl_minorversion,
5465 nfs4_client_id_uniquifier,
5466 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005467 clp->cl_owner_id = str;
5468 return 0;
5469}
5470
5471static int
5472nfs4_init_uniform_client_string(struct nfs_client *clp)
5473{
Jeff Layton873e3852015-06-09 19:44:00 -04005474 size_t len;
5475 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005476
5477 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005478 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005479
5480 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005481 return nfs4_init_uniquifier_client_string(clp);
5482
5483 len = 10 + 10 + 1 + 10 + 1 +
5484 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5485
5486 if (len > NFS4_OPAQUE_LIMIT + 1)
5487 return -EINVAL;
5488
5489 /*
5490 * Since this string is allocated at mount time, and held until the
5491 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5492 * about a memory-reclaim deadlock.
5493 */
5494 str = kmalloc(len, GFP_KERNEL);
5495 if (!str)
5496 return -ENOMEM;
5497
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005498 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005499 clp->rpc_ops->version, clp->cl_minorversion,
5500 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005501 clp->cl_owner_id = str;
5502 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005503}
5504
Chuck Lever706cb8d2014-03-12 12:51:47 -04005505/*
5506 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5507 * services. Advertise one based on the address family of the
5508 * clientaddr.
5509 */
5510static unsigned int
5511nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5512{
5513 if (strchr(clp->cl_ipaddr, ':') != NULL)
5514 return scnprintf(buf, len, "tcp6");
5515 else
5516 return scnprintf(buf, len, "tcp");
5517}
5518
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005519static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5520{
5521 struct nfs4_setclientid *sc = calldata;
5522
5523 if (task->tk_status == 0)
5524 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5525}
5526
5527static const struct rpc_call_ops nfs4_setclientid_ops = {
5528 .rpc_call_done = nfs4_setclientid_done,
5529};
5530
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005531/**
5532 * nfs4_proc_setclientid - Negotiate client ID
5533 * @clp: state data structure
5534 * @program: RPC program for NFSv4 callback service
5535 * @port: IP port number for NFS4 callback service
5536 * @cred: RPC credential to use for this call
5537 * @res: where to place the result
5538 *
5539 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5540 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005541int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5542 unsigned short port, struct rpc_cred *cred,
5543 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544{
5545 nfs4_verifier sc_verifier;
5546 struct nfs4_setclientid setclientid = {
5547 .sc_verifier = &sc_verifier,
5548 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005549 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 };
5551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5553 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005554 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005555 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005557 struct rpc_task *task;
5558 struct rpc_task_setup task_setup_data = {
5559 .rpc_client = clp->cl_rpcclient,
5560 .rpc_message = &msg,
5561 .callback_ops = &nfs4_setclientid_ops,
5562 .callback_data = &setclientid,
5563 .flags = RPC_TASK_TIMEOUT,
5564 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005565 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Chuck Leverde734832012-07-11 16:30:50 -04005567 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005568 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005569
5570 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5571 status = nfs4_init_uniform_client_string(clp);
5572 else
Jeff Laytona3192682015-06-09 19:43:59 -04005573 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005574
5575 if (status)
5576 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005577
Chuck Leverde734832012-07-11 16:30:50 -04005578 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005579 setclientid.sc_netid_len =
5580 nfs4_init_callback_netid(clp,
5581 setclientid.sc_netid,
5582 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005583 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005584 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 clp->cl_ipaddr, port >> 8, port & 255);
5586
Jeff Layton3a6bb732015-06-09 19:43:57 -04005587 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005588 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005589 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005590 task = rpc_run_task(&task_setup_data);
5591 if (IS_ERR(task)) {
5592 status = PTR_ERR(task);
5593 goto out;
5594 }
5595 status = task->tk_status;
5596 if (setclientid.sc_cred) {
5597 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5598 put_rpccred(setclientid.sc_cred);
5599 }
5600 rpc_put_task(task);
5601out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005602 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005603 dprintk("NFS reply setclientid: %d\n", status);
5604 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605}
5606
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005607/**
5608 * nfs4_proc_setclientid_confirm - Confirm client ID
5609 * @clp: state data structure
5610 * @res: result of a previous SETCLIENTID
5611 * @cred: RPC credential to use for this call
5612 *
5613 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5614 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005615int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005616 struct nfs4_setclientid_res *arg,
5617 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 struct rpc_message msg = {
5620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005621 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005622 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 int status;
5625
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005626 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5627 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5628 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005629 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005630 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005631 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 return status;
5633}
5634
Trond Myklebustfe650402006-01-03 09:55:18 +01005635struct nfs4_delegreturndata {
5636 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005637 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005638 struct nfs_fh fh;
5639 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005640 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005641 struct {
5642 struct nfs4_layoutreturn_args arg;
5643 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005644 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005645 u32 roc_barrier;
5646 bool roc;
5647 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005648 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005649 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005650 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005651};
5652
Trond Myklebustfe650402006-01-03 09:55:18 +01005653static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5654{
5655 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005656
Trond Myklebust14516c32010-07-31 14:29:06 -04005657 if (!nfs4_sequence_done(task, &data->res.seq_res))
5658 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005659
Trond Myklebustca8acf82013-08-13 10:36:56 -04005660 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005661
5662 /* Handle Layoutreturn errors */
5663 if (data->args.lr_args && task->tk_status != 0) {
5664 switch(data->res.lr_ret) {
5665 default:
5666 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5667 break;
5668 case 0:
5669 data->args.lr_args = NULL;
5670 data->res.lr_res = NULL;
5671 break;
5672 case -NFS4ERR_ADMIN_REVOKED:
5673 case -NFS4ERR_DELEG_REVOKED:
5674 case -NFS4ERR_EXPIRED:
5675 case -NFS4ERR_BAD_STATEID:
5676 case -NFS4ERR_OLD_STATEID:
5677 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5678 case -NFS4ERR_WRONG_CRED:
5679 data->args.lr_args = NULL;
5680 data->res.lr_res = NULL;
5681 data->res.lr_ret = 0;
5682 rpc_restart_call_prepare(task);
5683 return;
5684 }
5685 }
5686
Ricardo Labiaga79708862009-12-07 09:23:21 -05005687 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005688 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005689 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005690 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005691 case -NFS4ERR_ADMIN_REVOKED:
5692 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005693 case -NFS4ERR_EXPIRED:
5694 nfs4_free_revoked_stateid(data->res.server,
5695 data->args.stateid,
5696 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005697 case -NFS4ERR_BAD_STATEID:
5698 case -NFS4ERR_OLD_STATEID:
5699 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005700 task->tk_status = 0;
5701 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005702 case -NFS4ERR_ACCESS:
5703 if (data->args.bitmask) {
5704 data->args.bitmask = NULL;
5705 data->res.fattr = NULL;
5706 task->tk_status = 0;
5707 rpc_restart_call_prepare(task);
5708 return;
5709 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005710 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005711 if (nfs4_async_handle_error(task, data->res.server,
5712 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005713 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005714 return;
5715 }
5716 }
5717 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005718}
5719
5720static void nfs4_delegreturn_release(void *calldata)
5721{
Peng Tao039b7562014-07-03 13:05:02 +08005722 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005723 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005724
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005725 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005726 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005727 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5728 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005729 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005730 nfs_iput_and_deactive(inode);
5731 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005732 kfree(calldata);
5733}
5734
Andy Adamson938e1012009-04-01 09:22:28 -04005735static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5736{
5737 struct nfs4_delegreturndata *d_data;
5738
5739 d_data = (struct nfs4_delegreturndata *)data;
5740
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005741 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005742 return;
5743
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005744 nfs4_setup_sequence(d_data->res.server,
5745 &d_data->args.seq_args,
5746 &d_data->res.seq_res,
5747 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005748}
Andy Adamson938e1012009-04-01 09:22:28 -04005749
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005750static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005751 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005752 .rpc_call_done = nfs4_delegreturn_done,
5753 .rpc_release = nfs4_delegreturn_release,
5754};
5755
Trond Myklebuste6f81072008-01-24 18:14:34 -05005756static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005757{
5758 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005759 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005760 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005761 struct rpc_message msg = {
5762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5763 .rpc_cred = cred,
5764 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005765 struct rpc_task_setup task_setup_data = {
5766 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005767 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005768 .callback_ops = &nfs4_delegreturn_ops,
5769 .flags = RPC_TASK_ASYNC,
5770 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005771 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005772
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005773 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005774 if (data == NULL)
5775 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005776 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005777
5778 nfs4_state_protect(server->nfs_client,
5779 NFS_SP4_MACH_CRED_CLEANUP,
5780 &task_setup_data.rpc_client, &msg);
5781
Trond Myklebustfe650402006-01-03 09:55:18 +01005782 data->args.fhandle = &data->fh;
5783 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005784 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005785 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005786 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005787 data->res.fattr = &data->fattr;
5788 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005789 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005790 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005791 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005792 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005793 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005794 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005795 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005796 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005797 if (data->lr.roc) {
5798 data->args.lr_args = &data->lr.arg;
5799 data->res.lr_res = &data->lr.res;
5800 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005801 } else if (data->lr.roc) {
5802 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5803 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005804 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005805
Trond Myklebustc970aa82007-07-14 15:39:59 -04005806 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005807 msg.rpc_argp = &data->args;
5808 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005809 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005810 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005811 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005812 if (!issync)
5813 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005814 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005815 if (status != 0)
5816 goto out;
5817 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005818out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005819 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005820 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821}
5822
Trond Myklebuste6f81072008-01-24 18:14:34 -05005823int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
5825 struct nfs_server *server = NFS_SERVER(inode);
5826 struct nfs4_exception exception = { };
5827 int err;
5828 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005829 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005830 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 switch (err) {
5832 case -NFS4ERR_STALE_STATEID:
5833 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 case 0:
5835 return 0;
5836 }
5837 err = nfs4_handle_exception(server, err, &exception);
5838 } while (exception.retry);
5839 return err;
5840}
5841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5843{
5844 struct inode *inode = state->inode;
5845 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005846 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005847 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005849 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005851 struct nfs_lockt_res res = {
5852 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 };
5854 struct rpc_message msg = {
5855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5856 .rpc_argp = &arg,
5857 .rpc_resp = &res,
5858 .rpc_cred = state->owner->so_cred,
5859 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 struct nfs4_lock_state *lsp;
5861 int status;
5862
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005863 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005864 status = nfs4_set_lock_state(state, request);
5865 if (status != 0)
5866 goto out;
5867 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005868 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005869 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005870 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005871 switch (status) {
5872 case 0:
5873 request->fl_type = F_UNLCK;
5874 break;
5875 case -NFS4ERR_DENIED:
5876 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005878 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005879 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005880out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 return status;
5882}
5883
5884static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5885{
5886 struct nfs4_exception exception = { };
5887 int err;
5888
5889 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005890 err = _nfs4_proc_getlk(state, cmd, request);
5891 trace_nfs4_get_lock(request, state, cmd, err);
5892 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 &exception);
5894 } while (exception.retry);
5895 return err;
5896}
5897
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005898struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005899 struct nfs_locku_args arg;
5900 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005901 struct nfs4_lock_state *lsp;
5902 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005903 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005904 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005905 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005906};
5907
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005908static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5909 struct nfs_open_context *ctx,
5910 struct nfs4_lock_state *lsp,
5911 struct nfs_seqid *seqid)
5912{
5913 struct nfs4_unlockdata *p;
5914 struct inode *inode = lsp->ls_state->inode;
5915
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005916 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005917 if (p == NULL)
5918 return NULL;
5919 p->arg.fh = NFS_FH(inode);
5920 p->arg.fl = &p->fl;
5921 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005922 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005923 p->lsp = lsp;
5924 atomic_inc(&lsp->ls_count);
5925 /* Ensure we don't close file until we're done freeing locks! */
5926 p->ctx = get_nfs_open_context(ctx);
5927 memcpy(&p->fl, fl, sizeof(p->fl));
5928 p->server = NFS_SERVER(inode);
5929 return p;
5930}
5931
Trond Myklebust06f814a2006-01-03 09:55:07 +01005932static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005933{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005934 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005935 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005936 nfs4_put_lock_state(calldata->lsp);
5937 put_nfs_open_context(calldata->ctx);
5938 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939}
5940
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005941static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005942{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005943 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005944
Trond Myklebust14516c32010-07-31 14:29:06 -04005945 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5946 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005947 switch (task->tk_status) {
5948 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005949 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005950 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005951 if (nfs4_update_lock_stateid(calldata->lsp,
5952 &calldata->res.stateid))
5953 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005954 case -NFS4ERR_ADMIN_REVOKED:
5955 case -NFS4ERR_EXPIRED:
5956 nfs4_free_revoked_stateid(calldata->server,
5957 &calldata->arg.stateid,
5958 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005959 case -NFS4ERR_BAD_STATEID:
5960 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005961 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005962 if (!nfs4_stateid_match(&calldata->arg.stateid,
5963 &calldata->lsp->ls_stateid))
5964 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005965 break;
5966 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005967 if (nfs4_async_handle_error(task, calldata->server,
5968 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005969 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005970 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005971 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005972}
5973
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005974static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005975{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005976 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005978 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005979 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005980 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005981 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005982 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005983 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005984 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005985 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005986 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005987 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005988 &calldata->res.seq_res,
5989 task) != 0)
5990 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005991 return;
5992out_no_action:
5993 task->tk_action = NULL;
5994out_wait:
5995 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996}
5997
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005998static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005999 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006000 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006001 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006002};
6003
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6005 struct nfs_open_context *ctx,
6006 struct nfs4_lock_state *lsp,
6007 struct nfs_seqid *seqid)
6008{
6009 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006010 struct rpc_message msg = {
6011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6012 .rpc_cred = ctx->cred,
6013 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006014 struct rpc_task_setup task_setup_data = {
6015 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006016 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006017 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006018 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006019 .flags = RPC_TASK_ASYNC,
6020 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006021
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006022 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6023 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6024
Frank Filz137d6ac2007-07-09 15:32:29 -07006025 /* Ensure this is an unlock - when canceling a lock, the
6026 * canceled lock is passed in, and it won't be an unlock.
6027 */
6028 fl->fl_type = F_UNLCK;
6029
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6031 if (data == NULL) {
6032 nfs_free_seqid(seqid);
6033 return ERR_PTR(-ENOMEM);
6034 }
6035
Chuck Levera9c92d62013-08-09 12:48:18 -04006036 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006037 msg.rpc_argp = &data->arg;
6038 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006039 task_setup_data.callback_data = data;
6040 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006041}
6042
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6044{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006045 struct inode *inode = state->inode;
6046 struct nfs4_state_owner *sp = state->owner;
6047 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006048 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006049 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006050 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006051 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006052 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006053 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Trond Myklebust9b073572006-06-29 16:38:34 -04006055 status = nfs4_set_lock_state(state, request);
6056 /* Unlock _before_ we do the RPC call */
6057 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006058 /* Exclude nfs_delegation_claim_locks() */
6059 mutex_lock(&sp->so_delegreturn_mutex);
6060 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006061 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006062 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006063 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006064 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006065 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006066 }
6067 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006068 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006069 if (status != 0)
6070 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006071 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006072 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006073 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6074 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006075 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6076 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006077 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006078 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006079 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006080 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006081 status = PTR_ERR(task);
6082 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006083 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006084 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006085 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006086out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006087 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006088 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006089 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090}
6091
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006092struct nfs4_lockdata {
6093 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006094 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006095 struct nfs4_lock_state *lsp;
6096 struct nfs_open_context *ctx;
6097 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006098 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099 int rpc_status;
6100 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006101 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006102};
6103
6104static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006105 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6106 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006107{
6108 struct nfs4_lockdata *p;
6109 struct inode *inode = lsp->ls_state->inode;
6110 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006111 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006112
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006113 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006114 if (p == NULL)
6115 return NULL;
6116
6117 p->arg.fh = NFS_FH(inode);
6118 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006119 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006120 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006121 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006122 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6123 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006124 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006125 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006126 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006127 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006128 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006129 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006130 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006131 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006132 atomic_inc(&lsp->ls_count);
6133 p->ctx = get_nfs_open_context(ctx);
6134 memcpy(&p->fl, fl, sizeof(p->fl));
6135 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006136out_free_seqid:
6137 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138out_free:
6139 kfree(p);
6140 return NULL;
6141}
6142
6143static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6144{
6145 struct nfs4_lockdata *data = calldata;
6146 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
Harvey Harrison3110ff82008-05-02 13:42:44 -07006148 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006149 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006150 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006152 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006153 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006154 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006155 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006156 nfs4_stateid_copy(&data->arg.open_stateid,
6157 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006158 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006159 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006160 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006161 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006162 nfs4_stateid_copy(&data->arg.lock_stateid,
6163 &data->lsp->ls_stateid);
6164 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006165 if (!nfs4_valid_open_stateid(state)) {
6166 data->rpc_status = -EBADF;
6167 task->tk_action = NULL;
6168 goto out_release_open_seqid;
6169 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006170 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006171 if (nfs4_setup_sequence(data->server,
6172 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006173 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006174 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006175 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006176out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006177 nfs_release_seqid(data->arg.open_seqid);
6178out_release_lock_seqid:
6179 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006180out_wait:
6181 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006182 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006183}
6184
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6186{
6187 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006188 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006189
Harvey Harrison3110ff82008-05-02 13:42:44 -07006190 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006191
Trond Myklebust14516c32010-07-31 14:29:06 -04006192 if (!nfs4_sequence_done(task, &data->res.seq_res))
6193 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006194
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006195 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006196 switch (task->tk_status) {
6197 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006198 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006199 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006200 if (data->arg.new_lock) {
6201 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006202 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006203 rpc_restart_call_prepare(task);
6204 break;
6205 }
6206 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006207 if (data->arg.new_lock_owner != 0) {
6208 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6209 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6210 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6211 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6212 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006213 break;
6214 case -NFS4ERR_BAD_STATEID:
6215 case -NFS4ERR_OLD_STATEID:
6216 case -NFS4ERR_STALE_STATEID:
6217 case -NFS4ERR_EXPIRED:
6218 if (data->arg.new_lock_owner != 0) {
6219 if (!nfs4_stateid_match(&data->arg.open_stateid,
6220 &lsp->ls_state->open_stateid))
6221 rpc_restart_call_prepare(task);
6222 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6223 &lsp->ls_stateid))
6224 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006225 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006226 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006227}
6228
6229static void nfs4_lock_release(void *calldata)
6230{
6231 struct nfs4_lockdata *data = calldata;
6232
Harvey Harrison3110ff82008-05-02 13:42:44 -07006233 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006234 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006235 if (data->cancelled != 0) {
6236 struct rpc_task *task;
6237 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6238 data->arg.lock_seqid);
6239 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006240 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006241 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006242 } else
6243 nfs_free_seqid(data->arg.lock_seqid);
6244 nfs4_put_lock_state(data->lsp);
6245 put_nfs_open_context(data->ctx);
6246 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006247 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006248}
6249
6250static const struct rpc_call_ops nfs4_lock_ops = {
6251 .rpc_call_prepare = nfs4_lock_prepare,
6252 .rpc_call_done = nfs4_lock_done,
6253 .rpc_release = nfs4_lock_release,
6254};
6255
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006256static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6257{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006258 switch (error) {
6259 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006260 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006261 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006262 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006263 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006264 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006265 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006266 break;
6267 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006268 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006269 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006270 };
6271}
6272
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006273static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274{
6275 struct nfs4_lockdata *data;
6276 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006277 struct rpc_message msg = {
6278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6279 .rpc_cred = state->owner->so_cred,
6280 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006281 struct rpc_task_setup task_setup_data = {
6282 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006283 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006284 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006285 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006286 .flags = RPC_TASK_ASYNC,
6287 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006288 int ret;
6289
Harvey Harrison3110ff82008-05-02 13:42:44 -07006290 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006291 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006292 fl->fl_u.nfs4_fl.owner,
6293 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006294 if (data == NULL)
6295 return -ENOMEM;
6296 if (IS_SETLKW(cmd))
6297 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006298 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006299 msg.rpc_argp = &data->arg;
6300 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006301 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006302 if (recovery_type > NFS_LOCK_NEW) {
6303 if (recovery_type == NFS_LOCK_RECLAIM)
6304 data->arg.reclaim = NFS_LOCK_RECLAIM;
6305 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006306 } else
6307 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006308 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006309 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006310 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006311 ret = nfs4_wait_for_completion_rpc_task(task);
6312 if (ret == 0) {
6313 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006314 if (ret)
6315 nfs4_handle_setlk_error(data->server, data->lsp,
6316 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006317 } else
6318 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006319 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006320 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006321 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323}
6324
6325static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6326{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006327 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006328 struct nfs4_exception exception = {
6329 .inode = state->inode,
6330 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006331 int err;
6332
6333 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006334 /* Cache the lock if possible... */
6335 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6336 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006337 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006338 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006339 break;
6340 nfs4_handle_exception(server, err, &exception);
6341 } while (exception.retry);
6342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343}
6344
6345static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6346{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006347 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006348 struct nfs4_exception exception = {
6349 .inode = state->inode,
6350 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006351 int err;
6352
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006353 err = nfs4_set_lock_state(state, request);
6354 if (err != 0)
6355 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006356 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006357 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6358 return 0;
6359 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006360 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006361 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6362 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006363 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006364 switch (err) {
6365 default:
6366 goto out;
6367 case -NFS4ERR_GRACE:
6368 case -NFS4ERR_DELAY:
6369 nfs4_handle_exception(server, err, &exception);
6370 err = 0;
6371 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006372 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006373out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375}
6376
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006377#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006378static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6379{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006380 struct nfs4_lock_state *lsp;
6381 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006382
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006383 status = nfs4_set_lock_state(state, request);
6384 if (status != 0)
6385 return status;
6386 lsp = request->fl_u.nfs4_fl.owner;
6387 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6388 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6389 return 0;
6390 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006391 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006392}
6393#endif
6394
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6396{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006397 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006398 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006399 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006400 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
Trond Myklebust01c3b862006-06-29 16:38:39 -04006402 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006403 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006404 if (status < 0)
6405 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006406 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006407 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006408 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006409 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006410 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006411 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006412 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006413 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006414 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006415 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006416 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006417 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006418 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006419 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006420out:
6421 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 return status;
6423}
6424
6425static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6426{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006427 struct nfs4_exception exception = {
6428 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006429 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006430 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 int err;
6432
6433 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006434 err = _nfs4_proc_setlk(state, cmd, request);
6435 if (err == -NFS4ERR_DENIED)
6436 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006438 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 } while (exception.retry);
6440 return err;
6441}
6442
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006443#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6444#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6445
6446static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006447nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6448 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006449{
6450 int status = -ERESTARTSYS;
6451 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6452
6453 while(!signalled()) {
6454 status = nfs4_proc_setlk(state, cmd, request);
6455 if ((status != -EAGAIN) || IS_SETLK(cmd))
6456 break;
6457 freezable_schedule_timeout_interruptible(timeout);
6458 timeout *= 2;
6459 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6460 status = -ERESTARTSYS;
6461 }
6462 return status;
6463}
6464
Jeff Laytona1d617d82016-09-17 18:17:39 -04006465#ifdef CONFIG_NFS_V4_1
6466struct nfs4_lock_waiter {
6467 struct task_struct *task;
6468 struct inode *inode;
6469 struct nfs_lowner *owner;
6470 bool notified;
6471};
6472
6473static int
6474nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6475{
6476 int ret;
6477 struct cb_notify_lock_args *cbnl = key;
6478 struct nfs4_lock_waiter *waiter = wait->private;
6479 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6480 *wowner = waiter->owner;
6481
6482 /* Only wake if the callback was for the same owner */
6483 if (lowner->clientid != wowner->clientid ||
6484 lowner->id != wowner->id ||
6485 lowner->s_dev != wowner->s_dev)
6486 return 0;
6487
6488 /* Make sure it's for the right inode */
6489 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6490 return 0;
6491
6492 waiter->notified = true;
6493
6494 /* override "private" so we can use default_wake_function */
6495 wait->private = waiter->task;
6496 ret = autoremove_wake_function(wait, mode, flags, key);
6497 wait->private = waiter;
6498 return ret;
6499}
6500
6501static int
6502nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6503{
6504 int status = -ERESTARTSYS;
6505 unsigned long flags;
6506 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6507 struct nfs_server *server = NFS_SERVER(state->inode);
6508 struct nfs_client *clp = server->nfs_client;
6509 wait_queue_head_t *q = &clp->cl_lock_waitq;
6510 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6511 .id = lsp->ls_seqid.owner_id,
6512 .s_dev = server->s_dev };
6513 struct nfs4_lock_waiter waiter = { .task = current,
6514 .inode = state->inode,
6515 .owner = &owner,
6516 .notified = false };
6517 wait_queue_t wait;
6518
6519 /* Don't bother with waitqueue if we don't expect a callback */
6520 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6521 return nfs4_retry_setlk_simple(state, cmd, request);
6522
6523 init_wait(&wait);
6524 wait.private = &waiter;
6525 wait.func = nfs4_wake_lock_waiter;
6526 add_wait_queue(q, &wait);
6527
6528 while(!signalled()) {
6529 status = nfs4_proc_setlk(state, cmd, request);
6530 if ((status != -EAGAIN) || IS_SETLK(cmd))
6531 break;
6532
6533 status = -ERESTARTSYS;
6534 spin_lock_irqsave(&q->lock, flags);
6535 if (waiter.notified) {
6536 spin_unlock_irqrestore(&q->lock, flags);
6537 continue;
6538 }
6539 set_current_state(TASK_INTERRUPTIBLE);
6540 spin_unlock_irqrestore(&q->lock, flags);
6541
6542 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6543 }
6544
6545 finish_wait(q, &wait);
6546 return status;
6547}
6548#else /* !CONFIG_NFS_V4_1 */
6549static inline int
6550nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6551{
6552 return nfs4_retry_setlk_simple(state, cmd, request);
6553}
6554#endif
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556static int
6557nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6558{
6559 struct nfs_open_context *ctx;
6560 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 int status;
6562
6563 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006564 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 state = ctx->state;
6566
6567 if (request->fl_start < 0 || request->fl_end < 0)
6568 return -EINVAL;
6569
Trond Myklebustd9531262009-07-21 19:22:38 -04006570 if (IS_GETLK(cmd)) {
6571 if (state != NULL)
6572 return nfs4_proc_getlk(state, F_GETLK, request);
6573 return 0;
6574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
6576 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6577 return -EINVAL;
6578
Trond Myklebustd9531262009-07-21 19:22:38 -04006579 if (request->fl_type == F_UNLCK) {
6580 if (state != NULL)
6581 return nfs4_proc_unlck(state, cmd, request);
6582 return 0;
6583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
Trond Myklebustd9531262009-07-21 19:22:38 -04006585 if (state == NULL)
6586 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006587
6588 if ((request->fl_flags & FL_POSIX) &&
6589 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6590 return -ENOLCK;
6591
Trond Myklebust55725512012-04-18 12:48:35 -04006592 /*
6593 * Don't rely on the VFS having checked the file open mode,
6594 * since it won't do this for flock() locks.
6595 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006596 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006597 case F_RDLCK:
6598 if (!(filp->f_mode & FMODE_READ))
6599 return -EBADF;
6600 break;
6601 case F_WRLCK:
6602 if (!(filp->f_mode & FMODE_WRITE))
6603 return -EBADF;
6604 }
6605
Jeff Layton1ea67db2016-09-17 18:17:37 -04006606 status = nfs4_set_lock_state(state, request);
6607 if (status != 0)
6608 return status;
6609
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006610 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611}
6612
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006613int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006614{
6615 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006616 int err;
6617
6618 err = nfs4_set_lock_state(state, fl);
6619 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006620 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006621 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006622 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006623}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006624
Trond Myklebustcf470c32012-03-07 13:49:12 -05006625struct nfs_release_lockowner_data {
6626 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006627 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006628 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006629 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006630 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006631};
6632
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006633static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6634{
6635 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006636 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006637 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6638 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006639 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006640 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006641}
6642
6643static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6644{
6645 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006646 struct nfs_server *server = data->server;
6647
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006648 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006649
6650 switch (task->tk_status) {
6651 case 0:
6652 renew_lease(server, data->timestamp);
6653 break;
6654 case -NFS4ERR_STALE_CLIENTID:
6655 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006656 nfs4_schedule_lease_recovery(server->nfs_client);
6657 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006658 case -NFS4ERR_LEASE_MOVED:
6659 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006660 if (nfs4_async_handle_error(task, server,
6661 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006662 rpc_restart_call_prepare(task);
6663 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006664}
6665
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006666static void nfs4_release_lockowner_release(void *calldata)
6667{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006668 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006669 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006670 kfree(calldata);
6671}
6672
Trond Myklebust17280172012-03-11 13:11:00 -04006673static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006674 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6675 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006676 .rpc_release = nfs4_release_lockowner_release,
6677};
6678
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006679static void
6680nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006681{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006682 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006683 struct rpc_message msg = {
6684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6685 };
6686
6687 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006688 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006689
Trond Myklebustcf470c32012-03-07 13:49:12 -05006690 data = kmalloc(sizeof(*data), GFP_NOFS);
6691 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006692 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006693 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006694 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006695 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6696 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6697 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006698
Trond Myklebustcf470c32012-03-07 13:49:12 -05006699 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006700 msg.rpc_resp = &data->res;
6701 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006702 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006703}
6704
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006705#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6706
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006707static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006708 struct dentry *unused, struct inode *inode,
6709 const char *key, const void *buf,
6710 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006711{
Al Viro59301222016-05-27 10:19:30 -04006712 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006713}
6714
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006715static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006716 struct dentry *unused, struct inode *inode,
6717 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006718{
Al Virob2968212016-04-10 20:48:24 -04006719 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006720}
6721
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006722static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006723{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006724 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006725}
6726
David Quigleyc9bccef2013-05-22 12:50:45 -04006727#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006728
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006729static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006730 struct dentry *unused, struct inode *inode,
6731 const char *key, const void *buf,
6732 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006733{
6734 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006735 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006736
6737 return -EOPNOTSUPP;
6738}
6739
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006740static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006741 struct dentry *unused, struct inode *inode,
6742 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006743{
6744 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006745 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006746 return -EOPNOTSUPP;
6747}
6748
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006749static ssize_t
6750nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006751{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006752 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006753
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006754 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6755 len = security_inode_listsecurity(inode, list, list_len);
6756 if (list_len && len > list_len)
6757 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006758 }
6759 return len;
6760}
6761
6762static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6763 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006764 .get = nfs4_xattr_get_nfs4_label,
6765 .set = nfs4_xattr_set_nfs4_label,
6766};
David Quigleyc9bccef2013-05-22 12:50:45 -04006767
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006768#else
6769
6770static ssize_t
6771nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6772{
6773 return 0;
6774}
6775
6776#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006777
Andy Adamson533eb462011-06-13 18:25:56 -04006778/*
6779 * nfs_fhget will use either the mounted_on_fileid or the fileid
6780 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006781static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6782{
Andy Adamson533eb462011-06-13 18:25:56 -04006783 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6784 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6785 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006786 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006787 return;
6788
6789 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006790 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006791 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6792 fattr->nlink = 2;
6793}
6794
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006795static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6796 const struct qstr *name,
6797 struct nfs4_fs_locations *fs_locations,
6798 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006799{
6800 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006801 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006802 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006803 };
6804 struct nfs4_fs_locations_arg args = {
6805 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006806 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006807 .page = page,
6808 .bitmask = bitmask,
6809 };
Benny Halevy22958462009-04-01 09:22:02 -04006810 struct nfs4_fs_locations_res res = {
6811 .fs_locations = fs_locations,
6812 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006813 struct rpc_message msg = {
6814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6815 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006816 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006817 };
6818 int status;
6819
Harvey Harrison3110ff82008-05-02 13:42:44 -07006820 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006821
6822 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6823 * is not supported */
6824 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6825 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6826 else
6827 bitmask[0] |= FATTR4_WORD0_FILEID;
6828
Trond Myklebustc228fd32007-01-13 02:28:11 -05006829 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006830 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006831 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006832 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006833 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006834 return status;
6835}
6836
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006837int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6838 const struct qstr *name,
6839 struct nfs4_fs_locations *fs_locations,
6840 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006841{
6842 struct nfs4_exception exception = { };
6843 int err;
6844 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006845 err = _nfs4_proc_fs_locations(client, dir, name,
6846 fs_locations, page);
6847 trace_nfs4_get_fs_locations(dir, name, err);
6848 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006849 &exception);
6850 } while (exception.retry);
6851 return err;
6852}
6853
Chuck Leverb03d7352013-10-17 14:12:50 -04006854/*
6855 * This operation also signals the server that this client is
6856 * performing migration recovery. The server can stop returning
6857 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6858 * appended to this compound to identify the client ID which is
6859 * performing recovery.
6860 */
6861static int _nfs40_proc_get_locations(struct inode *inode,
6862 struct nfs4_fs_locations *locations,
6863 struct page *page, struct rpc_cred *cred)
6864{
6865 struct nfs_server *server = NFS_SERVER(inode);
6866 struct rpc_clnt *clnt = server->client;
6867 u32 bitmask[2] = {
6868 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6869 };
6870 struct nfs4_fs_locations_arg args = {
6871 .clientid = server->nfs_client->cl_clientid,
6872 .fh = NFS_FH(inode),
6873 .page = page,
6874 .bitmask = bitmask,
6875 .migration = 1, /* skip LOOKUP */
6876 .renew = 1, /* append RENEW */
6877 };
6878 struct nfs4_fs_locations_res res = {
6879 .fs_locations = locations,
6880 .migration = 1,
6881 .renew = 1,
6882 };
6883 struct rpc_message msg = {
6884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6885 .rpc_argp = &args,
6886 .rpc_resp = &res,
6887 .rpc_cred = cred,
6888 };
6889 unsigned long now = jiffies;
6890 int status;
6891
6892 nfs_fattr_init(&locations->fattr);
6893 locations->server = server;
6894 locations->nlocations = 0;
6895
6896 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6897 nfs4_set_sequence_privileged(&args.seq_args);
6898 status = nfs4_call_sync_sequence(clnt, server, &msg,
6899 &args.seq_args, &res.seq_res);
6900 if (status)
6901 return status;
6902
6903 renew_lease(server, now);
6904 return 0;
6905}
6906
6907#ifdef CONFIG_NFS_V4_1
6908
6909/*
6910 * This operation also signals the server that this client is
6911 * performing migration recovery. The server can stop asserting
6912 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6913 * performing this operation is identified in the SEQUENCE
6914 * operation in this compound.
6915 *
6916 * When the client supports GETATTR(fs_locations_info), it can
6917 * be plumbed in here.
6918 */
6919static int _nfs41_proc_get_locations(struct inode *inode,
6920 struct nfs4_fs_locations *locations,
6921 struct page *page, struct rpc_cred *cred)
6922{
6923 struct nfs_server *server = NFS_SERVER(inode);
6924 struct rpc_clnt *clnt = server->client;
6925 u32 bitmask[2] = {
6926 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6927 };
6928 struct nfs4_fs_locations_arg args = {
6929 .fh = NFS_FH(inode),
6930 .page = page,
6931 .bitmask = bitmask,
6932 .migration = 1, /* skip LOOKUP */
6933 };
6934 struct nfs4_fs_locations_res res = {
6935 .fs_locations = locations,
6936 .migration = 1,
6937 };
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6940 .rpc_argp = &args,
6941 .rpc_resp = &res,
6942 .rpc_cred = cred,
6943 };
6944 int status;
6945
6946 nfs_fattr_init(&locations->fattr);
6947 locations->server = server;
6948 locations->nlocations = 0;
6949
6950 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6951 nfs4_set_sequence_privileged(&args.seq_args);
6952 status = nfs4_call_sync_sequence(clnt, server, &msg,
6953 &args.seq_args, &res.seq_res);
6954 if (status == NFS4_OK &&
6955 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6956 status = -NFS4ERR_LEASE_MOVED;
6957 return status;
6958}
6959
6960#endif /* CONFIG_NFS_V4_1 */
6961
6962/**
6963 * nfs4_proc_get_locations - discover locations for a migrated FSID
6964 * @inode: inode on FSID that is migrating
6965 * @locations: result of query
6966 * @page: buffer
6967 * @cred: credential to use for this operation
6968 *
6969 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6970 * operation failed, or a negative errno if a local error occurred.
6971 *
6972 * On success, "locations" is filled in, but if the server has
6973 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6974 * asserted.
6975 *
6976 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6977 * from this client that require migration recovery.
6978 */
6979int nfs4_proc_get_locations(struct inode *inode,
6980 struct nfs4_fs_locations *locations,
6981 struct page *page, struct rpc_cred *cred)
6982{
6983 struct nfs_server *server = NFS_SERVER(inode);
6984 struct nfs_client *clp = server->nfs_client;
6985 const struct nfs4_mig_recovery_ops *ops =
6986 clp->cl_mvops->mig_recovery_ops;
6987 struct nfs4_exception exception = { };
6988 int status;
6989
6990 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6991 (unsigned long long)server->fsid.major,
6992 (unsigned long long)server->fsid.minor,
6993 clp->cl_hostname);
6994 nfs_display_fhandle(NFS_FH(inode), __func__);
6995
6996 do {
6997 status = ops->get_locations(inode, locations, page, cred);
6998 if (status != -NFS4ERR_DELAY)
6999 break;
7000 nfs4_handle_exception(server, status, &exception);
7001 } while (exception.retry);
7002 return status;
7003}
7004
Chuck Lever44c99932013-10-17 14:13:30 -04007005/*
7006 * This operation also signals the server that this client is
7007 * performing "lease moved" recovery. The server can stop
7008 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7009 * is appended to this compound to identify the client ID which is
7010 * performing recovery.
7011 */
7012static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7013{
7014 struct nfs_server *server = NFS_SERVER(inode);
7015 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7016 struct rpc_clnt *clnt = server->client;
7017 struct nfs4_fsid_present_arg args = {
7018 .fh = NFS_FH(inode),
7019 .clientid = clp->cl_clientid,
7020 .renew = 1, /* append RENEW */
7021 };
7022 struct nfs4_fsid_present_res res = {
7023 .renew = 1,
7024 };
7025 struct rpc_message msg = {
7026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7027 .rpc_argp = &args,
7028 .rpc_resp = &res,
7029 .rpc_cred = cred,
7030 };
7031 unsigned long now = jiffies;
7032 int status;
7033
7034 res.fh = nfs_alloc_fhandle();
7035 if (res.fh == NULL)
7036 return -ENOMEM;
7037
7038 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7039 nfs4_set_sequence_privileged(&args.seq_args);
7040 status = nfs4_call_sync_sequence(clnt, server, &msg,
7041 &args.seq_args, &res.seq_res);
7042 nfs_free_fhandle(res.fh);
7043 if (status)
7044 return status;
7045
7046 do_renew_lease(clp, now);
7047 return 0;
7048}
7049
7050#ifdef CONFIG_NFS_V4_1
7051
7052/*
7053 * This operation also signals the server that this client is
7054 * performing "lease moved" recovery. The server can stop asserting
7055 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7056 * this operation is identified in the SEQUENCE operation in this
7057 * compound.
7058 */
7059static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7060{
7061 struct nfs_server *server = NFS_SERVER(inode);
7062 struct rpc_clnt *clnt = server->client;
7063 struct nfs4_fsid_present_arg args = {
7064 .fh = NFS_FH(inode),
7065 };
7066 struct nfs4_fsid_present_res res = {
7067 };
7068 struct rpc_message msg = {
7069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7070 .rpc_argp = &args,
7071 .rpc_resp = &res,
7072 .rpc_cred = cred,
7073 };
7074 int status;
7075
7076 res.fh = nfs_alloc_fhandle();
7077 if (res.fh == NULL)
7078 return -ENOMEM;
7079
7080 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7081 nfs4_set_sequence_privileged(&args.seq_args);
7082 status = nfs4_call_sync_sequence(clnt, server, &msg,
7083 &args.seq_args, &res.seq_res);
7084 nfs_free_fhandle(res.fh);
7085 if (status == NFS4_OK &&
7086 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7087 status = -NFS4ERR_LEASE_MOVED;
7088 return status;
7089}
7090
7091#endif /* CONFIG_NFS_V4_1 */
7092
7093/**
7094 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7095 * @inode: inode on FSID to check
7096 * @cred: credential to use for this operation
7097 *
7098 * Server indicates whether the FSID is present, moved, or not
7099 * recognized. This operation is necessary to clear a LEASE_MOVED
7100 * condition for this client ID.
7101 *
7102 * Returns NFS4_OK if the FSID is present on this server,
7103 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7104 * NFS4ERR code if some error occurred on the server, or a
7105 * negative errno if a local failure occurred.
7106 */
7107int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7108{
7109 struct nfs_server *server = NFS_SERVER(inode);
7110 struct nfs_client *clp = server->nfs_client;
7111 const struct nfs4_mig_recovery_ops *ops =
7112 clp->cl_mvops->mig_recovery_ops;
7113 struct nfs4_exception exception = { };
7114 int status;
7115
7116 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7117 (unsigned long long)server->fsid.major,
7118 (unsigned long long)server->fsid.minor,
7119 clp->cl_hostname);
7120 nfs_display_fhandle(NFS_FH(inode), __func__);
7121
7122 do {
7123 status = ops->fsid_present(inode, cred);
7124 if (status != -NFS4ERR_DELAY)
7125 break;
7126 nfs4_handle_exception(server, status, &exception);
7127 } while (exception.retry);
7128 return status;
7129}
7130
Andy Adamson5ec16a82013-08-08 10:57:55 -04007131/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007132 * If 'use_integrity' is true and the state managment nfs_client
7133 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7134 * and the machine credential as per RFC3530bis and RFC5661 Security
7135 * Considerations sections. Otherwise, just use the user cred with the
7136 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007137 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007138static 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 +00007139{
7140 int status;
7141 struct nfs4_secinfo_arg args = {
7142 .dir_fh = NFS_FH(dir),
7143 .name = name,
7144 };
7145 struct nfs4_secinfo_res res = {
7146 .flavors = flavors,
7147 };
7148 struct rpc_message msg = {
7149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7150 .rpc_argp = &args,
7151 .rpc_resp = &res,
7152 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007153 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007154 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007155
7156 if (use_integrity) {
7157 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007158 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7159 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007160 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007161
7162 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007163
7164 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7165 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7166
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007167 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7168 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007169 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007170
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007171 if (cred)
7172 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007173
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007174 return status;
7175}
7176
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007177int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7178 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007179{
7180 struct nfs4_exception exception = { };
7181 int err;
7182 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007183 err = -NFS4ERR_WRONGSEC;
7184
7185 /* try to use integrity protection with machine cred */
7186 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7187 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7188
7189 /*
7190 * if unable to use integrity protection, or SECINFO with
7191 * integrity protection returns NFS4ERR_WRONGSEC (which is
7192 * disallowed by spec, but exists in deployed servers) use
7193 * the current filesystem's rpc_client and the user cred.
7194 */
7195 if (err == -NFS4ERR_WRONGSEC)
7196 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7197
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007198 trace_nfs4_secinfo(dir, name, err);
7199 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007200 &exception);
7201 } while (exception.retry);
7202 return err;
7203}
7204
Andy Adamson557134a2009-04-01 09:21:53 -04007205#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007206/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007207 * Check the exchange flags returned by the server for invalid flags, having
7208 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7209 * DS flags set.
7210 */
7211static int nfs4_check_cl_exchange_flags(u32 flags)
7212{
7213 if (flags & ~EXCHGID4_FLAG_MASK_R)
7214 goto out_inval;
7215 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7216 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7217 goto out_inval;
7218 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7219 goto out_inval;
7220 return NFS_OK;
7221out_inval:
7222 return -NFS4ERR_INVAL;
7223}
7224
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007225static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007226nfs41_same_server_scope(struct nfs41_server_scope *a,
7227 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007228{
7229 if (a->server_scope_sz == b->server_scope_sz &&
7230 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7231 return true;
7232
7233 return false;
7234}
7235
Andy Adamson02a95de2016-02-05 16:08:37 -05007236static void
7237nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7238{
7239}
7240
7241static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7242 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7243};
7244
Andy Adamson357f54d2010-12-14 10:11:57 -05007245/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007246 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007247 *
7248 * The 4.1 client currently uses the same TCP connection for the
7249 * fore and backchannel.
7250 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007251static
7252int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7253 struct rpc_xprt *xprt,
7254 struct nfs_client *clp,
7255 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007256{
7257 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007258 struct nfs41_bind_conn_to_session_args args = {
7259 .client = clp,
7260 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7261 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007262 struct nfs41_bind_conn_to_session_res res;
7263 struct rpc_message msg = {
7264 .rpc_proc =
7265 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007266 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007268 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007269 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007270 struct rpc_task_setup task_setup_data = {
7271 .rpc_client = clnt,
7272 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007273 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007274 .rpc_message = &msg,
7275 .flags = RPC_TASK_TIMEOUT,
7276 };
7277 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007278
7279 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007280
Trond Myklebust71a097c2015-02-18 09:27:18 -08007281 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7282 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7283 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007284
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007285 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7286 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7287 args.dir = NFS4_CDFC4_FORE;
7288
7289 task = rpc_run_task(&task_setup_data);
7290 if (!IS_ERR(task)) {
7291 status = task->tk_status;
7292 rpc_put_task(task);
7293 } else
7294 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007295 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007296 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007297 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007298 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7299 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7300 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007301 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007302 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007303 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007304 dprintk("NFS: %s: Unexpected direction from server\n",
7305 __func__);
7306 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007307 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007308 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007309 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007310 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7311 __func__);
7312 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007313 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007314 }
7315 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007316out:
7317 dprintk("<-- %s status= %d\n", __func__, status);
7318 return status;
7319}
7320
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007321struct rpc_bind_conn_calldata {
7322 struct nfs_client *clp;
7323 struct rpc_cred *cred;
7324};
7325
7326static int
7327nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7328 struct rpc_xprt *xprt,
7329 void *calldata)
7330{
7331 struct rpc_bind_conn_calldata *p = calldata;
7332
7333 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7334}
7335
7336int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7337{
7338 struct rpc_bind_conn_calldata data = {
7339 .clp = clp,
7340 .cred = cred,
7341 };
7342 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7343 nfs4_proc_bind_conn_to_session_callback, &data);
7344}
7345
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007346/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007347 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7348 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007349 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007350static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7351 .how = SP4_MACH_CRED,
7352 .enforce.u.words = {
7353 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7354 1 << (OP_EXCHANGE_ID - 32) |
7355 1 << (OP_CREATE_SESSION - 32) |
7356 1 << (OP_DESTROY_SESSION - 32) |
7357 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007358 },
7359 .allow.u.words = {
7360 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007361 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007362 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007363 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007364 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007365 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007366 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007367 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007368 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007369 1 << (OP_FREE_STATEID - 32) |
7370 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007371 }
7372};
7373
7374/*
7375 * Select the state protection mode for client `clp' given the server results
7376 * from exchange_id in `sp'.
7377 *
7378 * Returns 0 on success, negative errno otherwise.
7379 */
7380static int nfs4_sp4_select_mode(struct nfs_client *clp,
7381 struct nfs41_state_protection *sp)
7382{
7383 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7384 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7385 1 << (OP_EXCHANGE_ID - 32) |
7386 1 << (OP_CREATE_SESSION - 32) |
7387 1 << (OP_DESTROY_SESSION - 32) |
7388 1 << (OP_DESTROY_CLIENTID - 32)
7389 };
7390 unsigned int i;
7391
7392 if (sp->how == SP4_MACH_CRED) {
7393 /* Print state protect result */
7394 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7395 for (i = 0; i <= LAST_NFS4_OP; i++) {
7396 if (test_bit(i, sp->enforce.u.longs))
7397 dfprintk(MOUNT, " enforce op %d\n", i);
7398 if (test_bit(i, sp->allow.u.longs))
7399 dfprintk(MOUNT, " allow op %d\n", i);
7400 }
7401
7402 /* make sure nothing is on enforce list that isn't supported */
7403 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7404 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7405 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7406 return -EINVAL;
7407 }
7408 }
7409
7410 /*
7411 * Minimal mode - state operations are allowed to use machine
7412 * credential. Note this already happens by default, so the
7413 * client doesn't have to do anything more than the negotiation.
7414 *
7415 * NOTE: we don't care if EXCHANGE_ID is in the list -
7416 * we're already using the machine cred for exchange_id
7417 * and will never use a different cred.
7418 */
7419 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7420 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7421 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7422 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7423 dfprintk(MOUNT, "sp4_mach_cred:\n");
7424 dfprintk(MOUNT, " minimal mode enabled\n");
7425 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7426 } else {
7427 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7428 return -EINVAL;
7429 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007430
7431 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007432 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7433 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007434 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7435 dfprintk(MOUNT, " cleanup mode enabled\n");
7436 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7437 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007438
Andrew Elble99ade3c2015-12-02 09:39:51 -05007439 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7440 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7441 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7442 &clp->cl_sp4_flags);
7443 }
7444
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007445 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7446 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7447 dfprintk(MOUNT, " secinfo mode enabled\n");
7448 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7449 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007450
7451 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7452 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7453 dfprintk(MOUNT, " stateid mode enabled\n");
7454 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7455 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007456
7457 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7458 dfprintk(MOUNT, " write mode enabled\n");
7459 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7460 }
7461
7462 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7463 dfprintk(MOUNT, " commit mode enabled\n");
7464 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7465 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007466 }
7467
7468 return 0;
7469}
7470
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471struct nfs41_exchange_id_data {
7472 struct nfs41_exchange_id_res res;
7473 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007474 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 int rpc_status;
7476};
7477
7478static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 struct nfs41_exchange_id_data *cdata =
7481 (struct nfs41_exchange_id_data *)data;
7482 struct nfs_client *clp = cdata->args.client;
7483 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007485 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007486
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007487 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007489
7490 if (cdata->xprt && status == 0) {
7491 status = nfs4_detect_session_trunking(clp, &cdata->res,
7492 cdata->xprt);
7493 goto out;
7494 }
7495
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496 if (status == 0)
7497 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007498
Chuck Lever177313f2012-05-21 22:44:58 -04007499 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 clp->cl_clientid = cdata->res.clientid;
7501 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007502 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007504 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 &clp->cl_session->session_state);
7506 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007507 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007508
Chuck Leveracdeb692012-05-21 22:46:16 -04007509 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 clp->cl_serverowner = cdata->res.server_owner;
7511 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007512
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007513 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007514 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 clp->cl_implid = cdata->res.impl_id;
7516 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007517
Chuck Lever177313f2012-05-21 22:44:58 -04007518 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007519 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007521 dprintk("%s: server_scope mismatch detected\n",
7522 __func__);
7523 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007524 kfree(clp->cl_serverscope);
7525 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007526 }
7527
Chuck Lever177313f2012-05-21 22:44:58 -04007528 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007529 clp->cl_serverscope = cdata->res.server_scope;
7530 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007531 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007532 /* Save the EXCHANGE_ID verifier session trunk tests */
7533 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7534 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007535 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007536out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007537 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007538 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007539}
7540
7541static void nfs4_exchange_id_release(void *data)
7542{
7543 struct nfs41_exchange_id_data *cdata =
7544 (struct nfs41_exchange_id_data *)data;
7545
7546 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007547 if (cdata->xprt) {
7548 xprt_put(cdata->xprt);
7549 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7550 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007551 kfree(cdata->res.impl_id);
7552 kfree(cdata->res.server_scope);
7553 kfree(cdata->res.server_owner);
7554 kfree(cdata);
7555}
7556
7557static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7558 .rpc_call_done = nfs4_exchange_id_done,
7559 .rpc_release = nfs4_exchange_id_release,
7560};
7561
Benny Halevy99fe60d2009-04-01 09:22:29 -04007562/*
7563 * _nfs4_proc_exchange_id()
7564 *
7565 * Wrapper for EXCHANGE_ID operation.
7566 */
7567static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007568 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007569{
7570 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007571 struct rpc_message msg = {
7572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007573 .rpc_cred = cred,
7574 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007575 struct rpc_task_setup task_setup_data = {
7576 .rpc_client = clp->cl_rpcclient,
7577 .callback_ops = &nfs4_exchange_id_call_ops,
7578 .rpc_message = &msg,
7579 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7580 };
7581 struct nfs41_exchange_id_data *calldata;
7582 struct rpc_task *task;
7583 int status = -EIO;
7584
7585 if (!atomic_inc_not_zero(&clp->cl_count))
7586 goto out;
7587
7588 status = -ENOMEM;
7589 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7590 if (!calldata)
7591 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007592
Andy Adamsonad0849a2016-09-09 09:22:28 -04007593 if (!xprt)
7594 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007595
7596 status = nfs4_init_uniform_client_string(clp);
7597 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007598 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007599
7600 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7601 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7602 clp->cl_owner_id);
7603
Andy Adamson8d89bd72016-09-09 09:22:18 -04007604 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7605 GFP_NOFS);
7606 status = -ENOMEM;
7607 if (unlikely(calldata->res.server_owner == NULL))
7608 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007609
Andy Adamson8d89bd72016-09-09 09:22:18 -04007610 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007611 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007612 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007613 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007614
Andy Adamson8d89bd72016-09-09 09:22:18 -04007615 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7616 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007617 goto out_server_scope;
7618
7619 switch (sp4_how) {
7620 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007622 break;
7623
7624 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007625 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007626 break;
7627
7628 default:
7629 /* unsupported! */
7630 WARN_ON_ONCE(1);
7631 status = -EINVAL;
7632 goto out_impl_id;
7633 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007634 if (xprt) {
7635 calldata->xprt = xprt;
7636 task_setup_data.rpc_xprt = xprt;
7637 task_setup_data.flags =
7638 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7639 calldata->args.verifier = &clp->cl_confirm;
7640 } else {
7641 calldata->args.verifier = &verifier;
7642 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007643 calldata->args.client = clp;
7644#ifdef CONFIG_NFS_V4_1_MIGRATION
7645 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7646 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7647 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7648#else
7649 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7650 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7651#endif
7652 msg.rpc_argp = &calldata->args;
7653 msg.rpc_resp = &calldata->res;
7654 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007655
Andy Adamson8d89bd72016-09-09 09:22:18 -04007656 task = rpc_run_task(&task_setup_data);
7657 if (IS_ERR(task)) {
7658 status = PTR_ERR(task);
7659 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007660 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007661
Andy Adamsonad0849a2016-09-09 09:22:28 -04007662 if (!xprt) {
7663 status = rpc_wait_for_completion_task(task);
7664 if (!status)
7665 status = calldata->rpc_status;
7666 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007667 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007668
Andy Adamson8d89bd72016-09-09 09:22:18 -04007669 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007670out:
Chuck Lever177313f2012-05-21 22:44:58 -04007671 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007672 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007673 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007674 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007675 clp->cl_implid->date.seconds,
7676 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007677 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007678 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007679
7680out_impl_id:
7681 kfree(calldata->res.impl_id);
7682out_server_scope:
7683 kfree(calldata->res.server_scope);
7684out_server_owner:
7685 kfree(calldata->res.server_owner);
7686out_calldata:
7687 kfree(calldata);
7688 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007689}
7690
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007691/*
7692 * nfs4_proc_exchange_id()
7693 *
7694 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7695 *
7696 * Since the clientid has expired, all compounds using sessions
7697 * associated with the stale clientid will be returning
7698 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7699 * be in some phase of session reset.
7700 *
7701 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7702 */
7703int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7704{
7705 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7706 int status;
7707
7708 /* try SP4_MACH_CRED if krb5i/p */
7709 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7710 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007711 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007712 if (!status)
7713 return 0;
7714 }
7715
7716 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007717 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007718}
7719
Andy Adamson04fa2c62016-09-09 09:22:29 -04007720/**
7721 * nfs4_test_session_trunk
7722 *
7723 * This is an add_xprt_test() test function called from
7724 * rpc_clnt_setup_test_and_add_xprt.
7725 *
7726 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7727 * and is dereferrenced in nfs4_exchange_id_release
7728 *
7729 * Upon success, add the new transport to the rpc_clnt
7730 *
7731 * @clnt: struct rpc_clnt to get new transport
7732 * @xprt: the rpc_xprt to test
7733 * @data: call data for _nfs4_proc_exchange_id.
7734 */
7735int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7736 void *data)
7737{
7738 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7739 u32 sp4_how;
7740
7741 dprintk("--> %s try %s\n", __func__,
7742 xprt->address_strings[RPC_DISPLAY_ADDR]);
7743
7744 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7745
7746 /* Test connection for session trunking. Async exchange_id call */
7747 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7748}
7749EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7750
Trond Myklebust66245532012-05-25 17:18:09 -04007751static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7752 struct rpc_cred *cred)
7753{
7754 struct rpc_message msg = {
7755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7756 .rpc_argp = clp,
7757 .rpc_cred = cred,
7758 };
7759 int status;
7760
7761 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007762 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007763 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007764 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007765 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7766 return status;
7767}
7768
7769static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7770 struct rpc_cred *cred)
7771{
7772 unsigned int loop;
7773 int ret;
7774
7775 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7776 ret = _nfs4_proc_destroy_clientid(clp, cred);
7777 switch (ret) {
7778 case -NFS4ERR_DELAY:
7779 case -NFS4ERR_CLIENTID_BUSY:
7780 ssleep(1);
7781 break;
7782 default:
7783 return ret;
7784 }
7785 }
7786 return 0;
7787}
7788
7789int nfs4_destroy_clientid(struct nfs_client *clp)
7790{
7791 struct rpc_cred *cred;
7792 int ret = 0;
7793
7794 if (clp->cl_mvops->minor_version < 1)
7795 goto out;
7796 if (clp->cl_exchange_flags == 0)
7797 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007798 if (clp->cl_preserve_clid)
7799 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007800 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007801 ret = nfs4_proc_destroy_clientid(clp, cred);
7802 if (cred)
7803 put_rpccred(cred);
7804 switch (ret) {
7805 case 0:
7806 case -NFS4ERR_STALE_CLIENTID:
7807 clp->cl_exchange_flags = 0;
7808 }
7809out:
7810 return ret;
7811}
7812
Andy Adamson2050f0c2009-04-01 09:22:30 -04007813struct nfs4_get_lease_time_data {
7814 struct nfs4_get_lease_time_args *args;
7815 struct nfs4_get_lease_time_res *res;
7816 struct nfs_client *clp;
7817};
7818
7819static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7820 void *calldata)
7821{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007822 struct nfs4_get_lease_time_data *data =
7823 (struct nfs4_get_lease_time_data *)calldata;
7824
7825 dprintk("--> %s\n", __func__);
7826 /* just setup sequence, do not trigger session recovery
7827 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007828 nfs41_setup_sequence(data->clp->cl_session,
7829 &data->args->la_seq_args,
7830 &data->res->lr_seq_res,
7831 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007832 dprintk("<-- %s\n", __func__);
7833}
7834
7835/*
7836 * Called from nfs4_state_manager thread for session setup, so don't recover
7837 * from sequence operation or clientid errors.
7838 */
7839static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7840{
7841 struct nfs4_get_lease_time_data *data =
7842 (struct nfs4_get_lease_time_data *)calldata;
7843
7844 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007845 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7846 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007847 switch (task->tk_status) {
7848 case -NFS4ERR_DELAY:
7849 case -NFS4ERR_GRACE:
7850 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7851 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7852 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007853 /* fall through */
7854 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007855 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007856 return;
7857 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007858 dprintk("<-- %s\n", __func__);
7859}
7860
Trond Myklebust17280172012-03-11 13:11:00 -04007861static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007862 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7863 .rpc_call_done = nfs4_get_lease_time_done,
7864};
7865
7866int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7867{
7868 struct rpc_task *task;
7869 struct nfs4_get_lease_time_args args;
7870 struct nfs4_get_lease_time_res res = {
7871 .lr_fsinfo = fsinfo,
7872 };
7873 struct nfs4_get_lease_time_data data = {
7874 .args = &args,
7875 .res = &res,
7876 .clp = clp,
7877 };
7878 struct rpc_message msg = {
7879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7880 .rpc_argp = &args,
7881 .rpc_resp = &res,
7882 };
7883 struct rpc_task_setup task_setup = {
7884 .rpc_client = clp->cl_rpcclient,
7885 .rpc_message = &msg,
7886 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007887 .callback_data = &data,
7888 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007889 };
7890 int status;
7891
Chuck Levera9c92d62013-08-09 12:48:18 -04007892 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007893 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007894 dprintk("--> %s\n", __func__);
7895 task = rpc_run_task(&task_setup);
7896
7897 if (IS_ERR(task))
7898 status = PTR_ERR(task);
7899 else {
7900 status = task->tk_status;
7901 rpc_put_task(task);
7902 }
7903 dprintk("<-- %s return %d\n", __func__, status);
7904
7905 return status;
7906}
7907
Andy Adamsonfc931582009-04-01 09:22:31 -04007908/*
7909 * Initialize the values to be used by the client in CREATE_SESSION
7910 * If nfs4_init_session set the fore channel request and response sizes,
7911 * use them.
7912 *
7913 * Set the back channel max_resp_sz_cached to zero to force the client to
7914 * always set csa_cachethis to FALSE because the current implementation
7915 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7916 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007917static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7918 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007919{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007920 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007921 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007922
Andy Adamson18aad3d2013-06-26 12:21:49 -04007923 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7924 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7925
Andy Adamsonfc931582009-04-01 09:22:31 -04007926 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007927 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7928 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007930 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007931
7932 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007933 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007934 __func__,
7935 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007936 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007937
7938 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007939 args->bc_attrs.max_rqst_sz = max_bc_payload;
7940 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007941 args->bc_attrs.max_resp_sz_cached = 0;
7942 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007943 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007944
7945 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7946 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7947 __func__,
7948 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7949 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7950 args->bc_attrs.max_reqs);
7951}
7952
Trond Myklebust79969dd2015-02-18 11:30:18 -08007953static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7954 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007955{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007956 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007957 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007958
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007959 if (rcvd->max_resp_sz > sent->max_resp_sz)
7960 return -EINVAL;
7961 /*
7962 * Our requested max_ops is the minimum we need; we're not
7963 * prepared to break up compounds into smaller pieces than that.
7964 * So, no point even trying to continue if the server won't
7965 * cooperate:
7966 */
7967 if (rcvd->max_ops < sent->max_ops)
7968 return -EINVAL;
7969 if (rcvd->max_reqs == 0)
7970 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007971 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7972 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007973 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007974}
7975
Trond Myklebust79969dd2015-02-18 11:30:18 -08007976static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7977 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007978{
7979 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007980 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007981
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007982 if (!(res->flags & SESSION4_BACK_CHAN))
7983 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007984 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7985 return -EINVAL;
7986 if (rcvd->max_resp_sz < sent->max_resp_sz)
7987 return -EINVAL;
7988 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7989 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007990 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007991 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007992 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007993 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007994out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007995 return 0;
7996}
Andy Adamson8d353012009-04-01 09:22:32 -04007997
Andy Adamson8d353012009-04-01 09:22:32 -04007998static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007999 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008000{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008001 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008002
Trond Myklebust79969dd2015-02-18 11:30:18 -08008003 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008004 if (ret)
8005 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008006 return nfs4_verify_back_channel_attrs(args, res);
8007}
8008
8009static void nfs4_update_session(struct nfs4_session *session,
8010 struct nfs41_create_session_res *res)
8011{
8012 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008013 /* Mark client id and session as being confirmed */
8014 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8015 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008016 session->flags = res->flags;
8017 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008018 if (res->flags & SESSION4_BACK_CHAN)
8019 memcpy(&session->bc_attrs, &res->bc_attrs,
8020 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008021}
8022
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008023static int _nfs4_proc_create_session(struct nfs_client *clp,
8024 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008025{
8026 struct nfs4_session *session = clp->cl_session;
8027 struct nfs41_create_session_args args = {
8028 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008029 .clientid = clp->cl_clientid,
8030 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008031 .cb_program = NFS4_CALLBACK,
8032 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008033 struct nfs41_create_session_res res;
8034
Andy Adamsonfc931582009-04-01 09:22:31 -04008035 struct rpc_message msg = {
8036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8037 .rpc_argp = &args,
8038 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008039 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008040 };
8041 int status;
8042
Chuck Lever6b26cc82016-05-02 14:40:40 -04008043 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008044 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008045
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008046 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008047 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008048
Trond Myklebustb519d402016-09-11 14:50:01 -04008049 switch (status) {
8050 case -NFS4ERR_STALE_CLIENTID:
8051 case -NFS4ERR_DELAY:
8052 case -ETIMEDOUT:
8053 case -EACCES:
8054 case -EAGAIN:
8055 goto out;
8056 };
8057
8058 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008059 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008060 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008061 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008062 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008063 if (status)
8064 goto out;
8065 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008066 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008067out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008068 return status;
8069}
8070
8071/*
8072 * Issues a CREATE_SESSION operation to the server.
8073 * It is the responsibility of the caller to verify the session is
8074 * expired before calling this routine.
8075 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008076int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008077{
8078 int status;
8079 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008080 struct nfs4_session *session = clp->cl_session;
8081
8082 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8083
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008084 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008085 if (status)
8086 goto out;
8087
Andy Adamsonaacd5532011-11-09 13:58:21 -05008088 /* Init or reset the session slot tables */
8089 status = nfs4_setup_session_slot_tables(session);
8090 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008091 if (status)
8092 goto out;
8093
8094 ptr = (unsigned *)&session->sess_id.data[0];
8095 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8096 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008097out:
8098 dprintk("<-- %s\n", __func__);
8099 return status;
8100}
8101
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008102/*
8103 * Issue the over-the-wire RPC DESTROY_SESSION.
8104 * The caller must serialize access to this routine.
8105 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008106int nfs4_proc_destroy_session(struct nfs4_session *session,
8107 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008108{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008109 struct rpc_message msg = {
8110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8111 .rpc_argp = session,
8112 .rpc_cred = cred,
8113 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008114 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008115
8116 dprintk("--> nfs4_proc_destroy_session\n");
8117
8118 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008119 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8120 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008121
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008122 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008123 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008124
8125 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008126 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008127 "Session has been destroyed regardless...\n", status);
8128
8129 dprintk("<-- nfs4_proc_destroy_session\n");
8130 return status;
8131}
8132
Trond Myklebust7b38c362012-05-23 13:23:31 -04008133/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008134 * Renew the cl_session lease.
8135 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008136struct nfs4_sequence_data {
8137 struct nfs_client *clp;
8138 struct nfs4_sequence_args args;
8139 struct nfs4_sequence_res res;
8140};
8141
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008142static void nfs41_sequence_release(void *data)
8143{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008144 struct nfs4_sequence_data *calldata = data;
8145 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008146
Alexandros Batsakis71358402010-02-05 03:45:05 -08008147 if (atomic_read(&clp->cl_count) > 1)
8148 nfs4_schedule_state_renewal(clp);
8149 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008150 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008151}
8152
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008153static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8154{
8155 switch(task->tk_status) {
8156 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008157 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8158 return -EAGAIN;
8159 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008160 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008161 }
8162 return 0;
8163}
8164
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008165static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008166{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008167 struct nfs4_sequence_data *calldata = data;
8168 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008169
Trond Myklebust14516c32010-07-31 14:29:06 -04008170 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8171 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008172
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008173 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008174 if (task->tk_status < 0) {
8175 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008176 if (atomic_read(&clp->cl_count) == 1)
8177 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008178
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008179 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8180 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008181 return;
8182 }
8183 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008184 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008185out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008186 dprintk("<-- %s\n", __func__);
8187}
8188
8189static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8190{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008191 struct nfs4_sequence_data *calldata = data;
8192 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008193 struct nfs4_sequence_args *args;
8194 struct nfs4_sequence_res *res;
8195
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008196 args = task->tk_msg.rpc_argp;
8197 res = task->tk_msg.rpc_resp;
8198
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008199 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008200}
8201
8202static const struct rpc_call_ops nfs41_sequence_ops = {
8203 .rpc_call_done = nfs41_sequence_call_done,
8204 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008205 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008206};
8207
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008208static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8209 struct rpc_cred *cred,
8210 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008211{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008212 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008213 struct rpc_message msg = {
8214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8215 .rpc_cred = cred,
8216 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008217 struct rpc_task_setup task_setup_data = {
8218 .rpc_client = clp->cl_rpcclient,
8219 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008220 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008221 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008222 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008223
Alexandros Batsakis71358402010-02-05 03:45:05 -08008224 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008225 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008226 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008227 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008228 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008229 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008230 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008231 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008232 if (is_privileged)
8233 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008234 msg.rpc_argp = &calldata->args;
8235 msg.rpc_resp = &calldata->res;
8236 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008237 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008238
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008239 return rpc_run_task(&task_setup_data);
8240}
8241
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008242static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008243{
8244 struct rpc_task *task;
8245 int ret = 0;
8246
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008247 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008248 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008249 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008250 if (IS_ERR(task))
8251 ret = PTR_ERR(task);
8252 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008253 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008254 dprintk("<-- %s status=%d\n", __func__, ret);
8255 return ret;
8256}
8257
8258static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8259{
8260 struct rpc_task *task;
8261 int ret;
8262
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008263 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008264 if (IS_ERR(task)) {
8265 ret = PTR_ERR(task);
8266 goto out;
8267 }
8268 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008269 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008270 ret = task->tk_status;
8271 rpc_put_task(task);
8272out:
8273 dprintk("<-- %s status=%d\n", __func__, ret);
8274 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008275}
8276
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008277struct nfs4_reclaim_complete_data {
8278 struct nfs_client *clp;
8279 struct nfs41_reclaim_complete_args arg;
8280 struct nfs41_reclaim_complete_res res;
8281};
8282
8283static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8284{
8285 struct nfs4_reclaim_complete_data *calldata = data;
8286
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008287 nfs41_setup_sequence(calldata->clp->cl_session,
8288 &calldata->arg.seq_args,
8289 &calldata->res.seq_res,
8290 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008291}
8292
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008293static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8294{
8295 switch(task->tk_status) {
8296 case 0:
8297 case -NFS4ERR_COMPLETE_ALREADY:
8298 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8299 break;
8300 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008301 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008302 /* fall through */
8303 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008304 return -EAGAIN;
8305 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008306 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008307 }
8308 return 0;
8309}
8310
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008311static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8312{
8313 struct nfs4_reclaim_complete_data *calldata = data;
8314 struct nfs_client *clp = calldata->clp;
8315 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8316
8317 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008318 if (!nfs41_sequence_done(task, res))
8319 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008320
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008321 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008322 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8323 rpc_restart_call_prepare(task);
8324 return;
8325 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008326 dprintk("<-- %s\n", __func__);
8327}
8328
8329static void nfs4_free_reclaim_complete_data(void *data)
8330{
8331 struct nfs4_reclaim_complete_data *calldata = data;
8332
8333 kfree(calldata);
8334}
8335
8336static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8337 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8338 .rpc_call_done = nfs4_reclaim_complete_done,
8339 .rpc_release = nfs4_free_reclaim_complete_data,
8340};
8341
8342/*
8343 * Issue a global reclaim complete.
8344 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008345static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8346 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008347{
8348 struct nfs4_reclaim_complete_data *calldata;
8349 struct rpc_task *task;
8350 struct rpc_message msg = {
8351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008352 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008353 };
8354 struct rpc_task_setup task_setup_data = {
8355 .rpc_client = clp->cl_rpcclient,
8356 .rpc_message = &msg,
8357 .callback_ops = &nfs4_reclaim_complete_call_ops,
8358 .flags = RPC_TASK_ASYNC,
8359 };
8360 int status = -ENOMEM;
8361
8362 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008363 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008364 if (calldata == NULL)
8365 goto out;
8366 calldata->clp = clp;
8367 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008368
Chuck Levera9c92d62013-08-09 12:48:18 -04008369 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008370 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008371 msg.rpc_argp = &calldata->arg;
8372 msg.rpc_resp = &calldata->res;
8373 task_setup_data.callback_data = calldata;
8374 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008375 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008376 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008377 goto out;
8378 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008379 status = nfs4_wait_for_completion_rpc_task(task);
8380 if (status == 0)
8381 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008382 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008383 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008384out:
8385 dprintk("<-- %s status=%d\n", __func__, status);
8386 return status;
8387}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008388
8389static void
8390nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8391{
8392 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008393 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008394 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008395
8396 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008397 nfs41_setup_sequence(session, &lgp->args.seq_args,
8398 &lgp->res.seq_res, task);
8399 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008400}
8401
8402static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8403{
8404 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008405
8406 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008407 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008408 dprintk("<-- %s\n", __func__);
8409}
8410
8411static int
8412nfs4_layoutget_handle_exception(struct rpc_task *task,
8413 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8414{
Trond Myklebustee314c22012-10-01 17:25:48 -07008415 struct inode *inode = lgp->args.inode;
8416 struct nfs_server *server = NFS_SERVER(inode);
8417 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008418 int nfs4err = task->tk_status;
8419 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008420 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008421
Boaz Harroshed7e5422014-01-22 20:34:54 +02008422 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008423
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008424 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008425 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008426 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008427
8428 /*
8429 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8430 * on the file. set tk_status to -ENODATA to tell upper layer to
8431 * retry go inband.
8432 */
8433 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008434 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008435 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008436 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008437 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8438 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8439 */
8440 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008441 status = -EOVERFLOW;
8442 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008443 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008444 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008445 * (or clients) writing to the same RAID stripe except when
8446 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008447 *
8448 * Treat it like we would RECALLCONFLICT -- we retry for a little
8449 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008450 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008451 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008452 if (lgp->args.minlength == 0) {
8453 status = -EOVERFLOW;
8454 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008455 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008456 status = -EBUSY;
8457 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008458 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008459 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008460 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008461 case -NFS4ERR_DELEG_REVOKED:
8462 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008463 case -NFS4ERR_EXPIRED:
8464 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008465 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008466 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008467 lo = NFS_I(inode)->layout;
8468 /* If the open stateid was bad, then recover it. */
8469 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8470 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008471 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008472 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008473 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008474 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008475 break;
8476 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008477
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008478 /*
8479 * Mark the bad layout state as invalid, then retry
8480 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008481 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008482 spin_unlock(&inode->i_lock);
8483 pnfs_free_lseg_list(&head);
8484 status = -EAGAIN;
8485 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008487
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008488 err = nfs4_handle_exception(server, nfs4err, exception);
8489 if (!status) {
8490 if (exception->retry)
8491 status = -EAGAIN;
8492 else
8493 status = err;
8494 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008495out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008496 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008497 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008498}
8499
Idan Kedar85541162012-08-02 11:47:10 +03008500static size_t max_response_pages(struct nfs_server *server)
8501{
8502 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8503 return nfs_page_array_len(0, max_resp_sz);
8504}
8505
8506static void nfs4_free_pages(struct page **pages, size_t size)
8507{
8508 int i;
8509
8510 if (!pages)
8511 return;
8512
8513 for (i = 0; i < size; i++) {
8514 if (!pages[i])
8515 break;
8516 __free_page(pages[i]);
8517 }
8518 kfree(pages);
8519}
8520
8521static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8522{
8523 struct page **pages;
8524 int i;
8525
8526 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8527 if (!pages) {
8528 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8529 return NULL;
8530 }
8531
8532 for (i = 0; i < size; i++) {
8533 pages[i] = alloc_page(gfp_flags);
8534 if (!pages[i]) {
8535 dprintk("%s: failed to allocate page\n", __func__);
8536 nfs4_free_pages(pages, size);
8537 return NULL;
8538 }
8539 }
8540
8541 return pages;
8542}
8543
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008544static void nfs4_layoutget_release(void *calldata)
8545{
8546 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008547 struct inode *inode = lgp->args.inode;
8548 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008549 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008550
8551 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008552 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008553 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008554 put_nfs_open_context(lgp->args.ctx);
8555 kfree(calldata);
8556 dprintk("<-- %s\n", __func__);
8557}
8558
8559static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8560 .rpc_call_prepare = nfs4_layoutget_prepare,
8561 .rpc_call_done = nfs4_layoutget_done,
8562 .rpc_release = nfs4_layoutget_release,
8563};
8564
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008565struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008566nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008567{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008568 struct inode *inode = lgp->args.inode;
8569 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008570 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008571 struct rpc_task *task;
8572 struct rpc_message msg = {
8573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8574 .rpc_argp = &lgp->args,
8575 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008576 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008577 };
8578 struct rpc_task_setup task_setup_data = {
8579 .rpc_client = server->client,
8580 .rpc_message = &msg,
8581 .callback_ops = &nfs4_layoutget_call_ops,
8582 .callback_data = lgp,
8583 .flags = RPC_TASK_ASYNC,
8584 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008585 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008586 struct nfs4_exception exception = {
8587 .inode = inode,
8588 .timeout = *timeout,
8589 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008590 int status = 0;
8591
8592 dprintk("--> %s\n", __func__);
8593
Peng Tao4bd5a982014-11-17 11:05:17 +08008594 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8595 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8596
Idan Kedar85541162012-08-02 11:47:10 +03008597 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8598 if (!lgp->args.layout.pages) {
8599 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008600 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008601 }
8602 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8603
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008604 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008605 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008606 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008607
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008608 task = rpc_run_task(&task_setup_data);
8609 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008610 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008612 if (status == 0) {
8613 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8614 *timeout = exception.timeout;
8615 }
8616
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008617 trace_nfs4_layoutget(lgp->args.ctx,
8618 &lgp->args.range,
8619 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008620 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008621 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008622
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008623 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8624 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008625 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008626 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008627 rpc_put_task(task);
8628 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008629 if (status)
8630 return ERR_PTR(status);
8631 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008632}
8633
Benny Halevycbe82602011-05-22 19:52:37 +03008634static void
8635nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8636{
8637 struct nfs4_layoutreturn *lrp = calldata;
8638
8639 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008640 nfs41_setup_sequence(lrp->clp->cl_session,
8641 &lrp->args.seq_args,
8642 &lrp->res.seq_res,
8643 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008644}
8645
8646static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8647{
8648 struct nfs4_layoutreturn *lrp = calldata;
8649 struct nfs_server *server;
8650
8651 dprintk("--> %s\n", __func__);
8652
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008653 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008654 return;
8655
8656 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008657 switch (task->tk_status) {
8658 default:
8659 task->tk_status = 0;
8660 case 0:
8661 break;
8662 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008663 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008664 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008665 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008666 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008667 return;
8668 }
Benny Halevycbe82602011-05-22 19:52:37 +03008669 dprintk("<-- %s\n", __func__);
8670}
8671
8672static void nfs4_layoutreturn_release(void *calldata)
8673{
8674 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008675 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008676
8677 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008678 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008679 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008680 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008681 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8682 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008683 pnfs_put_layout_hdr(lrp->args.layout);
8684 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008685 kfree(calldata);
8686 dprintk("<-- %s\n", __func__);
8687}
8688
8689static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8690 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8691 .rpc_call_done = nfs4_layoutreturn_done,
8692 .rpc_release = nfs4_layoutreturn_release,
8693};
8694
Peng Tao6c166052014-11-17 09:30:40 +08008695int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008696{
8697 struct rpc_task *task;
8698 struct rpc_message msg = {
8699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8700 .rpc_argp = &lrp->args,
8701 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008702 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008703 };
8704 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008705 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008706 .rpc_message = &msg,
8707 .callback_ops = &nfs4_layoutreturn_call_ops,
8708 .callback_data = lrp,
8709 };
Peng Tao6c166052014-11-17 09:30:40 +08008710 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008711
Andrew Elble99ade3c2015-12-02 09:39:51 -05008712 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8713 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8714 &task_setup_data.rpc_client, &msg);
8715
Benny Halevycbe82602011-05-22 19:52:37 +03008716 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008717 if (!sync) {
8718 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8719 if (!lrp->inode) {
8720 nfs4_layoutreturn_release(lrp);
8721 return -EAGAIN;
8722 }
8723 task_setup_data.flags |= RPC_TASK_ASYNC;
8724 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008725 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008726 task = rpc_run_task(&task_setup_data);
8727 if (IS_ERR(task))
8728 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008729 if (sync)
8730 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008731 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008732 dprintk("<-- %s status=%d\n", __func__, status);
8733 rpc_put_task(task);
8734 return status;
8735}
8736
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008737static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008738_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8739 struct pnfs_device *pdev,
8740 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008741{
8742 struct nfs4_getdeviceinfo_args args = {
8743 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008744 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8745 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008746 };
8747 struct nfs4_getdeviceinfo_res res = {
8748 .pdev = pdev,
8749 };
8750 struct rpc_message msg = {
8751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8752 .rpc_argp = &args,
8753 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008754 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008755 };
8756 int status;
8757
8758 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008759 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008760 if (res.notification & ~args.notify_types)
8761 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008762 if (res.notification != args.notify_types)
8763 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008764
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008765 dprintk("<-- %s status=%d\n", __func__, status);
8766
8767 return status;
8768}
8769
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008770int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8771 struct pnfs_device *pdev,
8772 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008773{
8774 struct nfs4_exception exception = { };
8775 int err;
8776
8777 do {
8778 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008779 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008780 &exception);
8781 } while (exception.retry);
8782 return err;
8783}
8784EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8785
Andy Adamson863a3c62011-03-23 13:27:54 +00008786static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8787{
8788 struct nfs4_layoutcommit_data *data = calldata;
8789 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008790 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008791
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008792 nfs41_setup_sequence(session,
8793 &data->args.seq_args,
8794 &data->res.seq_res,
8795 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008796}
8797
8798static void
8799nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8800{
8801 struct nfs4_layoutcommit_data *data = calldata;
8802 struct nfs_server *server = NFS_SERVER(data->args.inode);
8803
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008804 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008805 return;
8806
8807 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008808 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8809 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8810 case -NFS4ERR_BADLAYOUT: /* no layout */
8811 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008812 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008813 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008814 break;
8815 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008816 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008817 rpc_restart_call_prepare(task);
8818 return;
8819 }
8820 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008821}
8822
8823static void nfs4_layoutcommit_release(void *calldata)
8824{
8825 struct nfs4_layoutcommit_data *data = calldata;
8826
Andy Adamsondb29c082011-07-30 20:52:38 -04008827 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008828 nfs_post_op_update_inode_force_wcc(data->args.inode,
8829 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008830 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008831 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008832 kfree(data);
8833}
8834
8835static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8836 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8837 .rpc_call_done = nfs4_layoutcommit_done,
8838 .rpc_release = nfs4_layoutcommit_release,
8839};
8840
8841int
Andy Adamsonef311532011-03-12 02:58:10 -05008842nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008843{
8844 struct rpc_message msg = {
8845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8846 .rpc_argp = &data->args,
8847 .rpc_resp = &data->res,
8848 .rpc_cred = data->cred,
8849 };
8850 struct rpc_task_setup task_setup_data = {
8851 .task = &data->task,
8852 .rpc_client = NFS_CLIENT(data->args.inode),
8853 .rpc_message = &msg,
8854 .callback_ops = &nfs4_layoutcommit_ops,
8855 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008856 };
8857 struct rpc_task *task;
8858 int status = 0;
8859
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008860 dprintk("NFS: initiating layoutcommit call. sync %d "
8861 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008862 data->args.lastbytewritten,
8863 data->args.inode->i_ino);
8864
Trond Myklebust472e2592015-02-05 16:50:30 -05008865 if (!sync) {
8866 data->inode = nfs_igrab_and_active(data->args.inode);
8867 if (data->inode == NULL) {
8868 nfs4_layoutcommit_release(data);
8869 return -EAGAIN;
8870 }
8871 task_setup_data.flags = RPC_TASK_ASYNC;
8872 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008873 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008874 task = rpc_run_task(&task_setup_data);
8875 if (IS_ERR(task))
8876 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008877 if (sync)
8878 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008879 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008880 dprintk("%s: status %d\n", __func__, status);
8881 rpc_put_task(task);
8882 return status;
8883}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008884
Andy Adamson97431202013-08-08 10:57:56 -04008885/**
8886 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8887 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8888 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008889static int
8890_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008891 struct nfs_fsinfo *info,
8892 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008893{
8894 struct nfs41_secinfo_no_name_args args = {
8895 .style = SECINFO_STYLE_CURRENT_FH,
8896 };
8897 struct nfs4_secinfo_res res = {
8898 .flavors = flavors,
8899 };
8900 struct rpc_message msg = {
8901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8902 .rpc_argp = &args,
8903 .rpc_resp = &res,
8904 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008905 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008906 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008907 int status;
8908
8909 if (use_integrity) {
8910 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008911 cred = nfs4_get_clid_cred(server->nfs_client);
8912 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008913 }
8914
8915 dprintk("--> %s\n", __func__);
8916 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8917 &res.seq_res, 0);
8918 dprintk("<-- %s status=%d\n", __func__, status);
8919
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008920 if (cred)
8921 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008922
8923 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008924}
8925
8926static int
8927nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8928 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8929{
8930 struct nfs4_exception exception = { };
8931 int err;
8932 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008933 /* first try using integrity protection */
8934 err = -NFS4ERR_WRONGSEC;
8935
8936 /* try to use integrity protection with machine cred */
8937 if (_nfs4_is_integrity_protected(server->nfs_client))
8938 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8939 flavors, true);
8940
8941 /*
8942 * if unable to use integrity protection, or SECINFO with
8943 * integrity protection returns NFS4ERR_WRONGSEC (which is
8944 * disallowed by spec, but exists in deployed servers) use
8945 * the current filesystem's rpc_client and the user cred.
8946 */
8947 if (err == -NFS4ERR_WRONGSEC)
8948 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8949 flavors, false);
8950
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008951 switch (err) {
8952 case 0:
8953 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008954 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008955 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008956 default:
8957 err = nfs4_handle_exception(server, err, &exception);
8958 }
8959 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008960out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008961 return err;
8962}
8963
8964static int
8965nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8966 struct nfs_fsinfo *info)
8967{
8968 int err;
8969 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008970 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008971 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008972 struct nfs4_secinfo4 *secinfo;
8973 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008974
8975 page = alloc_page(GFP_KERNEL);
8976 if (!page) {
8977 err = -ENOMEM;
8978 goto out;
8979 }
8980
8981 flavors = page_address(page);
8982 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8983
8984 /*
8985 * Fall back on "guess and check" method if
8986 * the server doesn't support SECINFO_NO_NAME
8987 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008988 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008989 err = nfs4_find_root_sec(server, fhandle, info);
8990 goto out_freepage;
8991 }
8992 if (err)
8993 goto out_freepage;
8994
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008995 for (i = 0; i < flavors->num_flavors; i++) {
8996 secinfo = &flavors->flavors[i];
8997
8998 switch (secinfo->flavor) {
8999 case RPC_AUTH_NULL:
9000 case RPC_AUTH_UNIX:
9001 case RPC_AUTH_GSS:
9002 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9003 &secinfo->flavor_info);
9004 break;
9005 default:
9006 flavor = RPC_AUTH_MAXFLAVOR;
9007 break;
9008 }
9009
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009010 if (!nfs_auth_info_match(&server->auth_info, flavor))
9011 flavor = RPC_AUTH_MAXFLAVOR;
9012
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009013 if (flavor != RPC_AUTH_MAXFLAVOR) {
9014 err = nfs4_lookup_root_sec(server, fhandle,
9015 info, flavor);
9016 if (!err)
9017 break;
9018 }
9019 }
9020
9021 if (flavor == RPC_AUTH_MAXFLAVOR)
9022 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009023
9024out_freepage:
9025 put_page(page);
9026 if (err == -EACCES)
9027 return -EPERM;
9028out:
9029 return err;
9030}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009031
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009032static int _nfs41_test_stateid(struct nfs_server *server,
9033 nfs4_stateid *stateid,
9034 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009035{
9036 int status;
9037 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009038 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009039 };
9040 struct nfs41_test_stateid_res res;
9041 struct rpc_message msg = {
9042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9043 .rpc_argp = &args,
9044 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009045 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009046 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009047 struct rpc_clnt *rpc_client = server->client;
9048
9049 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9050 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009051
Chuck Lever38527b12012-07-11 16:30:23 -04009052 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009053 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009054 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009055 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009056 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009057 if (status != NFS_OK) {
9058 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009059 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009060 }
9061 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009062 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009063}
9064
Trond Myklebust43912bb2016-09-22 13:38:56 -04009065static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9066 int err, struct nfs4_exception *exception)
9067{
9068 exception->retry = 0;
9069 switch(err) {
9070 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009071 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009072 nfs4_handle_exception(server, err, exception);
9073 break;
9074 case -NFS4ERR_BADSESSION:
9075 case -NFS4ERR_BADSLOT:
9076 case -NFS4ERR_BAD_HIGH_SLOT:
9077 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9078 case -NFS4ERR_DEADSESSION:
9079 nfs4_do_handle_exception(server, err, exception);
9080 }
9081}
9082
Chuck Lever38527b12012-07-11 16:30:23 -04009083/**
9084 * nfs41_test_stateid - perform a TEST_STATEID operation
9085 *
9086 * @server: server / transport on which to perform the operation
9087 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009088 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009089 *
9090 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9091 * Otherwise a negative NFS4ERR value is returned if the operation
9092 * failed or the state ID is not currently valid.
9093 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009094static int nfs41_test_stateid(struct nfs_server *server,
9095 nfs4_stateid *stateid,
9096 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009097{
9098 struct nfs4_exception exception = { };
9099 int err;
9100 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009101 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009102 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009103 } while (exception.retry);
9104 return err;
9105}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009106
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009107struct nfs_free_stateid_data {
9108 struct nfs_server *server;
9109 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009110 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009111};
9112
9113static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9114{
9115 struct nfs_free_stateid_data *data = calldata;
9116 nfs41_setup_sequence(nfs4_get_session(data->server),
9117 &data->args.seq_args,
9118 &data->res.seq_res,
9119 task);
9120}
9121
9122static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9123{
9124 struct nfs_free_stateid_data *data = calldata;
9125
9126 nfs41_sequence_done(task, &data->res.seq_res);
9127
9128 switch (task->tk_status) {
9129 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009130 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009131 rpc_restart_call_prepare(task);
9132 }
9133}
9134
9135static void nfs41_free_stateid_release(void *calldata)
9136{
9137 kfree(calldata);
9138}
9139
Trond Myklebust17f26b12013-08-21 15:48:42 -04009140static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009141 .rpc_call_prepare = nfs41_free_stateid_prepare,
9142 .rpc_call_done = nfs41_free_stateid_done,
9143 .rpc_release = nfs41_free_stateid_release,
9144};
9145
9146static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009147 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009148 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009149 bool privileged)
9150{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009151 struct rpc_message msg = {
9152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009153 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009154 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009155 struct rpc_task_setup task_setup = {
9156 .rpc_client = server->client,
9157 .rpc_message = &msg,
9158 .callback_ops = &nfs41_free_stateid_ops,
9159 .flags = RPC_TASK_ASYNC,
9160 };
9161 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009162
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009163 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9164 &task_setup.rpc_client, &msg);
9165
Chuck Lever38527b12012-07-11 16:30:23 -04009166 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009167 data = kmalloc(sizeof(*data), GFP_NOFS);
9168 if (!data)
9169 return ERR_PTR(-ENOMEM);
9170 data->server = server;
9171 nfs4_stateid_copy(&data->args.stateid, stateid);
9172
9173 task_setup.callback_data = data;
9174
9175 msg.rpc_argp = &data->args;
9176 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009177 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009178 if (privileged)
9179 nfs4_set_sequence_privileged(&data->args.seq_args);
9180
9181 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009182}
9183
Chuck Lever38527b12012-07-11 16:30:23 -04009184/**
9185 * nfs41_free_stateid - perform a FREE_STATEID operation
9186 *
9187 * @server: server / transport on which to perform the operation
9188 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009189 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009190 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009191 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009192 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009193 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009194static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009195 const nfs4_stateid *stateid,
9196 struct rpc_cred *cred,
9197 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009198{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009199 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009200
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009201 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009202 if (IS_ERR(task))
9203 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009204 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009205 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009206}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009207
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009208static void
9209nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009210{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009211 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009212
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009213 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009214 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009215}
9216
Trond Myklebust36281ca2012-03-04 18:13:56 -05009217static bool nfs41_match_stateid(const nfs4_stateid *s1,
9218 const nfs4_stateid *s2)
9219{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009220 if (s1->type != s2->type)
9221 return false;
9222
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009223 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009224 return false;
9225
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009226 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009227 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009228 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009229 return true;
9230
9231 return false;
9232}
9233
Andy Adamson557134a2009-04-01 09:21:53 -04009234#endif /* CONFIG_NFS_V4_1 */
9235
Trond Myklebust36281ca2012-03-04 18:13:56 -05009236static bool nfs4_match_stateid(const nfs4_stateid *s1,
9237 const nfs4_stateid *s2)
9238{
Trond Myklebustf597c532012-03-04 18:13:56 -05009239 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009240}
9241
9242
Trond Myklebust17280172012-03-11 13:11:00 -04009243static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009244 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009245 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 .recover_open = nfs4_open_reclaim,
9247 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009248 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009249 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250};
9251
Andy Adamson591d71c2009-04-01 09:22:47 -04009252#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009253static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009254 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9255 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9256 .recover_open = nfs4_open_reclaim,
9257 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009258 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009259 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009260 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009261};
9262#endif /* CONFIG_NFS_V4_1 */
9263
Trond Myklebust17280172012-03-11 13:11:00 -04009264static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009265 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009266 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009267 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009269 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270};
9271
Andy Adamson591d71c2009-04-01 09:22:47 -04009272#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009273static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009274 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9275 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009276 .recover_open = nfs41_open_expired,
9277 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009278 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009279};
9280#endif /* CONFIG_NFS_V4_1 */
9281
Trond Myklebust17280172012-03-11 13:11:00 -04009282static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009283 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009284 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009285 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009286};
9287
9288#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009289static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009290 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009291 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009292 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009293};
9294#endif
9295
Chuck Leverec011fe2013-10-17 14:12:39 -04009296static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009297 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009298 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009299};
9300
9301#if defined(CONFIG_NFS_V4_1)
9302static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009303 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009304 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009305};
9306#endif /* CONFIG_NFS_V4_1 */
9307
Trond Myklebust97dc1352010-06-16 09:52:26 -04009308static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9309 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009310 .init_caps = NFS_CAP_READDIRPLUS
9311 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009312 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009313 .init_client = nfs40_init_client,
9314 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009315 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009316 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009317 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009318 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009319 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009320 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009321 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9322 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9323 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009324 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009325};
9326
9327#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009328static struct nfs_seqid *
9329nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9330{
9331 return NULL;
9332}
9333
Trond Myklebust97dc1352010-06-16 09:52:26 -04009334static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9335 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009336 .init_caps = NFS_CAP_READDIRPLUS
9337 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009338 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009339 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009340 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009341 .init_client = nfs41_init_client,
9342 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009343 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009344 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009345 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009346 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009347 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009348 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009349 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009350 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9351 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9352 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009353 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009354};
9355#endif
9356
Steve Dickson42c2c422013-05-22 12:50:38 -04009357#if defined(CONFIG_NFS_V4_2)
9358static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9359 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009360 .init_caps = NFS_CAP_READDIRPLUS
9361 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009362 | NFS_CAP_POSIX_LOCK
9363 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009364 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009365 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009366 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009367 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009368 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009369 | NFS_CAP_LAYOUTSTATS
9370 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009371 .init_client = nfs41_init_client,
9372 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009373 .match_stateid = nfs41_match_stateid,
9374 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009375 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009376 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009377 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009378 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009379 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009380 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9381 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9382 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009383 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009384};
9385#endif
9386
Trond Myklebust97dc1352010-06-16 09:52:26 -04009387const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9388 [0] = &nfs_v4_0_minor_ops,
9389#if defined(CONFIG_NFS_V4_1)
9390 [1] = &nfs_v4_1_minor_ops,
9391#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009392#if defined(CONFIG_NFS_V4_2)
9393 [2] = &nfs_v4_2_minor_ops,
9394#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009395};
9396
Trond Myklebust13997822016-07-24 17:10:52 -04009397static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009398{
9399 ssize_t error, error2;
9400
9401 error = generic_listxattr(dentry, list, size);
9402 if (error < 0)
9403 return error;
9404 if (list) {
9405 list += error;
9406 size -= error;
9407 }
9408
9409 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9410 if (error2 < 0)
9411 return error2;
9412 return error + error2;
9413}
9414
Trond Myklebust17f26b12013-08-21 15:48:42 -04009415static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009416 .create = nfs_create,
9417 .lookup = nfs_lookup,
9418 .atomic_open = nfs_atomic_open,
9419 .link = nfs_link,
9420 .unlink = nfs_unlink,
9421 .symlink = nfs_symlink,
9422 .mkdir = nfs_mkdir,
9423 .rmdir = nfs_rmdir,
9424 .mknod = nfs_mknod,
9425 .rename = nfs_rename,
9426 .permission = nfs_permission,
9427 .getattr = nfs_getattr,
9428 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009429 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009430};
9431
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009432static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009433 .permission = nfs_permission,
9434 .getattr = nfs_getattr,
9435 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009436 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009437};
9438
David Howells509de812006-08-22 20:06:11 -04009439const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 .version = 4, /* protocol version */
9441 .dentry_ops = &nfs4_dentry_operations,
9442 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009443 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009444 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009445 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009446 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009447 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 .getattr = nfs4_proc_getattr,
9449 .setattr = nfs4_proc_setattr,
9450 .lookup = nfs4_proc_lookup,
9451 .access = nfs4_proc_access,
9452 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453 .create = nfs4_proc_create,
9454 .remove = nfs4_proc_remove,
9455 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009456 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009458 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009459 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009460 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461 .link = nfs4_proc_link,
9462 .symlink = nfs4_proc_symlink,
9463 .mkdir = nfs4_proc_mkdir,
9464 .rmdir = nfs4_proc_remove,
9465 .readdir = nfs4_proc_readdir,
9466 .mknod = nfs4_proc_mknod,
9467 .statfs = nfs4_proc_statfs,
9468 .fsinfo = nfs4_proc_fsinfo,
9469 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009470 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009472 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009474 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009476 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009478 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009479 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009481 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009482 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009483 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009484 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009485 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009486 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009487 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009488 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009489 .create_server = nfs4_create_server,
9490 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491};
9492
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009493static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009494 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009495 .list = nfs4_xattr_list_nfs4_acl,
9496 .get = nfs4_xattr_get_nfs4_acl,
9497 .set = nfs4_xattr_set_nfs4_acl,
9498};
9499
9500const struct xattr_handler *nfs4_xattr_handlers[] = {
9501 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009502#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9503 &nfs4_xattr_nfs4_label_handler,
9504#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009505 NULL
9506};
9507
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508/*
9509 * Local variables:
9510 * c-basic-offset: 8
9511 * End:
9512 */