blob: 1e797bf74aaf8cdb7df39977e39adf53e5fedaba [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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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;
4128 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004129 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
Jeff Laytond3d41522010-09-17 17:31:57 -04004132static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4133{
4134 struct nfs_server *server = NFS_SERVER(dir);
4135 struct nfs_renameargs *arg = msg->rpc_argp;
4136 struct nfs_renameres *res = msg->rpc_resp;
4137
4138 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004139 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004140 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004141}
4142
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004143static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4144{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004145 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4146 &data->args.seq_args,
4147 &data->res.seq_res,
4148 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004149}
4150
4151static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4152 struct inode *new_dir)
4153{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004154 struct nfs_renamedata *data = task->tk_calldata;
4155 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004156
4157 if (!nfs4_sequence_done(task, &res->seq_res))
4158 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004159 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004160 return 0;
4161
Trond Myklebustc733c492017-01-11 12:32:26 -05004162 if (task->tk_status == 0) {
4163 update_changeattr(old_dir, &res->old_cinfo);
4164 if (new_dir != old_dir)
4165 update_changeattr(new_dir, &res->new_cinfo);
4166 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004167 return 1;
4168}
4169
Al Virobeffb8f2016-07-20 16:34:42 -04004170static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004172 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 struct nfs4_link_arg arg = {
4174 .fh = NFS_FH(inode),
4175 .dir_fh = NFS_FH(dir),
4176 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004177 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004179 struct nfs4_link_res res = {
4180 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004181 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 struct rpc_message msg = {
4184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4185 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004186 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004188 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Trond Myklebust136f2622010-04-16 16:22:49 -04004190 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004191 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004192 goto out;
4193
David Quigley14c43f72013-05-22 12:50:43 -04004194 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4195 if (IS_ERR(res.label)) {
4196 status = PTR_ERR(res.label);
4197 goto out;
4198 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004199 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004200
Bryan Schumaker7c513052011-03-24 17:12:24 +00004201 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004202 if (!status) {
4203 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004204 status = nfs_post_op_update_inode(inode, res.fattr);
4205 if (!status)
4206 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004207 }
David Quigley14c43f72013-05-22 12:50:43 -04004208
4209
4210 nfs4_label_free(res.label);
4211
Trond Myklebust136f2622010-04-16 16:22:49 -04004212out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004213 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return status;
4215}
4216
Al Virobeffb8f2016-07-20 16:34:42 -04004217static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
4219 struct nfs4_exception exception = { };
4220 int err;
4221 do {
4222 err = nfs4_handle_exception(NFS_SERVER(inode),
4223 _nfs4_proc_link(inode, dir, name),
4224 &exception);
4225 } while (exception.retry);
4226 return err;
4227}
4228
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004229struct nfs4_createdata {
4230 struct rpc_message msg;
4231 struct nfs4_create_arg arg;
4232 struct nfs4_create_res res;
4233 struct nfs_fh fh;
4234 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004235 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236};
4237
4238static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004239 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004240{
4241 struct nfs4_createdata *data;
4242
4243 data = kzalloc(sizeof(*data), GFP_KERNEL);
4244 if (data != NULL) {
4245 struct nfs_server *server = NFS_SERVER(dir);
4246
David Quigley14c43f72013-05-22 12:50:43 -04004247 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4248 if (IS_ERR(data->label))
4249 goto out_free;
4250
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004251 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4252 data->msg.rpc_argp = &data->arg;
4253 data->msg.rpc_resp = &data->res;
4254 data->arg.dir_fh = NFS_FH(dir);
4255 data->arg.server = server;
4256 data->arg.name = name;
4257 data->arg.attrs = sattr;
4258 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004259 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004260 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004261 data->res.server = server;
4262 data->res.fh = &data->fh;
4263 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004264 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004265 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004266 }
4267 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004268out_free:
4269 kfree(data);
4270 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004271}
4272
4273static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4274{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004275 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004276 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004277 if (status == 0) {
4278 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004279 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004280 }
4281 return status;
4282}
4283
4284static void nfs4_free_createdata(struct nfs4_createdata *data)
4285{
David Quigley14c43f72013-05-22 12:50:43 -04004286 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004287 kfree(data);
4288}
4289
Chuck Lever4f390c12006-08-22 20:06:22 -04004290static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004291 struct page *page, unsigned int len, struct iattr *sattr,
4292 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004294 struct nfs4_createdata *data;
4295 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Chuck Lever94a6d752006-08-22 20:06:23 -04004297 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004298 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004299
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004300 status = -ENOMEM;
4301 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4302 if (data == NULL)
4303 goto out;
4304
4305 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4306 data->arg.u.symlink.pages = &page;
4307 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004308 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004310 status = nfs4_do_create(dir, dentry, data);
4311
4312 nfs4_free_createdata(data);
4313out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return status;
4315}
4316
Chuck Lever4f390c12006-08-22 20:06:22 -04004317static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004318 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
4320 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004321 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004323
4324 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004327 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4328 trace_nfs4_symlink(dir, &dentry->d_name, err);
4329 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 &exception);
4331 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004332
4333 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return err;
4335}
4336
4337static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004338 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004340 struct nfs4_createdata *data;
4341 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004343 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4344 if (data == NULL)
4345 goto out;
4346
David Quigley1775fd32013-05-22 12:50:42 -04004347 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 status = nfs4_do_create(dir, dentry, data);
4349
4350 nfs4_free_createdata(data);
4351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 return status;
4353}
4354
4355static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4356 struct iattr *sattr)
4357{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004358 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004360 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004362
David Quigleyaa9c2662013-05-22 12:50:44 -04004363 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4364
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004365 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4366 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004368 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4369 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4370 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 &exception);
4372 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004373 nfs4_label_release_security(label);
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return err;
4376}
4377
4378static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004379 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
David Howells2b0143b2015-03-17 22:25:59 +00004381 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 struct nfs4_readdir_arg args = {
4383 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004384 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 .pgbase = 0,
4386 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004387 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004388 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 };
4390 struct nfs4_readdir_res res;
4391 struct rpc_message msg = {
4392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4393 .rpc_argp = &args,
4394 .rpc_resp = &res,
4395 .rpc_cred = cred,
4396 };
4397 int status;
4398
Al Viro6de14722013-09-16 10:53:17 -04004399 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4400 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004401 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004402 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004404 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 -05004405 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004406 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004407 status += args.pgbase;
4408 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004409
4410 nfs_invalidate_atime(dir);
4411
Harvey Harrison3110ff82008-05-02 13:42:44 -07004412 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return status;
4414}
4415
4416static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004417 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
4419 struct nfs4_exception exception = { };
4420 int err;
4421 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004422 err = _nfs4_proc_readdir(dentry, cred, cookie,
4423 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004424 trace_nfs4_readdir(d_inode(dentry), err);
4425 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 &exception);
4427 } while (exception.retry);
4428 return err;
4429}
4430
4431static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004432 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004434 struct nfs4_createdata *data;
4435 int mode = sattr->ia_mode;
4436 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004438 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4439 if (data == NULL)
4440 goto out;
4441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004443 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445 data->arg.ftype = NF4BLK;
4446 data->arg.u.device.specdata1 = MAJOR(rdev);
4447 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 }
4449 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 data->arg.ftype = NF4CHR;
4451 data->arg.u.device.specdata1 = MAJOR(rdev);
4452 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004453 } else if (!S_ISSOCK(mode)) {
4454 status = -EINVAL;
4455 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 }
David Quigley1775fd32013-05-22 12:50:42 -04004457
David Quigleyaa9c2662013-05-22 12:50:44 -04004458 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004459 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004460out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004461 nfs4_free_createdata(data);
4462out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return status;
4464}
4465
4466static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4467 struct iattr *sattr, dev_t rdev)
4468{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004469 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004471 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004473
David Quigleyaa9c2662013-05-22 12:50:44 -04004474 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4475
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004476 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4477 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004479 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4480 trace_nfs4_mknod(dir, &dentry->d_name, err);
4481 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 &exception);
4483 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004484
4485 nfs4_label_release_security(label);
4486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 return err;
4488}
4489
4490static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4491 struct nfs_fsstat *fsstat)
4492{
4493 struct nfs4_statfs_arg args = {
4494 .fh = fhandle,
4495 .bitmask = server->attr_bitmask,
4496 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004497 struct nfs4_statfs_res res = {
4498 .fsstat = fsstat,
4499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 struct rpc_message msg = {
4501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4502 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004503 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 };
4505
Trond Myklebust0e574af2005-10-27 22:12:38 -04004506 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004507 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508}
4509
4510static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4511{
4512 struct nfs4_exception exception = { };
4513 int err;
4514 do {
4515 err = nfs4_handle_exception(server,
4516 _nfs4_proc_statfs(server, fhandle, fsstat),
4517 &exception);
4518 } while (exception.retry);
4519 return err;
4520}
4521
4522static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4523 struct nfs_fsinfo *fsinfo)
4524{
4525 struct nfs4_fsinfo_arg args = {
4526 .fh = fhandle,
4527 .bitmask = server->attr_bitmask,
4528 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004529 struct nfs4_fsinfo_res res = {
4530 .fsinfo = fsinfo,
4531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4534 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004535 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 };
4537
Bryan Schumaker7c513052011-03-24 17:12:24 +00004538 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539}
4540
4541static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4542{
4543 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004544 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 int err;
4546
4547 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004548 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004549 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004550 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004551 nfs4_set_lease_period(server->nfs_client,
4552 fsinfo->lease_time * HZ,
4553 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004554 break;
4555 }
4556 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 } while (exception.retry);
4558 return err;
4559}
4560
4561static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4562{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004563 int error;
4564
Trond Myklebust0e574af2005-10-27 22:12:38 -04004565 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004566 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004567 if (error == 0) {
4568 /* block layout checks this! */
4569 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004570 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004571 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004572
4573 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}
4575
4576static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4577 struct nfs_pathconf *pathconf)
4578{
4579 struct nfs4_pathconf_arg args = {
4580 .fh = fhandle,
4581 .bitmask = server->attr_bitmask,
4582 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004583 struct nfs4_pathconf_res res = {
4584 .pathconf = pathconf,
4585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 struct rpc_message msg = {
4587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4588 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004589 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 };
4591
4592 /* None of the pathconf attributes are mandatory to implement */
4593 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4594 memset(pathconf, 0, sizeof(*pathconf));
4595 return 0;
4596 }
4597
Trond Myklebust0e574af2005-10-27 22:12:38 -04004598 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004599 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
4602static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4603 struct nfs_pathconf *pathconf)
4604{
4605 struct nfs4_exception exception = { };
4606 int err;
4607
4608 do {
4609 err = nfs4_handle_exception(server,
4610 _nfs4_proc_pathconf(server, fhandle, pathconf),
4611 &exception);
4612 } while (exception.retry);
4613 return err;
4614}
4615
Trond Myklebust5521abf2013-03-16 20:54:34 -04004616int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004617 const struct nfs_open_context *ctx,
4618 const struct nfs_lock_context *l_ctx,
4619 fmode_t fmode)
4620{
NeilBrown17393472016-10-13 15:26:47 +11004621 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004622}
4623EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4624
Trond Myklebust5521abf2013-03-16 20:54:34 -04004625static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4626 const struct nfs_open_context *ctx,
4627 const struct nfs_lock_context *l_ctx,
4628 fmode_t fmode)
4629{
4630 nfs4_stateid current_stateid;
4631
Trond Myklebuste1253be2014-03-05 08:44:23 -05004632 /* If the current stateid represents a lost lock, then exit */
4633 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4634 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004635 return nfs4_stateid_match(stateid, &current_stateid);
4636}
4637
4638static bool nfs4_error_stateid_expired(int err)
4639{
4640 switch (err) {
4641 case -NFS4ERR_DELEG_REVOKED:
4642 case -NFS4ERR_ADMIN_REVOKED:
4643 case -NFS4ERR_BAD_STATEID:
4644 case -NFS4ERR_STALE_STATEID:
4645 case -NFS4ERR_OLD_STATEID:
4646 case -NFS4ERR_OPENMODE:
4647 case -NFS4ERR_EXPIRED:
4648 return true;
4649 }
4650 return false;
4651}
4652
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004655 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004658 if (task->tk_status < 0) {
4659 struct nfs4_exception exception = {
4660 .inode = hdr->inode,
4661 .state = hdr->args.context->state,
4662 .stateid = &hdr->args.stateid,
4663 };
4664 task->tk_status = nfs4_async_handle_exception(task,
4665 server, task->tk_status, &exception);
4666 if (exception.retry) {
4667 rpc_restart_call_prepare(task);
4668 return -EAGAIN;
4669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
Trond Myklebust5521abf2013-03-16 20:54:34 -04004677static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004678 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004679{
4680
4681 if (!nfs4_error_stateid_expired(task->tk_status) ||
4682 nfs4_stateid_is_current(&args->stateid,
4683 args->context,
4684 args->lock_context,
4685 FMODE_READ))
4686 return false;
4687 rpc_restart_call_prepare(task);
4688 return true;
4689}
4690
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004692{
4693
4694 dprintk("--> %s\n", __func__);
4695
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004697 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004699 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004700 if (task->tk_status > 0)
4701 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4703 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004704}
4705
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4707 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004710 if (!hdr->pgio_done_cb)
4711 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004712 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004716static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4717 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004718{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4720 &hdr->args.seq_args,
4721 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004722 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004723 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4725 hdr->args.lock_context,
4726 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004727 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004729 return -EIO;
4730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004733static int nfs4_write_done_cb(struct rpc_task *task,
4734 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004736 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004737
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004738 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004739 if (task->tk_status < 0) {
4740 struct nfs4_exception exception = {
4741 .inode = hdr->inode,
4742 .state = hdr->args.context->state,
4743 .stateid = &hdr->args.stateid,
4744 };
4745 task->tk_status = nfs4_async_handle_exception(task,
4746 NFS_SERVER(inode), task->tk_status,
4747 &exception);
4748 if (exception.retry) {
4749 rpc_restart_call_prepare(task);
4750 return -EAGAIN;
4751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004753 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004754 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004755 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004756 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
Trond Myklebust5521abf2013-03-16 20:54:34 -04004760static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004761 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004762{
4763
4764 if (!nfs4_error_stateid_expired(task->tk_status) ||
4765 nfs4_stateid_is_current(&args->stateid,
4766 args->context,
4767 args->lock_context,
4768 FMODE_WRITE))
4769 return false;
4770 rpc_restart_call_prepare(task);
4771 return true;
4772}
4773
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004774static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004775{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004776 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004777 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004778 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004779 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4781 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004782}
4783
Trond Myklebust5a37f852012-04-28 14:55:16 -04004784static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004785bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004786{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004787 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004788 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004789 return false;
4790 /* Otherwise, request attributes if and only if we don't hold
4791 * a delegation
4792 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004793 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004794}
Fred Isamana69aef12011-03-03 15:13:47 +00004795
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004796static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4797 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004799 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004800
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004801 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4802 hdr->args.bitmask = NULL;
4803 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004804 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004806
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004807 if (!hdr->pgio_done_cb)
4808 hdr->pgio_done_cb = nfs4_write_done_cb;
4809 hdr->res.server = server;
4810 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004812 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004813 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814}
4815
Fred Isaman0b7c0152012-04-20 14:47:39 -04004816static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4817{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004818 nfs4_setup_sequence(NFS_SERVER(data->inode),
4819 &data->args.seq_args,
4820 &data->res.seq_res,
4821 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
4823
Fred Isaman0b7c0152012-04-20 14:47:39 -04004824static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004827
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004828 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004829 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4830 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004831 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004832 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
Fred Isaman0b7c0152012-04-20 14:47:39 -04004837static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004838{
4839 if (!nfs4_sequence_done(task, &data->res.seq_res))
4840 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004841 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004842}
4843
Fred Isaman0b7c0152012-04-20 14:47:39 -04004844static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004846 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004847
Fred Isaman0b7c0152012-04-20 14:47:39 -04004848 if (data->commit_done_cb == NULL)
4849 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004850 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004851 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004852 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004855struct nfs4_renewdata {
4856 struct nfs_client *client;
4857 unsigned long timestamp;
4858};
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860/*
4861 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4862 * standalone procedure for queueing an asynchronous RENEW.
4863 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004864static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004865{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004866 struct nfs4_renewdata *data = calldata;
4867 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004868
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004869 if (atomic_read(&clp->cl_count) > 1)
4870 nfs4_schedule_state_renewal(clp);
4871 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004872 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004873}
4874
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004875static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004877 struct nfs4_renewdata *data = calldata;
4878 struct nfs_client *clp = data->client;
4879 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004881 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004882 switch (task->tk_status) {
4883 case 0:
4884 break;
4885 case -NFS4ERR_LEASE_MOVED:
4886 nfs4_schedule_lease_moved_recovery(clp);
4887 break;
4888 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004889 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004890 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4891 return;
4892 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004893 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004894 return;
4895 }
4896 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004898 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899}
4900
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004901static const struct rpc_call_ops nfs4_renew_ops = {
4902 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004903 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004904};
4905
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004906static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
4908 struct rpc_message msg = {
4909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4910 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004911 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004913 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004915 if (renew_flags == 0)
4916 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004917 if (!atomic_inc_not_zero(&clp->cl_count))
4918 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004919 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004920 if (data == NULL)
4921 return -ENOMEM;
4922 data->client = clp;
4923 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004924 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004925 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926}
4927
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004928static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
4930 struct rpc_message msg = {
4931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4932 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004933 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 };
4935 unsigned long now = jiffies;
4936 int status;
4937
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004938 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 if (status < 0)
4940 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004941 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 return 0;
4943}
4944
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004945static inline int nfs4_server_supports_acls(struct nfs_server *server)
4946{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004947 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004948}
4949
Trond Myklebust21f498c2012-08-24 10:59:25 -04004950/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4951 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004952 * the stack.
4953 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004954#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004955
Neil Hormane9e3d722011-03-04 19:26:03 -05004956static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004957 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004958{
4959 struct page *newpage, **spages;
4960 int rc = 0;
4961 size_t len;
4962 spages = pages;
4963
4964 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004965 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004966 newpage = alloc_page(GFP_KERNEL);
4967
4968 if (newpage == NULL)
4969 goto unwind;
4970 memcpy(page_address(newpage), buf, len);
4971 buf += len;
4972 buflen -= len;
4973 *pages++ = newpage;
4974 rc++;
4975 } while (buflen != 0);
4976
4977 return rc;
4978
4979unwind:
4980 for(; rc > 0; rc--)
4981 __free_page(spages[rc-1]);
4982 return -ENOMEM;
4983}
4984
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004985struct nfs4_cached_acl {
4986 int cached;
4987 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004988 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004989};
4990
4991static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004992{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004993 struct nfs_inode *nfsi = NFS_I(inode);
4994
4995 spin_lock(&inode->i_lock);
4996 kfree(nfsi->nfs4_acl);
4997 nfsi->nfs4_acl = acl;
4998 spin_unlock(&inode->i_lock);
4999}
5000
5001static void nfs4_zap_acl_attr(struct inode *inode)
5002{
5003 nfs4_set_cached_acl(inode, NULL);
5004}
5005
5006static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5007{
5008 struct nfs_inode *nfsi = NFS_I(inode);
5009 struct nfs4_cached_acl *acl;
5010 int ret = -ENOENT;
5011
5012 spin_lock(&inode->i_lock);
5013 acl = nfsi->nfs4_acl;
5014 if (acl == NULL)
5015 goto out;
5016 if (buf == NULL) /* user is just asking for length */
5017 goto out_len;
5018 if (acl->cached == 0)
5019 goto out;
5020 ret = -ERANGE; /* see getxattr(2) man page */
5021 if (acl->len > buflen)
5022 goto out;
5023 memcpy(buf, acl->data, acl->len);
5024out_len:
5025 ret = acl->len;
5026out:
5027 spin_unlock(&inode->i_lock);
5028 return ret;
5029}
5030
Sachin Prabhu5794d212012-04-17 14:36:40 +01005031static 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 +00005032{
5033 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005034 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005035
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005036 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005037 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005038 if (acl == NULL)
5039 goto out;
5040 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005041 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005042 } else {
5043 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5044 if (acl == NULL)
5045 goto out;
5046 acl->cached = 0;
5047 }
5048 acl->len = acl_len;
5049out:
5050 nfs4_set_cached_acl(inode, acl);
5051}
5052
Andy Adamsonbf118a32011-12-07 11:55:27 -05005053/*
5054 * The getxattr API returns the required buffer length when called with a
5055 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5056 * the required buf. On a NULL buf, we send a page of data to the server
5057 * guessing that the ACL request can be serviced by a page. If so, we cache
5058 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5059 * the cache. If not so, we throw away the page, and cache the required
5060 * length. The next getxattr call will then produce another round trip to
5061 * the server, this time with the input buf of the required size.
5062 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005063static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005065 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005066 struct nfs_getaclargs args = {
5067 .fh = NFS_FH(inode),
5068 .acl_pages = pages,
5069 .acl_len = buflen,
5070 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005071 struct nfs_getaclres res = {
5072 .acl_len = buflen,
5073 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005074 struct rpc_message msg = {
5075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5076 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005077 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005078 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005079 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5080 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081
Andy Adamsonbf118a32011-12-07 11:55:27 -05005082 /* As long as we're doing a round trip to the server anyway,
5083 * let's be prepared for a page of acl data. */
5084 if (npages == 0)
5085 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005086 if (npages > ARRAY_SIZE(pages))
5087 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005088
Andy Adamsonbf118a32011-12-07 11:55:27 -05005089 for (i = 0; i < npages; i++) {
5090 pages[i] = alloc_page(GFP_KERNEL);
5091 if (!pages[i])
5092 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005093 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005094
5095 /* for decoding across pages */
5096 res.acl_scratch = alloc_page(GFP_KERNEL);
5097 if (!res.acl_scratch)
5098 goto out_free;
5099
Andy Adamsonbf118a32011-12-07 11:55:27 -05005100 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005101
Peng Taode040be2012-01-10 22:42:47 +08005102 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005103 __func__, buf, buflen, npages, args.acl_len);
5104 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5105 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005106 if (ret)
5107 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005108
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005109 /* Handle the case where the passed-in buffer is too short */
5110 if (res.acl_flags & NFS4_ACL_TRUNC) {
5111 /* Did the user only issue a request for the acl length? */
5112 if (buf == NULL)
5113 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005114 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005115 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005116 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005117 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005118 if (buf) {
5119 if (res.acl_len > buflen) {
5120 ret = -ERANGE;
5121 goto out_free;
5122 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005123 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005124 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005125out_ok:
5126 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005127out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005128 for (i = 0; i < npages; i++)
5129 if (pages[i])
5130 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005131 if (res.acl_scratch)
5132 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005133 return ret;
5134}
5135
Trond Myklebust16b42892006-08-24 12:27:15 -04005136static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5137{
5138 struct nfs4_exception exception = { };
5139 ssize_t ret;
5140 do {
5141 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005142 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005143 if (ret >= 0)
5144 break;
5145 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5146 } while (exception.retry);
5147 return ret;
5148}
5149
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005150static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5151{
5152 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005153 int ret;
5154
5155 if (!nfs4_server_supports_acls(server))
5156 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005157 ret = nfs_revalidate_inode(server, inode);
5158 if (ret < 0)
5159 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005160 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5161 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005162 ret = nfs4_read_cached_acl(inode, buf, buflen);
5163 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005164 /* -ENOENT is returned if there is no ACL or if there is an ACL
5165 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005166 return ret;
5167 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005168}
5169
Trond Myklebust16b42892006-08-24 12:27:15 -04005170static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005171{
5172 struct nfs_server *server = NFS_SERVER(inode);
5173 struct page *pages[NFS4ACL_MAXPAGES];
5174 struct nfs_setaclargs arg = {
5175 .fh = NFS_FH(inode),
5176 .acl_pages = pages,
5177 .acl_len = buflen,
5178 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005179 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005180 struct rpc_message msg = {
5181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5182 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005183 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005184 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005185 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005186 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005187
5188 if (!nfs4_server_supports_acls(server))
5189 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005190 if (npages > ARRAY_SIZE(pages))
5191 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005192 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005193 if (i < 0)
5194 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005195 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005196 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005197
5198 /*
5199 * Free each page after tx, so the only ref left is
5200 * held by the network stack
5201 */
5202 for (; i > 0; i--)
5203 put_page(pages[i-1]);
5204
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005205 /*
5206 * Acl update can result in inode attribute update.
5207 * so mark the attribute cache invalid.
5208 */
5209 spin_lock(&inode->i_lock);
5210 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5211 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005212 nfs_access_zap_cache(inode);
5213 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005214 return ret;
5215}
5216
Trond Myklebust16b42892006-08-24 12:27:15 -04005217static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5218{
5219 struct nfs4_exception exception = { };
5220 int err;
5221 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005222 err = __nfs4_proc_set_acl(inode, buf, buflen);
5223 trace_nfs4_set_acl(inode, err);
5224 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005225 &exception);
5226 } while (exception.retry);
5227 return err;
5228}
5229
David Quigleyaa9c2662013-05-22 12:50:44 -04005230#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5231static int _nfs4_get_security_label(struct inode *inode, void *buf,
5232 size_t buflen)
5233{
5234 struct nfs_server *server = NFS_SERVER(inode);
5235 struct nfs_fattr fattr;
5236 struct nfs4_label label = {0, 0, buflen, buf};
5237
5238 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005239 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005240 .fh = NFS_FH(inode),
5241 .bitmask = bitmask,
5242 };
5243 struct nfs4_getattr_res res = {
5244 .fattr = &fattr,
5245 .label = &label,
5246 .server = server,
5247 };
5248 struct rpc_message msg = {
5249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005250 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005251 .rpc_resp = &res,
5252 };
5253 int ret;
5254
5255 nfs_fattr_init(&fattr);
5256
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005257 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005258 if (ret)
5259 return ret;
5260 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5261 return -ENOENT;
5262 if (buflen < label.len)
5263 return -ERANGE;
5264 return 0;
5265}
5266
5267static int nfs4_get_security_label(struct inode *inode, void *buf,
5268 size_t buflen)
5269{
5270 struct nfs4_exception exception = { };
5271 int err;
5272
5273 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5274 return -EOPNOTSUPP;
5275
5276 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005277 err = _nfs4_get_security_label(inode, buf, buflen);
5278 trace_nfs4_get_security_label(inode, err);
5279 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005280 &exception);
5281 } while (exception.retry);
5282 return err;
5283}
5284
5285static int _nfs4_do_set_security_label(struct inode *inode,
5286 struct nfs4_label *ilabel,
5287 struct nfs_fattr *fattr,
5288 struct nfs4_label *olabel)
5289{
5290
5291 struct iattr sattr = {0};
5292 struct nfs_server *server = NFS_SERVER(inode);
5293 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005294 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005295 .fh = NFS_FH(inode),
5296 .iap = &sattr,
5297 .server = server,
5298 .bitmask = bitmask,
5299 .label = ilabel,
5300 };
5301 struct nfs_setattrres res = {
5302 .fattr = fattr,
5303 .label = olabel,
5304 .server = server,
5305 };
5306 struct rpc_message msg = {
5307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005308 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005309 .rpc_resp = &res,
5310 };
5311 int status;
5312
Jeff Layton12207f62013-11-01 10:49:32 -04005313 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005314
Jeff Layton12207f62013-11-01 10:49:32 -04005315 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005316 if (status)
5317 dprintk("%s failed: %d\n", __func__, status);
5318
5319 return status;
5320}
5321
5322static int nfs4_do_set_security_label(struct inode *inode,
5323 struct nfs4_label *ilabel,
5324 struct nfs_fattr *fattr,
5325 struct nfs4_label *olabel)
5326{
5327 struct nfs4_exception exception = { };
5328 int err;
5329
5330 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005331 err = _nfs4_do_set_security_label(inode, ilabel,
5332 fattr, olabel);
5333 trace_nfs4_set_security_label(inode, err);
5334 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005335 &exception);
5336 } while (exception.retry);
5337 return err;
5338}
5339
5340static int
Al Viro59301222016-05-27 10:19:30 -04005341nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005342{
5343 struct nfs4_label ilabel, *olabel = NULL;
5344 struct nfs_fattr fattr;
5345 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005346 int status;
5347
5348 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5349 return -EOPNOTSUPP;
5350
5351 nfs_fattr_init(&fattr);
5352
5353 ilabel.pi = 0;
5354 ilabel.lfs = 0;
5355 ilabel.label = (char *)buf;
5356 ilabel.len = buflen;
5357
5358 cred = rpc_lookup_cred();
5359 if (IS_ERR(cred))
5360 return PTR_ERR(cred);
5361
5362 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5363 if (IS_ERR(olabel)) {
5364 status = -PTR_ERR(olabel);
5365 goto out;
5366 }
5367
5368 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5369 if (status == 0)
5370 nfs_setsecurity(inode, &fattr, olabel);
5371
5372 nfs4_label_free(olabel);
5373out:
5374 put_rpccred(cred);
5375 return status;
5376}
5377#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5378
5379
Chuck Leverf0920752012-05-21 22:45:41 -04005380static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5381 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005382{
5383 __be32 verf[2];
5384
Chuck Lever2c820d92012-05-21 22:45:33 -04005385 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5386 /* An impossible timestamp guarantees this value
5387 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005388 verf[0] = cpu_to_be32(U32_MAX);
5389 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005390 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005391 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005392 u64 ns = ktime_to_ns(nn->boot_time);
5393
5394 verf[0] = cpu_to_be32(ns >> 32);
5395 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005396 }
Chuck Levercd937102012-03-02 17:14:31 -05005397 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5398}
5399
Jeff Laytona3192682015-06-09 19:43:59 -04005400static int
5401nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005402{
Jeff Laytona3192682015-06-09 19:43:59 -04005403 size_t len;
5404 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005405
Trond Myklebustceb3a162015-01-03 15:16:04 -05005406 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005407 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005408
Jeff Laytona3192682015-06-09 19:43:59 -04005409 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005410 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005411 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5412 1 +
5413 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5414 1;
5415 rcu_read_unlock();
5416
5417 if (len > NFS4_OPAQUE_LIMIT + 1)
5418 return -EINVAL;
5419
5420 /*
5421 * Since this string is allocated at mount time, and held until the
5422 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5423 * about a memory-reclaim deadlock.
5424 */
5425 str = kmalloc(len, GFP_KERNEL);
5426 if (!str)
5427 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005428
Chuck Levere984a552012-09-14 17:24:21 -04005429 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005430 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005431 clp->cl_ipaddr,
5432 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5433 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005434 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005435
Jeff Laytona3192682015-06-09 19:43:59 -04005436 clp->cl_owner_id = str;
5437 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005438}
5439
Jeff Layton873e3852015-06-09 19:44:00 -04005440static int
5441nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005442{
Jeff Layton873e3852015-06-09 19:44:00 -04005443 size_t len;
5444 char *str;
5445
5446 len = 10 + 10 + 1 + 10 + 1 +
5447 strlen(nfs4_client_id_uniquifier) + 1 +
5448 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5449
5450 if (len > NFS4_OPAQUE_LIMIT + 1)
5451 return -EINVAL;
5452
5453 /*
5454 * Since this string is allocated at mount time, and held until the
5455 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5456 * about a memory-reclaim deadlock.
5457 */
5458 str = kmalloc(len, GFP_KERNEL);
5459 if (!str)
5460 return -ENOMEM;
5461
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005462 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005463 clp->rpc_ops->version, clp->cl_minorversion,
5464 nfs4_client_id_uniquifier,
5465 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005466 clp->cl_owner_id = str;
5467 return 0;
5468}
5469
5470static int
5471nfs4_init_uniform_client_string(struct nfs_client *clp)
5472{
Jeff Layton873e3852015-06-09 19:44:00 -04005473 size_t len;
5474 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005475
5476 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005477 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005478
5479 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005480 return nfs4_init_uniquifier_client_string(clp);
5481
5482 len = 10 + 10 + 1 + 10 + 1 +
5483 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5484
5485 if (len > NFS4_OPAQUE_LIMIT + 1)
5486 return -EINVAL;
5487
5488 /*
5489 * Since this string is allocated at mount time, and held until the
5490 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5491 * about a memory-reclaim deadlock.
5492 */
5493 str = kmalloc(len, GFP_KERNEL);
5494 if (!str)
5495 return -ENOMEM;
5496
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005497 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005498 clp->rpc_ops->version, clp->cl_minorversion,
5499 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005500 clp->cl_owner_id = str;
5501 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005502}
5503
Chuck Lever706cb8d2014-03-12 12:51:47 -04005504/*
5505 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5506 * services. Advertise one based on the address family of the
5507 * clientaddr.
5508 */
5509static unsigned int
5510nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5511{
5512 if (strchr(clp->cl_ipaddr, ':') != NULL)
5513 return scnprintf(buf, len, "tcp6");
5514 else
5515 return scnprintf(buf, len, "tcp");
5516}
5517
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005518static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5519{
5520 struct nfs4_setclientid *sc = calldata;
5521
5522 if (task->tk_status == 0)
5523 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5524}
5525
5526static const struct rpc_call_ops nfs4_setclientid_ops = {
5527 .rpc_call_done = nfs4_setclientid_done,
5528};
5529
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005530/**
5531 * nfs4_proc_setclientid - Negotiate client ID
5532 * @clp: state data structure
5533 * @program: RPC program for NFSv4 callback service
5534 * @port: IP port number for NFS4 callback service
5535 * @cred: RPC credential to use for this call
5536 * @res: where to place the result
5537 *
5538 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5539 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005540int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5541 unsigned short port, struct rpc_cred *cred,
5542 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
5544 nfs4_verifier sc_verifier;
5545 struct nfs4_setclientid setclientid = {
5546 .sc_verifier = &sc_verifier,
5547 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005548 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 };
5550 struct rpc_message msg = {
5551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5552 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005553 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005554 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005556 struct rpc_task *task;
5557 struct rpc_task_setup task_setup_data = {
5558 .rpc_client = clp->cl_rpcclient,
5559 .rpc_message = &msg,
5560 .callback_ops = &nfs4_setclientid_ops,
5561 .callback_data = &setclientid,
5562 .flags = RPC_TASK_TIMEOUT,
5563 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005564 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Chuck Leverde734832012-07-11 16:30:50 -04005566 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005567 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005568
5569 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5570 status = nfs4_init_uniform_client_string(clp);
5571 else
Jeff Laytona3192682015-06-09 19:43:59 -04005572 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005573
5574 if (status)
5575 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005576
Chuck Leverde734832012-07-11 16:30:50 -04005577 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005578 setclientid.sc_netid_len =
5579 nfs4_init_callback_netid(clp,
5580 setclientid.sc_netid,
5581 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005582 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005583 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 clp->cl_ipaddr, port >> 8, port & 255);
5585
Jeff Layton3a6bb732015-06-09 19:43:57 -04005586 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005587 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005588 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005589 task = rpc_run_task(&task_setup_data);
5590 if (IS_ERR(task)) {
5591 status = PTR_ERR(task);
5592 goto out;
5593 }
5594 status = task->tk_status;
5595 if (setclientid.sc_cred) {
5596 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5597 put_rpccred(setclientid.sc_cred);
5598 }
5599 rpc_put_task(task);
5600out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005601 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005602 dprintk("NFS reply setclientid: %d\n", status);
5603 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604}
5605
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005606/**
5607 * nfs4_proc_setclientid_confirm - Confirm client ID
5608 * @clp: state data structure
5609 * @res: result of a previous SETCLIENTID
5610 * @cred: RPC credential to use for this call
5611 *
5612 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5613 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005614int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005615 struct nfs4_setclientid_res *arg,
5616 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 struct rpc_message msg = {
5619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005620 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005621 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 int status;
5624
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005625 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5626 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5627 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005628 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005629 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005630 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 return status;
5632}
5633
Trond Myklebustfe650402006-01-03 09:55:18 +01005634struct nfs4_delegreturndata {
5635 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005636 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005637 struct nfs_fh fh;
5638 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005639 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005640 struct {
5641 struct nfs4_layoutreturn_args arg;
5642 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005643 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005644 u32 roc_barrier;
5645 bool roc;
5646 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005647 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005648 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005649 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005650};
5651
Trond Myklebustfe650402006-01-03 09:55:18 +01005652static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5653{
5654 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005655
Trond Myklebust14516c32010-07-31 14:29:06 -04005656 if (!nfs4_sequence_done(task, &data->res.seq_res))
5657 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005658
Trond Myklebustca8acf82013-08-13 10:36:56 -04005659 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005660
5661 /* Handle Layoutreturn errors */
5662 if (data->args.lr_args && task->tk_status != 0) {
5663 switch(data->res.lr_ret) {
5664 default:
5665 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5666 break;
5667 case 0:
5668 data->args.lr_args = NULL;
5669 data->res.lr_res = NULL;
5670 break;
5671 case -NFS4ERR_ADMIN_REVOKED:
5672 case -NFS4ERR_DELEG_REVOKED:
5673 case -NFS4ERR_EXPIRED:
5674 case -NFS4ERR_BAD_STATEID:
5675 case -NFS4ERR_OLD_STATEID:
5676 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5677 case -NFS4ERR_WRONG_CRED:
5678 data->args.lr_args = NULL;
5679 data->res.lr_res = NULL;
5680 data->res.lr_ret = 0;
5681 rpc_restart_call_prepare(task);
5682 return;
5683 }
5684 }
5685
Ricardo Labiaga79708862009-12-07 09:23:21 -05005686 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005687 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005688 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005689 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005690 case -NFS4ERR_ADMIN_REVOKED:
5691 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005692 case -NFS4ERR_EXPIRED:
5693 nfs4_free_revoked_stateid(data->res.server,
5694 data->args.stateid,
5695 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005696 case -NFS4ERR_BAD_STATEID:
5697 case -NFS4ERR_OLD_STATEID:
5698 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005699 task->tk_status = 0;
5700 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005701 case -NFS4ERR_ACCESS:
5702 if (data->args.bitmask) {
5703 data->args.bitmask = NULL;
5704 data->res.fattr = NULL;
5705 task->tk_status = 0;
5706 rpc_restart_call_prepare(task);
5707 return;
5708 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005709 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005710 if (nfs4_async_handle_error(task, data->res.server,
5711 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005712 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005713 return;
5714 }
5715 }
5716 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005717}
5718
5719static void nfs4_delegreturn_release(void *calldata)
5720{
Peng Tao039b7562014-07-03 13:05:02 +08005721 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005722 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005723
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005724 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005725 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005726 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5727 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005728 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005729 nfs_iput_and_deactive(inode);
5730 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005731 kfree(calldata);
5732}
5733
Andy Adamson938e1012009-04-01 09:22:28 -04005734static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5735{
5736 struct nfs4_delegreturndata *d_data;
5737
5738 d_data = (struct nfs4_delegreturndata *)data;
5739
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005740 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005741 return;
5742
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005743 nfs4_setup_sequence(d_data->res.server,
5744 &d_data->args.seq_args,
5745 &d_data->res.seq_res,
5746 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005747}
Andy Adamson938e1012009-04-01 09:22:28 -04005748
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005749static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005750 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005751 .rpc_call_done = nfs4_delegreturn_done,
5752 .rpc_release = nfs4_delegreturn_release,
5753};
5754
Trond Myklebuste6f81072008-01-24 18:14:34 -05005755static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005756{
5757 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005758 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005759 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005760 struct rpc_message msg = {
5761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5762 .rpc_cred = cred,
5763 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005764 struct rpc_task_setup task_setup_data = {
5765 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005766 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005767 .callback_ops = &nfs4_delegreturn_ops,
5768 .flags = RPC_TASK_ASYNC,
5769 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005770 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005771
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005772 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005773 if (data == NULL)
5774 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005775 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005776
5777 nfs4_state_protect(server->nfs_client,
5778 NFS_SP4_MACH_CRED_CLEANUP,
5779 &task_setup_data.rpc_client, &msg);
5780
Trond Myklebustfe650402006-01-03 09:55:18 +01005781 data->args.fhandle = &data->fh;
5782 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005783 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005784 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005785 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005786 data->res.fattr = &data->fattr;
5787 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005788 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005789 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005790 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005791 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005792 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005793 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005794 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005795 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005796 if (data->lr.roc) {
5797 data->args.lr_args = &data->lr.arg;
5798 data->res.lr_res = &data->lr.res;
5799 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005800 } else if (data->lr.roc) {
5801 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5802 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005803 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005804
Trond Myklebustc970aa82007-07-14 15:39:59 -04005805 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005806 msg.rpc_argp = &data->args;
5807 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005808 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005809 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005810 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005811 if (!issync)
5812 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005813 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005814 if (status != 0)
5815 goto out;
5816 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005817out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005818 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005819 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820}
5821
Trond Myklebuste6f81072008-01-24 18:14:34 -05005822int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823{
5824 struct nfs_server *server = NFS_SERVER(inode);
5825 struct nfs4_exception exception = { };
5826 int err;
5827 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005828 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005829 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 switch (err) {
5831 case -NFS4ERR_STALE_STATEID:
5832 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 case 0:
5834 return 0;
5835 }
5836 err = nfs4_handle_exception(server, err, &exception);
5837 } while (exception.retry);
5838 return err;
5839}
5840
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5842{
5843 struct inode *inode = state->inode;
5844 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005845 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005846 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005848 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005850 struct nfs_lockt_res res = {
5851 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 };
5853 struct rpc_message msg = {
5854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5855 .rpc_argp = &arg,
5856 .rpc_resp = &res,
5857 .rpc_cred = state->owner->so_cred,
5858 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 struct nfs4_lock_state *lsp;
5860 int status;
5861
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005862 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005863 status = nfs4_set_lock_state(state, request);
5864 if (status != 0)
5865 goto out;
5866 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005867 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005868 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005869 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005870 switch (status) {
5871 case 0:
5872 request->fl_type = F_UNLCK;
5873 break;
5874 case -NFS4ERR_DENIED:
5875 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005877 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005878 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 return status;
5881}
5882
5883static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5884{
5885 struct nfs4_exception exception = { };
5886 int err;
5887
5888 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005889 err = _nfs4_proc_getlk(state, cmd, request);
5890 trace_nfs4_get_lock(request, state, cmd, err);
5891 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 &exception);
5893 } while (exception.retry);
5894 return err;
5895}
5896
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005897struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005898 struct nfs_locku_args arg;
5899 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005900 struct nfs4_lock_state *lsp;
5901 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005902 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005903 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005904 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005905};
5906
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005907static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5908 struct nfs_open_context *ctx,
5909 struct nfs4_lock_state *lsp,
5910 struct nfs_seqid *seqid)
5911{
5912 struct nfs4_unlockdata *p;
5913 struct inode *inode = lsp->ls_state->inode;
5914
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005915 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005916 if (p == NULL)
5917 return NULL;
5918 p->arg.fh = NFS_FH(inode);
5919 p->arg.fl = &p->fl;
5920 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005921 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005922 p->lsp = lsp;
5923 atomic_inc(&lsp->ls_count);
5924 /* Ensure we don't close file until we're done freeing locks! */
5925 p->ctx = get_nfs_open_context(ctx);
5926 memcpy(&p->fl, fl, sizeof(p->fl));
5927 p->server = NFS_SERVER(inode);
5928 return p;
5929}
5930
Trond Myklebust06f814a2006-01-03 09:55:07 +01005931static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005932{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005933 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005934 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005935 nfs4_put_lock_state(calldata->lsp);
5936 put_nfs_open_context(calldata->ctx);
5937 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005938}
5939
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005940static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005941{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005942 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005943
Trond Myklebust14516c32010-07-31 14:29:06 -04005944 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5945 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005946 switch (task->tk_status) {
5947 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005948 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005949 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005950 if (nfs4_update_lock_stateid(calldata->lsp,
5951 &calldata->res.stateid))
5952 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005953 case -NFS4ERR_ADMIN_REVOKED:
5954 case -NFS4ERR_EXPIRED:
5955 nfs4_free_revoked_stateid(calldata->server,
5956 &calldata->arg.stateid,
5957 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005958 case -NFS4ERR_BAD_STATEID:
5959 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005960 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005961 if (!nfs4_stateid_match(&calldata->arg.stateid,
5962 &calldata->lsp->ls_stateid))
5963 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005964 break;
5965 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005966 if (nfs4_async_handle_error(task, calldata->server,
5967 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005968 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005969 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005970 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005971}
5972
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005973static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005974{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005975 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005977 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005978 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005979 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005980 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005981 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005982 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005983 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005984 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005985 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005986 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005987 &calldata->res.seq_res,
5988 task) != 0)
5989 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005990 return;
5991out_no_action:
5992 task->tk_action = NULL;
5993out_wait:
5994 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995}
5996
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005997static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005998 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005999 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006000 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006001};
6002
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6004 struct nfs_open_context *ctx,
6005 struct nfs4_lock_state *lsp,
6006 struct nfs_seqid *seqid)
6007{
6008 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006009 struct rpc_message msg = {
6010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6011 .rpc_cred = ctx->cred,
6012 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006013 struct rpc_task_setup task_setup_data = {
6014 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006015 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006016 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006017 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006018 .flags = RPC_TASK_ASYNC,
6019 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006021 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6022 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6023
Frank Filz137d6ac2007-07-09 15:32:29 -07006024 /* Ensure this is an unlock - when canceling a lock, the
6025 * canceled lock is passed in, and it won't be an unlock.
6026 */
6027 fl->fl_type = F_UNLCK;
6028
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006029 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6030 if (data == NULL) {
6031 nfs_free_seqid(seqid);
6032 return ERR_PTR(-ENOMEM);
6033 }
6034
Chuck Levera9c92d62013-08-09 12:48:18 -04006035 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006036 msg.rpc_argp = &data->arg;
6037 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006038 task_setup_data.callback_data = data;
6039 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040}
6041
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6043{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006044 struct inode *inode = state->inode;
6045 struct nfs4_state_owner *sp = state->owner;
6046 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006047 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006048 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006049 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006050 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006051 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006052 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Trond Myklebust9b073572006-06-29 16:38:34 -04006054 status = nfs4_set_lock_state(state, request);
6055 /* Unlock _before_ we do the RPC call */
6056 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006057 /* Exclude nfs_delegation_claim_locks() */
6058 mutex_lock(&sp->so_delegreturn_mutex);
6059 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006060 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006061 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006062 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006063 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006064 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006065 }
6066 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006067 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006068 if (status != 0)
6069 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006070 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006071 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006072 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6073 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006074 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6075 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006076 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006077 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006078 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006079 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006080 status = PTR_ERR(task);
6081 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006082 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006083 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006084 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006085out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006086 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006087 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006088 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089}
6090
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091struct nfs4_lockdata {
6092 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006093 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094 struct nfs4_lock_state *lsp;
6095 struct nfs_open_context *ctx;
6096 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006097 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006098 int rpc_status;
6099 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006100 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101};
6102
6103static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006104 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6105 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006106{
6107 struct nfs4_lockdata *p;
6108 struct inode *inode = lsp->ls_state->inode;
6109 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006110 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006111
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006112 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113 if (p == NULL)
6114 return NULL;
6115
6116 p->arg.fh = NFS_FH(inode);
6117 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006118 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006119 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006120 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006121 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6122 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006123 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006124 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006125 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006126 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006127 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006128 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006129 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006130 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006131 atomic_inc(&lsp->ls_count);
6132 p->ctx = get_nfs_open_context(ctx);
6133 memcpy(&p->fl, fl, sizeof(p->fl));
6134 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006135out_free_seqid:
6136 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006137out_free:
6138 kfree(p);
6139 return NULL;
6140}
6141
6142static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6143{
6144 struct nfs4_lockdata *data = calldata;
6145 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Harvey Harrison3110ff82008-05-02 13:42:44 -07006147 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006148 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006149 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006150 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006151 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006152 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006153 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006154 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006155 nfs4_stateid_copy(&data->arg.open_stateid,
6156 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006158 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006159 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006160 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006161 nfs4_stateid_copy(&data->arg.lock_stateid,
6162 &data->lsp->ls_stateid);
6163 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006164 if (!nfs4_valid_open_stateid(state)) {
6165 data->rpc_status = -EBADF;
6166 task->tk_action = NULL;
6167 goto out_release_open_seqid;
6168 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006169 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006170 if (nfs4_setup_sequence(data->server,
6171 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006172 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006173 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006174 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006175out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006176 nfs_release_seqid(data->arg.open_seqid);
6177out_release_lock_seqid:
6178 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006179out_wait:
6180 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006181 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006182}
6183
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6185{
6186 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006187 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006188
Harvey Harrison3110ff82008-05-02 13:42:44 -07006189 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190
Trond Myklebust14516c32010-07-31 14:29:06 -04006191 if (!nfs4_sequence_done(task, &data->res.seq_res))
6192 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006193
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006194 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006195 switch (task->tk_status) {
6196 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006197 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006198 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006199 if (data->arg.new_lock) {
6200 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006201 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006202 rpc_restart_call_prepare(task);
6203 break;
6204 }
6205 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006206 if (data->arg.new_lock_owner != 0) {
6207 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6208 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6209 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6210 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6211 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006212 break;
6213 case -NFS4ERR_BAD_STATEID:
6214 case -NFS4ERR_OLD_STATEID:
6215 case -NFS4ERR_STALE_STATEID:
6216 case -NFS4ERR_EXPIRED:
6217 if (data->arg.new_lock_owner != 0) {
6218 if (!nfs4_stateid_match(&data->arg.open_stateid,
6219 &lsp->ls_state->open_stateid))
6220 rpc_restart_call_prepare(task);
6221 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6222 &lsp->ls_stateid))
6223 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006224 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006225 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006226}
6227
6228static void nfs4_lock_release(void *calldata)
6229{
6230 struct nfs4_lockdata *data = calldata;
6231
Harvey Harrison3110ff82008-05-02 13:42:44 -07006232 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006233 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006234 if (data->cancelled != 0) {
6235 struct rpc_task *task;
6236 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6237 data->arg.lock_seqid);
6238 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006239 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006240 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 } else
6242 nfs_free_seqid(data->arg.lock_seqid);
6243 nfs4_put_lock_state(data->lsp);
6244 put_nfs_open_context(data->ctx);
6245 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006246 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006247}
6248
6249static const struct rpc_call_ops nfs4_lock_ops = {
6250 .rpc_call_prepare = nfs4_lock_prepare,
6251 .rpc_call_done = nfs4_lock_done,
6252 .rpc_release = nfs4_lock_release,
6253};
6254
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006255static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6256{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006257 switch (error) {
6258 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006259 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006260 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006261 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006262 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006263 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006264 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006265 break;
6266 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006267 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006268 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006269 };
6270}
6271
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006272static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006273{
6274 struct nfs4_lockdata *data;
6275 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006276 struct rpc_message msg = {
6277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6278 .rpc_cred = state->owner->so_cred,
6279 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006280 struct rpc_task_setup task_setup_data = {
6281 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006282 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006283 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006284 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006285 .flags = RPC_TASK_ASYNC,
6286 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006287 int ret;
6288
Harvey Harrison3110ff82008-05-02 13:42:44 -07006289 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006290 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006291 fl->fl_u.nfs4_fl.owner,
6292 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 if (data == NULL)
6294 return -ENOMEM;
6295 if (IS_SETLKW(cmd))
6296 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006297 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006298 msg.rpc_argp = &data->arg;
6299 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006300 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006301 if (recovery_type > NFS_LOCK_NEW) {
6302 if (recovery_type == NFS_LOCK_RECLAIM)
6303 data->arg.reclaim = NFS_LOCK_RECLAIM;
6304 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006305 } else
6306 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006307 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006308 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006309 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006310 ret = nfs4_wait_for_completion_rpc_task(task);
6311 if (ret == 0) {
6312 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006313 if (ret)
6314 nfs4_handle_setlk_error(data->server, data->lsp,
6315 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006316 } else
6317 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006318 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006319 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006320 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
6324static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6325{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006326 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006327 struct nfs4_exception exception = {
6328 .inode = state->inode,
6329 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006330 int err;
6331
6332 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006333 /* Cache the lock if possible... */
6334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6335 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006336 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006337 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006338 break;
6339 nfs4_handle_exception(server, err, &exception);
6340 } while (exception.retry);
6341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342}
6343
6344static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6345{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006346 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006347 struct nfs4_exception exception = {
6348 .inode = state->inode,
6349 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006350 int err;
6351
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006352 err = nfs4_set_lock_state(state, request);
6353 if (err != 0)
6354 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006355 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006356 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6357 return 0;
6358 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006359 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006360 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6361 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006362 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006363 switch (err) {
6364 default:
6365 goto out;
6366 case -NFS4ERR_GRACE:
6367 case -NFS4ERR_DELAY:
6368 nfs4_handle_exception(server, err, &exception);
6369 err = 0;
6370 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006371 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006372out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374}
6375
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006376#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006377static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6378{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006379 struct nfs4_lock_state *lsp;
6380 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006381
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006382 status = nfs4_set_lock_state(state, request);
6383 if (status != 0)
6384 return status;
6385 lsp = request->fl_u.nfs4_fl.owner;
6386 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6387 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6388 return 0;
6389 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006390 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006391}
6392#endif
6393
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6395{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006396 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006397 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006398 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006399 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
Trond Myklebust01c3b862006-06-29 16:38:39 -04006401 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006402 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006403 if (status < 0)
6404 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006405 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006406 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006407 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006408 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006409 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006410 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006411 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006412 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006413 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006414 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006415 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006416 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006417 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006418 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006419out:
6420 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 return status;
6422}
6423
6424static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6425{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006426 struct nfs4_exception exception = {
6427 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006428 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006429 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 int err;
6431
6432 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006433 err = _nfs4_proc_setlk(state, cmd, request);
6434 if (err == -NFS4ERR_DENIED)
6435 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006437 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 } while (exception.retry);
6439 return err;
6440}
6441
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006442#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6443#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6444
6445static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006446nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6447 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006448{
6449 int status = -ERESTARTSYS;
6450 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6451
6452 while(!signalled()) {
6453 status = nfs4_proc_setlk(state, cmd, request);
6454 if ((status != -EAGAIN) || IS_SETLK(cmd))
6455 break;
6456 freezable_schedule_timeout_interruptible(timeout);
6457 timeout *= 2;
6458 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6459 status = -ERESTARTSYS;
6460 }
6461 return status;
6462}
6463
Jeff Laytona1d617d82016-09-17 18:17:39 -04006464#ifdef CONFIG_NFS_V4_1
6465struct nfs4_lock_waiter {
6466 struct task_struct *task;
6467 struct inode *inode;
6468 struct nfs_lowner *owner;
6469 bool notified;
6470};
6471
6472static int
6473nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6474{
6475 int ret;
6476 struct cb_notify_lock_args *cbnl = key;
6477 struct nfs4_lock_waiter *waiter = wait->private;
6478 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6479 *wowner = waiter->owner;
6480
6481 /* Only wake if the callback was for the same owner */
6482 if (lowner->clientid != wowner->clientid ||
6483 lowner->id != wowner->id ||
6484 lowner->s_dev != wowner->s_dev)
6485 return 0;
6486
6487 /* Make sure it's for the right inode */
6488 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6489 return 0;
6490
6491 waiter->notified = true;
6492
6493 /* override "private" so we can use default_wake_function */
6494 wait->private = waiter->task;
6495 ret = autoremove_wake_function(wait, mode, flags, key);
6496 wait->private = waiter;
6497 return ret;
6498}
6499
6500static int
6501nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6502{
6503 int status = -ERESTARTSYS;
6504 unsigned long flags;
6505 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6506 struct nfs_server *server = NFS_SERVER(state->inode);
6507 struct nfs_client *clp = server->nfs_client;
6508 wait_queue_head_t *q = &clp->cl_lock_waitq;
6509 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6510 .id = lsp->ls_seqid.owner_id,
6511 .s_dev = server->s_dev };
6512 struct nfs4_lock_waiter waiter = { .task = current,
6513 .inode = state->inode,
6514 .owner = &owner,
6515 .notified = false };
6516 wait_queue_t wait;
6517
6518 /* Don't bother with waitqueue if we don't expect a callback */
6519 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6520 return nfs4_retry_setlk_simple(state, cmd, request);
6521
6522 init_wait(&wait);
6523 wait.private = &waiter;
6524 wait.func = nfs4_wake_lock_waiter;
6525 add_wait_queue(q, &wait);
6526
6527 while(!signalled()) {
6528 status = nfs4_proc_setlk(state, cmd, request);
6529 if ((status != -EAGAIN) || IS_SETLK(cmd))
6530 break;
6531
6532 status = -ERESTARTSYS;
6533 spin_lock_irqsave(&q->lock, flags);
6534 if (waiter.notified) {
6535 spin_unlock_irqrestore(&q->lock, flags);
6536 continue;
6537 }
6538 set_current_state(TASK_INTERRUPTIBLE);
6539 spin_unlock_irqrestore(&q->lock, flags);
6540
6541 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6542 }
6543
6544 finish_wait(q, &wait);
6545 return status;
6546}
6547#else /* !CONFIG_NFS_V4_1 */
6548static inline int
6549nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6550{
6551 return nfs4_retry_setlk_simple(state, cmd, request);
6552}
6553#endif
6554
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555static int
6556nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6557{
6558 struct nfs_open_context *ctx;
6559 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 int status;
6561
6562 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006563 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 state = ctx->state;
6565
6566 if (request->fl_start < 0 || request->fl_end < 0)
6567 return -EINVAL;
6568
Trond Myklebustd9531262009-07-21 19:22:38 -04006569 if (IS_GETLK(cmd)) {
6570 if (state != NULL)
6571 return nfs4_proc_getlk(state, F_GETLK, request);
6572 return 0;
6573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574
6575 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6576 return -EINVAL;
6577
Trond Myklebustd9531262009-07-21 19:22:38 -04006578 if (request->fl_type == F_UNLCK) {
6579 if (state != NULL)
6580 return nfs4_proc_unlck(state, cmd, request);
6581 return 0;
6582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Trond Myklebustd9531262009-07-21 19:22:38 -04006584 if (state == NULL)
6585 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006586
6587 if ((request->fl_flags & FL_POSIX) &&
6588 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6589 return -ENOLCK;
6590
Trond Myklebust55725512012-04-18 12:48:35 -04006591 /*
6592 * Don't rely on the VFS having checked the file open mode,
6593 * since it won't do this for flock() locks.
6594 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006595 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006596 case F_RDLCK:
6597 if (!(filp->f_mode & FMODE_READ))
6598 return -EBADF;
6599 break;
6600 case F_WRLCK:
6601 if (!(filp->f_mode & FMODE_WRITE))
6602 return -EBADF;
6603 }
6604
Jeff Layton1ea67db2016-09-17 18:17:37 -04006605 status = nfs4_set_lock_state(state, request);
6606 if (status != 0)
6607 return status;
6608
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006609 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610}
6611
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006612int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006613{
6614 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006615 int err;
6616
6617 err = nfs4_set_lock_state(state, fl);
6618 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006619 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006620 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006621 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006622}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006623
Trond Myklebustcf470c32012-03-07 13:49:12 -05006624struct nfs_release_lockowner_data {
6625 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006626 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006627 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006628 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006629 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006630};
6631
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006632static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6633{
6634 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006635 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006636 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6637 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006638 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006639 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006640}
6641
6642static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6643{
6644 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006645 struct nfs_server *server = data->server;
6646
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006647 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006648
6649 switch (task->tk_status) {
6650 case 0:
6651 renew_lease(server, data->timestamp);
6652 break;
6653 case -NFS4ERR_STALE_CLIENTID:
6654 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006655 nfs4_schedule_lease_recovery(server->nfs_client);
6656 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006657 case -NFS4ERR_LEASE_MOVED:
6658 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006659 if (nfs4_async_handle_error(task, server,
6660 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006661 rpc_restart_call_prepare(task);
6662 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006663}
6664
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006665static void nfs4_release_lockowner_release(void *calldata)
6666{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006667 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006668 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006669 kfree(calldata);
6670}
6671
Trond Myklebust17280172012-03-11 13:11:00 -04006672static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006673 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6674 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006675 .rpc_release = nfs4_release_lockowner_release,
6676};
6677
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006678static void
6679nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006680{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006681 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006682 struct rpc_message msg = {
6683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6684 };
6685
6686 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006687 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006688
Trond Myklebustcf470c32012-03-07 13:49:12 -05006689 data = kmalloc(sizeof(*data), GFP_NOFS);
6690 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006691 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006692 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006693 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006694 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6695 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6696 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006697
Trond Myklebustcf470c32012-03-07 13:49:12 -05006698 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006699 msg.rpc_resp = &data->res;
6700 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006701 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006702}
6703
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006704#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6705
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006706static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006707 struct dentry *unused, struct inode *inode,
6708 const char *key, const void *buf,
6709 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006710{
Al Viro59301222016-05-27 10:19:30 -04006711 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006712}
6713
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006714static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006715 struct dentry *unused, struct inode *inode,
6716 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006717{
Al Virob2968212016-04-10 20:48:24 -04006718 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006719}
6720
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006721static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006722{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006723 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006724}
6725
David Quigleyc9bccef2013-05-22 12:50:45 -04006726#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006727
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006728static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006729 struct dentry *unused, struct inode *inode,
6730 const char *key, const void *buf,
6731 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006732{
6733 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006734 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006735
6736 return -EOPNOTSUPP;
6737}
6738
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006739static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006740 struct dentry *unused, struct inode *inode,
6741 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006742{
6743 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006744 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006745 return -EOPNOTSUPP;
6746}
6747
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006748static ssize_t
6749nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006750{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006751 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006752
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006753 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6754 len = security_inode_listsecurity(inode, list, list_len);
6755 if (list_len && len > list_len)
6756 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006757 }
6758 return len;
6759}
6760
6761static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6762 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006763 .get = nfs4_xattr_get_nfs4_label,
6764 .set = nfs4_xattr_set_nfs4_label,
6765};
David Quigleyc9bccef2013-05-22 12:50:45 -04006766
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006767#else
6768
6769static ssize_t
6770nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6771{
6772 return 0;
6773}
6774
6775#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006776
Andy Adamson533eb462011-06-13 18:25:56 -04006777/*
6778 * nfs_fhget will use either the mounted_on_fileid or the fileid
6779 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006780static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6781{
Andy Adamson533eb462011-06-13 18:25:56 -04006782 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6783 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6784 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006785 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006786 return;
6787
6788 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006789 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006790 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6791 fattr->nlink = 2;
6792}
6793
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006794static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6795 const struct qstr *name,
6796 struct nfs4_fs_locations *fs_locations,
6797 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006798{
6799 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006800 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006801 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006802 };
6803 struct nfs4_fs_locations_arg args = {
6804 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006805 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006806 .page = page,
6807 .bitmask = bitmask,
6808 };
Benny Halevy22958462009-04-01 09:22:02 -04006809 struct nfs4_fs_locations_res res = {
6810 .fs_locations = fs_locations,
6811 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006812 struct rpc_message msg = {
6813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6814 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006815 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006816 };
6817 int status;
6818
Harvey Harrison3110ff82008-05-02 13:42:44 -07006819 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006820
6821 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6822 * is not supported */
6823 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6824 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6825 else
6826 bitmask[0] |= FATTR4_WORD0_FILEID;
6827
Trond Myklebustc228fd32007-01-13 02:28:11 -05006828 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006829 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006830 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006831 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006832 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006833 return status;
6834}
6835
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006836int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6837 const struct qstr *name,
6838 struct nfs4_fs_locations *fs_locations,
6839 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006840{
6841 struct nfs4_exception exception = { };
6842 int err;
6843 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006844 err = _nfs4_proc_fs_locations(client, dir, name,
6845 fs_locations, page);
6846 trace_nfs4_get_fs_locations(dir, name, err);
6847 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006848 &exception);
6849 } while (exception.retry);
6850 return err;
6851}
6852
Chuck Leverb03d7352013-10-17 14:12:50 -04006853/*
6854 * This operation also signals the server that this client is
6855 * performing migration recovery. The server can stop returning
6856 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6857 * appended to this compound to identify the client ID which is
6858 * performing recovery.
6859 */
6860static int _nfs40_proc_get_locations(struct inode *inode,
6861 struct nfs4_fs_locations *locations,
6862 struct page *page, struct rpc_cred *cred)
6863{
6864 struct nfs_server *server = NFS_SERVER(inode);
6865 struct rpc_clnt *clnt = server->client;
6866 u32 bitmask[2] = {
6867 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6868 };
6869 struct nfs4_fs_locations_arg args = {
6870 .clientid = server->nfs_client->cl_clientid,
6871 .fh = NFS_FH(inode),
6872 .page = page,
6873 .bitmask = bitmask,
6874 .migration = 1, /* skip LOOKUP */
6875 .renew = 1, /* append RENEW */
6876 };
6877 struct nfs4_fs_locations_res res = {
6878 .fs_locations = locations,
6879 .migration = 1,
6880 .renew = 1,
6881 };
6882 struct rpc_message msg = {
6883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6884 .rpc_argp = &args,
6885 .rpc_resp = &res,
6886 .rpc_cred = cred,
6887 };
6888 unsigned long now = jiffies;
6889 int status;
6890
6891 nfs_fattr_init(&locations->fattr);
6892 locations->server = server;
6893 locations->nlocations = 0;
6894
6895 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6896 nfs4_set_sequence_privileged(&args.seq_args);
6897 status = nfs4_call_sync_sequence(clnt, server, &msg,
6898 &args.seq_args, &res.seq_res);
6899 if (status)
6900 return status;
6901
6902 renew_lease(server, now);
6903 return 0;
6904}
6905
6906#ifdef CONFIG_NFS_V4_1
6907
6908/*
6909 * This operation also signals the server that this client is
6910 * performing migration recovery. The server can stop asserting
6911 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6912 * performing this operation is identified in the SEQUENCE
6913 * operation in this compound.
6914 *
6915 * When the client supports GETATTR(fs_locations_info), it can
6916 * be plumbed in here.
6917 */
6918static int _nfs41_proc_get_locations(struct inode *inode,
6919 struct nfs4_fs_locations *locations,
6920 struct page *page, struct rpc_cred *cred)
6921{
6922 struct nfs_server *server = NFS_SERVER(inode);
6923 struct rpc_clnt *clnt = server->client;
6924 u32 bitmask[2] = {
6925 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6926 };
6927 struct nfs4_fs_locations_arg args = {
6928 .fh = NFS_FH(inode),
6929 .page = page,
6930 .bitmask = bitmask,
6931 .migration = 1, /* skip LOOKUP */
6932 };
6933 struct nfs4_fs_locations_res res = {
6934 .fs_locations = locations,
6935 .migration = 1,
6936 };
6937 struct rpc_message msg = {
6938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6939 .rpc_argp = &args,
6940 .rpc_resp = &res,
6941 .rpc_cred = cred,
6942 };
6943 int status;
6944
6945 nfs_fattr_init(&locations->fattr);
6946 locations->server = server;
6947 locations->nlocations = 0;
6948
6949 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6950 nfs4_set_sequence_privileged(&args.seq_args);
6951 status = nfs4_call_sync_sequence(clnt, server, &msg,
6952 &args.seq_args, &res.seq_res);
6953 if (status == NFS4_OK &&
6954 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6955 status = -NFS4ERR_LEASE_MOVED;
6956 return status;
6957}
6958
6959#endif /* CONFIG_NFS_V4_1 */
6960
6961/**
6962 * nfs4_proc_get_locations - discover locations for a migrated FSID
6963 * @inode: inode on FSID that is migrating
6964 * @locations: result of query
6965 * @page: buffer
6966 * @cred: credential to use for this operation
6967 *
6968 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6969 * operation failed, or a negative errno if a local error occurred.
6970 *
6971 * On success, "locations" is filled in, but if the server has
6972 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6973 * asserted.
6974 *
6975 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6976 * from this client that require migration recovery.
6977 */
6978int nfs4_proc_get_locations(struct inode *inode,
6979 struct nfs4_fs_locations *locations,
6980 struct page *page, struct rpc_cred *cred)
6981{
6982 struct nfs_server *server = NFS_SERVER(inode);
6983 struct nfs_client *clp = server->nfs_client;
6984 const struct nfs4_mig_recovery_ops *ops =
6985 clp->cl_mvops->mig_recovery_ops;
6986 struct nfs4_exception exception = { };
6987 int status;
6988
6989 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6990 (unsigned long long)server->fsid.major,
6991 (unsigned long long)server->fsid.minor,
6992 clp->cl_hostname);
6993 nfs_display_fhandle(NFS_FH(inode), __func__);
6994
6995 do {
6996 status = ops->get_locations(inode, locations, page, cred);
6997 if (status != -NFS4ERR_DELAY)
6998 break;
6999 nfs4_handle_exception(server, status, &exception);
7000 } while (exception.retry);
7001 return status;
7002}
7003
Chuck Lever44c99932013-10-17 14:13:30 -04007004/*
7005 * This operation also signals the server that this client is
7006 * performing "lease moved" recovery. The server can stop
7007 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7008 * is appended to this compound to identify the client ID which is
7009 * performing recovery.
7010 */
7011static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7012{
7013 struct nfs_server *server = NFS_SERVER(inode);
7014 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7015 struct rpc_clnt *clnt = server->client;
7016 struct nfs4_fsid_present_arg args = {
7017 .fh = NFS_FH(inode),
7018 .clientid = clp->cl_clientid,
7019 .renew = 1, /* append RENEW */
7020 };
7021 struct nfs4_fsid_present_res res = {
7022 .renew = 1,
7023 };
7024 struct rpc_message msg = {
7025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7026 .rpc_argp = &args,
7027 .rpc_resp = &res,
7028 .rpc_cred = cred,
7029 };
7030 unsigned long now = jiffies;
7031 int status;
7032
7033 res.fh = nfs_alloc_fhandle();
7034 if (res.fh == NULL)
7035 return -ENOMEM;
7036
7037 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7038 nfs4_set_sequence_privileged(&args.seq_args);
7039 status = nfs4_call_sync_sequence(clnt, server, &msg,
7040 &args.seq_args, &res.seq_res);
7041 nfs_free_fhandle(res.fh);
7042 if (status)
7043 return status;
7044
7045 do_renew_lease(clp, now);
7046 return 0;
7047}
7048
7049#ifdef CONFIG_NFS_V4_1
7050
7051/*
7052 * This operation also signals the server that this client is
7053 * performing "lease moved" recovery. The server can stop asserting
7054 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7055 * this operation is identified in the SEQUENCE operation in this
7056 * compound.
7057 */
7058static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7059{
7060 struct nfs_server *server = NFS_SERVER(inode);
7061 struct rpc_clnt *clnt = server->client;
7062 struct nfs4_fsid_present_arg args = {
7063 .fh = NFS_FH(inode),
7064 };
7065 struct nfs4_fsid_present_res res = {
7066 };
7067 struct rpc_message msg = {
7068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7069 .rpc_argp = &args,
7070 .rpc_resp = &res,
7071 .rpc_cred = cred,
7072 };
7073 int status;
7074
7075 res.fh = nfs_alloc_fhandle();
7076 if (res.fh == NULL)
7077 return -ENOMEM;
7078
7079 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7080 nfs4_set_sequence_privileged(&args.seq_args);
7081 status = nfs4_call_sync_sequence(clnt, server, &msg,
7082 &args.seq_args, &res.seq_res);
7083 nfs_free_fhandle(res.fh);
7084 if (status == NFS4_OK &&
7085 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7086 status = -NFS4ERR_LEASE_MOVED;
7087 return status;
7088}
7089
7090#endif /* CONFIG_NFS_V4_1 */
7091
7092/**
7093 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7094 * @inode: inode on FSID to check
7095 * @cred: credential to use for this operation
7096 *
7097 * Server indicates whether the FSID is present, moved, or not
7098 * recognized. This operation is necessary to clear a LEASE_MOVED
7099 * condition for this client ID.
7100 *
7101 * Returns NFS4_OK if the FSID is present on this server,
7102 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7103 * NFS4ERR code if some error occurred on the server, or a
7104 * negative errno if a local failure occurred.
7105 */
7106int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7107{
7108 struct nfs_server *server = NFS_SERVER(inode);
7109 struct nfs_client *clp = server->nfs_client;
7110 const struct nfs4_mig_recovery_ops *ops =
7111 clp->cl_mvops->mig_recovery_ops;
7112 struct nfs4_exception exception = { };
7113 int status;
7114
7115 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7116 (unsigned long long)server->fsid.major,
7117 (unsigned long long)server->fsid.minor,
7118 clp->cl_hostname);
7119 nfs_display_fhandle(NFS_FH(inode), __func__);
7120
7121 do {
7122 status = ops->fsid_present(inode, cred);
7123 if (status != -NFS4ERR_DELAY)
7124 break;
7125 nfs4_handle_exception(server, status, &exception);
7126 } while (exception.retry);
7127 return status;
7128}
7129
Andy Adamson5ec16a82013-08-08 10:57:55 -04007130/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007131 * If 'use_integrity' is true and the state managment nfs_client
7132 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7133 * and the machine credential as per RFC3530bis and RFC5661 Security
7134 * Considerations sections. Otherwise, just use the user cred with the
7135 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007136 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007137static 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 +00007138{
7139 int status;
7140 struct nfs4_secinfo_arg args = {
7141 .dir_fh = NFS_FH(dir),
7142 .name = name,
7143 };
7144 struct nfs4_secinfo_res res = {
7145 .flavors = flavors,
7146 };
7147 struct rpc_message msg = {
7148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7149 .rpc_argp = &args,
7150 .rpc_resp = &res,
7151 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007152 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007153 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007154
7155 if (use_integrity) {
7156 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007157 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7158 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007159 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007160
7161 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007162
7163 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7164 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7165
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007166 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7167 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007168 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007169
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007170 if (cred)
7171 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007172
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007173 return status;
7174}
7175
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007176int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7177 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007178{
7179 struct nfs4_exception exception = { };
7180 int err;
7181 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007182 err = -NFS4ERR_WRONGSEC;
7183
7184 /* try to use integrity protection with machine cred */
7185 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7186 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7187
7188 /*
7189 * if unable to use integrity protection, or SECINFO with
7190 * integrity protection returns NFS4ERR_WRONGSEC (which is
7191 * disallowed by spec, but exists in deployed servers) use
7192 * the current filesystem's rpc_client and the user cred.
7193 */
7194 if (err == -NFS4ERR_WRONGSEC)
7195 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7196
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007197 trace_nfs4_secinfo(dir, name, err);
7198 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007199 &exception);
7200 } while (exception.retry);
7201 return err;
7202}
7203
Andy Adamson557134a2009-04-01 09:21:53 -04007204#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007205/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007206 * Check the exchange flags returned by the server for invalid flags, having
7207 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7208 * DS flags set.
7209 */
7210static int nfs4_check_cl_exchange_flags(u32 flags)
7211{
7212 if (flags & ~EXCHGID4_FLAG_MASK_R)
7213 goto out_inval;
7214 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7215 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7216 goto out_inval;
7217 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7218 goto out_inval;
7219 return NFS_OK;
7220out_inval:
7221 return -NFS4ERR_INVAL;
7222}
7223
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007224static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007225nfs41_same_server_scope(struct nfs41_server_scope *a,
7226 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007227{
7228 if (a->server_scope_sz == b->server_scope_sz &&
7229 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7230 return true;
7231
7232 return false;
7233}
7234
Andy Adamson02a95de2016-02-05 16:08:37 -05007235static void
7236nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7237{
7238}
7239
7240static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7241 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7242};
7243
Andy Adamson357f54d2010-12-14 10:11:57 -05007244/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007245 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007246 *
7247 * The 4.1 client currently uses the same TCP connection for the
7248 * fore and backchannel.
7249 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007250static
7251int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7252 struct rpc_xprt *xprt,
7253 struct nfs_client *clp,
7254 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007255{
7256 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007257 struct nfs41_bind_conn_to_session_args args = {
7258 .client = clp,
7259 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7260 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007261 struct nfs41_bind_conn_to_session_res res;
7262 struct rpc_message msg = {
7263 .rpc_proc =
7264 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007265 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007266 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007267 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007268 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007269 struct rpc_task_setup task_setup_data = {
7270 .rpc_client = clnt,
7271 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007272 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007273 .rpc_message = &msg,
7274 .flags = RPC_TASK_TIMEOUT,
7275 };
7276 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007277
7278 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007279
Trond Myklebust71a097c2015-02-18 09:27:18 -08007280 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7281 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7282 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007283
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007284 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7285 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7286 args.dir = NFS4_CDFC4_FORE;
7287
7288 task = rpc_run_task(&task_setup_data);
7289 if (!IS_ERR(task)) {
7290 status = task->tk_status;
7291 rpc_put_task(task);
7292 } else
7293 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007294 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007295 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007296 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007297 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7298 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7299 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007300 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007301 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007302 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007303 dprintk("NFS: %s: Unexpected direction from server\n",
7304 __func__);
7305 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007306 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007307 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007308 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007309 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7310 __func__);
7311 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007312 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007313 }
7314 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007315out:
7316 dprintk("<-- %s status= %d\n", __func__, status);
7317 return status;
7318}
7319
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007320struct rpc_bind_conn_calldata {
7321 struct nfs_client *clp;
7322 struct rpc_cred *cred;
7323};
7324
7325static int
7326nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7327 struct rpc_xprt *xprt,
7328 void *calldata)
7329{
7330 struct rpc_bind_conn_calldata *p = calldata;
7331
7332 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7333}
7334
7335int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7336{
7337 struct rpc_bind_conn_calldata data = {
7338 .clp = clp,
7339 .cred = cred,
7340 };
7341 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7342 nfs4_proc_bind_conn_to_session_callback, &data);
7343}
7344
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007345/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007346 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7347 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007348 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007349static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7350 .how = SP4_MACH_CRED,
7351 .enforce.u.words = {
7352 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7353 1 << (OP_EXCHANGE_ID - 32) |
7354 1 << (OP_CREATE_SESSION - 32) |
7355 1 << (OP_DESTROY_SESSION - 32) |
7356 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007357 },
7358 .allow.u.words = {
7359 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007360 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007361 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007362 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007363 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007364 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007365 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007366 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007367 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007368 1 << (OP_FREE_STATEID - 32) |
7369 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007370 }
7371};
7372
7373/*
7374 * Select the state protection mode for client `clp' given the server results
7375 * from exchange_id in `sp'.
7376 *
7377 * Returns 0 on success, negative errno otherwise.
7378 */
7379static int nfs4_sp4_select_mode(struct nfs_client *clp,
7380 struct nfs41_state_protection *sp)
7381{
7382 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7383 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7384 1 << (OP_EXCHANGE_ID - 32) |
7385 1 << (OP_CREATE_SESSION - 32) |
7386 1 << (OP_DESTROY_SESSION - 32) |
7387 1 << (OP_DESTROY_CLIENTID - 32)
7388 };
7389 unsigned int i;
7390
7391 if (sp->how == SP4_MACH_CRED) {
7392 /* Print state protect result */
7393 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7394 for (i = 0; i <= LAST_NFS4_OP; i++) {
7395 if (test_bit(i, sp->enforce.u.longs))
7396 dfprintk(MOUNT, " enforce op %d\n", i);
7397 if (test_bit(i, sp->allow.u.longs))
7398 dfprintk(MOUNT, " allow op %d\n", i);
7399 }
7400
7401 /* make sure nothing is on enforce list that isn't supported */
7402 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7403 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7404 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7405 return -EINVAL;
7406 }
7407 }
7408
7409 /*
7410 * Minimal mode - state operations are allowed to use machine
7411 * credential. Note this already happens by default, so the
7412 * client doesn't have to do anything more than the negotiation.
7413 *
7414 * NOTE: we don't care if EXCHANGE_ID is in the list -
7415 * we're already using the machine cred for exchange_id
7416 * and will never use a different cred.
7417 */
7418 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7419 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7420 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7421 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7422 dfprintk(MOUNT, "sp4_mach_cred:\n");
7423 dfprintk(MOUNT, " minimal mode enabled\n");
7424 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7425 } else {
7426 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7427 return -EINVAL;
7428 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007429
7430 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007431 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7432 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007433 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7434 dfprintk(MOUNT, " cleanup mode enabled\n");
7435 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7436 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007437
Andrew Elble99ade3c2015-12-02 09:39:51 -05007438 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7439 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7440 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7441 &clp->cl_sp4_flags);
7442 }
7443
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007444 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7445 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7446 dfprintk(MOUNT, " secinfo mode enabled\n");
7447 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7448 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007449
7450 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7451 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7452 dfprintk(MOUNT, " stateid mode enabled\n");
7453 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7454 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007455
7456 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7457 dfprintk(MOUNT, " write mode enabled\n");
7458 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7459 }
7460
7461 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7462 dfprintk(MOUNT, " commit mode enabled\n");
7463 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7464 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007465 }
7466
7467 return 0;
7468}
7469
Andy Adamson8d89bd72016-09-09 09:22:18 -04007470struct nfs41_exchange_id_data {
7471 struct nfs41_exchange_id_res res;
7472 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007473 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007474 int rpc_status;
7475};
7476
7477static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007478{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 struct nfs41_exchange_id_data *cdata =
7480 (struct nfs41_exchange_id_data *)data;
7481 struct nfs_client *clp = cdata->args.client;
7482 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007483
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007484 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007485
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007486 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007487 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007488
7489 if (cdata->xprt && status == 0) {
7490 status = nfs4_detect_session_trunking(clp, &cdata->res,
7491 cdata->xprt);
7492 goto out;
7493 }
7494
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 if (status == 0)
7496 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007497
Chuck Lever177313f2012-05-21 22:44:58 -04007498 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007499 clp->cl_clientid = cdata->res.clientid;
7500 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007501 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007502 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007503 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007504 &clp->cl_session->session_state);
7505 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007506 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007507
Chuck Leveracdeb692012-05-21 22:46:16 -04007508 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 clp->cl_serverowner = cdata->res.server_owner;
7510 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007511
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007512 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007513 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007514 clp->cl_implid = cdata->res.impl_id;
7515 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007516
Chuck Lever177313f2012-05-21 22:44:58 -04007517 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007518 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007519 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007520 dprintk("%s: server_scope mismatch detected\n",
7521 __func__);
7522 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007523 kfree(clp->cl_serverscope);
7524 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007525 }
7526
Chuck Lever177313f2012-05-21 22:44:58 -04007527 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007528 clp->cl_serverscope = cdata->res.server_scope;
7529 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007530 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007531 /* Save the EXCHANGE_ID verifier session trunk tests */
7532 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7533 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007535out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007536 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007537 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538}
7539
7540static void nfs4_exchange_id_release(void *data)
7541{
7542 struct nfs41_exchange_id_data *cdata =
7543 (struct nfs41_exchange_id_data *)data;
7544
7545 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007546 if (cdata->xprt) {
7547 xprt_put(cdata->xprt);
7548 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7549 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007550 kfree(cdata->res.impl_id);
7551 kfree(cdata->res.server_scope);
7552 kfree(cdata->res.server_owner);
7553 kfree(cdata);
7554}
7555
7556static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7557 .rpc_call_done = nfs4_exchange_id_done,
7558 .rpc_release = nfs4_exchange_id_release,
7559};
7560
Benny Halevy99fe60d2009-04-01 09:22:29 -04007561/*
7562 * _nfs4_proc_exchange_id()
7563 *
7564 * Wrapper for EXCHANGE_ID operation.
7565 */
7566static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007567 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007568{
7569 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570 struct rpc_message msg = {
7571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007572 .rpc_cred = cred,
7573 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007574 struct rpc_task_setup task_setup_data = {
7575 .rpc_client = clp->cl_rpcclient,
7576 .callback_ops = &nfs4_exchange_id_call_ops,
7577 .rpc_message = &msg,
7578 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7579 };
7580 struct nfs41_exchange_id_data *calldata;
7581 struct rpc_task *task;
7582 int status = -EIO;
7583
7584 if (!atomic_inc_not_zero(&clp->cl_count))
7585 goto out;
7586
7587 status = -ENOMEM;
7588 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7589 if (!calldata)
7590 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007591
Andy Adamsonad0849a2016-09-09 09:22:28 -04007592 if (!xprt)
7593 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007594
7595 status = nfs4_init_uniform_client_string(clp);
7596 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007597 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007598
7599 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7600 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7601 clp->cl_owner_id);
7602
Andy Adamson8d89bd72016-09-09 09:22:18 -04007603 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7604 GFP_NOFS);
7605 status = -ENOMEM;
7606 if (unlikely(calldata->res.server_owner == NULL))
7607 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007608
Andy Adamson8d89bd72016-09-09 09:22:18 -04007609 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007610 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007611 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007612 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007613
Andy Adamson8d89bd72016-09-09 09:22:18 -04007614 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7615 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007616 goto out_server_scope;
7617
7618 switch (sp4_how) {
7619 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007620 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007621 break;
7622
7623 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007624 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007625 break;
7626
7627 default:
7628 /* unsupported! */
7629 WARN_ON_ONCE(1);
7630 status = -EINVAL;
7631 goto out_impl_id;
7632 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007633 if (xprt) {
7634 calldata->xprt = xprt;
7635 task_setup_data.rpc_xprt = xprt;
7636 task_setup_data.flags =
7637 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7638 calldata->args.verifier = &clp->cl_confirm;
7639 } else {
7640 calldata->args.verifier = &verifier;
7641 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007642 calldata->args.client = clp;
7643#ifdef CONFIG_NFS_V4_1_MIGRATION
7644 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7645 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7646 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7647#else
7648 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7649 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7650#endif
7651 msg.rpc_argp = &calldata->args;
7652 msg.rpc_resp = &calldata->res;
7653 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007654
Andy Adamson8d89bd72016-09-09 09:22:18 -04007655 task = rpc_run_task(&task_setup_data);
7656 if (IS_ERR(task)) {
7657 status = PTR_ERR(task);
7658 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007659 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007660
Andy Adamsonad0849a2016-09-09 09:22:28 -04007661 if (!xprt) {
7662 status = rpc_wait_for_completion_task(task);
7663 if (!status)
7664 status = calldata->rpc_status;
7665 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007666 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007667
Andy Adamson8d89bd72016-09-09 09:22:18 -04007668 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007669out:
Chuck Lever177313f2012-05-21 22:44:58 -04007670 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007671 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007672 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007673 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007674 clp->cl_implid->date.seconds,
7675 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007676 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007677 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007678
7679out_impl_id:
7680 kfree(calldata->res.impl_id);
7681out_server_scope:
7682 kfree(calldata->res.server_scope);
7683out_server_owner:
7684 kfree(calldata->res.server_owner);
7685out_calldata:
7686 kfree(calldata);
7687 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007688}
7689
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007690/*
7691 * nfs4_proc_exchange_id()
7692 *
7693 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7694 *
7695 * Since the clientid has expired, all compounds using sessions
7696 * associated with the stale clientid will be returning
7697 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7698 * be in some phase of session reset.
7699 *
7700 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7701 */
7702int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7703{
7704 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7705 int status;
7706
7707 /* try SP4_MACH_CRED if krb5i/p */
7708 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7709 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007710 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007711 if (!status)
7712 return 0;
7713 }
7714
7715 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007716 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007717}
7718
Andy Adamson04fa2c62016-09-09 09:22:29 -04007719/**
7720 * nfs4_test_session_trunk
7721 *
7722 * This is an add_xprt_test() test function called from
7723 * rpc_clnt_setup_test_and_add_xprt.
7724 *
7725 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7726 * and is dereferrenced in nfs4_exchange_id_release
7727 *
7728 * Upon success, add the new transport to the rpc_clnt
7729 *
7730 * @clnt: struct rpc_clnt to get new transport
7731 * @xprt: the rpc_xprt to test
7732 * @data: call data for _nfs4_proc_exchange_id.
7733 */
7734int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7735 void *data)
7736{
7737 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7738 u32 sp4_how;
7739
7740 dprintk("--> %s try %s\n", __func__,
7741 xprt->address_strings[RPC_DISPLAY_ADDR]);
7742
7743 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7744
7745 /* Test connection for session trunking. Async exchange_id call */
7746 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7747}
7748EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7749
Trond Myklebust66245532012-05-25 17:18:09 -04007750static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7751 struct rpc_cred *cred)
7752{
7753 struct rpc_message msg = {
7754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7755 .rpc_argp = clp,
7756 .rpc_cred = cred,
7757 };
7758 int status;
7759
7760 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007761 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007762 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007763 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007764 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7765 return status;
7766}
7767
7768static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7769 struct rpc_cred *cred)
7770{
7771 unsigned int loop;
7772 int ret;
7773
7774 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7775 ret = _nfs4_proc_destroy_clientid(clp, cred);
7776 switch (ret) {
7777 case -NFS4ERR_DELAY:
7778 case -NFS4ERR_CLIENTID_BUSY:
7779 ssleep(1);
7780 break;
7781 default:
7782 return ret;
7783 }
7784 }
7785 return 0;
7786}
7787
7788int nfs4_destroy_clientid(struct nfs_client *clp)
7789{
7790 struct rpc_cred *cred;
7791 int ret = 0;
7792
7793 if (clp->cl_mvops->minor_version < 1)
7794 goto out;
7795 if (clp->cl_exchange_flags == 0)
7796 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007797 if (clp->cl_preserve_clid)
7798 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007799 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007800 ret = nfs4_proc_destroy_clientid(clp, cred);
7801 if (cred)
7802 put_rpccred(cred);
7803 switch (ret) {
7804 case 0:
7805 case -NFS4ERR_STALE_CLIENTID:
7806 clp->cl_exchange_flags = 0;
7807 }
7808out:
7809 return ret;
7810}
7811
Andy Adamson2050f0c2009-04-01 09:22:30 -04007812struct nfs4_get_lease_time_data {
7813 struct nfs4_get_lease_time_args *args;
7814 struct nfs4_get_lease_time_res *res;
7815 struct nfs_client *clp;
7816};
7817
7818static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7819 void *calldata)
7820{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007821 struct nfs4_get_lease_time_data *data =
7822 (struct nfs4_get_lease_time_data *)calldata;
7823
7824 dprintk("--> %s\n", __func__);
7825 /* just setup sequence, do not trigger session recovery
7826 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007827 nfs41_setup_sequence(data->clp->cl_session,
7828 &data->args->la_seq_args,
7829 &data->res->lr_seq_res,
7830 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007831 dprintk("<-- %s\n", __func__);
7832}
7833
7834/*
7835 * Called from nfs4_state_manager thread for session setup, so don't recover
7836 * from sequence operation or clientid errors.
7837 */
7838static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7839{
7840 struct nfs4_get_lease_time_data *data =
7841 (struct nfs4_get_lease_time_data *)calldata;
7842
7843 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007844 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7845 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007846 switch (task->tk_status) {
7847 case -NFS4ERR_DELAY:
7848 case -NFS4ERR_GRACE:
7849 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7850 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7851 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007852 /* fall through */
7853 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007854 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007855 return;
7856 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007857 dprintk("<-- %s\n", __func__);
7858}
7859
Trond Myklebust17280172012-03-11 13:11:00 -04007860static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007861 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7862 .rpc_call_done = nfs4_get_lease_time_done,
7863};
7864
7865int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7866{
7867 struct rpc_task *task;
7868 struct nfs4_get_lease_time_args args;
7869 struct nfs4_get_lease_time_res res = {
7870 .lr_fsinfo = fsinfo,
7871 };
7872 struct nfs4_get_lease_time_data data = {
7873 .args = &args,
7874 .res = &res,
7875 .clp = clp,
7876 };
7877 struct rpc_message msg = {
7878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7879 .rpc_argp = &args,
7880 .rpc_resp = &res,
7881 };
7882 struct rpc_task_setup task_setup = {
7883 .rpc_client = clp->cl_rpcclient,
7884 .rpc_message = &msg,
7885 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007886 .callback_data = &data,
7887 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007888 };
7889 int status;
7890
Chuck Levera9c92d62013-08-09 12:48:18 -04007891 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007892 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007893 dprintk("--> %s\n", __func__);
7894 task = rpc_run_task(&task_setup);
7895
7896 if (IS_ERR(task))
7897 status = PTR_ERR(task);
7898 else {
7899 status = task->tk_status;
7900 rpc_put_task(task);
7901 }
7902 dprintk("<-- %s return %d\n", __func__, status);
7903
7904 return status;
7905}
7906
Andy Adamsonfc931582009-04-01 09:22:31 -04007907/*
7908 * Initialize the values to be used by the client in CREATE_SESSION
7909 * If nfs4_init_session set the fore channel request and response sizes,
7910 * use them.
7911 *
7912 * Set the back channel max_resp_sz_cached to zero to force the client to
7913 * always set csa_cachethis to FALSE because the current implementation
7914 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7915 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007916static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7917 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007918{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007919 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007920 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007921
Andy Adamson18aad3d2013-06-26 12:21:49 -04007922 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7923 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7924
Andy Adamsonfc931582009-04-01 09:22:31 -04007925 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007926 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7927 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007928 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007929 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007930
7931 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007932 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007933 __func__,
7934 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007935 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007936
7937 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007938 args->bc_attrs.max_rqst_sz = max_bc_payload;
7939 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 args->bc_attrs.max_resp_sz_cached = 0;
7941 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007942 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007943
7944 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7945 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7946 __func__,
7947 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7948 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7949 args->bc_attrs.max_reqs);
7950}
7951
Trond Myklebust79969dd2015-02-18 11:30:18 -08007952static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7953 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007954{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007955 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007956 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007957
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007958 if (rcvd->max_resp_sz > sent->max_resp_sz)
7959 return -EINVAL;
7960 /*
7961 * Our requested max_ops is the minimum we need; we're not
7962 * prepared to break up compounds into smaller pieces than that.
7963 * So, no point even trying to continue if the server won't
7964 * cooperate:
7965 */
7966 if (rcvd->max_ops < sent->max_ops)
7967 return -EINVAL;
7968 if (rcvd->max_reqs == 0)
7969 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007970 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7971 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007972 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007973}
7974
Trond Myklebust79969dd2015-02-18 11:30:18 -08007975static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7976 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007977{
7978 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007979 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007980
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007981 if (!(res->flags & SESSION4_BACK_CHAN))
7982 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007983 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7984 return -EINVAL;
7985 if (rcvd->max_resp_sz < sent->max_resp_sz)
7986 return -EINVAL;
7987 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7988 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007989 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007990 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007991 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007992 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007993out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007994 return 0;
7995}
Andy Adamson8d353012009-04-01 09:22:32 -04007996
Andy Adamson8d353012009-04-01 09:22:32 -04007997static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007998 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007999{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008000 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008001
Trond Myklebust79969dd2015-02-18 11:30:18 -08008002 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008003 if (ret)
8004 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008005 return nfs4_verify_back_channel_attrs(args, res);
8006}
8007
8008static void nfs4_update_session(struct nfs4_session *session,
8009 struct nfs41_create_session_res *res)
8010{
8011 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008012 /* Mark client id and session as being confirmed */
8013 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8014 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008015 session->flags = res->flags;
8016 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008017 if (res->flags & SESSION4_BACK_CHAN)
8018 memcpy(&session->bc_attrs, &res->bc_attrs,
8019 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008020}
8021
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008022static int _nfs4_proc_create_session(struct nfs_client *clp,
8023 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008024{
8025 struct nfs4_session *session = clp->cl_session;
8026 struct nfs41_create_session_args args = {
8027 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008028 .clientid = clp->cl_clientid,
8029 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008030 .cb_program = NFS4_CALLBACK,
8031 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008032 struct nfs41_create_session_res res;
8033
Andy Adamsonfc931582009-04-01 09:22:31 -04008034 struct rpc_message msg = {
8035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8036 .rpc_argp = &args,
8037 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008038 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008039 };
8040 int status;
8041
Chuck Lever6b26cc82016-05-02 14:40:40 -04008042 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008043 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008044
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008045 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008046 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008047
Trond Myklebustb519d402016-09-11 14:50:01 -04008048 switch (status) {
8049 case -NFS4ERR_STALE_CLIENTID:
8050 case -NFS4ERR_DELAY:
8051 case -ETIMEDOUT:
8052 case -EACCES:
8053 case -EAGAIN:
8054 goto out;
8055 };
8056
8057 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008058 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008059 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008060 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008061 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008062 if (status)
8063 goto out;
8064 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008065 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008066out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008067 return status;
8068}
8069
8070/*
8071 * Issues a CREATE_SESSION operation to the server.
8072 * It is the responsibility of the caller to verify the session is
8073 * expired before calling this routine.
8074 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008075int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008076{
8077 int status;
8078 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008079 struct nfs4_session *session = clp->cl_session;
8080
8081 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8082
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008083 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008084 if (status)
8085 goto out;
8086
Andy Adamsonaacd5532011-11-09 13:58:21 -05008087 /* Init or reset the session slot tables */
8088 status = nfs4_setup_session_slot_tables(session);
8089 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008090 if (status)
8091 goto out;
8092
8093 ptr = (unsigned *)&session->sess_id.data[0];
8094 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8095 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008096out:
8097 dprintk("<-- %s\n", __func__);
8098 return status;
8099}
8100
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008101/*
8102 * Issue the over-the-wire RPC DESTROY_SESSION.
8103 * The caller must serialize access to this routine.
8104 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008105int nfs4_proc_destroy_session(struct nfs4_session *session,
8106 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008107{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008108 struct rpc_message msg = {
8109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8110 .rpc_argp = session,
8111 .rpc_cred = cred,
8112 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008113 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008114
8115 dprintk("--> nfs4_proc_destroy_session\n");
8116
8117 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008118 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8119 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008120
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008121 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008122 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008123
8124 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008125 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008126 "Session has been destroyed regardless...\n", status);
8127
8128 dprintk("<-- nfs4_proc_destroy_session\n");
8129 return status;
8130}
8131
Trond Myklebust7b38c362012-05-23 13:23:31 -04008132/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008133 * Renew the cl_session lease.
8134 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008135struct nfs4_sequence_data {
8136 struct nfs_client *clp;
8137 struct nfs4_sequence_args args;
8138 struct nfs4_sequence_res res;
8139};
8140
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008141static void nfs41_sequence_release(void *data)
8142{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008143 struct nfs4_sequence_data *calldata = data;
8144 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008145
Alexandros Batsakis71358402010-02-05 03:45:05 -08008146 if (atomic_read(&clp->cl_count) > 1)
8147 nfs4_schedule_state_renewal(clp);
8148 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008149 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008150}
8151
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008152static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8153{
8154 switch(task->tk_status) {
8155 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008156 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8157 return -EAGAIN;
8158 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008159 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008160 }
8161 return 0;
8162}
8163
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008164static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008165{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008166 struct nfs4_sequence_data *calldata = data;
8167 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008168
Trond Myklebust14516c32010-07-31 14:29:06 -04008169 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8170 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008171
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008172 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008173 if (task->tk_status < 0) {
8174 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008175 if (atomic_read(&clp->cl_count) == 1)
8176 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008177
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008178 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8179 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180 return;
8181 }
8182 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008183 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008184out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008185 dprintk("<-- %s\n", __func__);
8186}
8187
8188static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8189{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008190 struct nfs4_sequence_data *calldata = data;
8191 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008192 struct nfs4_sequence_args *args;
8193 struct nfs4_sequence_res *res;
8194
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008195 args = task->tk_msg.rpc_argp;
8196 res = task->tk_msg.rpc_resp;
8197
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008198 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008199}
8200
8201static const struct rpc_call_ops nfs41_sequence_ops = {
8202 .rpc_call_done = nfs41_sequence_call_done,
8203 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008204 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008205};
8206
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008207static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8208 struct rpc_cred *cred,
8209 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008210{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008211 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008212 struct rpc_message msg = {
8213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8214 .rpc_cred = cred,
8215 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008216 struct rpc_task_setup task_setup_data = {
8217 .rpc_client = clp->cl_rpcclient,
8218 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008219 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008220 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008221 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008222
Alexandros Batsakis71358402010-02-05 03:45:05 -08008223 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008224 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008225 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008226 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008227 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008228 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008229 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008230 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008231 if (is_privileged)
8232 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008233 msg.rpc_argp = &calldata->args;
8234 msg.rpc_resp = &calldata->res;
8235 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008236 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008237
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008238 return rpc_run_task(&task_setup_data);
8239}
8240
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008241static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008242{
8243 struct rpc_task *task;
8244 int ret = 0;
8245
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008246 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008247 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008248 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008249 if (IS_ERR(task))
8250 ret = PTR_ERR(task);
8251 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008252 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008253 dprintk("<-- %s status=%d\n", __func__, ret);
8254 return ret;
8255}
8256
8257static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8258{
8259 struct rpc_task *task;
8260 int ret;
8261
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008262 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008263 if (IS_ERR(task)) {
8264 ret = PTR_ERR(task);
8265 goto out;
8266 }
8267 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008268 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008269 ret = task->tk_status;
8270 rpc_put_task(task);
8271out:
8272 dprintk("<-- %s status=%d\n", __func__, ret);
8273 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008274}
8275
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008276struct nfs4_reclaim_complete_data {
8277 struct nfs_client *clp;
8278 struct nfs41_reclaim_complete_args arg;
8279 struct nfs41_reclaim_complete_res res;
8280};
8281
8282static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8283{
8284 struct nfs4_reclaim_complete_data *calldata = data;
8285
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008286 nfs41_setup_sequence(calldata->clp->cl_session,
8287 &calldata->arg.seq_args,
8288 &calldata->res.seq_res,
8289 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008290}
8291
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008292static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8293{
8294 switch(task->tk_status) {
8295 case 0:
8296 case -NFS4ERR_COMPLETE_ALREADY:
8297 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8298 break;
8299 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008300 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008301 /* fall through */
8302 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008303 return -EAGAIN;
8304 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008305 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008306 }
8307 return 0;
8308}
8309
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008310static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8311{
8312 struct nfs4_reclaim_complete_data *calldata = data;
8313 struct nfs_client *clp = calldata->clp;
8314 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8315
8316 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008317 if (!nfs41_sequence_done(task, res))
8318 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008319
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008320 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008321 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8322 rpc_restart_call_prepare(task);
8323 return;
8324 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008325 dprintk("<-- %s\n", __func__);
8326}
8327
8328static void nfs4_free_reclaim_complete_data(void *data)
8329{
8330 struct nfs4_reclaim_complete_data *calldata = data;
8331
8332 kfree(calldata);
8333}
8334
8335static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8336 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8337 .rpc_call_done = nfs4_reclaim_complete_done,
8338 .rpc_release = nfs4_free_reclaim_complete_data,
8339};
8340
8341/*
8342 * Issue a global reclaim complete.
8343 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008344static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8345 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008346{
8347 struct nfs4_reclaim_complete_data *calldata;
8348 struct rpc_task *task;
8349 struct rpc_message msg = {
8350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008351 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008352 };
8353 struct rpc_task_setup task_setup_data = {
8354 .rpc_client = clp->cl_rpcclient,
8355 .rpc_message = &msg,
8356 .callback_ops = &nfs4_reclaim_complete_call_ops,
8357 .flags = RPC_TASK_ASYNC,
8358 };
8359 int status = -ENOMEM;
8360
8361 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008362 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008363 if (calldata == NULL)
8364 goto out;
8365 calldata->clp = clp;
8366 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008367
Chuck Levera9c92d62013-08-09 12:48:18 -04008368 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008369 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008370 msg.rpc_argp = &calldata->arg;
8371 msg.rpc_resp = &calldata->res;
8372 task_setup_data.callback_data = calldata;
8373 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008374 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008375 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008376 goto out;
8377 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008378 status = nfs4_wait_for_completion_rpc_task(task);
8379 if (status == 0)
8380 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008381 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008382 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008383out:
8384 dprintk("<-- %s status=%d\n", __func__, status);
8385 return status;
8386}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008387
8388static void
8389nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8390{
8391 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008392 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008393 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008394
8395 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008396 nfs41_setup_sequence(session, &lgp->args.seq_args,
8397 &lgp->res.seq_res, task);
8398 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008399}
8400
8401static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8402{
8403 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008404
8405 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008406 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008407 dprintk("<-- %s\n", __func__);
8408}
8409
8410static int
8411nfs4_layoutget_handle_exception(struct rpc_task *task,
8412 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8413{
Trond Myklebustee314c22012-10-01 17:25:48 -07008414 struct inode *inode = lgp->args.inode;
8415 struct nfs_server *server = NFS_SERVER(inode);
8416 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008417 int nfs4err = task->tk_status;
8418 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008419 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008420
Boaz Harroshed7e5422014-01-22 20:34:54 +02008421 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008422
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008423 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008424 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008425 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008426
8427 /*
8428 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8429 * on the file. set tk_status to -ENODATA to tell upper layer to
8430 * retry go inband.
8431 */
8432 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008433 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008434 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008435 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008436 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8437 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8438 */
8439 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008440 status = -EOVERFLOW;
8441 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008442 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008443 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008444 * (or clients) writing to the same RAID stripe except when
8445 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008446 *
8447 * Treat it like we would RECALLCONFLICT -- we retry for a little
8448 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008449 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008450 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008451 if (lgp->args.minlength == 0) {
8452 status = -EOVERFLOW;
8453 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008454 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008455 status = -EBUSY;
8456 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008457 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008458 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008459 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008460 case -NFS4ERR_DELEG_REVOKED:
8461 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008462 case -NFS4ERR_EXPIRED:
8463 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008464 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008465 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008466 lo = NFS_I(inode)->layout;
8467 /* If the open stateid was bad, then recover it. */
8468 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8469 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008470 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008471 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008472 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008473 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008474 break;
8475 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008476
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008477 /*
8478 * Mark the bad layout state as invalid, then retry
8479 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008480 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008481 spin_unlock(&inode->i_lock);
8482 pnfs_free_lseg_list(&head);
8483 status = -EAGAIN;
8484 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008485 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008486
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008487 err = nfs4_handle_exception(server, nfs4err, exception);
8488 if (!status) {
8489 if (exception->retry)
8490 status = -EAGAIN;
8491 else
8492 status = err;
8493 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008494out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008495 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008496 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008497}
8498
Idan Kedar85541162012-08-02 11:47:10 +03008499static size_t max_response_pages(struct nfs_server *server)
8500{
8501 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8502 return nfs_page_array_len(0, max_resp_sz);
8503}
8504
8505static void nfs4_free_pages(struct page **pages, size_t size)
8506{
8507 int i;
8508
8509 if (!pages)
8510 return;
8511
8512 for (i = 0; i < size; i++) {
8513 if (!pages[i])
8514 break;
8515 __free_page(pages[i]);
8516 }
8517 kfree(pages);
8518}
8519
8520static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8521{
8522 struct page **pages;
8523 int i;
8524
8525 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8526 if (!pages) {
8527 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8528 return NULL;
8529 }
8530
8531 for (i = 0; i < size; i++) {
8532 pages[i] = alloc_page(gfp_flags);
8533 if (!pages[i]) {
8534 dprintk("%s: failed to allocate page\n", __func__);
8535 nfs4_free_pages(pages, size);
8536 return NULL;
8537 }
8538 }
8539
8540 return pages;
8541}
8542
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008543static void nfs4_layoutget_release(void *calldata)
8544{
8545 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008546 struct inode *inode = lgp->args.inode;
8547 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008548 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008549
8550 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008551 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008552 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008553 put_nfs_open_context(lgp->args.ctx);
8554 kfree(calldata);
8555 dprintk("<-- %s\n", __func__);
8556}
8557
8558static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8559 .rpc_call_prepare = nfs4_layoutget_prepare,
8560 .rpc_call_done = nfs4_layoutget_done,
8561 .rpc_release = nfs4_layoutget_release,
8562};
8563
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008564struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008565nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008566{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008567 struct inode *inode = lgp->args.inode;
8568 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008569 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008570 struct rpc_task *task;
8571 struct rpc_message msg = {
8572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8573 .rpc_argp = &lgp->args,
8574 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008575 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008576 };
8577 struct rpc_task_setup task_setup_data = {
8578 .rpc_client = server->client,
8579 .rpc_message = &msg,
8580 .callback_ops = &nfs4_layoutget_call_ops,
8581 .callback_data = lgp,
8582 .flags = RPC_TASK_ASYNC,
8583 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008584 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008585 struct nfs4_exception exception = {
8586 .inode = inode,
8587 .timeout = *timeout,
8588 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008589 int status = 0;
8590
8591 dprintk("--> %s\n", __func__);
8592
Peng Tao4bd5a9802014-11-17 11:05:17 +08008593 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8594 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8595
Idan Kedar85541162012-08-02 11:47:10 +03008596 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8597 if (!lgp->args.layout.pages) {
8598 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008599 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008600 }
8601 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8602
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008603 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008604 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008605 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008606
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607 task = rpc_run_task(&task_setup_data);
8608 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008609 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008610 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008611 if (status == 0) {
8612 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8613 *timeout = exception.timeout;
8614 }
8615
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008616 trace_nfs4_layoutget(lgp->args.ctx,
8617 &lgp->args.range,
8618 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008619 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008620 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008621
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008622 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8623 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008624 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008625 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008626 rpc_put_task(task);
8627 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008628 if (status)
8629 return ERR_PTR(status);
8630 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008631}
8632
Benny Halevycbe82602011-05-22 19:52:37 +03008633static void
8634nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8635{
8636 struct nfs4_layoutreturn *lrp = calldata;
8637
8638 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008639 nfs41_setup_sequence(lrp->clp->cl_session,
8640 &lrp->args.seq_args,
8641 &lrp->res.seq_res,
8642 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008643}
8644
8645static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8646{
8647 struct nfs4_layoutreturn *lrp = calldata;
8648 struct nfs_server *server;
8649
8650 dprintk("--> %s\n", __func__);
8651
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008652 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008653 return;
8654
8655 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008656 switch (task->tk_status) {
8657 default:
8658 task->tk_status = 0;
8659 case 0:
8660 break;
8661 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008662 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008663 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008664 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008665 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008666 return;
8667 }
Benny Halevycbe82602011-05-22 19:52:37 +03008668 dprintk("<-- %s\n", __func__);
8669}
8670
8671static void nfs4_layoutreturn_release(void *calldata)
8672{
8673 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008674 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008675
8676 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008677 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008678 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008679 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008680 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8681 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008682 pnfs_put_layout_hdr(lrp->args.layout);
8683 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008684 kfree(calldata);
8685 dprintk("<-- %s\n", __func__);
8686}
8687
8688static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8689 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8690 .rpc_call_done = nfs4_layoutreturn_done,
8691 .rpc_release = nfs4_layoutreturn_release,
8692};
8693
Peng Tao6c166052014-11-17 09:30:40 +08008694int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008695{
8696 struct rpc_task *task;
8697 struct rpc_message msg = {
8698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8699 .rpc_argp = &lrp->args,
8700 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008701 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008702 };
8703 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008704 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008705 .rpc_message = &msg,
8706 .callback_ops = &nfs4_layoutreturn_call_ops,
8707 .callback_data = lrp,
8708 };
Peng Tao6c166052014-11-17 09:30:40 +08008709 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008710
Andrew Elble99ade3c2015-12-02 09:39:51 -05008711 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8712 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8713 &task_setup_data.rpc_client, &msg);
8714
Benny Halevycbe82602011-05-22 19:52:37 +03008715 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008716 if (!sync) {
8717 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8718 if (!lrp->inode) {
8719 nfs4_layoutreturn_release(lrp);
8720 return -EAGAIN;
8721 }
8722 task_setup_data.flags |= RPC_TASK_ASYNC;
8723 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008724 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008725 task = rpc_run_task(&task_setup_data);
8726 if (IS_ERR(task))
8727 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008728 if (sync)
8729 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008730 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008731 dprintk("<-- %s status=%d\n", __func__, status);
8732 rpc_put_task(task);
8733 return status;
8734}
8735
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008736static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008737_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8738 struct pnfs_device *pdev,
8739 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740{
8741 struct nfs4_getdeviceinfo_args args = {
8742 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008743 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8744 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008745 };
8746 struct nfs4_getdeviceinfo_res res = {
8747 .pdev = pdev,
8748 };
8749 struct rpc_message msg = {
8750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8751 .rpc_argp = &args,
8752 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008753 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008754 };
8755 int status;
8756
8757 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008758 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008759 if (res.notification & ~args.notify_types)
8760 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008761 if (res.notification != args.notify_types)
8762 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008763
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008764 dprintk("<-- %s status=%d\n", __func__, status);
8765
8766 return status;
8767}
8768
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008769int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8770 struct pnfs_device *pdev,
8771 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008772{
8773 struct nfs4_exception exception = { };
8774 int err;
8775
8776 do {
8777 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008778 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008779 &exception);
8780 } while (exception.retry);
8781 return err;
8782}
8783EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8784
Andy Adamson863a3c62011-03-23 13:27:54 +00008785static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8786{
8787 struct nfs4_layoutcommit_data *data = calldata;
8788 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008789 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008790
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008791 nfs41_setup_sequence(session,
8792 &data->args.seq_args,
8793 &data->res.seq_res,
8794 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008795}
8796
8797static void
8798nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8799{
8800 struct nfs4_layoutcommit_data *data = calldata;
8801 struct nfs_server *server = NFS_SERVER(data->args.inode);
8802
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008803 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008804 return;
8805
8806 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008807 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8808 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8809 case -NFS4ERR_BADLAYOUT: /* no layout */
8810 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008811 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008812 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008813 break;
8814 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008815 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008816 rpc_restart_call_prepare(task);
8817 return;
8818 }
8819 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008820}
8821
8822static void nfs4_layoutcommit_release(void *calldata)
8823{
8824 struct nfs4_layoutcommit_data *data = calldata;
8825
Andy Adamsondb29c082011-07-30 20:52:38 -04008826 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008827 nfs_post_op_update_inode_force_wcc(data->args.inode,
8828 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008829 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008830 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008831 kfree(data);
8832}
8833
8834static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8835 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8836 .rpc_call_done = nfs4_layoutcommit_done,
8837 .rpc_release = nfs4_layoutcommit_release,
8838};
8839
8840int
Andy Adamsonef311532011-03-12 02:58:10 -05008841nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008842{
8843 struct rpc_message msg = {
8844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8845 .rpc_argp = &data->args,
8846 .rpc_resp = &data->res,
8847 .rpc_cred = data->cred,
8848 };
8849 struct rpc_task_setup task_setup_data = {
8850 .task = &data->task,
8851 .rpc_client = NFS_CLIENT(data->args.inode),
8852 .rpc_message = &msg,
8853 .callback_ops = &nfs4_layoutcommit_ops,
8854 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008855 };
8856 struct rpc_task *task;
8857 int status = 0;
8858
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008859 dprintk("NFS: initiating layoutcommit call. sync %d "
8860 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008861 data->args.lastbytewritten,
8862 data->args.inode->i_ino);
8863
Trond Myklebust472e2592015-02-05 16:50:30 -05008864 if (!sync) {
8865 data->inode = nfs_igrab_and_active(data->args.inode);
8866 if (data->inode == NULL) {
8867 nfs4_layoutcommit_release(data);
8868 return -EAGAIN;
8869 }
8870 task_setup_data.flags = RPC_TASK_ASYNC;
8871 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008873 task = rpc_run_task(&task_setup_data);
8874 if (IS_ERR(task))
8875 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008876 if (sync)
8877 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008878 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008879 dprintk("%s: status %d\n", __func__, status);
8880 rpc_put_task(task);
8881 return status;
8882}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008883
Andy Adamson97431202013-08-08 10:57:56 -04008884/**
8885 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8886 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8887 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888static int
8889_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008890 struct nfs_fsinfo *info,
8891 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008892{
8893 struct nfs41_secinfo_no_name_args args = {
8894 .style = SECINFO_STYLE_CURRENT_FH,
8895 };
8896 struct nfs4_secinfo_res res = {
8897 .flavors = flavors,
8898 };
8899 struct rpc_message msg = {
8900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8901 .rpc_argp = &args,
8902 .rpc_resp = &res,
8903 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008904 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008905 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008906 int status;
8907
8908 if (use_integrity) {
8909 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008910 cred = nfs4_get_clid_cred(server->nfs_client);
8911 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008912 }
8913
8914 dprintk("--> %s\n", __func__);
8915 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8916 &res.seq_res, 0);
8917 dprintk("<-- %s status=%d\n", __func__, status);
8918
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008919 if (cred)
8920 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008921
8922 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008923}
8924
8925static int
8926nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8927 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8928{
8929 struct nfs4_exception exception = { };
8930 int err;
8931 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008932 /* first try using integrity protection */
8933 err = -NFS4ERR_WRONGSEC;
8934
8935 /* try to use integrity protection with machine cred */
8936 if (_nfs4_is_integrity_protected(server->nfs_client))
8937 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8938 flavors, true);
8939
8940 /*
8941 * if unable to use integrity protection, or SECINFO with
8942 * integrity protection returns NFS4ERR_WRONGSEC (which is
8943 * disallowed by spec, but exists in deployed servers) use
8944 * the current filesystem's rpc_client and the user cred.
8945 */
8946 if (err == -NFS4ERR_WRONGSEC)
8947 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8948 flavors, false);
8949
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008950 switch (err) {
8951 case 0:
8952 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008953 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008954 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008955 default:
8956 err = nfs4_handle_exception(server, err, &exception);
8957 }
8958 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008959out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008960 return err;
8961}
8962
8963static int
8964nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8965 struct nfs_fsinfo *info)
8966{
8967 int err;
8968 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008969 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008970 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008971 struct nfs4_secinfo4 *secinfo;
8972 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008973
8974 page = alloc_page(GFP_KERNEL);
8975 if (!page) {
8976 err = -ENOMEM;
8977 goto out;
8978 }
8979
8980 flavors = page_address(page);
8981 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8982
8983 /*
8984 * Fall back on "guess and check" method if
8985 * the server doesn't support SECINFO_NO_NAME
8986 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008987 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008988 err = nfs4_find_root_sec(server, fhandle, info);
8989 goto out_freepage;
8990 }
8991 if (err)
8992 goto out_freepage;
8993
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008994 for (i = 0; i < flavors->num_flavors; i++) {
8995 secinfo = &flavors->flavors[i];
8996
8997 switch (secinfo->flavor) {
8998 case RPC_AUTH_NULL:
8999 case RPC_AUTH_UNIX:
9000 case RPC_AUTH_GSS:
9001 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9002 &secinfo->flavor_info);
9003 break;
9004 default:
9005 flavor = RPC_AUTH_MAXFLAVOR;
9006 break;
9007 }
9008
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009009 if (!nfs_auth_info_match(&server->auth_info, flavor))
9010 flavor = RPC_AUTH_MAXFLAVOR;
9011
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009012 if (flavor != RPC_AUTH_MAXFLAVOR) {
9013 err = nfs4_lookup_root_sec(server, fhandle,
9014 info, flavor);
9015 if (!err)
9016 break;
9017 }
9018 }
9019
9020 if (flavor == RPC_AUTH_MAXFLAVOR)
9021 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009022
9023out_freepage:
9024 put_page(page);
9025 if (err == -EACCES)
9026 return -EPERM;
9027out:
9028 return err;
9029}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009030
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009031static int _nfs41_test_stateid(struct nfs_server *server,
9032 nfs4_stateid *stateid,
9033 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009034{
9035 int status;
9036 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009037 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009038 };
9039 struct nfs41_test_stateid_res res;
9040 struct rpc_message msg = {
9041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9042 .rpc_argp = &args,
9043 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009044 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009045 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009046 struct rpc_clnt *rpc_client = server->client;
9047
9048 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9049 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009050
Chuck Lever38527b12012-07-11 16:30:23 -04009051 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009052 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009053 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009054 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009055 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009056 if (status != NFS_OK) {
9057 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009058 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009059 }
9060 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009061 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009062}
9063
Trond Myklebust43912bb2016-09-22 13:38:56 -04009064static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9065 int err, struct nfs4_exception *exception)
9066{
9067 exception->retry = 0;
9068 switch(err) {
9069 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009070 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009071 nfs4_handle_exception(server, err, exception);
9072 break;
9073 case -NFS4ERR_BADSESSION:
9074 case -NFS4ERR_BADSLOT:
9075 case -NFS4ERR_BAD_HIGH_SLOT:
9076 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9077 case -NFS4ERR_DEADSESSION:
9078 nfs4_do_handle_exception(server, err, exception);
9079 }
9080}
9081
Chuck Lever38527b12012-07-11 16:30:23 -04009082/**
9083 * nfs41_test_stateid - perform a TEST_STATEID operation
9084 *
9085 * @server: server / transport on which to perform the operation
9086 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009087 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009088 *
9089 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9090 * Otherwise a negative NFS4ERR value is returned if the operation
9091 * failed or the state ID is not currently valid.
9092 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009093static int nfs41_test_stateid(struct nfs_server *server,
9094 nfs4_stateid *stateid,
9095 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009096{
9097 struct nfs4_exception exception = { };
9098 int err;
9099 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009100 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009101 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009102 } while (exception.retry);
9103 return err;
9104}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009105
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009106struct nfs_free_stateid_data {
9107 struct nfs_server *server;
9108 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009109 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110};
9111
9112static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9113{
9114 struct nfs_free_stateid_data *data = calldata;
9115 nfs41_setup_sequence(nfs4_get_session(data->server),
9116 &data->args.seq_args,
9117 &data->res.seq_res,
9118 task);
9119}
9120
9121static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9122{
9123 struct nfs_free_stateid_data *data = calldata;
9124
9125 nfs41_sequence_done(task, &data->res.seq_res);
9126
9127 switch (task->tk_status) {
9128 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009129 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009130 rpc_restart_call_prepare(task);
9131 }
9132}
9133
9134static void nfs41_free_stateid_release(void *calldata)
9135{
9136 kfree(calldata);
9137}
9138
Trond Myklebust17f26b12013-08-21 15:48:42 -04009139static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009140 .rpc_call_prepare = nfs41_free_stateid_prepare,
9141 .rpc_call_done = nfs41_free_stateid_done,
9142 .rpc_release = nfs41_free_stateid_release,
9143};
9144
9145static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009146 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009147 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009148 bool privileged)
9149{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009150 struct rpc_message msg = {
9151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009152 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009153 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009154 struct rpc_task_setup task_setup = {
9155 .rpc_client = server->client,
9156 .rpc_message = &msg,
9157 .callback_ops = &nfs41_free_stateid_ops,
9158 .flags = RPC_TASK_ASYNC,
9159 };
9160 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009161
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009162 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9163 &task_setup.rpc_client, &msg);
9164
Chuck Lever38527b12012-07-11 16:30:23 -04009165 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009166 data = kmalloc(sizeof(*data), GFP_NOFS);
9167 if (!data)
9168 return ERR_PTR(-ENOMEM);
9169 data->server = server;
9170 nfs4_stateid_copy(&data->args.stateid, stateid);
9171
9172 task_setup.callback_data = data;
9173
9174 msg.rpc_argp = &data->args;
9175 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009176 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009177 if (privileged)
9178 nfs4_set_sequence_privileged(&data->args.seq_args);
9179
9180 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009181}
9182
Chuck Lever38527b12012-07-11 16:30:23 -04009183/**
9184 * nfs41_free_stateid - perform a FREE_STATEID operation
9185 *
9186 * @server: server / transport on which to perform the operation
9187 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009188 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009189 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009190 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009191 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009192 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009193static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009194 const nfs4_stateid *stateid,
9195 struct rpc_cred *cred,
9196 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009197{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009198 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009199
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009200 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009201 if (IS_ERR(task))
9202 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009203 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009204 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009205}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009206
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009207static void
9208nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009209{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009210 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009211
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009212 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009213 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009214}
9215
Trond Myklebust36281ca2012-03-04 18:13:56 -05009216static bool nfs41_match_stateid(const nfs4_stateid *s1,
9217 const nfs4_stateid *s2)
9218{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009219 if (s1->type != s2->type)
9220 return false;
9221
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009222 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009223 return false;
9224
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009225 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009226 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009227 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009228 return true;
9229
9230 return false;
9231}
9232
Andy Adamson557134a2009-04-01 09:21:53 -04009233#endif /* CONFIG_NFS_V4_1 */
9234
Trond Myklebust36281ca2012-03-04 18:13:56 -05009235static bool nfs4_match_stateid(const nfs4_stateid *s1,
9236 const nfs4_stateid *s2)
9237{
Trond Myklebustf597c532012-03-04 18:13:56 -05009238 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009239}
9240
9241
Trond Myklebust17280172012-03-11 13:11:00 -04009242static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009243 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009244 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 .recover_open = nfs4_open_reclaim,
9246 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009247 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009248 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249};
9250
Andy Adamson591d71c2009-04-01 09:22:47 -04009251#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009252static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009253 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9254 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9255 .recover_open = nfs4_open_reclaim,
9256 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009257 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009258 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009259 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009260};
9261#endif /* CONFIG_NFS_V4_1 */
9262
Trond Myklebust17280172012-03-11 13:11:00 -04009263static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009264 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009265 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009266 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009268 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269};
9270
Andy Adamson591d71c2009-04-01 09:22:47 -04009271#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009272static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009273 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9274 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009275 .recover_open = nfs41_open_expired,
9276 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009277 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009278};
9279#endif /* CONFIG_NFS_V4_1 */
9280
Trond Myklebust17280172012-03-11 13:11:00 -04009281static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009282 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009283 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009284 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009285};
9286
9287#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009288static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009289 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009290 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009291 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009292};
9293#endif
9294
Chuck Leverec011fe2013-10-17 14:12:39 -04009295static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009296 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009297 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009298};
9299
9300#if defined(CONFIG_NFS_V4_1)
9301static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009302 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009303 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009304};
9305#endif /* CONFIG_NFS_V4_1 */
9306
Trond Myklebust97dc1352010-06-16 09:52:26 -04009307static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9308 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009309 .init_caps = NFS_CAP_READDIRPLUS
9310 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009311 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009312 .init_client = nfs40_init_client,
9313 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009314 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009315 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009316 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009317 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009318 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009319 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009320 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9321 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9322 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009323 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009324};
9325
9326#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009327static struct nfs_seqid *
9328nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9329{
9330 return NULL;
9331}
9332
Trond Myklebust97dc1352010-06-16 09:52:26 -04009333static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9334 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009335 .init_caps = NFS_CAP_READDIRPLUS
9336 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009337 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009338 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009339 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009340 .init_client = nfs41_init_client,
9341 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009342 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009343 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009344 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009345 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009346 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009347 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009348 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009349 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9350 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9351 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009352 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009353};
9354#endif
9355
Steve Dickson42c2c422013-05-22 12:50:38 -04009356#if defined(CONFIG_NFS_V4_2)
9357static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9358 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009359 .init_caps = NFS_CAP_READDIRPLUS
9360 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009361 | NFS_CAP_POSIX_LOCK
9362 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009363 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009364 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009365 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009366 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009367 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009368 | NFS_CAP_LAYOUTSTATS
9369 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009370 .init_client = nfs41_init_client,
9371 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009372 .match_stateid = nfs41_match_stateid,
9373 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009374 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009375 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009376 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009377 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009378 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009379 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9380 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9381 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009382 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009383};
9384#endif
9385
Trond Myklebust97dc1352010-06-16 09:52:26 -04009386const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9387 [0] = &nfs_v4_0_minor_ops,
9388#if defined(CONFIG_NFS_V4_1)
9389 [1] = &nfs_v4_1_minor_ops,
9390#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009391#if defined(CONFIG_NFS_V4_2)
9392 [2] = &nfs_v4_2_minor_ops,
9393#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009394};
9395
Trond Myklebust13997822016-07-24 17:10:52 -04009396static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009397{
9398 ssize_t error, error2;
9399
9400 error = generic_listxattr(dentry, list, size);
9401 if (error < 0)
9402 return error;
9403 if (list) {
9404 list += error;
9405 size -= error;
9406 }
9407
9408 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9409 if (error2 < 0)
9410 return error2;
9411 return error + error2;
9412}
9413
Trond Myklebust17f26b12013-08-21 15:48:42 -04009414static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009415 .create = nfs_create,
9416 .lookup = nfs_lookup,
9417 .atomic_open = nfs_atomic_open,
9418 .link = nfs_link,
9419 .unlink = nfs_unlink,
9420 .symlink = nfs_symlink,
9421 .mkdir = nfs_mkdir,
9422 .rmdir = nfs_rmdir,
9423 .mknod = nfs_mknod,
9424 .rename = nfs_rename,
9425 .permission = nfs_permission,
9426 .getattr = nfs_getattr,
9427 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009428 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009429};
9430
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009431static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009432 .permission = nfs_permission,
9433 .getattr = nfs_getattr,
9434 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009435 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009436};
9437
David Howells509de812006-08-22 20:06:11 -04009438const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 .version = 4, /* protocol version */
9440 .dentry_ops = &nfs4_dentry_operations,
9441 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009442 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009443 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009445 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009446 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 .getattr = nfs4_proc_getattr,
9448 .setattr = nfs4_proc_setattr,
9449 .lookup = nfs4_proc_lookup,
9450 .access = nfs4_proc_access,
9451 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 .create = nfs4_proc_create,
9453 .remove = nfs4_proc_remove,
9454 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009455 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009457 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009458 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009459 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 .link = nfs4_proc_link,
9461 .symlink = nfs4_proc_symlink,
9462 .mkdir = nfs4_proc_mkdir,
9463 .rmdir = nfs4_proc_remove,
9464 .readdir = nfs4_proc_readdir,
9465 .mknod = nfs4_proc_mknod,
9466 .statfs = nfs4_proc_statfs,
9467 .fsinfo = nfs4_proc_fsinfo,
9468 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009469 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009471 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009473 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009475 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009477 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009478 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009480 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009481 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009482 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009483 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009484 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009485 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009486 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009487 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009488 .create_server = nfs4_create_server,
9489 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490};
9491
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009492static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009493 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009494 .list = nfs4_xattr_list_nfs4_acl,
9495 .get = nfs4_xattr_get_nfs4_acl,
9496 .set = nfs4_xattr_set_nfs4_acl,
9497};
9498
9499const struct xattr_handler *nfs4_xattr_handlers[] = {
9500 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009501#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9502 &nfs4_xattr_nfs4_label_handler,
9503#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009504 NULL
9505};
9506
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507/*
9508 * Local variables:
9509 * c-basic-offset: 8
9510 * End:
9511 */