blob: f62d58cb30145a6f0b82abbe2ca3d8cc3adfb27a [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
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500950static int nfs4_setup_sequence(const struct nfs_client *client,
Chuck Lever5a580e02013-08-09 12:48:09 -0400951 struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400954{
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500955 struct nfs4_session *session = nfs4_get_session(client);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400956 int ret = 0;
957
Chuck Lever3bd23842013-08-09 12:49:19 -0400958 if (!session)
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500959 return nfs40_setup_sequence(client->cl_slot_tbl,
Peng Taocb04ad22014-06-11 05:24:15 +0800960 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;
Anna Schumaker172d9de2015-05-14 10:56:10 -0400975 struct nfs4_session *session = nfs4_get_session(data->seq_server->nfs_client);
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
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500996static int nfs4_setup_sequence(const struct nfs_client *client,
Chuck Lever5a580e02013-08-09 12:48:09 -0400997 struct nfs4_sequence_args *args,
998 struct nfs4_sequence_res *res,
999 struct rpc_task *task)
1000{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001001 return nfs40_setup_sequence(client->cl_slot_tbl, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001002}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001003
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001004static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1005{
1006 return nfs40_sequence_done(task, res);
1007}
1008
1009static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1010{
1011 if (res->sr_slot != NULL)
1012 nfs40_sequence_free_slot(res);
1013}
1014
Peng Tao2c4b1312014-06-11 05:24:16 +08001015int nfs4_sequence_done(struct rpc_task *task,
1016 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001017{
Chuck Lever3bd23842013-08-09 12:49:19 -04001018 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001019}
Peng Tao2c4b1312014-06-11 05:24:16 +08001020EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001021
1022#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001023
Chuck Lever9915ea72013-08-09 12:48:27 -04001024static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025{
1026 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001027 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001028 data->seq_args, data->seq_res, task);
1029}
1030
1031static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032{
1033 struct nfs4_call_sync_data *data = calldata;
1034 nfs4_sequence_done(task, data->seq_res);
1035}
1036
1037static const struct rpc_call_ops nfs40_call_sync_ops = {
1038 .rpc_call_prepare = nfs40_call_sync_prepare,
1039 .rpc_call_done = nfs40_call_sync_done,
1040};
1041
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001043 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001047{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 int ret;
1049 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001050 struct nfs_client *clp = server->nfs_client;
1051 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .seq_server = server,
1053 .seq_args = args,
1054 .seq_res = res,
1055 };
1056 struct rpc_task_setup task_setup = {
1057 .rpc_client = clnt,
1058 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001059 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060 .callback_data = &data
1061 };
1062
1063 task = rpc_run_task(&task_setup);
1064 if (IS_ERR(task))
1065 ret = PTR_ERR(task);
1066 else {
1067 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001068 rpc_put_task(task);
1069 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 return ret;
1071}
1072
Bryan Schumaker7c513052011-03-24 17:12:24 +00001073int nfs4_call_sync(struct rpc_clnt *clnt,
1074 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001075 struct rpc_message *msg,
1076 struct nfs4_sequence_args *args,
1077 struct nfs4_sequence_res *res,
1078 int cache_reply)
1079{
Chuck Levera9c92d62013-08-09 12:48:18 -04001080 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001081 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001084static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1085 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
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 Myklebustd3129ef2017-01-11 22:07:28 -05001101 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001102 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001103 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 spin_unlock(&dir->i_lock);
1105}
1106
1107struct nfs4_opendata {
1108 struct kref kref;
1109 struct nfs_openargs o_arg;
1110 struct nfs_openres o_res;
1111 struct nfs_open_confirmargs c_arg;
1112 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001113 struct nfs4_string owner_name;
1114 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001115 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001119 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct nfs4_state_owner *owner;
1121 struct nfs4_state *state;
1122 struct iattr attrs;
1123 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001124 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001125 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001126 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001127 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int cancelled;
1129};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001130
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001131static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1132 int err, struct nfs4_exception *exception)
1133{
1134 if (err != -EINVAL)
1135 return false;
1136 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1137 return false;
1138 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1139 exception->retry = 1;
1140 return true;
1141}
1142
Trond Myklebust6ae37332015-01-30 14:21:14 -05001143static u32
1144nfs4_map_atomic_open_share(struct nfs_server *server,
1145 fmode_t fmode, int openflags)
1146{
1147 u32 res = 0;
1148
1149 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1150 case FMODE_READ:
1151 res = NFS4_SHARE_ACCESS_READ;
1152 break;
1153 case FMODE_WRITE:
1154 res = NFS4_SHARE_ACCESS_WRITE;
1155 break;
1156 case FMODE_READ|FMODE_WRITE:
1157 res = NFS4_SHARE_ACCESS_BOTH;
1158 }
1159 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1160 goto out;
1161 /* Want no delegation if we're using O_DIRECT */
1162 if (openflags & O_DIRECT)
1163 res |= NFS4_SHARE_WANT_NO_DELEG;
1164out:
1165 return res;
1166}
1167
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001168static enum open_claim_type4
1169nfs4_map_atomic_open_claim(struct nfs_server *server,
1170 enum open_claim_type4 claim)
1171{
1172 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1173 return claim;
1174 switch (claim) {
1175 default:
1176 return claim;
1177 case NFS4_OPEN_CLAIM_FH:
1178 return NFS4_OPEN_CLAIM_NULL;
1179 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1180 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1181 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1182 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1183 }
1184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187{
1188 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001189 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 p->o_res.seqid = p->o_arg.seqid;
1191 p->c_res.seqid = p->c_arg.seqid;
1192 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001193 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001195 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196}
1197
Al Viro82a2c1b2011-06-22 18:30:55 -04001198static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001200 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001201 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001202 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001203 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204{
Al Viro82a2c1b2011-06-22 18:30:55 -04001205 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001206 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001208 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001209 struct nfs4_opendata *p;
1210
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001211 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 if (p == NULL)
1213 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001214
1215 p->f_label = nfs4_label_alloc(server, gfp_mask);
1216 if (IS_ERR(p->f_label))
1217 goto err_free_p;
1218
Kinglong Meea49c2692015-07-27 15:31:38 +08001219 p->a_label = nfs4_label_alloc(server, gfp_mask);
1220 if (IS_ERR(p->a_label))
1221 goto err_free_f;
1222
Trond Myklebust63f5f792015-01-23 19:19:25 -05001223 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1224 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001225 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001226 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001227 nfs_sb_active(dentry->d_sb);
1228 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001229 p->dir = parent;
1230 p->owner = sp;
1231 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001232 p->o_arg.open_flags = flags;
1233 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001234 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001236 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1237 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001238 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1239 * will return permission denied for all bits until close */
1240 if (!(flags & O_EXCL)) {
1241 /* ask server to check for all possible rights as results
1242 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001243 switch (p->o_arg.claim) {
1244 default:
1245 break;
1246 case NFS4_OPEN_CLAIM_NULL:
1247 case NFS4_OPEN_CLAIM_FH:
1248 p->o_arg.access = NFS4_ACCESS_READ |
1249 NFS4_ACCESS_MODIFY |
1250 NFS4_ACCESS_EXTEND |
1251 NFS4_ACCESS_EXECUTE;
1252 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001253 }
David Howells7539bba2006-08-22 20:06:09 -04001254 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001255 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1256 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001257 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001259 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001260 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001261 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001262 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001263 case NFS4_OPEN_CLAIM_NULL:
1264 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1265 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1266 p->o_arg.fh = NFS_FH(dir);
1267 break;
1268 case NFS4_OPEN_CLAIM_PREVIOUS:
1269 case NFS4_OPEN_CLAIM_FH:
1270 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1271 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001272 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001273 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001274 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001275 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001276
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 p->o_arg.u.attrs = &p->attrs;
1278 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001279
1280 verf[0] = jiffies;
1281 verf[1] = current->pid;
1282 memcpy(p->o_arg.u.verifier.data, verf,
1283 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285 p->c_arg.fh = &p->o_res.fh;
1286 p->c_arg.stateid = &p->o_res.stateid;
1287 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001288 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001291
1292err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001293 nfs4_label_free(p->a_label);
1294err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001295 nfs4_label_free(p->f_label);
1296err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001297 kfree(p);
1298err:
1299 dput(parent);
1300 return NULL;
1301}
1302
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001303static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 struct nfs4_opendata *p = container_of(kref,
1306 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001307 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001308
1309 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001310 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001311 if (p->state != NULL)
1312 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001313 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001314
Kinglong Meea49c2692015-07-27 15:31:38 +08001315 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001316 nfs4_label_free(p->f_label);
1317
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001319 dput(p->dentry);
1320 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001321 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001322 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 kfree(p);
1324}
1325
1326static void nfs4_opendata_put(struct nfs4_opendata *p)
1327{
1328 if (p != NULL)
1329 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330}
1331
Trond Myklebust06f814a2006-01-03 09:55:07 +01001332static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1333{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001334 int ret;
1335
Trond Myklebust06f814a2006-01-03 09:55:07 +01001336 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001337 return ret;
1338}
1339
Trond Myklebust24311f82015-09-20 10:50:17 -04001340static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1341 fmode_t fmode)
1342{
1343 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1344 case FMODE_READ|FMODE_WRITE:
1345 return state->n_rdwr != 0;
1346 case FMODE_WRITE:
1347 return state->n_wronly != 0;
1348 case FMODE_READ:
1349 return state->n_rdonly != 0;
1350 }
1351 WARN_ON_ONCE(1);
1352 return false;
1353}
1354
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001356{
1357 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358
Trond Myklebust536e43d2012-01-17 22:04:26 -05001359 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360 goto out;
1361 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001362 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001363 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1364 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 break;
1366 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001367 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1368 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 break;
1370 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001371 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1372 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001373 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001374out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 return ret;
1376}
1377
Trond Myklebust2a606182015-08-19 22:30:00 -05001378static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1379 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001381 if (delegation == NULL)
1382 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001385 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1386 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001387 switch (claim) {
1388 case NFS4_OPEN_CLAIM_NULL:
1389 case NFS4_OPEN_CLAIM_FH:
1390 break;
1391 case NFS4_OPEN_CLAIM_PREVIOUS:
1392 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1393 break;
1394 default:
1395 return 0;
1396 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001397 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001398 return 1;
1399}
1400
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001402{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001403 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001404 case FMODE_WRITE:
1405 state->n_wronly++;
1406 break;
1407 case FMODE_READ:
1408 state->n_rdonly++;
1409 break;
1410 case FMODE_READ|FMODE_WRITE:
1411 state->n_rdwr++;
1412 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001413 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001414}
1415
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001416#ifdef CONFIG_NFS_V4_1
1417static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1418{
1419 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1420 return true;
1421 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1422 return true;
1423 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1424 return true;
1425 return false;
1426}
1427#endif /* CONFIG_NFS_V4_1 */
1428
Trond Myklebust4f14c192014-02-12 19:15:06 -05001429static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001430{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001431 struct nfs_client *clp = state->owner->so_server->nfs_client;
1432 bool need_recover = false;
1433
1434 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1435 need_recover = true;
1436 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1437 need_recover = true;
1438 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1439 need_recover = true;
1440 if (need_recover)
1441 nfs4_state_mark_reclaim_nograce(clp, state);
1442}
1443
Trond Myklebuste999e802014-02-10 18:20:47 -05001444static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001445 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001446{
1447 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1448 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001449 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001450 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001451 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001452 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001453 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001454 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1455 return true;
1456 return false;
1457}
1458
Trond Myklebustf95549c2015-01-23 18:06:09 -05001459static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1460{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001461 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1462 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001463 if (state->n_wronly)
1464 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1465 if (state->n_rdonly)
1466 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 if (state->n_rdwr)
1468 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001469 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001470}
1471
Trond Myklebust226056c2014-02-11 10:41:07 -05001472static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1473 nfs4_stateid *stateid, fmode_t fmode)
1474{
1475 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1476 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1477 case FMODE_WRITE:
1478 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1479 break;
1480 case FMODE_READ:
1481 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1482 break;
1483 case 0:
1484 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1486 clear_bit(NFS_OPEN_STATE, &state->flags);
1487 }
1488 if (stateid == NULL)
1489 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001490 /* Handle OPEN+OPEN_DOWNGRADE races */
1491 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1492 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001493 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001494 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001495 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001496 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001497 nfs4_stateid_copy(&state->stateid, stateid);
1498 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499}
1500
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001501static void nfs_clear_open_stateid(struct nfs4_state *state,
1502 nfs4_stateid *arg_stateid,
1503 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001504{
1505 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001506 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1507 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1508 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001509 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001510 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1511 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001512}
1513
Trond Myklebust1393d962016-09-22 13:39:13 -04001514static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1515 const nfs4_stateid *stateid, fmode_t fmode,
1516 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001517{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001518 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001519 case FMODE_READ:
1520 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1521 break;
1522 case FMODE_WRITE:
1523 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1524 break;
1525 case FMODE_READ|FMODE_WRITE:
1526 set_bit(NFS_O_RDWR_STATE, &state->flags);
1527 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001528 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001529 return;
1530 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1531 nfs4_stateid_copy(&state->stateid, stateid);
1532 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001533}
1534
Trond Myklebust1393d962016-09-22 13:39:13 -04001535static void __update_open_stateid(struct nfs4_state *state,
1536 const nfs4_stateid *open_stateid,
1537 const nfs4_stateid *deleg_stateid,
1538 fmode_t fmode,
1539 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001541 /*
1542 * Protect the call to nfs4_state_set_mode_locked and
1543 * serialise the stateid update
1544 */
Andrew Elble361cad32015-12-02 09:20:57 -05001545 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001546 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001547 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001548 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001549 set_bit(NFS_DELEGATED_STATE, &state->flags);
1550 }
1551 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001552 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001553 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001554 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001555 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Trond Myklebust1393d962016-09-22 13:39:13 -04001558static int update_open_stateid(struct nfs4_state *state,
1559 const nfs4_stateid *open_stateid,
1560 const nfs4_stateid *delegation,
1561 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001562{
Trond Myklebust1393d962016-09-22 13:39:13 -04001563 struct nfs_server *server = NFS_SERVER(state->inode);
1564 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001565 struct nfs_inode *nfsi = NFS_I(state->inode);
1566 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001567 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001568 int ret = 0;
1569
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001570 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001571
1572 rcu_read_lock();
1573 deleg_cur = rcu_dereference(nfsi->delegation);
1574 if (deleg_cur == NULL)
1575 goto no_delegation;
1576
1577 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001578 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001579 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001580 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001581 goto no_delegation_unlock;
1582
1583 if (delegation == NULL)
1584 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001585 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001586 goto no_delegation_unlock;
1587
Trond Myklebustb7391f42008-12-23 15:21:52 -05001588 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001589 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1590 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001591 ret = 1;
1592no_delegation_unlock:
1593 spin_unlock(&deleg_cur->lock);
1594no_delegation:
1595 rcu_read_unlock();
1596
1597 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001598 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001599 ret = 1;
1600 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001601 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001602 nfs4_schedule_state_manager(clp);
1603 if (freeme.type != 0)
1604 nfs4_test_and_free_stateid(server, &freeme,
1605 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001606
1607 return ret;
1608}
1609
Trond Myklebust39071e62015-01-24 15:07:56 -05001610static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1611 const nfs4_stateid *stateid)
1612{
1613 struct nfs4_state *state = lsp->ls_state;
1614 bool ret = false;
1615
1616 spin_lock(&state->state_lock);
1617 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1618 goto out_noupdate;
1619 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1620 goto out_noupdate;
1621 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1622 ret = true;
1623out_noupdate:
1624 spin_unlock(&state->state_lock);
1625 return ret;
1626}
Trond Myklebust34310432008-12-23 15:21:38 -05001627
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001628static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629{
1630 struct nfs_delegation *delegation;
1631
1632 rcu_read_lock();
1633 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 rcu_read_unlock();
1636 return;
1637 }
1638 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001639 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001640}
1641
Trond Myklebust6ee41262007-07-08 14:11:36 -04001642static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643{
1644 struct nfs4_state *state = opendata->state;
1645 struct nfs_inode *nfsi = NFS_I(state->inode);
1646 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001647 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001648 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001649 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 nfs4_stateid stateid;
1651 int ret = -EAGAIN;
1652
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001654 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001655 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001656 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001657 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001658 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001659 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001660 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001661 rcu_read_lock();
1662 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001663 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001664 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001665 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001666 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001667 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001668 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001670 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001671 if (!opendata->is_recover) {
1672 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1673 if (ret != 0)
1674 goto out;
1675 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001677
1678 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001679 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001680 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682out:
1683 return ERR_PTR(ret);
1684out_return_state:
1685 atomic_inc(&state->count);
1686 return state;
1687}
1688
Andy Adamsone23008e2012-10-02 21:07:32 -04001689static void
1690nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1691{
1692 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1693 struct nfs_delegation *delegation;
1694 int delegation_flags = 0;
1695
1696 rcu_read_lock();
1697 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1698 if (delegation)
1699 delegation_flags = delegation->flags;
1700 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001701 switch (data->o_arg.claim) {
1702 default:
1703 break;
1704 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1705 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001706 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1707 "returning a delegation for "
1708 "OPEN(CLAIM_DELEGATE_CUR)\n",
1709 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001710 return;
1711 }
1712 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001713 nfs_inode_set_delegation(state->inode,
1714 data->owner->so_cred,
1715 &data->o_res);
1716 else
1717 nfs_inode_reclaim_delegation(state->inode,
1718 data->owner->so_cred,
1719 &data->o_res);
1720}
1721
1722/*
1723 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1724 * and update the nfs4_state.
1725 */
1726static struct nfs4_state *
1727_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1728{
1729 struct inode *inode = data->state->inode;
1730 struct nfs4_state *state = data->state;
1731 int ret;
1732
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001733 if (!data->rpc_done) {
1734 if (data->rpc_status) {
1735 ret = data->rpc_status;
1736 goto err;
1737 }
1738 /* cached opens have already been processed */
1739 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001740 }
1741
Andy Adamsone23008e2012-10-02 21:07:32 -04001742 ret = nfs_refresh_inode(inode, &data->f_attr);
1743 if (ret)
1744 goto err;
1745
1746 if (data->o_res.delegation_type != 0)
1747 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001748update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001749 update_open_stateid(state, &data->o_res.stateid, NULL,
1750 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001751 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001752
1753 return state;
1754err:
1755 return ERR_PTR(ret);
1756
1757}
1758
1759static struct nfs4_state *
1760_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761{
1762 struct inode *inode;
1763 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765
Trond Myklebustaac00a82007-07-05 19:02:21 -04001766 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001767 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001768 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769 goto out;
1770 }
1771
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001772 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001775 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001776 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001777 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001778 goto err;
1779 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001780 state = nfs4_get_open_state(inode, data->owner);
1781 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001782 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001783 if (data->o_res.delegation_type != 0)
1784 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001785 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001786 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001788out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001789 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001791err_put_inode:
1792 iput(inode);
1793err:
1794 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001795}
1796
Andy Adamsone23008e2012-10-02 21:07:32 -04001797static struct nfs4_state *
1798nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1799{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001800 struct nfs4_state *ret;
1801
Andy Adamsone23008e2012-10-02 21:07:32 -04001802 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001803 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1804 else
1805 ret = _nfs4_opendata_to_nfs4_state(data);
1806 nfs4_sequence_free_slot(&data->o_res.seq_res);
1807 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001808}
1809
Trond Myklebust864472e2006-01-03 09:55:15 +01001810static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1811{
1812 struct nfs_inode *nfsi = NFS_I(state->inode);
1813 struct nfs_open_context *ctx;
1814
1815 spin_lock(&state->inode->i_lock);
1816 list_for_each_entry(ctx, &nfsi->open_files, list) {
1817 if (ctx->state != state)
1818 continue;
1819 get_nfs_open_context(ctx);
1820 spin_unlock(&state->inode->i_lock);
1821 return ctx;
1822 }
1823 spin_unlock(&state->inode->i_lock);
1824 return ERR_PTR(-ENOENT);
1825}
1826
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001827static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1828 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001829{
1830 struct nfs4_opendata *opendata;
1831
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001832 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001833 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001834 if (opendata == NULL)
1835 return ERR_PTR(-ENOMEM);
1836 opendata->state = state;
1837 atomic_inc(&state->count);
1838 return opendata;
1839}
1840
Trond Myklebust24311f82015-09-20 10:50:17 -04001841static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1842 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001843{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001844 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 int ret;
1846
Trond Myklebust24311f82015-09-20 10:50:17 -04001847 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001848 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001849 opendata->o_arg.open_flags = 0;
1850 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001851 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1852 NFS_SB(opendata->dentry->d_sb),
1853 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001854 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1855 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1856 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001857 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001858 if (ret != 0)
1859 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001860 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001861 if (IS_ERR(newstate))
1862 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001863 if (newstate != opendata->state)
1864 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001865 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001866 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001867}
1868
1869static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1870{
Trond Myklebust864472e2006-01-03 09:55:15 +01001871 int ret;
1872
Trond Myklebust4f14c192014-02-12 19:15:06 -05001873 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1874 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1875 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1876 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001877 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001878 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001879 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001880 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001881 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1882 if (ret != 0)
1883 return ret;
1884 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1885 if (ret != 0)
1886 return ret;
1887 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1888 if (ret != 0)
1889 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001890 /*
1891 * We may have performed cached opens for all three recoveries.
1892 * Check if we need to update the current stateid.
1893 */
1894 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001895 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001896 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001897 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001898 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001899 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001900 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001901 return 0;
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/*
1905 * OPEN_RECLAIM:
1906 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001908static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001910 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001911 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001912 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int status;
1914
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001915 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1916 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001917 if (IS_ERR(opendata))
1918 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001919 rcu_read_lock();
1920 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001921 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001922 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001923 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 opendata->o_arg.u.delegation_type = delegation_type;
1925 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001926 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return status;
1928}
1929
Trond Myklebust539cd032007-06-05 11:46:42 -04001930static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 struct nfs_server *server = NFS_SERVER(state->inode);
1933 struct nfs4_exception exception = { };
1934 int err;
1935 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001936 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001937 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001938 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1939 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001940 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001941 break;
1942 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 } while (exception.retry);
1944 return err;
1945}
1946
Trond Myklebust864472e2006-01-03 09:55:15 +01001947static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1948{
1949 struct nfs_open_context *ctx;
1950 int ret;
1951
1952 ctx = nfs4_state_find_open_context(state);
1953 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001954 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001955 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001956 put_nfs_open_context(ctx);
1957 return ret;
1958}
1959
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001960static 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 -07001961{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001962 switch (err) {
1963 default:
1964 printk(KERN_ERR "NFS: %s: unhandled error "
1965 "%d.\n", __func__, err);
1966 case 0:
1967 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001968 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001969 case -ESTALE:
1970 break;
1971 case -NFS4ERR_BADSESSION:
1972 case -NFS4ERR_BADSLOT:
1973 case -NFS4ERR_BAD_HIGH_SLOT:
1974 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1975 case -NFS4ERR_DEADSESSION:
1976 set_bit(NFS_DELEGATED_STATE, &state->flags);
1977 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1978 return -EAGAIN;
1979 case -NFS4ERR_STALE_CLIENTID:
1980 case -NFS4ERR_STALE_STATEID:
1981 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001982 /* Don't recall a delegation if it was lost */
1983 nfs4_schedule_lease_recovery(server->nfs_client);
1984 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001985 case -NFS4ERR_MOVED:
1986 nfs4_schedule_migration_recovery(server);
1987 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001988 case -NFS4ERR_LEASE_MOVED:
1989 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1990 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001991 case -NFS4ERR_DELEG_REVOKED:
1992 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001993 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001994 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001995 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001996 nfs_inode_find_state_and_recover(state->inode,
1997 stateid);
1998 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001999 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002000 case -NFS4ERR_DELAY:
2001 case -NFS4ERR_GRACE:
2002 set_bit(NFS_DELEGATED_STATE, &state->flags);
2003 ssleep(1);
2004 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002005 case -ENOMEM:
2006 case -NFS4ERR_DENIED:
2007 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2008 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return err;
2011}
2012
Trond Myklebust24311f82015-09-20 10:50:17 -04002013int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2014 struct nfs4_state *state, const nfs4_stateid *stateid,
2015 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002016{
2017 struct nfs_server *server = NFS_SERVER(state->inode);
2018 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002019 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002020
2021 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2022 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2023 if (IS_ERR(opendata))
2024 return PTR_ERR(opendata);
2025 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002026 write_seqlock(&state->seqlock);
2027 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2028 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002029 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2030 switch (type & (FMODE_READ|FMODE_WRITE)) {
2031 case FMODE_READ|FMODE_WRITE:
2032 case FMODE_WRITE:
2033 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2034 if (err)
2035 break;
2036 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2037 if (err)
2038 break;
2039 case FMODE_READ:
2040 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2041 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002042 nfs4_opendata_put(opendata);
2043 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2044}
2045
Chuck Leverbe05c862013-08-09 12:49:47 -04002046static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2047{
2048 struct nfs4_opendata *data = calldata;
2049
Peng Taocb04ad22014-06-11 05:24:15 +08002050 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2051 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002052}
2053
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002054static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2055{
2056 struct nfs4_opendata *data = calldata;
2057
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002058 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002059
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002061 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002062 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002063 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002064 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002065 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002066 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067}
2068
2069static void nfs4_open_confirm_release(void *calldata)
2070{
2071 struct nfs4_opendata *data = calldata;
2072 struct nfs4_state *state = NULL;
2073
2074 /* If this request hasn't been cancelled, do nothing */
2075 if (data->cancelled == 0)
2076 goto out_free;
2077 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002078 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002079 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002080 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002081 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002082 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002083out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002084 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002085}
2086
2087static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002088 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002089 .rpc_call_done = nfs4_open_confirm_done,
2090 .rpc_release = nfs4_open_confirm_release,
2091};
2092
2093/*
2094 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2095 */
2096static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2097{
David Howells2b0143b2015-03-17 22:25:59 +00002098 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002099 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002100 struct rpc_message msg = {
2101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2102 .rpc_argp = &data->c_arg,
2103 .rpc_resp = &data->c_res,
2104 .rpc_cred = data->owner->so_cred,
2105 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002106 struct rpc_task_setup task_setup_data = {
2107 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002108 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002109 .callback_ops = &nfs4_open_confirm_ops,
2110 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002111 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002112 .flags = RPC_TASK_ASYNC,
2113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 int status;
2115
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002116 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002117 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002118 data->rpc_done = 0;
2119 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002120 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002121 if (data->is_recover)
2122 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002123 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002124 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002125 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002126 status = nfs4_wait_for_completion_rpc_task(task);
2127 if (status != 0) {
2128 data->cancelled = 1;
2129 smp_wmb();
2130 } else
2131 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002132 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return status;
2134}
2135
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002136static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138 struct nfs4_opendata *data = calldata;
2139 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002140 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002141 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002142
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002144 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002145 /*
2146 * Check if we still need to send an OPEN call, or if we can use
2147 * a delegation instead.
2148 */
2149 if (data->state != NULL) {
2150 struct nfs_delegation *delegation;
2151
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002152 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002153 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002154 rcu_read_lock();
2155 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002156 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002157 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002158 rcu_read_unlock();
2159 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002160 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002161 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002162 switch (claim) {
2163 default:
2164 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002165 case NFS4_OPEN_CLAIM_PREVIOUS:
2166 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2167 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002168 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002169 case NFS4_OPEN_CLAIM_FH:
2170 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002171 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2172 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002173 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002174 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002175 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002176 &data->o_res.seq_res,
2177 task) != 0)
2178 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002179
2180 /* Set the create mode (note dependency on the session type) */
2181 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2182 if (data->o_arg.open_flags & O_EXCL) {
2183 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2184 if (nfs4_has_persistent_session(clp))
2185 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2186 else if (clp->cl_mvops->minor_version > 0)
2187 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2188 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002189 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002190unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002191 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002192 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002193out_no_action:
2194 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002195out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002196 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002197}
2198
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002199static void nfs4_open_done(struct rpc_task *task, void *calldata)
2200{
2201 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002203 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002204
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002205 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002206 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002207
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002209 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2210 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002211 case S_IFREG:
2212 break;
2213 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002214 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002215 break;
2216 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002218 break;
2219 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002221 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002222 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002223 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002224 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2225 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002226 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002227 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002229
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230static void nfs4_open_release(void *calldata)
2231{
2232 struct nfs4_opendata *data = calldata;
2233 struct nfs4_state *state = NULL;
2234
2235 /* If this request hasn't been cancelled, do nothing */
2236 if (data->cancelled == 0)
2237 goto out_free;
2238 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002239 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002240 goto out_free;
2241 /* In case we need an open_confirm, no cleanup! */
2242 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2243 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002244 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002245 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002246 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002247out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002248 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002249}
2250
2251static const struct rpc_call_ops nfs4_open_ops = {
2252 .rpc_call_prepare = nfs4_open_prepare,
2253 .rpc_call_done = nfs4_open_done,
2254 .rpc_release = nfs4_open_release,
2255};
2256
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002257static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002258{
David Howells2b0143b2015-03-17 22:25:59 +00002259 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260 struct nfs_server *server = NFS_SERVER(dir);
2261 struct nfs_openargs *o_arg = &data->o_arg;
2262 struct nfs_openres *o_res = &data->o_res;
2263 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002264 struct rpc_message msg = {
2265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2266 .rpc_argp = o_arg,
2267 .rpc_resp = o_res,
2268 .rpc_cred = data->owner->so_cred,
2269 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002270 struct rpc_task_setup task_setup_data = {
2271 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002272 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002273 .callback_ops = &nfs4_open_ops,
2274 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002275 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002276 .flags = RPC_TASK_ASYNC,
2277 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 int status;
2279
Chuck Levera9c92d62013-08-09 12:48:18 -04002280 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002281 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002282 data->rpc_done = 0;
2283 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002284 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002285 data->is_recover = 0;
2286 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002287 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002288 data->is_recover = 1;
2289 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002290 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002291 if (IS_ERR(task))
2292 return PTR_ERR(task);
2293 status = nfs4_wait_for_completion_rpc_task(task);
2294 if (status != 0) {
2295 data->cancelled = 1;
2296 smp_wmb();
2297 } else
2298 status = data->rpc_status;
2299 rpc_put_task(task);
2300
2301 return status;
2302}
2303
2304static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2305{
David Howells2b0143b2015-03-17 22:25:59 +00002306 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002307 struct nfs_openres *o_res = &data->o_res;
2308 int status;
2309
2310 status = nfs4_run_open_task(data, 1);
2311 if (status != 0 || !data->rpc_done)
2312 return status;
2313
Trond Myklebust6926afd2012-01-07 13:22:46 -05002314 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2315
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002316 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2317 status = _nfs4_proc_open_confirm(data);
2318 if (status != 0)
2319 return status;
2320 }
2321
2322 return status;
2323}
2324
Trond Myklebustf3792d62014-07-10 08:54:32 -04002325/*
2326 * Additional permission checks in order to distinguish between an
2327 * open for read, and an open for execute. This works around the
2328 * fact that NFSv4 OPEN treats read and execute permissions as being
2329 * the same.
2330 * Note that in the non-execute case, we want to turn off permission
2331 * checking if we just created a new file (POSIX open() semantics).
2332 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002333static int nfs4_opendata_access(struct rpc_cred *cred,
2334 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002335 struct nfs4_state *state, fmode_t fmode,
2336 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002337{
2338 struct nfs_access_entry cache;
2339 u32 mask;
2340
2341 /* access call failed or for some reason the server doesn't
2342 * support any access modes -- defer access call until later */
2343 if (opendata->o_res.access_supported == 0)
2344 return 0;
2345
2346 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002347 /*
2348 * Use openflags to check for exec, because fmode won't
2349 * always have FMODE_EXEC set when file open for exec.
2350 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002351 if (openflags & __FMODE_EXEC) {
2352 /* ONLY check for exec rights */
2353 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002354 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002355 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002356
2357 cache.cred = cred;
2358 cache.jiffies = jiffies;
2359 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2360 nfs_access_add_cache(state->inode, &cache);
2361
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002362 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002363 return 0;
2364
2365 /* even though OPEN succeeded, access is denied. Close the file */
2366 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002367 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002368}
2369
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370/*
2371 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2372 */
2373static int _nfs4_proc_open(struct nfs4_opendata *data)
2374{
David Howells2b0143b2015-03-17 22:25:59 +00002375 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376 struct nfs_server *server = NFS_SERVER(dir);
2377 struct nfs_openargs *o_arg = &data->o_arg;
2378 struct nfs_openres *o_res = &data->o_res;
2379 int status;
2380
2381 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002382 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002384 if (status != 0) {
2385 if (status == -NFS4ERR_BADNAME &&
2386 !(o_arg->open_flags & O_CREAT))
2387 return -ENOENT;
2388 return status;
2389 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002390
Trond Myklebust6926afd2012-01-07 13:22:46 -05002391 nfs_fattr_map_and_free_names(server, &data->f_attr);
2392
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002393 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002394 if (o_arg->open_flags & O_EXCL)
2395 data->file_created = 1;
2396 else if (o_res->cinfo.before != o_res->cinfo.after)
2397 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002398 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002399 update_changeattr(dir, &o_res->cinfo,
2400 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002401 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002402 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2403 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002405 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002407 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002410 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
Andy Adamsond83217c2011-03-01 01:34:17 +00002414static int nfs4_recover_expired_lease(struct nfs_server *server)
2415{
2416 return nfs4_client_recover_expired_lease(server->nfs_client);
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/*
2420 * OPEN_EXPIRED:
2421 * reclaim state on the server after a network partition.
2422 * Assumes caller holds the appropriate lock
2423 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002424static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002426 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002427 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002429 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002430 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002431 if (IS_ERR(opendata))
2432 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002433 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002434 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002435 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002436 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002440static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002441{
Trond Myklebust539cd032007-06-05 11:46:42 -04002442 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002443 struct nfs4_exception exception = { };
2444 int err;
2445
2446 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002447 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002448 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002449 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2450 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002451 switch (err) {
2452 default:
2453 goto out;
2454 case -NFS4ERR_GRACE:
2455 case -NFS4ERR_DELAY:
2456 nfs4_handle_exception(server, err, &exception);
2457 err = 0;
2458 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002459 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002460out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002461 return err;
2462}
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2465{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002467 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Trond Myklebust864472e2006-01-03 09:55:15 +01002469 ctx = nfs4_state_find_open_context(state);
2470 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002471 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002472 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002473 put_nfs_open_context(ctx);
2474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Trond Myklebust41020b62016-09-22 13:38:58 -04002477static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2478 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002479{
Trond Myklebust41020b62016-09-22 13:38:58 -04002480 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002481 write_seqlock(&state->seqlock);
2482 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2483 write_sequnlock(&state->seqlock);
2484 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2485}
2486
2487static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2488{
2489 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002490 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002491}
2492
2493static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2494{
2495 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2496 nfs40_clear_delegation_stateid(state);
2497 return nfs4_open_expired(sp, state);
2498}
2499
Trond Myklebust45870d62016-09-22 13:38:59 -04002500static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2501 nfs4_stateid *stateid,
2502 struct rpc_cred *cred)
2503{
2504 return -NFS4ERR_BAD_STATEID;
2505}
2506
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002507#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002508static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2509 nfs4_stateid *stateid,
2510 struct rpc_cred *cred)
2511{
2512 int status;
2513
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002514 switch (stateid->type) {
2515 default:
2516 break;
2517 case NFS4_INVALID_STATEID_TYPE:
2518 case NFS4_SPECIAL_STATEID_TYPE:
2519 return -NFS4ERR_BAD_STATEID;
2520 case NFS4_REVOKED_STATEID_TYPE:
2521 goto out_free;
2522 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002523
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002524 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002525 switch (status) {
2526 case -NFS4ERR_EXPIRED:
2527 case -NFS4ERR_ADMIN_REVOKED:
2528 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002529 break;
2530 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002531 return status;
2532 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002533out_free:
2534 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002535 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002536 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002537}
2538
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002539static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002540{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002541 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002543 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002544 struct rpc_cred *cred;
2545 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002546
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002547 /* Get the delegation credential for use by test/free_stateid */
2548 rcu_read_lock();
2549 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002550 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002551 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002552 return;
2553 }
2554
2555 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002556 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2557 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002558 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002559 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002560 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002561
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002562 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2563 rcu_read_unlock();
2564 return;
2565 }
2566
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002567 cred = get_rpccred(delegation->cred);
2568 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002569 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002570 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002571 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002572 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002573
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002574 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002575}
2576
2577/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002578 * nfs41_check_expired_locks - possibly free a lock stateid
2579 *
2580 * @state: NFSv4 state for an inode
2581 *
2582 * Returns NFS_OK if recovery for this stateid is now finished.
2583 * Otherwise a negative NFS4ERR value is returned.
2584 */
2585static int nfs41_check_expired_locks(struct nfs4_state *state)
2586{
2587 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002588 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002589 struct nfs_server *server = NFS_SERVER(state->inode);
2590
2591 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2592 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002593
2594 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002595 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2596 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2597 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2598
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002599 atomic_inc(&lsp->ls_count);
2600 spin_unlock(&state->state_lock);
2601
2602 nfs4_put_lock_state(prev);
2603 prev = lsp;
2604
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002605 status = nfs41_test_and_free_expired_stateid(server,
2606 &lsp->ls_stateid,
2607 cred);
2608 trace_nfs4_test_lock_stateid(state, lsp, status);
2609 if (status == -NFS4ERR_EXPIRED ||
2610 status == -NFS4ERR_BAD_STATEID) {
2611 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002612 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002613 if (!recover_lost_locks)
2614 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2615 } else if (status != NFS_OK) {
2616 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002617 nfs4_put_lock_state(prev);
2618 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002619 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002620 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002621 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002622 }
2623 spin_unlock(&state->state_lock);
2624 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002625out:
2626 return ret;
2627}
2628
2629/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002630 * nfs41_check_open_stateid - possibly free an open stateid
2631 *
2632 * @state: NFSv4 state for an inode
2633 *
2634 * Returns NFS_OK if recovery for this stateid is now finished.
2635 * Otherwise a negative NFS4ERR value is returned.
2636 */
2637static int nfs41_check_open_stateid(struct nfs4_state *state)
2638{
2639 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002640 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002641 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002642 int status;
2643
Trond Myklebustb134fc42016-09-22 13:39:16 -04002644 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002645 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2646 if (nfs4_have_delegation(state->inode, state->state))
2647 return NFS_OK;
2648 return -NFS4ERR_OPENMODE;
2649 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002650 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002651 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002652 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002653 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002654 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002655 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2656 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2657 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002658 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002659 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002660 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002661 if (status != NFS_OK)
2662 return status;
2663 if (nfs_open_stateid_recover_openmode(state))
2664 return -NFS4ERR_OPENMODE;
2665 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002666}
2667
2668static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2669{
Chuck Levereb64cf92012-07-11 16:30:05 -04002670 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002671
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002672 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002673 status = nfs41_check_expired_locks(state);
2674 if (status != NFS_OK)
2675 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002676 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002677 if (status != NFS_OK)
2678 status = nfs4_open_expired(sp, state);
2679 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002680}
2681#endif
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002684 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2685 * fields corresponding to attributes that were used to store the verifier.
2686 * Make sure we clobber those fields in the later setattr call
2687 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002688static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2689 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002690{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002691 const u32 *attrset = opendata->o_res.attrset;
2692
2693 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002694 !(sattr->ia_valid & ATTR_ATIME_SET))
2695 sattr->ia_valid |= ATTR_ATIME;
2696
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002697 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002698 !(sattr->ia_valid & ATTR_MTIME_SET))
2699 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002700
2701 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002702 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2703 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002704 sattr->ia_valid &= ~ATTR_MODE;
2705
2706 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2707 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002708}
2709
Trond Myklebustc21443c2013-02-07 14:26:21 -05002710static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2711 fmode_t fmode,
2712 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002713 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002714{
2715 struct nfs4_state_owner *sp = opendata->owner;
2716 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002717 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002718 struct nfs4_state *state;
2719 unsigned int seq;
2720 int ret;
2721
2722 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2723
2724 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002725 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002726 goto out;
2727
2728 state = nfs4_opendata_to_nfs4_state(opendata);
2729 ret = PTR_ERR(state);
2730 if (IS_ERR(state))
2731 goto out;
2732 if (server->caps & NFS_CAP_POSIX_LOCK)
2733 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002734 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2735 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002736
Trond Myklebust275bb302013-05-29 13:11:28 -04002737 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002738 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002739 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002740 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002741 alias = d_exact_alias(dentry, state->inode);
2742 if (!alias)
2743 alias = d_splice_alias(igrab(state->inode), dentry);
2744 /* d_splice_alias() can't fail here - it's a non-directory */
2745 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002746 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002747 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002748 }
2749 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002750 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002751 }
2752
Trond Myklebustc21443c2013-02-07 14:26:21 -05002753 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2754 if (ret != 0)
2755 goto out;
2756
Trond Myklebust3efb9722013-05-29 13:17:04 -04002757 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002758 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002759 nfs_inode_attach_open_context(ctx);
2760 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2761 nfs4_schedule_stateid_recovery(server, state);
2762 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002763out:
2764 return ret;
2765}
2766
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002767/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002768 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 */
Andy Adamson82be4172012-05-23 05:02:35 -04002770static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002771 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002772 int flags,
2773 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002774 struct nfs4_label *label,
2775 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 struct nfs4_state_owner *sp;
2778 struct nfs4_state *state = NULL;
2779 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002780 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002781 struct dentry *dentry = ctx->dentry;
2782 struct rpc_cred *cred = ctx->cred;
2783 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2784 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002785 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002786 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002787 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002791 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2792 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2794 goto out_err;
2795 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002796 status = nfs4_recover_expired_lease(server);
2797 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002798 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002799 if (d_really_is_positive(dentry))
2800 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002801 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002802 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002803 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002804 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002805 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002806 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002807 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
David Quigley14c43f72013-05-22 12:50:43 -04002809 if (label) {
2810 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2811 if (IS_ERR(olabel)) {
2812 status = PTR_ERR(olabel);
2813 goto err_opendata_put;
2814 }
2815 }
2816
Trond Myklebuste911b812014-03-26 13:24:37 -07002817 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2818 if (!opendata->f_attr.mdsthreshold) {
2819 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2820 if (!opendata->f_attr.mdsthreshold)
2821 goto err_free_label;
2822 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002823 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002824 }
David Howells2b0143b2015-03-17 22:25:59 +00002825 if (d_really_is_positive(dentry))
2826 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002827
Trond Myklebust3efb9722013-05-29 13:17:04 -04002828 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002829 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002830 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002831 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002832
NeilBrownefcbc042015-07-30 13:00:56 +10002833 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002834 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002835 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002836 /*
2837 * send create attributes which was not set by open
2838 * with an extra setattr.
2839 */
2840 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2841 nfs_fattr_init(opendata->o_res.f_attr);
2842 status = nfs4_do_setattr(state->inode, cred,
2843 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002844 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002845 if (status == 0) {
2846 nfs_setattr_update_inode(state->inode, sattr,
2847 opendata->o_res.f_attr);
2848 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2849 }
David Quigley1775fd32013-05-22 12:50:42 -04002850 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002851 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002852 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002853 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002854
Trond Myklebuste911b812014-03-26 13:24:37 -07002855 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002856 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002857 opendata->f_attr.mdsthreshold = NULL;
2858 }
Andy Adamson82be4172012-05-23 05:02:35 -04002859
David Quigley14c43f72013-05-22 12:50:43 -04002860 nfs4_label_free(olabel);
2861
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002862 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002865err_free_label:
2866 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002867err_opendata_put:
2868 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002869err_put_state_owner:
2870 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return status;
2873}
2874
2875
Andy Adamson82be4172012-05-23 05:02:35 -04002876static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002877 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002878 int flags,
2879 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002880 struct nfs4_label *label,
2881 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002883 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 struct nfs4_exception exception = { };
2885 struct nfs4_state *res;
2886 int status;
2887
2888 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002889 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002890 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002891 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (status == 0)
2893 break;
2894 /* NOTE: BAD_SEQID means the server and client disagree about the
2895 * book-keeping w.r.t. state-changing operations
2896 * (OPEN/CLOSE/LOCK/LOCKU...)
2897 * It is actually a sign of a bug on the client or on the server.
2898 *
2899 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002900 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 * have unhashed the old state_owner for us, and that we can
2902 * therefore safely retry using a new one. We should still warn
2903 * the user though...
2904 */
2905 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002906 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002907 " returned a bad sequence-id error!\n",
2908 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 exception.retry = 1;
2910 continue;
2911 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002912 /*
2913 * BAD_STATEID on OPEN means that the server cancelled our
2914 * state before it received the OPEN_CONFIRM.
2915 * Recover by retrying the request as per the discussion
2916 * on Page 181 of RFC3530.
2917 */
2918 if (status == -NFS4ERR_BAD_STATEID) {
2919 exception.retry = 1;
2920 continue;
2921 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002922 if (status == -EAGAIN) {
2923 /* We must have found a delegation */
2924 exception.retry = 1;
2925 continue;
2926 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002927 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2928 continue;
2929 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 status, &exception));
2931 } while (exception.retry);
2932 return res;
2933}
2934
Trond Myklebust8487c472016-06-26 08:44:35 -04002935static int _nfs4_do_setattr(struct inode *inode,
2936 struct nfs_setattrargs *arg,
2937 struct nfs_setattrres *res,
2938 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002939 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002940{
2941 struct nfs_server *server = NFS_SERVER(inode);
2942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2944 .rpc_argp = arg,
2945 .rpc_resp = res,
2946 .rpc_cred = cred,
2947 };
2948 struct rpc_cred *delegation_cred = NULL;
2949 unsigned long timestamp = jiffies;
2950 fmode_t fmode;
2951 bool truncate;
2952 int status;
2953
2954 nfs_fattr_init(res->fattr);
2955
2956 /* Servers should only apply open mode checks for file size changes */
2957 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2958 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2959
2960 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2961 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002962 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002963 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002964 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002965 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002966 l_ctx = nfs_get_lock_context(ctx);
2967 if (IS_ERR(l_ctx))
2968 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002969 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2970 &arg->stateid, &delegation_cred);
2971 nfs_put_lock_context(l_ctx);
2972 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002973 return -EBADF;
2974 } else
2975 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2976 if (delegation_cred)
2977 msg.rpc_cred = delegation_cred;
2978
2979 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2980
2981 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002982 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002983 renew_lease(server, timestamp);
2984 trace_nfs4_setattr(inode, &arg->stateid, status);
2985 return status;
2986}
2987
2988static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2989 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002990 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002991 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002993 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002994 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002996 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 .iap = sattr,
2998 .server = server,
2999 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003000 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 };
3002 struct nfs_setattrres res = {
3003 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003004 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 .server = server,
3006 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003007 struct nfs4_exception exception = {
3008 .state = state,
3009 .inode = inode,
3010 .stateid = &arg.stateid,
3011 };
3012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
David Quigleyaa9c2662013-05-22 12:50:44 -04003014 arg.bitmask = nfs4_bitmask(server, ilabel);
3015 if (ilabel)
3016 arg.bitmask = nfs4_bitmask(server, olabel);
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003019 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003020 switch (err) {
3021 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003022 if (!(sattr->ia_valid & ATTR_SIZE)) {
3023 pr_warn_once("NFSv4: server %s is incorrectly "
3024 "applying open mode checks to "
3025 "a SETATTR that is not "
3026 "changing file size.\n",
3027 server->nfs_client->cl_hostname);
3028 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003029 if (state && !(state->state & FMODE_WRITE)) {
3030 err = -EBADF;
3031 if (sattr->ia_valid & ATTR_OPEN)
3032 err = -EACCES;
3033 goto out;
3034 }
3035 }
3036 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003038out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return err;
3040}
3041
Peng Tao500d7012015-09-22 11:35:22 +08003042static bool
3043nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3044{
3045 if (inode == NULL || !nfs_have_layout(inode))
3046 return false;
3047
3048 return pnfs_wait_on_layoutreturn(inode, task);
3049}
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051struct nfs4_closedata {
3052 struct inode *inode;
3053 struct nfs4_state *state;
3054 struct nfs_closeargs arg;
3055 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003056 struct {
3057 struct nfs4_layoutreturn_args arg;
3058 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003059 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003060 u32 roc_barrier;
3061 bool roc;
3062 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003063 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003064 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065};
3066
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003067static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003068{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003069 struct nfs4_closedata *calldata = data;
3070 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003071 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003072
Trond Myklebustcf805162016-11-15 14:56:07 -05003073 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003074 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3075 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003076 nfs4_put_open_state(calldata->state);
3077 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003078 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003079 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003080 kfree(calldata);
3081}
3082
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003083static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003085 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003088 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Chuck Levera3ca5652012-03-01 17:00:40 -05003090 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003091 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3092 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003093 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003094
3095 /* Handle Layoutreturn errors */
3096 if (calldata->arg.lr_args && task->tk_status != 0) {
3097 switch (calldata->res.lr_ret) {
3098 default:
3099 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3100 break;
3101 case 0:
3102 calldata->arg.lr_args = NULL;
3103 calldata->res.lr_res = NULL;
3104 break;
3105 case -NFS4ERR_ADMIN_REVOKED:
3106 case -NFS4ERR_DELEG_REVOKED:
3107 case -NFS4ERR_EXPIRED:
3108 case -NFS4ERR_BAD_STATEID:
3109 case -NFS4ERR_OLD_STATEID:
3110 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3111 case -NFS4ERR_WRONG_CRED:
3112 calldata->arg.lr_args = NULL;
3113 calldata->res.lr_res = NULL;
3114 calldata->res.lr_ret = 0;
3115 rpc_restart_call_prepare(task);
3116 return;
3117 }
3118 }
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 /* hmm. we are done with the inode, and in the process of freeing
3121 * the state_owner. we keep this around to process errors
3122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 switch (task->tk_status) {
3124 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003125 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003126 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003127 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003128 case -NFS4ERR_ACCESS:
3129 if (calldata->arg.bitmask != NULL) {
3130 calldata->arg.bitmask = NULL;
3131 calldata->res.fattr = NULL;
3132 task->tk_status = 0;
3133 rpc_restart_call_prepare(task);
3134 goto out_release;
3135
3136 }
3137 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003138 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003140 case -NFS4ERR_EXPIRED:
3141 nfs4_free_revoked_stateid(server,
3142 &calldata->arg.stateid,
3143 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003144 case -NFS4ERR_OLD_STATEID:
3145 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003146 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003147 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003148 rpc_restart_call_prepare(task);
3149 goto out_release;
3150 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003151 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003154 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003155 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003156 goto out_release;
3157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003159 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3160 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003161out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003162 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003163 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003164 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003167static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003169 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003170 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003171 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003172 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003173 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003174
Chuck Levera3ca5652012-03-01 17:00:40 -05003175 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003177 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003178
Trond Myklebust88069f72009-12-08 08:33:16 -05003179 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003180 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003181 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3182 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3183 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003184 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003185 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003186 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003187 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003188 if (state->n_rdonly == 0)
3189 call_close |= is_rdonly;
3190 else if (is_rdonly)
3191 calldata->arg.fmode |= FMODE_READ;
3192 if (state->n_wronly == 0)
3193 call_close |= is_wronly;
3194 else if (is_wronly)
3195 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003196 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3197 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003198 } else if (is_rdwr)
3199 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3200
Trond Myklebust5cc78612016-11-14 11:19:56 -05003201 if (!nfs4_valid_open_stateid(state) ||
3202 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003203 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003204 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003205
3206 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003207 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003208 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003209 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003210
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003211 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003212 nfs_release_seqid(calldata->arg.seqid);
3213 goto out_wait;
3214 }
3215
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003216 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003217 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003218
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003219 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003220 /* Close-to-open cache consistency revalidation */
3221 if (!nfs4_have_delegation(inode, FMODE_READ))
3222 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3223 else
3224 calldata->arg.bitmask = NULL;
3225 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003226
Trond Myklebust6ae37332015-01-30 14:21:14 -05003227 calldata->arg.share_access =
3228 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3229 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003230
Trond Myklebustd8d84982016-12-19 12:14:44 -05003231 if (calldata->res.fattr == NULL)
3232 calldata->arg.bitmask = NULL;
3233 else if (calldata->arg.bitmask == NULL)
3234 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003235 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003236 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003237 &calldata->arg.seq_args,
3238 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003239 task) != 0)
3240 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003241 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003242 return;
3243out_no_action:
3244 task->tk_action = NULL;
3245out_wait:
3246 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003249static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003250 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003251 .rpc_call_done = nfs4_close_done,
3252 .rpc_release = nfs4_free_closedata,
3253};
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255/*
3256 * It is possible for data to be read/written from a mem-mapped file
3257 * after the sys_close call (which hits the vfs layer as a flush).
3258 * This means that we can't safely call nfsv4 close on a file until
3259 * the inode is cleared. This in turn means that we are not good
3260 * NFSv4 citizens - we do not indicate to the server to update the file's
3261 * share state even when we are done with one of the three share
3262 * stateid's in the inode.
3263 *
3264 * NOTE: Caller must be holding the sp->so_owner semaphore!
3265 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003266int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003268 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003269 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003271 struct nfs4_state_owner *sp = state->owner;
3272 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003273 struct rpc_message msg = {
3274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3275 .rpc_cred = state->owner->so_cred,
3276 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003277 struct rpc_task_setup task_setup_data = {
3278 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003279 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003280 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003281 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003282 .flags = RPC_TASK_ASYNC,
3283 };
Trond Myklebust95121352005-10-18 14:20:12 -07003284 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003286 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3287 &task_setup_data.rpc_client, &msg);
3288
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003289 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003291 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003292 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003293 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003295 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003297 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3298 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003299 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003300 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003301 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003302 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003303 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003304 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003305 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003306 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003307 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003308 calldata->lr.roc = pnfs_roc(state->inode,
3309 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3310 if (calldata->lr.roc) {
3311 calldata->arg.lr_args = &calldata->lr.arg;
3312 calldata->res.lr_res = &calldata->lr.res;
3313 }
Al Viro643168c2011-06-22 18:20:23 -04003314 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003315
Trond Myklebust1174dd12010-12-21 10:52:24 -05003316 msg.rpc_argp = &calldata->arg;
3317 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003318 task_setup_data.callback_data = calldata;
3319 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003320 if (IS_ERR(task))
3321 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003322 status = 0;
3323 if (wait)
3324 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003325 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003326 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003327out_free_calldata:
3328 kfree(calldata);
3329out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003330 nfs4_put_open_state(state);
3331 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003332 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
Trond Myklebust2b484292010-09-17 10:56:51 -04003335static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003336nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3337 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003340 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3341
3342 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Trond Myklebust565277f2007-10-15 18:17:53 -04003344 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003345 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003346
3347 nfs4_label_release_security(label);
3348
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003349 if (IS_ERR(state))
3350 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003351 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
Trond Myklebust1185a552009-12-03 15:54:02 -05003354static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003355{
3356 if (ctx->state == NULL)
3357 return;
3358 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003359 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003360 else
Al Viro643168c2011-06-22 18:20:23 -04003361 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Trond Myklebustb944dba2013-11-04 15:20:20 -05003364#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3365#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003366#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3369{
Kinglong Mee8c612822015-08-26 21:12:58 +08003370 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003371 struct nfs4_server_caps_arg args = {
3372 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003373 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003374 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct nfs4_server_caps_res res = {};
3376 struct rpc_message msg = {
3377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003378 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 .rpc_resp = &res,
3380 };
3381 int status;
3382
Kinglong Mee8c612822015-08-26 21:12:58 +08003383 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3384 FATTR4_WORD0_FH_EXPIRE_TYPE |
3385 FATTR4_WORD0_LINK_SUPPORT |
3386 FATTR4_WORD0_SYMLINK_SUPPORT |
3387 FATTR4_WORD0_ACLSUPPORT;
3388 if (minorversion)
3389 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3390
Bryan Schumaker7c513052011-03-24 17:12:24 +00003391 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003393 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003394 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003395 case 0:
3396 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3397 res.attr_bitmask[2] = 0;
3398 break;
3399 case 1:
3400 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3401 break;
3402 case 2:
3403 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003406 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3407 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3408 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3409 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003410 NFS_CAP_CTIME|NFS_CAP_MTIME|
3411 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003412 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3413 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 server->caps |= NFS_CAP_ACLS;
3415 if (res.has_links != 0)
3416 server->caps |= NFS_CAP_HARDLINKS;
3417 if (res.has_symlinks != 0)
3418 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003419 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3420 server->caps |= NFS_CAP_FILEID;
3421 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3422 server->caps |= NFS_CAP_MODE;
3423 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3424 server->caps |= NFS_CAP_NLINK;
3425 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3426 server->caps |= NFS_CAP_OWNER;
3427 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3428 server->caps |= NFS_CAP_OWNER_GROUP;
3429 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3430 server->caps |= NFS_CAP_ATIME;
3431 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3432 server->caps |= NFS_CAP_CTIME;
3433 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3434 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003435#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3436 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3437 server->caps |= NFS_CAP_SECURITY_LABEL;
3438#endif
3439 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3440 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003441 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003442
Trond Myklebusta65318b2009-03-11 14:10:28 -04003443 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3444 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3445 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003446 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003447 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3448 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003450 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return status;
3454}
3455
Trond Myklebust55a97592006-06-09 09:34:19 -04003456int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 struct nfs4_exception exception = { };
3459 int err;
3460 do {
3461 err = nfs4_handle_exception(server,
3462 _nfs4_server_capabilities(server, fhandle),
3463 &exception);
3464 } while (exception.retry);
3465 return err;
3466}
3467
3468static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3469 struct nfs_fsinfo *info)
3470{
David Quigleyaa9c2662013-05-22 12:50:44 -04003471 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003473 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 };
3475 struct nfs4_lookup_res res = {
3476 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003477 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 .fh = fhandle,
3479 };
3480 struct rpc_message msg = {
3481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3482 .rpc_argp = &args,
3483 .rpc_resp = &res,
3484 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003485
David Quigleyaa9c2662013-05-22 12:50:44 -04003486 bitmask[0] = nfs4_fattr_bitmap[0];
3487 bitmask[1] = nfs4_fattr_bitmap[1];
3488 /*
3489 * Process the label in the upcoming getfattr
3490 */
3491 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3492
Trond Myklebust0e574af2005-10-27 22:12:38 -04003493 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003494 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
3497static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3498 struct nfs_fsinfo *info)
3499{
3500 struct nfs4_exception exception = { };
3501 int err;
3502 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003503 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003504 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003505 switch (err) {
3506 case 0:
3507 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003508 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003509 default:
3510 err = nfs4_handle_exception(server, err, &exception);
3511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003513out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 return err;
3515}
3516
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003517static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3518 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3519{
Trond Myklebustc2190662013-08-26 19:23:04 -04003520 struct rpc_auth_create_args auth_args = {
3521 .pseudoflavor = flavor,
3522 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003523 struct rpc_auth *auth;
3524 int ret;
3525
Trond Myklebustc2190662013-08-26 19:23:04 -04003526 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003527 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003528 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003529 goto out;
3530 }
3531 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003532out:
3533 return ret;
3534}
3535
Chuck Lever9a744ba2013-03-16 15:56:02 -04003536/*
3537 * Retry pseudoroot lookup with various security flavors. We do this when:
3538 *
3539 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3540 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3541 *
3542 * Returns zero on success, or a negative NFS4ERR value, or a
3543 * negative errno value.
3544 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003545static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003546 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003548 /* Per 3530bis 15.33.5 */
3549 static const rpc_authflavor_t flav_array[] = {
3550 RPC_AUTH_GSS_KRB5P,
3551 RPC_AUTH_GSS_KRB5I,
3552 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003553 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003554 RPC_AUTH_NULL,
3555 };
3556 int status = -EPERM;
3557 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003559 if (server->auth_info.flavor_len > 0) {
3560 /* try each flavor specified by user */
3561 for (i = 0; i < server->auth_info.flavor_len; i++) {
3562 status = nfs4_lookup_root_sec(server, fhandle, info,
3563 server->auth_info.flavors[i]);
3564 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3565 continue;
3566 break;
3567 }
3568 } else {
3569 /* no flavors specified by user, try default list */
3570 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3571 status = nfs4_lookup_root_sec(server, fhandle, info,
3572 flav_array[i]);
3573 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3574 continue;
3575 break;
3576 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003577 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003578
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003579 /*
3580 * -EACCESS could mean that the user doesn't have correct permissions
3581 * to access the mount. It could also mean that we tried to mount
3582 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3583 * existing mount programs don't handle -EACCES very well so it should
3584 * be mapped to -EPERM instead.
3585 */
3586 if (status == -EACCES)
3587 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003588 return status;
3589}
3590
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003591/**
3592 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3593 * @server: initialized nfs_server handle
3594 * @fhandle: we fill in the pseudo-fs root file handle
3595 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003596 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003597 *
3598 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003599 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003600int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003601 struct nfs_fsinfo *info,
3602 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003603{
Andre Przywarac7757072015-04-23 17:17:40 +01003604 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003605
Andre Przywarac7757072015-04-23 17:17:40 +01003606 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003607 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003608
3609 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003610 status = server->nfs_client->cl_mvops->find_root_sec(server,
3611 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 if (status == 0)
3614 status = nfs4_server_capabilities(server, fhandle);
3615 if (status == 0)
3616 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003617
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003618 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
Bryan Schumakerbae36242012-05-10 15:07:31 -04003621static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3622 struct nfs_fsinfo *info)
3623{
3624 int error;
3625 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003626 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003627
3628 error = nfs4_server_capabilities(server, mntfh);
3629 if (error < 0) {
3630 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3631 return error;
3632 }
3633
David Quigley14c43f72013-05-22 12:50:43 -04003634 label = nfs4_label_alloc(server, GFP_KERNEL);
3635 if (IS_ERR(label))
3636 return PTR_ERR(label);
3637
David Quigley1775fd32013-05-22 12:50:42 -04003638 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003639 if (error < 0) {
3640 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003641 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003642 }
3643
3644 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3645 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3646 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3647
David Quigley14c43f72013-05-22 12:50:43 -04003648err_free_label:
3649 nfs4_label_free(label);
3650
Bryan Schumakerbae36242012-05-10 15:07:31 -04003651 return error;
3652}
3653
Manoj Naik6b97fd32006-06-09 09:34:29 -04003654/*
3655 * Get locations and (maybe) other attributes of a referral.
3656 * Note that we'll actually follow the referral later when
3657 * we detect fsid mismatch in inode revalidation
3658 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003659static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3660 const struct qstr *name, struct nfs_fattr *fattr,
3661 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003662{
3663 int status = -ENOMEM;
3664 struct page *page = NULL;
3665 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003666
3667 page = alloc_page(GFP_KERNEL);
3668 if (page == NULL)
3669 goto out;
3670 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3671 if (locations == NULL)
3672 goto out;
3673
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003674 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003675 if (status != 0)
3676 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003677
3678 /*
3679 * If the fsid didn't change, this is a migration event, not a
3680 * referral. Cause us to drop into the exception handler, which
3681 * will kick off migration recovery.
3682 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003683 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003684 dprintk("%s: server did not return a different fsid for"
3685 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003686 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003687 goto out;
3688 }
Andy Adamson533eb462011-06-13 18:25:56 -04003689 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3690 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003691
Andy Adamson533eb462011-06-13 18:25:56 -04003692 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003693 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003694 memset(fhandle, 0, sizeof(struct nfs_fh));
3695out:
3696 if (page)
3697 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003698 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003699 return status;
3700}
3701
David Quigley1775fd32013-05-22 12:50:42 -04003702static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3703 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
3705 struct nfs4_getattr_arg args = {
3706 .fh = fhandle,
3707 .bitmask = server->attr_bitmask,
3708 };
3709 struct nfs4_getattr_res res = {
3710 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003711 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 .server = server,
3713 };
3714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3716 .rpc_argp = &args,
3717 .rpc_resp = &res,
3718 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003719
3720 args.bitmask = nfs4_bitmask(server, label);
3721
Trond Myklebust0e574af2005-10-27 22:12:38 -04003722 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003723 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
David Quigley1775fd32013-05-22 12:50:42 -04003726static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3727 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 struct nfs4_exception exception = { };
3730 int err;
3731 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003732 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3733 trace_nfs4_getattr(server, fhandle, fattr, err);
3734 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 &exception);
3736 } while (exception.retry);
3737 return err;
3738}
3739
3740/*
3741 * The file is not closed if it is opened due to the a request to change
3742 * the size of the file. The open call will not be needed once the
3743 * VFS layer lookup-intents are implemented.
3744 *
3745 * Close is called when the inode is destroyed.
3746 * If we haven't opened the file for O_WRONLY, we
3747 * need to in the size_change case to obtain a stateid.
3748 *
3749 * Got race?
3750 * Because OPEN is always done by name in nfsv4, it is
3751 * possible that we opened a different file by the same
3752 * name. We can recognize this race condition, but we
3753 * can't do anything about it besides returning an error.
3754 *
3755 * This will be fixed with VFS changes (lookup-intent).
3756 */
3757static int
3758nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3759 struct iattr *sattr)
3760{
David Howells2b0143b2015-03-17 22:25:59 +00003761 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003762 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003763 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003764 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 int status;
3766
Peng Tao88ac8152014-09-12 11:04:10 +08003767 if (pnfs_ld_layoutret_on_setattr(inode) &&
3768 sattr->ia_valid & ATTR_SIZE &&
3769 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003770 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003771
Trond Myklebust0e574af2005-10-27 22:12:38 -04003772 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Andy Adamson26699402012-05-30 16:12:24 -04003774 /* Deal with open(O_TRUNC) */
3775 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003776 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003777
3778 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003779 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003780 return 0;
3781
Trond Myklebustd5308382005-11-04 15:33:38 -05003782 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003783 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003784
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003785 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003786 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003787 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003788 }
3789
David Quigley14c43f72013-05-22 12:50:43 -04003790 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3791 if (IS_ERR(label))
3792 return PTR_ERR(label);
3793
NeilBrown29b59f92016-10-13 15:26:47 +11003794 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003795 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003796 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003797 nfs_setsecurity(inode, fattr, label);
3798 }
David Quigley14c43f72013-05-22 12:50:43 -04003799 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return status;
3801}
3802
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003803static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3804 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003805 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003806{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003807 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003808 int status;
3809 struct nfs4_lookup_arg args = {
3810 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003811 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003812 .name = name,
3813 };
3814 struct nfs4_lookup_res res = {
3815 .server = server,
3816 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003817 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003818 .fh = fhandle,
3819 };
3820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3822 .rpc_argp = &args,
3823 .rpc_resp = &res,
3824 };
3825
David Quigleyaa9c2662013-05-22 12:50:44 -04003826 args.bitmask = nfs4_bitmask(server, label);
3827
David Howells2b3de442006-08-22 20:06:09 -04003828 nfs_fattr_init(fattr);
3829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003831 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 dprintk("NFS reply lookup: %d\n", status);
3833 return status;
3834}
3835
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003836static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003837{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003838 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003839 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003840 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3841 fattr->nlink = 2;
3842}
3843
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003844static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003845 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003846 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003847{
3848 struct nfs4_exception exception = { };
3849 struct rpc_clnt *client = *clnt;
3850 int err;
3851 do {
David Quigley1775fd32013-05-22 12:50:42 -04003852 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003853 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003854 switch (err) {
3855 case -NFS4ERR_BADNAME:
3856 err = -ENOENT;
3857 goto out;
3858 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003859 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003860 if (err == -NFS4ERR_MOVED)
3861 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003862 goto out;
3863 case -NFS4ERR_WRONGSEC:
3864 err = -EPERM;
3865 if (client != *clnt)
3866 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003867 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003868 if (IS_ERR(client))
3869 return PTR_ERR(client);
3870
3871 exception.retry = 1;
3872 break;
3873 default:
3874 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3875 }
3876 } while (exception.retry);
3877
3878out:
3879 if (err == 0)
3880 *clnt = client;
3881 else if (client != *clnt)
3882 rpc_shutdown_client(client);
3883
3884 return err;
3885}
3886
Al Virobeffb8f2016-07-20 16:34:42 -04003887static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003888 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3889 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003891 int status;
3892 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003893
David Quigley1775fd32013-05-22 12:50:42 -04003894 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003895 if (client != NFS_CLIENT(dir)) {
3896 rpc_shutdown_client(client);
3897 nfs_fixup_secinfo_attributes(fattr);
3898 }
3899 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
3901
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003902struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003903nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003904 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3905{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003906 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003907 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003908
David Quigley1775fd32013-05-22 12:50:42 -04003909 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003910 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003911 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003912 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003913}
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3916{
Trond Myklebust76b32992007-08-10 17:45:11 -04003917 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 struct nfs4_accessargs args = {
3919 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003920 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003922 struct nfs4_accessres res = {
3923 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003924 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 struct rpc_message msg = {
3926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3927 .rpc_argp = &args,
3928 .rpc_resp = &res,
3929 .rpc_cred = entry->cred,
3930 };
3931 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003932 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
3934 /*
3935 * Determine which access bits we want to ask for...
3936 */
3937 if (mode & MAY_READ)
3938 args.access |= NFS4_ACCESS_READ;
3939 if (S_ISDIR(inode->i_mode)) {
3940 if (mode & MAY_WRITE)
3941 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3942 if (mode & MAY_EXEC)
3943 args.access |= NFS4_ACCESS_LOOKUP;
3944 } else {
3945 if (mode & MAY_WRITE)
3946 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3947 if (mode & MAY_EXEC)
3948 args.access |= NFS4_ACCESS_EXECUTE;
3949 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003950
3951 res.fattr = nfs_alloc_fattr();
3952 if (res.fattr == NULL)
3953 return -ENOMEM;
3954
Bryan Schumaker7c513052011-03-24 17:12:24 +00003955 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003957 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003958 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003960 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return status;
3962}
3963
3964static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3965{
3966 struct nfs4_exception exception = { };
3967 int err;
3968 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003969 err = _nfs4_proc_access(inode, entry);
3970 trace_nfs4_access(inode, err);
3971 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 &exception);
3973 } while (exception.retry);
3974 return err;
3975}
3976
3977/*
3978 * TODO: For the time being, we don't try to get any attributes
3979 * along with any of the zero-copy operations READ, READDIR,
3980 * READLINK, WRITE.
3981 *
3982 * In the case of the first three, we want to put the GETATTR
3983 * after the read-type operation -- this is because it is hard
3984 * to predict the length of a GETATTR response in v4, and thus
3985 * align the READ data correctly. This means that the GETATTR
3986 * may end up partially falling into the page cache, and we should
3987 * shift it into the 'tail' of the xdr_buf before processing.
3988 * To do this efficiently, we need to know the total length
3989 * of data received, which doesn't seem to be available outside
3990 * of the RPC layer.
3991 *
3992 * In the case of WRITE, we also want to put the GETATTR after
3993 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003994 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 *
3996 * Both of these changes to the XDR layer would in fact be quite
3997 * minor, but I decided to leave them for a subsequent patch.
3998 */
3999static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4000 unsigned int pgbase, unsigned int pglen)
4001{
4002 struct nfs4_readlink args = {
4003 .fh = NFS_FH(inode),
4004 .pgbase = pgbase,
4005 .pglen = pglen,
4006 .pages = &page,
4007 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004008 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 struct rpc_message msg = {
4010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4011 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004012 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 };
4014
Bryan Schumaker7c513052011-03-24 17:12:24 +00004015 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 -07004016}
4017
4018static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4019 unsigned int pgbase, unsigned int pglen)
4020{
4021 struct nfs4_exception exception = { };
4022 int err;
4023 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004024 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4025 trace_nfs4_readlink(inode, err);
4026 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 &exception);
4028 } while (exception.retry);
4029 return err;
4030}
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004033 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035static int
4036nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004037 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004039 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004040 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004041 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 int status = 0;
4044
NeilBrown532d4de2016-10-13 15:26:47 +11004045 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004046 if (IS_ERR(ctx))
4047 return PTR_ERR(ctx);
4048
David Quigleyaa9c2662013-05-22 12:50:44 -04004049 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4050
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004051 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4052 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004053 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (IS_ERR(state)) {
4055 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004059 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004060 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return status;
4062}
4063
Al Virobeffb8f2016-07-20 16:34:42 -04004064static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Trond Myklebust16e42952005-10-27 22:12:44 -04004066 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004067 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004069 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004071 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004072 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004073 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4076 .rpc_argp = &args,
4077 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004079 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004080 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004081
Bryan Schumaker7c513052011-03-24 17:12:24 +00004082 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004083 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004084 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return status;
4086}
4087
Al Virobeffb8f2016-07-20 16:34:42 -04004088static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
4090 struct nfs4_exception exception = { };
4091 int err;
4092 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004093 err = _nfs4_proc_remove(dir, name);
4094 trace_nfs4_remove(dir, name, err);
4095 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 &exception);
4097 } while (exception.retry);
4098 return err;
4099}
4100
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004101static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004103 struct nfs_server *server = NFS_SERVER(dir);
4104 struct nfs_removeargs *args = msg->rpc_argp;
4105 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004107 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004109 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004110
4111 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004114static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4115{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004116 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004117 &data->args.seq_args,
4118 &data->res.seq_res,
4119 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004122static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004124 struct nfs_unlinkdata *data = task->tk_calldata;
4125 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004126
Trond Myklebust14516c32010-07-31 14:29:06 -04004127 if (!nfs4_sequence_done(task, &res->seq_res))
4128 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004129 if (nfs4_async_handle_error(task, res->server, NULL,
4130 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004131 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004132 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004133 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
Jeff Laytond3d41522010-09-17 17:31:57 -04004137static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4138{
4139 struct nfs_server *server = NFS_SERVER(dir);
4140 struct nfs_renameargs *arg = msg->rpc_argp;
4141 struct nfs_renameres *res = msg->rpc_resp;
4142
4143 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004144 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004145 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004146}
4147
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004148static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4149{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004150 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004151 &data->args.seq_args,
4152 &data->res.seq_res,
4153 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004154}
4155
4156static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4157 struct inode *new_dir)
4158{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004159 struct nfs_renamedata *data = task->tk_calldata;
4160 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004161
4162 if (!nfs4_sequence_done(task, &res->seq_res))
4163 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004164 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004165 return 0;
4166
Trond Myklebustc733c492017-01-11 12:32:26 -05004167 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004168 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004169 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004170 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004171 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004172 return 1;
4173}
4174
Al Virobeffb8f2016-07-20 16:34:42 -04004175static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004177 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 struct nfs4_link_arg arg = {
4179 .fh = NFS_FH(inode),
4180 .dir_fh = NFS_FH(dir),
4181 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004182 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004184 struct nfs4_link_res res = {
4185 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004186 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 struct rpc_message msg = {
4189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4190 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004191 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004193 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Trond Myklebust136f2622010-04-16 16:22:49 -04004195 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004196 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004197 goto out;
4198
David Quigley14c43f72013-05-22 12:50:43 -04004199 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4200 if (IS_ERR(res.label)) {
4201 status = PTR_ERR(res.label);
4202 goto out;
4203 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004204 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004205
Bryan Schumaker7c513052011-03-24 17:12:24 +00004206 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004207 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004208 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004209 status = nfs_post_op_update_inode(inode, res.fattr);
4210 if (!status)
4211 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004212 }
David Quigley14c43f72013-05-22 12:50:43 -04004213
4214
4215 nfs4_label_free(res.label);
4216
Trond Myklebust136f2622010-04-16 16:22:49 -04004217out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004218 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 return status;
4220}
4221
Al Virobeffb8f2016-07-20 16:34:42 -04004222static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224 struct nfs4_exception exception = { };
4225 int err;
4226 do {
4227 err = nfs4_handle_exception(NFS_SERVER(inode),
4228 _nfs4_proc_link(inode, dir, name),
4229 &exception);
4230 } while (exception.retry);
4231 return err;
4232}
4233
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234struct nfs4_createdata {
4235 struct rpc_message msg;
4236 struct nfs4_create_arg arg;
4237 struct nfs4_create_res res;
4238 struct nfs_fh fh;
4239 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004240 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241};
4242
4243static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004244 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004245{
4246 struct nfs4_createdata *data;
4247
4248 data = kzalloc(sizeof(*data), GFP_KERNEL);
4249 if (data != NULL) {
4250 struct nfs_server *server = NFS_SERVER(dir);
4251
David Quigley14c43f72013-05-22 12:50:43 -04004252 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4253 if (IS_ERR(data->label))
4254 goto out_free;
4255
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004256 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4257 data->msg.rpc_argp = &data->arg;
4258 data->msg.rpc_resp = &data->res;
4259 data->arg.dir_fh = NFS_FH(dir);
4260 data->arg.server = server;
4261 data->arg.name = name;
4262 data->arg.attrs = sattr;
4263 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004264 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004265 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004266 data->res.server = server;
4267 data->res.fh = &data->fh;
4268 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004269 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004270 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004271 }
4272 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004273out_free:
4274 kfree(data);
4275 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004276}
4277
4278static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4279{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004280 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004281 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004282 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004283 update_changeattr(dir, &data->res.dir_cinfo,
4284 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004285 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004286 }
4287 return status;
4288}
4289
4290static void nfs4_free_createdata(struct nfs4_createdata *data)
4291{
David Quigley14c43f72013-05-22 12:50:43 -04004292 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004293 kfree(data);
4294}
4295
Chuck Lever4f390c12006-08-22 20:06:22 -04004296static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004297 struct page *page, unsigned int len, struct iattr *sattr,
4298 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004300 struct nfs4_createdata *data;
4301 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Chuck Lever94a6d752006-08-22 20:06:23 -04004303 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004304 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004305
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004306 status = -ENOMEM;
4307 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4308 if (data == NULL)
4309 goto out;
4310
4311 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4312 data->arg.u.symlink.pages = &page;
4313 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004314 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004316 status = nfs4_do_create(dir, dentry, data);
4317
4318 nfs4_free_createdata(data);
4319out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return status;
4321}
4322
Chuck Lever4f390c12006-08-22 20:06:22 -04004323static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004324 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
4326 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004327 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004329
4330 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004333 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4334 trace_nfs4_symlink(dir, &dentry->d_name, err);
4335 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 &exception);
4337 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004338
4339 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return err;
4341}
4342
4343static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004344 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004346 struct nfs4_createdata *data;
4347 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4350 if (data == NULL)
4351 goto out;
4352
David Quigley1775fd32013-05-22 12:50:42 -04004353 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004354 status = nfs4_do_create(dir, dentry, data);
4355
4356 nfs4_free_createdata(data);
4357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return status;
4359}
4360
4361static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4362 struct iattr *sattr)
4363{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004364 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004366 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004368
David Quigleyaa9c2662013-05-22 12:50:44 -04004369 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4370
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004371 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4372 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004374 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4375 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4376 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 &exception);
4378 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004379 nfs4_label_release_security(label);
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return err;
4382}
4383
4384static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004385 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
David Howells2b0143b2015-03-17 22:25:59 +00004387 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 struct nfs4_readdir_arg args = {
4389 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004390 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 .pgbase = 0,
4392 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004393 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004394 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 };
4396 struct nfs4_readdir_res res;
4397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4399 .rpc_argp = &args,
4400 .rpc_resp = &res,
4401 .rpc_cred = cred,
4402 };
4403 int status;
4404
Al Viro6de14722013-09-16 10:53:17 -04004405 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4406 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004407 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004408 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004410 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 -05004411 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004412 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004413 status += args.pgbase;
4414 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004415
4416 nfs_invalidate_atime(dir);
4417
Harvey Harrison3110ff82008-05-02 13:42:44 -07004418 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return status;
4420}
4421
4422static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004423 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
4425 struct nfs4_exception exception = { };
4426 int err;
4427 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004428 err = _nfs4_proc_readdir(dentry, cred, cookie,
4429 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004430 trace_nfs4_readdir(d_inode(dentry), err);
4431 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 &exception);
4433 } while (exception.retry);
4434 return err;
4435}
4436
4437static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004438 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004440 struct nfs4_createdata *data;
4441 int mode = sattr->ia_mode;
4442 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004444 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4445 if (data == NULL)
4446 goto out;
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004451 data->arg.ftype = NF4BLK;
4452 data->arg.u.device.specdata1 = MAJOR(rdev);
4453 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004456 data->arg.ftype = NF4CHR;
4457 data->arg.u.device.specdata1 = MAJOR(rdev);
4458 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004459 } else if (!S_ISSOCK(mode)) {
4460 status = -EINVAL;
4461 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
David Quigley1775fd32013-05-22 12:50:42 -04004463
David Quigleyaa9c2662013-05-22 12:50:44 -04004464 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004465 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004466out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004467 nfs4_free_createdata(data);
4468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return status;
4470}
4471
4472static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4473 struct iattr *sattr, dev_t rdev)
4474{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004475 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004477 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004479
David Quigleyaa9c2662013-05-22 12:50:44 -04004480 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4481
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004482 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4483 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004485 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4486 trace_nfs4_mknod(dir, &dentry->d_name, err);
4487 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 &exception);
4489 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004490
4491 nfs4_label_release_security(label);
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return err;
4494}
4495
4496static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4497 struct nfs_fsstat *fsstat)
4498{
4499 struct nfs4_statfs_arg args = {
4500 .fh = fhandle,
4501 .bitmask = server->attr_bitmask,
4502 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004503 struct nfs4_statfs_res res = {
4504 .fsstat = fsstat,
4505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 struct rpc_message msg = {
4507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4508 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004509 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 };
4511
Trond Myklebust0e574af2005-10-27 22:12:38 -04004512 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004513 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4517{
4518 struct nfs4_exception exception = { };
4519 int err;
4520 do {
4521 err = nfs4_handle_exception(server,
4522 _nfs4_proc_statfs(server, fhandle, fsstat),
4523 &exception);
4524 } while (exception.retry);
4525 return err;
4526}
4527
4528static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4529 struct nfs_fsinfo *fsinfo)
4530{
4531 struct nfs4_fsinfo_arg args = {
4532 .fh = fhandle,
4533 .bitmask = server->attr_bitmask,
4534 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004535 struct nfs4_fsinfo_res res = {
4536 .fsinfo = fsinfo,
4537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 struct rpc_message msg = {
4539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4540 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004541 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 };
4543
Bryan Schumaker7c513052011-03-24 17:12:24 +00004544 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
4547static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4548{
4549 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004550 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 int err;
4552
4553 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004554 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004555 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004556 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004557 nfs4_set_lease_period(server->nfs_client,
4558 fsinfo->lease_time * HZ,
4559 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004560 break;
4561 }
4562 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 } while (exception.retry);
4564 return err;
4565}
4566
4567static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4568{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004569 int error;
4570
Trond Myklebust0e574af2005-10-27 22:12:38 -04004571 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004572 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004573 if (error == 0) {
4574 /* block layout checks this! */
4575 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004576 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004577 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004578
4579 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
4581
4582static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4583 struct nfs_pathconf *pathconf)
4584{
4585 struct nfs4_pathconf_arg args = {
4586 .fh = fhandle,
4587 .bitmask = server->attr_bitmask,
4588 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004589 struct nfs4_pathconf_res res = {
4590 .pathconf = pathconf,
4591 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4594 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004595 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 };
4597
4598 /* None of the pathconf attributes are mandatory to implement */
4599 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4600 memset(pathconf, 0, sizeof(*pathconf));
4601 return 0;
4602 }
4603
Trond Myklebust0e574af2005-10-27 22:12:38 -04004604 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004605 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
4607
4608static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4609 struct nfs_pathconf *pathconf)
4610{
4611 struct nfs4_exception exception = { };
4612 int err;
4613
4614 do {
4615 err = nfs4_handle_exception(server,
4616 _nfs4_proc_pathconf(server, fhandle, pathconf),
4617 &exception);
4618 } while (exception.retry);
4619 return err;
4620}
4621
Trond Myklebust5521abf2013-03-16 20:54:34 -04004622int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004623 const struct nfs_open_context *ctx,
4624 const struct nfs_lock_context *l_ctx,
4625 fmode_t fmode)
4626{
NeilBrown17393472016-10-13 15:26:47 +11004627 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004628}
4629EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4630
Trond Myklebust5521abf2013-03-16 20:54:34 -04004631static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4632 const struct nfs_open_context *ctx,
4633 const struct nfs_lock_context *l_ctx,
4634 fmode_t fmode)
4635{
4636 nfs4_stateid current_stateid;
4637
Trond Myklebuste1253be2014-03-05 08:44:23 -05004638 /* If the current stateid represents a lost lock, then exit */
4639 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4640 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004641 return nfs4_stateid_match(stateid, &current_stateid);
4642}
4643
4644static bool nfs4_error_stateid_expired(int err)
4645{
4646 switch (err) {
4647 case -NFS4ERR_DELEG_REVOKED:
4648 case -NFS4ERR_ADMIN_REVOKED:
4649 case -NFS4ERR_BAD_STATEID:
4650 case -NFS4ERR_STALE_STATEID:
4651 case -NFS4ERR_OLD_STATEID:
4652 case -NFS4ERR_OPENMODE:
4653 case -NFS4ERR_EXPIRED:
4654 return true;
4655 }
4656 return false;
4657}
4658
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004659static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004664 if (task->tk_status < 0) {
4665 struct nfs4_exception exception = {
4666 .inode = hdr->inode,
4667 .state = hdr->args.context->state,
4668 .stateid = &hdr->args.stateid,
4669 };
4670 task->tk_status = nfs4_async_handle_exception(task,
4671 server, task->tk_status, &exception);
4672 if (exception.retry) {
4673 rpc_restart_call_prepare(task);
4674 return -EAGAIN;
4675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Trond Myklebust5521abf2013-03-16 20:54:34 -04004683static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004684 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004685{
4686
4687 if (!nfs4_error_stateid_expired(task->tk_status) ||
4688 nfs4_stateid_is_current(&args->stateid,
4689 args->context,
4690 args->lock_context,
4691 FMODE_READ))
4692 return false;
4693 rpc_restart_call_prepare(task);
4694 return true;
4695}
4696
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004698{
4699
4700 dprintk("--> %s\n", __func__);
4701
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004703 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004704 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004705 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004706 if (task->tk_status > 0)
4707 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004708 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4709 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004710}
4711
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4713 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004716 if (!hdr->pgio_done_cb)
4717 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004718 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004722static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4723 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004724{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004725 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 &hdr->args.seq_args,
4727 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004728 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004729 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004730 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4731 hdr->args.lock_context,
4732 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004733 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004735 return -EIO;
4736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737}
4738
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004739static int nfs4_write_done_cb(struct rpc_task *task,
4740 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004742 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004743
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004744 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004745 if (task->tk_status < 0) {
4746 struct nfs4_exception exception = {
4747 .inode = hdr->inode,
4748 .state = hdr->args.context->state,
4749 .stateid = &hdr->args.stateid,
4750 };
4751 task->tk_status = nfs4_async_handle_exception(task,
4752 NFS_SERVER(inode), task->tk_status,
4753 &exception);
4754 if (exception.retry) {
4755 rpc_restart_call_prepare(task);
4756 return -EAGAIN;
4757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004759 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004760 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004761 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004762 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Trond Myklebust5521abf2013-03-16 20:54:34 -04004766static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004767 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004768{
4769
4770 if (!nfs4_error_stateid_expired(task->tk_status) ||
4771 nfs4_stateid_is_current(&args->stateid,
4772 args->context,
4773 args->lock_context,
4774 FMODE_WRITE))
4775 return false;
4776 rpc_restart_call_prepare(task);
4777 return true;
4778}
4779
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004781{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004783 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004784 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004785 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004786 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4787 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004788}
4789
Trond Myklebust5a37f852012-04-28 14:55:16 -04004790static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004791bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004792{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004793 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004794 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004795 return false;
4796 /* Otherwise, request attributes if and only if we don't hold
4797 * a delegation
4798 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004799 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004800}
Fred Isamana69aef12011-03-03 15:13:47 +00004801
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004802static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4803 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004806
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004807 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4808 hdr->args.bitmask = NULL;
4809 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004810 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004811 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004812
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004813 if (!hdr->pgio_done_cb)
4814 hdr->pgio_done_cb = nfs4_write_done_cb;
4815 hdr->res.server = server;
4816 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004818 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004819 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
Fred Isaman0b7c0152012-04-20 14:47:39 -04004822static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4823{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004824 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004825 &data->args.seq_args,
4826 &data->res.seq_res,
4827 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Fred Isaman0b7c0152012-04-20 14:47:39 -04004830static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004833
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004834 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004835 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4836 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004837 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004838 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841}
4842
Fred Isaman0b7c0152012-04-20 14:47:39 -04004843static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004844{
4845 if (!nfs4_sequence_done(task, &data->res.seq_res))
4846 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004847 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004848}
4849
Fred Isaman0b7c0152012-04-20 14:47:39 -04004850static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004852 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004853
Fred Isaman0b7c0152012-04-20 14:47:39 -04004854 if (data->commit_done_cb == NULL)
4855 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004856 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004857 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004858 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004861struct nfs4_renewdata {
4862 struct nfs_client *client;
4863 unsigned long timestamp;
4864};
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866/*
4867 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4868 * standalone procedure for queueing an asynchronous RENEW.
4869 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004870static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004871{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004872 struct nfs4_renewdata *data = calldata;
4873 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004874
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004875 if (atomic_read(&clp->cl_count) > 1)
4876 nfs4_schedule_state_renewal(clp);
4877 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004878 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004879}
4880
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004881static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004883 struct nfs4_renewdata *data = calldata;
4884 struct nfs_client *clp = data->client;
4885 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004887 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004888 switch (task->tk_status) {
4889 case 0:
4890 break;
4891 case -NFS4ERR_LEASE_MOVED:
4892 nfs4_schedule_lease_moved_recovery(clp);
4893 break;
4894 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004895 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004896 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4897 return;
4898 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004899 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004900 return;
4901 }
4902 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004904 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905}
4906
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004907static const struct rpc_call_ops nfs4_renew_ops = {
4908 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004909 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004910};
4911
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004912static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
4914 struct rpc_message msg = {
4915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4916 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004917 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004919 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004921 if (renew_flags == 0)
4922 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004923 if (!atomic_inc_not_zero(&clp->cl_count))
4924 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004925 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004926 if (data == NULL)
4927 return -ENOMEM;
4928 data->client = clp;
4929 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004930 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004931 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932}
4933
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004934static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
4936 struct rpc_message msg = {
4937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4938 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004939 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 };
4941 unsigned long now = jiffies;
4942 int status;
4943
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004944 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (status < 0)
4946 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004947 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return 0;
4949}
4950
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004951static inline int nfs4_server_supports_acls(struct nfs_server *server)
4952{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004953 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004954}
4955
Trond Myklebust21f498c2012-08-24 10:59:25 -04004956/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4957 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004958 * the stack.
4959 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004960#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004961
Neil Hormane9e3d722011-03-04 19:26:03 -05004962static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004963 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004964{
4965 struct page *newpage, **spages;
4966 int rc = 0;
4967 size_t len;
4968 spages = pages;
4969
4970 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004971 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004972 newpage = alloc_page(GFP_KERNEL);
4973
4974 if (newpage == NULL)
4975 goto unwind;
4976 memcpy(page_address(newpage), buf, len);
4977 buf += len;
4978 buflen -= len;
4979 *pages++ = newpage;
4980 rc++;
4981 } while (buflen != 0);
4982
4983 return rc;
4984
4985unwind:
4986 for(; rc > 0; rc--)
4987 __free_page(spages[rc-1]);
4988 return -ENOMEM;
4989}
4990
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004991struct nfs4_cached_acl {
4992 int cached;
4993 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004994 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004995};
4996
4997static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004998{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004999 struct nfs_inode *nfsi = NFS_I(inode);
5000
5001 spin_lock(&inode->i_lock);
5002 kfree(nfsi->nfs4_acl);
5003 nfsi->nfs4_acl = acl;
5004 spin_unlock(&inode->i_lock);
5005}
5006
5007static void nfs4_zap_acl_attr(struct inode *inode)
5008{
5009 nfs4_set_cached_acl(inode, NULL);
5010}
5011
5012static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5013{
5014 struct nfs_inode *nfsi = NFS_I(inode);
5015 struct nfs4_cached_acl *acl;
5016 int ret = -ENOENT;
5017
5018 spin_lock(&inode->i_lock);
5019 acl = nfsi->nfs4_acl;
5020 if (acl == NULL)
5021 goto out;
5022 if (buf == NULL) /* user is just asking for length */
5023 goto out_len;
5024 if (acl->cached == 0)
5025 goto out;
5026 ret = -ERANGE; /* see getxattr(2) man page */
5027 if (acl->len > buflen)
5028 goto out;
5029 memcpy(buf, acl->data, acl->len);
5030out_len:
5031 ret = acl->len;
5032out:
5033 spin_unlock(&inode->i_lock);
5034 return ret;
5035}
5036
Sachin Prabhu5794d212012-04-17 14:36:40 +01005037static 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 +00005038{
5039 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005040 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005041
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005042 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005043 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005044 if (acl == NULL)
5045 goto out;
5046 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005047 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005048 } else {
5049 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5050 if (acl == NULL)
5051 goto out;
5052 acl->cached = 0;
5053 }
5054 acl->len = acl_len;
5055out:
5056 nfs4_set_cached_acl(inode, acl);
5057}
5058
Andy Adamsonbf118a32011-12-07 11:55:27 -05005059/*
5060 * The getxattr API returns the required buffer length when called with a
5061 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5062 * the required buf. On a NULL buf, we send a page of data to the server
5063 * guessing that the ACL request can be serviced by a page. If so, we cache
5064 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5065 * the cache. If not so, we throw away the page, and cache the required
5066 * length. The next getxattr call will then produce another round trip to
5067 * the server, this time with the input buf of the required size.
5068 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005069static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005070{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005071 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005072 struct nfs_getaclargs args = {
5073 .fh = NFS_FH(inode),
5074 .acl_pages = pages,
5075 .acl_len = buflen,
5076 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005077 struct nfs_getaclres res = {
5078 .acl_len = buflen,
5079 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5082 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005083 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005084 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005085 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5086 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005087
Andy Adamsonbf118a32011-12-07 11:55:27 -05005088 /* As long as we're doing a round trip to the server anyway,
5089 * let's be prepared for a page of acl data. */
5090 if (npages == 0)
5091 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005092 if (npages > ARRAY_SIZE(pages))
5093 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005094
Andy Adamsonbf118a32011-12-07 11:55:27 -05005095 for (i = 0; i < npages; i++) {
5096 pages[i] = alloc_page(GFP_KERNEL);
5097 if (!pages[i])
5098 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005099 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005100
5101 /* for decoding across pages */
5102 res.acl_scratch = alloc_page(GFP_KERNEL);
5103 if (!res.acl_scratch)
5104 goto out_free;
5105
Andy Adamsonbf118a32011-12-07 11:55:27 -05005106 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005107
Peng Taode040be2012-01-10 22:42:47 +08005108 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005109 __func__, buf, buflen, npages, args.acl_len);
5110 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5111 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005112 if (ret)
5113 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005114
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005115 /* Handle the case where the passed-in buffer is too short */
5116 if (res.acl_flags & NFS4_ACL_TRUNC) {
5117 /* Did the user only issue a request for the acl length? */
5118 if (buf == NULL)
5119 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005120 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005121 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005122 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005123 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005124 if (buf) {
5125 if (res.acl_len > buflen) {
5126 ret = -ERANGE;
5127 goto out_free;
5128 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005129 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005130 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005131out_ok:
5132 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005133out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005134 for (i = 0; i < npages; i++)
5135 if (pages[i])
5136 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005137 if (res.acl_scratch)
5138 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005139 return ret;
5140}
5141
Trond Myklebust16b42892006-08-24 12:27:15 -04005142static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5143{
5144 struct nfs4_exception exception = { };
5145 ssize_t ret;
5146 do {
5147 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005148 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005149 if (ret >= 0)
5150 break;
5151 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5152 } while (exception.retry);
5153 return ret;
5154}
5155
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005156static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5157{
5158 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005159 int ret;
5160
5161 if (!nfs4_server_supports_acls(server))
5162 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005163 ret = nfs_revalidate_inode(server, inode);
5164 if (ret < 0)
5165 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005166 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5167 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005168 ret = nfs4_read_cached_acl(inode, buf, buflen);
5169 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005170 /* -ENOENT is returned if there is no ACL or if there is an ACL
5171 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005172 return ret;
5173 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005174}
5175
Trond Myklebust16b42892006-08-24 12:27:15 -04005176static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005177{
5178 struct nfs_server *server = NFS_SERVER(inode);
5179 struct page *pages[NFS4ACL_MAXPAGES];
5180 struct nfs_setaclargs arg = {
5181 .fh = NFS_FH(inode),
5182 .acl_pages = pages,
5183 .acl_len = buflen,
5184 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005185 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005186 struct rpc_message msg = {
5187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5188 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005189 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005190 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005191 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005192 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005193
5194 if (!nfs4_server_supports_acls(server))
5195 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005196 if (npages > ARRAY_SIZE(pages))
5197 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005198 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005199 if (i < 0)
5200 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005201 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005202 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005203
5204 /*
5205 * Free each page after tx, so the only ref left is
5206 * held by the network stack
5207 */
5208 for (; i > 0; i--)
5209 put_page(pages[i-1]);
5210
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005211 /*
5212 * Acl update can result in inode attribute update.
5213 * so mark the attribute cache invalid.
5214 */
5215 spin_lock(&inode->i_lock);
5216 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5217 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005218 nfs_access_zap_cache(inode);
5219 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005220 return ret;
5221}
5222
Trond Myklebust16b42892006-08-24 12:27:15 -04005223static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5224{
5225 struct nfs4_exception exception = { };
5226 int err;
5227 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005228 err = __nfs4_proc_set_acl(inode, buf, buflen);
5229 trace_nfs4_set_acl(inode, err);
5230 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005231 &exception);
5232 } while (exception.retry);
5233 return err;
5234}
5235
David Quigleyaa9c2662013-05-22 12:50:44 -04005236#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5237static int _nfs4_get_security_label(struct inode *inode, void *buf,
5238 size_t buflen)
5239{
5240 struct nfs_server *server = NFS_SERVER(inode);
5241 struct nfs_fattr fattr;
5242 struct nfs4_label label = {0, 0, buflen, buf};
5243
5244 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005245 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005246 .fh = NFS_FH(inode),
5247 .bitmask = bitmask,
5248 };
5249 struct nfs4_getattr_res res = {
5250 .fattr = &fattr,
5251 .label = &label,
5252 .server = server,
5253 };
5254 struct rpc_message msg = {
5255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005256 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005257 .rpc_resp = &res,
5258 };
5259 int ret;
5260
5261 nfs_fattr_init(&fattr);
5262
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005263 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005264 if (ret)
5265 return ret;
5266 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5267 return -ENOENT;
5268 if (buflen < label.len)
5269 return -ERANGE;
5270 return 0;
5271}
5272
5273static int nfs4_get_security_label(struct inode *inode, void *buf,
5274 size_t buflen)
5275{
5276 struct nfs4_exception exception = { };
5277 int err;
5278
5279 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5280 return -EOPNOTSUPP;
5281
5282 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005283 err = _nfs4_get_security_label(inode, buf, buflen);
5284 trace_nfs4_get_security_label(inode, err);
5285 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005286 &exception);
5287 } while (exception.retry);
5288 return err;
5289}
5290
5291static int _nfs4_do_set_security_label(struct inode *inode,
5292 struct nfs4_label *ilabel,
5293 struct nfs_fattr *fattr,
5294 struct nfs4_label *olabel)
5295{
5296
5297 struct iattr sattr = {0};
5298 struct nfs_server *server = NFS_SERVER(inode);
5299 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005300 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005301 .fh = NFS_FH(inode),
5302 .iap = &sattr,
5303 .server = server,
5304 .bitmask = bitmask,
5305 .label = ilabel,
5306 };
5307 struct nfs_setattrres res = {
5308 .fattr = fattr,
5309 .label = olabel,
5310 .server = server,
5311 };
5312 struct rpc_message msg = {
5313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005314 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005315 .rpc_resp = &res,
5316 };
5317 int status;
5318
Jeff Layton12207f62013-11-01 10:49:32 -04005319 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005320
Jeff Layton12207f62013-11-01 10:49:32 -04005321 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005322 if (status)
5323 dprintk("%s failed: %d\n", __func__, status);
5324
5325 return status;
5326}
5327
5328static int nfs4_do_set_security_label(struct inode *inode,
5329 struct nfs4_label *ilabel,
5330 struct nfs_fattr *fattr,
5331 struct nfs4_label *olabel)
5332{
5333 struct nfs4_exception exception = { };
5334 int err;
5335
5336 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005337 err = _nfs4_do_set_security_label(inode, ilabel,
5338 fattr, olabel);
5339 trace_nfs4_set_security_label(inode, err);
5340 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005341 &exception);
5342 } while (exception.retry);
5343 return err;
5344}
5345
5346static int
Al Viro59301222016-05-27 10:19:30 -04005347nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005348{
5349 struct nfs4_label ilabel, *olabel = NULL;
5350 struct nfs_fattr fattr;
5351 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005352 int status;
5353
5354 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5355 return -EOPNOTSUPP;
5356
5357 nfs_fattr_init(&fattr);
5358
5359 ilabel.pi = 0;
5360 ilabel.lfs = 0;
5361 ilabel.label = (char *)buf;
5362 ilabel.len = buflen;
5363
5364 cred = rpc_lookup_cred();
5365 if (IS_ERR(cred))
5366 return PTR_ERR(cred);
5367
5368 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5369 if (IS_ERR(olabel)) {
5370 status = -PTR_ERR(olabel);
5371 goto out;
5372 }
5373
5374 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5375 if (status == 0)
5376 nfs_setsecurity(inode, &fattr, olabel);
5377
5378 nfs4_label_free(olabel);
5379out:
5380 put_rpccred(cred);
5381 return status;
5382}
5383#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5384
5385
Chuck Leverf0920752012-05-21 22:45:41 -04005386static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5387 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005388{
5389 __be32 verf[2];
5390
Chuck Lever2c820d92012-05-21 22:45:33 -04005391 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5392 /* An impossible timestamp guarantees this value
5393 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005394 verf[0] = cpu_to_be32(U32_MAX);
5395 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005396 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005397 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005398 u64 ns = ktime_to_ns(nn->boot_time);
5399
5400 verf[0] = cpu_to_be32(ns >> 32);
5401 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005402 }
Chuck Levercd937102012-03-02 17:14:31 -05005403 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5404}
5405
Jeff Laytona3192682015-06-09 19:43:59 -04005406static int
5407nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005408{
Jeff Laytona3192682015-06-09 19:43:59 -04005409 size_t len;
5410 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005411
Trond Myklebustceb3a162015-01-03 15:16:04 -05005412 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005413 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005414
Jeff Laytona3192682015-06-09 19:43:59 -04005415 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005416 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005417 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5418 1 +
5419 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5420 1;
5421 rcu_read_unlock();
5422
5423 if (len > NFS4_OPAQUE_LIMIT + 1)
5424 return -EINVAL;
5425
5426 /*
5427 * Since this string is allocated at mount time, and held until the
5428 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5429 * about a memory-reclaim deadlock.
5430 */
5431 str = kmalloc(len, GFP_KERNEL);
5432 if (!str)
5433 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005434
Chuck Levere984a552012-09-14 17:24:21 -04005435 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005436 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005437 clp->cl_ipaddr,
5438 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5439 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005440 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005441
Jeff Laytona3192682015-06-09 19:43:59 -04005442 clp->cl_owner_id = str;
5443 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005444}
5445
Jeff Layton873e3852015-06-09 19:44:00 -04005446static int
5447nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005448{
Jeff Layton873e3852015-06-09 19:44:00 -04005449 size_t len;
5450 char *str;
5451
5452 len = 10 + 10 + 1 + 10 + 1 +
5453 strlen(nfs4_client_id_uniquifier) + 1 +
5454 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5455
5456 if (len > NFS4_OPAQUE_LIMIT + 1)
5457 return -EINVAL;
5458
5459 /*
5460 * Since this string is allocated at mount time, and held until the
5461 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5462 * about a memory-reclaim deadlock.
5463 */
5464 str = kmalloc(len, GFP_KERNEL);
5465 if (!str)
5466 return -ENOMEM;
5467
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005468 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005469 clp->rpc_ops->version, clp->cl_minorversion,
5470 nfs4_client_id_uniquifier,
5471 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005472 clp->cl_owner_id = str;
5473 return 0;
5474}
5475
5476static int
5477nfs4_init_uniform_client_string(struct nfs_client *clp)
5478{
Jeff Layton873e3852015-06-09 19:44:00 -04005479 size_t len;
5480 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005481
5482 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005483 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005484
5485 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005486 return nfs4_init_uniquifier_client_string(clp);
5487
5488 len = 10 + 10 + 1 + 10 + 1 +
5489 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5490
5491 if (len > NFS4_OPAQUE_LIMIT + 1)
5492 return -EINVAL;
5493
5494 /*
5495 * Since this string is allocated at mount time, and held until the
5496 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5497 * about a memory-reclaim deadlock.
5498 */
5499 str = kmalloc(len, GFP_KERNEL);
5500 if (!str)
5501 return -ENOMEM;
5502
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005503 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005504 clp->rpc_ops->version, clp->cl_minorversion,
5505 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005506 clp->cl_owner_id = str;
5507 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005508}
5509
Chuck Lever706cb8d2014-03-12 12:51:47 -04005510/*
5511 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5512 * services. Advertise one based on the address family of the
5513 * clientaddr.
5514 */
5515static unsigned int
5516nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5517{
5518 if (strchr(clp->cl_ipaddr, ':') != NULL)
5519 return scnprintf(buf, len, "tcp6");
5520 else
5521 return scnprintf(buf, len, "tcp");
5522}
5523
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005524static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5525{
5526 struct nfs4_setclientid *sc = calldata;
5527
5528 if (task->tk_status == 0)
5529 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5530}
5531
5532static const struct rpc_call_ops nfs4_setclientid_ops = {
5533 .rpc_call_done = nfs4_setclientid_done,
5534};
5535
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005536/**
5537 * nfs4_proc_setclientid - Negotiate client ID
5538 * @clp: state data structure
5539 * @program: RPC program for NFSv4 callback service
5540 * @port: IP port number for NFS4 callback service
5541 * @cred: RPC credential to use for this call
5542 * @res: where to place the result
5543 *
5544 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5545 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005546int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5547 unsigned short port, struct rpc_cred *cred,
5548 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
5550 nfs4_verifier sc_verifier;
5551 struct nfs4_setclientid setclientid = {
5552 .sc_verifier = &sc_verifier,
5553 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005554 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 };
5556 struct rpc_message msg = {
5557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5558 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005559 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005560 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005562 struct rpc_task *task;
5563 struct rpc_task_setup task_setup_data = {
5564 .rpc_client = clp->cl_rpcclient,
5565 .rpc_message = &msg,
5566 .callback_ops = &nfs4_setclientid_ops,
5567 .callback_data = &setclientid,
5568 .flags = RPC_TASK_TIMEOUT,
5569 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005570 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Chuck Leverde734832012-07-11 16:30:50 -04005572 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005573 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005574
5575 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5576 status = nfs4_init_uniform_client_string(clp);
5577 else
Jeff Laytona3192682015-06-09 19:43:59 -04005578 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005579
5580 if (status)
5581 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005582
Chuck Leverde734832012-07-11 16:30:50 -04005583 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005584 setclientid.sc_netid_len =
5585 nfs4_init_callback_netid(clp,
5586 setclientid.sc_netid,
5587 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005588 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005589 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 clp->cl_ipaddr, port >> 8, port & 255);
5591
Jeff Layton3a6bb732015-06-09 19:43:57 -04005592 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005593 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005594 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005595 task = rpc_run_task(&task_setup_data);
5596 if (IS_ERR(task)) {
5597 status = PTR_ERR(task);
5598 goto out;
5599 }
5600 status = task->tk_status;
5601 if (setclientid.sc_cred) {
5602 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5603 put_rpccred(setclientid.sc_cred);
5604 }
5605 rpc_put_task(task);
5606out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005607 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005608 dprintk("NFS reply setclientid: %d\n", status);
5609 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610}
5611
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005612/**
5613 * nfs4_proc_setclientid_confirm - Confirm client ID
5614 * @clp: state data structure
5615 * @res: result of a previous SETCLIENTID
5616 * @cred: RPC credential to use for this call
5617 *
5618 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5619 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005620int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005621 struct nfs4_setclientid_res *arg,
5622 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 struct rpc_message msg = {
5625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005626 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005627 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 int status;
5630
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005631 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5632 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5633 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005634 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005635 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005636 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 return status;
5638}
5639
Trond Myklebustfe650402006-01-03 09:55:18 +01005640struct nfs4_delegreturndata {
5641 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005642 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005643 struct nfs_fh fh;
5644 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005645 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005646 struct {
5647 struct nfs4_layoutreturn_args arg;
5648 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005649 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005650 u32 roc_barrier;
5651 bool roc;
5652 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005653 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005654 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005655 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005656};
5657
Trond Myklebustfe650402006-01-03 09:55:18 +01005658static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5659{
5660 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005661
Trond Myklebust14516c32010-07-31 14:29:06 -04005662 if (!nfs4_sequence_done(task, &data->res.seq_res))
5663 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005664
Trond Myklebustca8acf82013-08-13 10:36:56 -04005665 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005666
5667 /* Handle Layoutreturn errors */
5668 if (data->args.lr_args && task->tk_status != 0) {
5669 switch(data->res.lr_ret) {
5670 default:
5671 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5672 break;
5673 case 0:
5674 data->args.lr_args = NULL;
5675 data->res.lr_res = NULL;
5676 break;
5677 case -NFS4ERR_ADMIN_REVOKED:
5678 case -NFS4ERR_DELEG_REVOKED:
5679 case -NFS4ERR_EXPIRED:
5680 case -NFS4ERR_BAD_STATEID:
5681 case -NFS4ERR_OLD_STATEID:
5682 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5683 case -NFS4ERR_WRONG_CRED:
5684 data->args.lr_args = NULL;
5685 data->res.lr_res = NULL;
5686 data->res.lr_ret = 0;
5687 rpc_restart_call_prepare(task);
5688 return;
5689 }
5690 }
5691
Ricardo Labiaga79708862009-12-07 09:23:21 -05005692 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005693 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005694 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005695 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005696 case -NFS4ERR_ADMIN_REVOKED:
5697 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005698 case -NFS4ERR_EXPIRED:
5699 nfs4_free_revoked_stateid(data->res.server,
5700 data->args.stateid,
5701 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005702 case -NFS4ERR_BAD_STATEID:
5703 case -NFS4ERR_OLD_STATEID:
5704 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005705 task->tk_status = 0;
5706 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005707 case -NFS4ERR_ACCESS:
5708 if (data->args.bitmask) {
5709 data->args.bitmask = NULL;
5710 data->res.fattr = NULL;
5711 task->tk_status = 0;
5712 rpc_restart_call_prepare(task);
5713 return;
5714 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005715 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005716 if (nfs4_async_handle_error(task, data->res.server,
5717 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005718 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005719 return;
5720 }
5721 }
5722 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005723}
5724
5725static void nfs4_delegreturn_release(void *calldata)
5726{
Peng Tao039b7562014-07-03 13:05:02 +08005727 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005728 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005729
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005730 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005731 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005732 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5733 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005734 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005735 nfs_iput_and_deactive(inode);
5736 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005737 kfree(calldata);
5738}
5739
Andy Adamson938e1012009-04-01 09:22:28 -04005740static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5741{
5742 struct nfs4_delegreturndata *d_data;
5743
5744 d_data = (struct nfs4_delegreturndata *)data;
5745
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005746 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005747 return;
5748
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005749 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005750 &d_data->args.seq_args,
5751 &d_data->res.seq_res,
5752 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005753}
Andy Adamson938e1012009-04-01 09:22:28 -04005754
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005755static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005756 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005757 .rpc_call_done = nfs4_delegreturn_done,
5758 .rpc_release = nfs4_delegreturn_release,
5759};
5760
Trond Myklebuste6f81072008-01-24 18:14:34 -05005761static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005762{
5763 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005764 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005765 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005766 struct rpc_message msg = {
5767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5768 .rpc_cred = cred,
5769 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005770 struct rpc_task_setup task_setup_data = {
5771 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005772 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005773 .callback_ops = &nfs4_delegreturn_ops,
5774 .flags = RPC_TASK_ASYNC,
5775 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005776 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005777
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005778 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005779 if (data == NULL)
5780 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005782
5783 nfs4_state_protect(server->nfs_client,
5784 NFS_SP4_MACH_CRED_CLEANUP,
5785 &task_setup_data.rpc_client, &msg);
5786
Trond Myklebustfe650402006-01-03 09:55:18 +01005787 data->args.fhandle = &data->fh;
5788 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005789 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005790 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005791 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005792 data->res.fattr = &data->fattr;
5793 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005794 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005795 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005796 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005797 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005798 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005799 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005800 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005801 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005802 if (data->lr.roc) {
5803 data->args.lr_args = &data->lr.arg;
5804 data->res.lr_res = &data->lr.res;
5805 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005806 } else if (data->lr.roc) {
5807 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5808 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005809 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005810
Trond Myklebustc970aa82007-07-14 15:39:59 -04005811 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005812 msg.rpc_argp = &data->args;
5813 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005814 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005815 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005816 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005817 if (!issync)
5818 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005819 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005820 if (status != 0)
5821 goto out;
5822 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005823out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005824 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005825 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
Trond Myklebuste6f81072008-01-24 18:14:34 -05005828int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
5830 struct nfs_server *server = NFS_SERVER(inode);
5831 struct nfs4_exception exception = { };
5832 int err;
5833 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005834 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005835 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 switch (err) {
5837 case -NFS4ERR_STALE_STATEID:
5838 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 case 0:
5840 return 0;
5841 }
5842 err = nfs4_handle_exception(server, err, &exception);
5843 } while (exception.retry);
5844 return err;
5845}
5846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5848{
5849 struct inode *inode = state->inode;
5850 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005851 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005852 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005854 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005856 struct nfs_lockt_res res = {
5857 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 };
5859 struct rpc_message msg = {
5860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5861 .rpc_argp = &arg,
5862 .rpc_resp = &res,
5863 .rpc_cred = state->owner->so_cred,
5864 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 struct nfs4_lock_state *lsp;
5866 int status;
5867
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005868 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005869 status = nfs4_set_lock_state(state, request);
5870 if (status != 0)
5871 goto out;
5872 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005873 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005874 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005875 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005876 switch (status) {
5877 case 0:
5878 request->fl_type = F_UNLCK;
5879 break;
5880 case -NFS4ERR_DENIED:
5881 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005883 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005884 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005885out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 return status;
5887}
5888
5889static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5890{
5891 struct nfs4_exception exception = { };
5892 int err;
5893
5894 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005895 err = _nfs4_proc_getlk(state, cmd, request);
5896 trace_nfs4_get_lock(request, state, cmd, err);
5897 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 &exception);
5899 } while (exception.retry);
5900 return err;
5901}
5902
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005903struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005904 struct nfs_locku_args arg;
5905 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005906 struct nfs4_lock_state *lsp;
5907 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005908 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005909 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005910 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005911};
5912
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005913static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5914 struct nfs_open_context *ctx,
5915 struct nfs4_lock_state *lsp,
5916 struct nfs_seqid *seqid)
5917{
5918 struct nfs4_unlockdata *p;
5919 struct inode *inode = lsp->ls_state->inode;
5920
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005921 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005922 if (p == NULL)
5923 return NULL;
5924 p->arg.fh = NFS_FH(inode);
5925 p->arg.fl = &p->fl;
5926 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005927 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005928 p->lsp = lsp;
5929 atomic_inc(&lsp->ls_count);
5930 /* Ensure we don't close file until we're done freeing locks! */
5931 p->ctx = get_nfs_open_context(ctx);
5932 memcpy(&p->fl, fl, sizeof(p->fl));
5933 p->server = NFS_SERVER(inode);
5934 return p;
5935}
5936
Trond Myklebust06f814a2006-01-03 09:55:07 +01005937static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005938{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005939 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005940 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005941 nfs4_put_lock_state(calldata->lsp);
5942 put_nfs_open_context(calldata->ctx);
5943 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005944}
5945
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005946static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005947{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005948 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005949
Trond Myklebust14516c32010-07-31 14:29:06 -04005950 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5951 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005952 switch (task->tk_status) {
5953 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005954 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005955 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005956 if (nfs4_update_lock_stateid(calldata->lsp,
5957 &calldata->res.stateid))
5958 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005959 case -NFS4ERR_ADMIN_REVOKED:
5960 case -NFS4ERR_EXPIRED:
5961 nfs4_free_revoked_stateid(calldata->server,
5962 &calldata->arg.stateid,
5963 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005964 case -NFS4ERR_BAD_STATEID:
5965 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005966 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005967 if (!nfs4_stateid_match(&calldata->arg.stateid,
5968 &calldata->lsp->ls_stateid))
5969 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005970 break;
5971 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005972 if (nfs4_async_handle_error(task, calldata->server,
5973 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005974 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005975 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005976 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005977}
5978
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005979static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005980{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005981 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005983 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005984 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005985 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005986 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005987 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005988 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005989 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005990 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005991 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005992 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005993 &calldata->res.seq_res,
5994 task) != 0)
5995 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005996 return;
5997out_no_action:
5998 task->tk_action = NULL;
5999out_wait:
6000 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001}
6002
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006003static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006004 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006005 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006006 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006007};
6008
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006009static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6010 struct nfs_open_context *ctx,
6011 struct nfs4_lock_state *lsp,
6012 struct nfs_seqid *seqid)
6013{
6014 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006015 struct rpc_message msg = {
6016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6017 .rpc_cred = ctx->cred,
6018 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006019 struct rpc_task_setup task_setup_data = {
6020 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006021 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006022 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006023 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006024 .flags = RPC_TASK_ASYNC,
6025 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006026
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006027 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6028 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6029
Frank Filz137d6ac2007-07-09 15:32:29 -07006030 /* Ensure this is an unlock - when canceling a lock, the
6031 * canceled lock is passed in, and it won't be an unlock.
6032 */
6033 fl->fl_type = F_UNLCK;
6034
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6036 if (data == NULL) {
6037 nfs_free_seqid(seqid);
6038 return ERR_PTR(-ENOMEM);
6039 }
6040
Chuck Levera9c92d62013-08-09 12:48:18 -04006041 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006042 msg.rpc_argp = &data->arg;
6043 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006044 task_setup_data.callback_data = data;
6045 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006046}
6047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6049{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006050 struct inode *inode = state->inode;
6051 struct nfs4_state_owner *sp = state->owner;
6052 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006053 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006054 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006055 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006056 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006057 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006058 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Trond Myklebust9b073572006-06-29 16:38:34 -04006060 status = nfs4_set_lock_state(state, request);
6061 /* Unlock _before_ we do the RPC call */
6062 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006063 /* Exclude nfs_delegation_claim_locks() */
6064 mutex_lock(&sp->so_delegreturn_mutex);
6065 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006066 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006067 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006068 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006069 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006070 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006071 }
6072 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006073 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006074 if (status != 0)
6075 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006076 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006077 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006078 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6079 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006080 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6081 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006082 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006083 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006084 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006085 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006086 status = PTR_ERR(task);
6087 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006088 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006090 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006091out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006092 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006093 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006094 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095}
6096
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097struct nfs4_lockdata {
6098 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006099 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006100 struct nfs4_lock_state *lsp;
6101 struct nfs_open_context *ctx;
6102 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006103 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104 int rpc_status;
6105 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006106 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006107};
6108
6109static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006110 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6111 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006112{
6113 struct nfs4_lockdata *p;
6114 struct inode *inode = lsp->ls_state->inode;
6115 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006116 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006117
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006118 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006119 if (p == NULL)
6120 return NULL;
6121
6122 p->arg.fh = NFS_FH(inode);
6123 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006124 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006125 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006126 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006127 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6128 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006129 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006130 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006131 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006132 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006133 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006134 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006136 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006137 atomic_inc(&lsp->ls_count);
6138 p->ctx = get_nfs_open_context(ctx);
6139 memcpy(&p->fl, fl, sizeof(p->fl));
6140 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006141out_free_seqid:
6142 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006143out_free:
6144 kfree(p);
6145 return NULL;
6146}
6147
6148static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6149{
6150 struct nfs4_lockdata *data = calldata;
6151 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Harvey Harrison3110ff82008-05-02 13:42:44 -07006153 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006154 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006155 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006156 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006157 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006158 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006159 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006160 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006161 nfs4_stateid_copy(&data->arg.open_stateid,
6162 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006163 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006164 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006165 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006166 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006167 nfs4_stateid_copy(&data->arg.lock_stateid,
6168 &data->lsp->ls_stateid);
6169 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006170 if (!nfs4_valid_open_stateid(state)) {
6171 data->rpc_status = -EBADF;
6172 task->tk_action = NULL;
6173 goto out_release_open_seqid;
6174 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006175 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006176 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168a2010-06-16 09:52:26 -04006177 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006178 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006179 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006180 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006181out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006182 nfs_release_seqid(data->arg.open_seqid);
6183out_release_lock_seqid:
6184 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006185out_wait:
6186 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006187 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006188}
6189
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6191{
6192 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006193 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006194
Harvey Harrison3110ff82008-05-02 13:42:44 -07006195 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006196
Trond Myklebust14516c32010-07-31 14:29:06 -04006197 if (!nfs4_sequence_done(task, &data->res.seq_res))
6198 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006199
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006201 switch (task->tk_status) {
6202 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006203 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006204 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006205 if (data->arg.new_lock) {
6206 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006207 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006208 rpc_restart_call_prepare(task);
6209 break;
6210 }
6211 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006212 if (data->arg.new_lock_owner != 0) {
6213 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6214 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6215 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6216 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6217 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006218 break;
6219 case -NFS4ERR_BAD_STATEID:
6220 case -NFS4ERR_OLD_STATEID:
6221 case -NFS4ERR_STALE_STATEID:
6222 case -NFS4ERR_EXPIRED:
6223 if (data->arg.new_lock_owner != 0) {
6224 if (!nfs4_stateid_match(&data->arg.open_stateid,
6225 &lsp->ls_state->open_stateid))
6226 rpc_restart_call_prepare(task);
6227 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6228 &lsp->ls_stateid))
6229 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006231 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006232}
6233
6234static void nfs4_lock_release(void *calldata)
6235{
6236 struct nfs4_lockdata *data = calldata;
6237
Harvey Harrison3110ff82008-05-02 13:42:44 -07006238 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006239 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006240 if (data->cancelled != 0) {
6241 struct rpc_task *task;
6242 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6243 data->arg.lock_seqid);
6244 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006245 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006246 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006247 } else
6248 nfs_free_seqid(data->arg.lock_seqid);
6249 nfs4_put_lock_state(data->lsp);
6250 put_nfs_open_context(data->ctx);
6251 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006252 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006253}
6254
6255static const struct rpc_call_ops nfs4_lock_ops = {
6256 .rpc_call_prepare = nfs4_lock_prepare,
6257 .rpc_call_done = nfs4_lock_done,
6258 .rpc_release = nfs4_lock_release,
6259};
6260
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006261static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6262{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006263 switch (error) {
6264 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006265 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006266 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006267 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006268 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006269 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006270 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006271 break;
6272 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006273 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006274 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006275 };
6276}
6277
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006278static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006279{
6280 struct nfs4_lockdata *data;
6281 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006282 struct rpc_message msg = {
6283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6284 .rpc_cred = state->owner->so_cred,
6285 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006286 struct rpc_task_setup task_setup_data = {
6287 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006288 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006289 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006290 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006291 .flags = RPC_TASK_ASYNC,
6292 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 int ret;
6294
Harvey Harrison3110ff82008-05-02 13:42:44 -07006295 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006296 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006297 fl->fl_u.nfs4_fl.owner,
6298 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006299 if (data == NULL)
6300 return -ENOMEM;
6301 if (IS_SETLKW(cmd))
6302 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006303 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006304 msg.rpc_argp = &data->arg;
6305 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006306 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006307 if (recovery_type > NFS_LOCK_NEW) {
6308 if (recovery_type == NFS_LOCK_RECLAIM)
6309 data->arg.reclaim = NFS_LOCK_RECLAIM;
6310 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006311 } else
6312 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006313 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006314 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006316 ret = nfs4_wait_for_completion_rpc_task(task);
6317 if (ret == 0) {
6318 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006319 if (ret)
6320 nfs4_handle_setlk_error(data->server, data->lsp,
6321 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006322 } else
6323 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006324 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006325 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006326 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328}
6329
6330static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6331{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006332 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006333 struct nfs4_exception exception = {
6334 .inode = state->inode,
6335 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006336 int err;
6337
6338 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006339 /* Cache the lock if possible... */
6340 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6341 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006342 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006343 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006344 break;
6345 nfs4_handle_exception(server, err, &exception);
6346 } while (exception.retry);
6347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348}
6349
6350static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6351{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006352 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006353 struct nfs4_exception exception = {
6354 .inode = state->inode,
6355 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006356 int err;
6357
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006358 err = nfs4_set_lock_state(state, request);
6359 if (err != 0)
6360 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006361 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006362 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6363 return 0;
6364 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006365 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006366 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6367 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006368 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006369 switch (err) {
6370 default:
6371 goto out;
6372 case -NFS4ERR_GRACE:
6373 case -NFS4ERR_DELAY:
6374 nfs4_handle_exception(server, err, &exception);
6375 err = 0;
6376 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006377 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006378out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}
6381
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006382#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006383static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6384{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006385 struct nfs4_lock_state *lsp;
6386 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006387
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006388 status = nfs4_set_lock_state(state, request);
6389 if (status != 0)
6390 return status;
6391 lsp = request->fl_u.nfs4_fl.owner;
6392 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6393 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6394 return 0;
6395 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006396 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006397}
6398#endif
6399
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6401{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006402 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006403 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006404 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006405 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
Trond Myklebust01c3b862006-06-29 16:38:39 -04006407 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006408 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006409 if (status < 0)
6410 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006411 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006412 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006413 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006414 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006415 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006416 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006417 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006418 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006419 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006420 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006421 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006422 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006423 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006424 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006425out:
6426 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 return status;
6428}
6429
6430static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6431{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006432 struct nfs4_exception exception = {
6433 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006434 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006435 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 int err;
6437
6438 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006439 err = _nfs4_proc_setlk(state, cmd, request);
6440 if (err == -NFS4ERR_DENIED)
6441 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006443 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 } while (exception.retry);
6445 return err;
6446}
6447
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006448#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6449#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6450
6451static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006452nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6453 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006454{
6455 int status = -ERESTARTSYS;
6456 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6457
6458 while(!signalled()) {
6459 status = nfs4_proc_setlk(state, cmd, request);
6460 if ((status != -EAGAIN) || IS_SETLK(cmd))
6461 break;
6462 freezable_schedule_timeout_interruptible(timeout);
6463 timeout *= 2;
6464 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6465 status = -ERESTARTSYS;
6466 }
6467 return status;
6468}
6469
Jeff Laytona1d617d82016-09-17 18:17:39 -04006470#ifdef CONFIG_NFS_V4_1
6471struct nfs4_lock_waiter {
6472 struct task_struct *task;
6473 struct inode *inode;
6474 struct nfs_lowner *owner;
6475 bool notified;
6476};
6477
6478static int
6479nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6480{
6481 int ret;
6482 struct cb_notify_lock_args *cbnl = key;
6483 struct nfs4_lock_waiter *waiter = wait->private;
6484 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6485 *wowner = waiter->owner;
6486
6487 /* Only wake if the callback was for the same owner */
6488 if (lowner->clientid != wowner->clientid ||
6489 lowner->id != wowner->id ||
6490 lowner->s_dev != wowner->s_dev)
6491 return 0;
6492
6493 /* Make sure it's for the right inode */
6494 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6495 return 0;
6496
6497 waiter->notified = true;
6498
6499 /* override "private" so we can use default_wake_function */
6500 wait->private = waiter->task;
6501 ret = autoremove_wake_function(wait, mode, flags, key);
6502 wait->private = waiter;
6503 return ret;
6504}
6505
6506static int
6507nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6508{
6509 int status = -ERESTARTSYS;
6510 unsigned long flags;
6511 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6512 struct nfs_server *server = NFS_SERVER(state->inode);
6513 struct nfs_client *clp = server->nfs_client;
6514 wait_queue_head_t *q = &clp->cl_lock_waitq;
6515 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6516 .id = lsp->ls_seqid.owner_id,
6517 .s_dev = server->s_dev };
6518 struct nfs4_lock_waiter waiter = { .task = current,
6519 .inode = state->inode,
6520 .owner = &owner,
6521 .notified = false };
6522 wait_queue_t wait;
6523
6524 /* Don't bother with waitqueue if we don't expect a callback */
6525 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6526 return nfs4_retry_setlk_simple(state, cmd, request);
6527
6528 init_wait(&wait);
6529 wait.private = &waiter;
6530 wait.func = nfs4_wake_lock_waiter;
6531 add_wait_queue(q, &wait);
6532
6533 while(!signalled()) {
6534 status = nfs4_proc_setlk(state, cmd, request);
6535 if ((status != -EAGAIN) || IS_SETLK(cmd))
6536 break;
6537
6538 status = -ERESTARTSYS;
6539 spin_lock_irqsave(&q->lock, flags);
6540 if (waiter.notified) {
6541 spin_unlock_irqrestore(&q->lock, flags);
6542 continue;
6543 }
6544 set_current_state(TASK_INTERRUPTIBLE);
6545 spin_unlock_irqrestore(&q->lock, flags);
6546
6547 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6548 }
6549
6550 finish_wait(q, &wait);
6551 return status;
6552}
6553#else /* !CONFIG_NFS_V4_1 */
6554static inline int
6555nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6556{
6557 return nfs4_retry_setlk_simple(state, cmd, request);
6558}
6559#endif
6560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561static int
6562nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6563{
6564 struct nfs_open_context *ctx;
6565 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 int status;
6567
6568 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006569 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 state = ctx->state;
6571
6572 if (request->fl_start < 0 || request->fl_end < 0)
6573 return -EINVAL;
6574
Trond Myklebustd9531262009-07-21 19:22:38 -04006575 if (IS_GETLK(cmd)) {
6576 if (state != NULL)
6577 return nfs4_proc_getlk(state, F_GETLK, request);
6578 return 0;
6579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
6581 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6582 return -EINVAL;
6583
Trond Myklebustd9531262009-07-21 19:22:38 -04006584 if (request->fl_type == F_UNLCK) {
6585 if (state != NULL)
6586 return nfs4_proc_unlck(state, cmd, request);
6587 return 0;
6588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
Trond Myklebustd9531262009-07-21 19:22:38 -04006590 if (state == NULL)
6591 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006592
6593 if ((request->fl_flags & FL_POSIX) &&
6594 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6595 return -ENOLCK;
6596
Trond Myklebust55725512012-04-18 12:48:35 -04006597 /*
6598 * Don't rely on the VFS having checked the file open mode,
6599 * since it won't do this for flock() locks.
6600 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006601 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006602 case F_RDLCK:
6603 if (!(filp->f_mode & FMODE_READ))
6604 return -EBADF;
6605 break;
6606 case F_WRLCK:
6607 if (!(filp->f_mode & FMODE_WRITE))
6608 return -EBADF;
6609 }
6610
Jeff Layton1ea67db2016-09-17 18:17:37 -04006611 status = nfs4_set_lock_state(state, request);
6612 if (status != 0)
6613 return status;
6614
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006615 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616}
6617
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006618int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006619{
6620 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006621 int err;
6622
6623 err = nfs4_set_lock_state(state, fl);
6624 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006625 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006626 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006627 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006628}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006629
Trond Myklebustcf470c32012-03-07 13:49:12 -05006630struct nfs_release_lockowner_data {
6631 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006632 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006633 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006634 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006635 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006636};
6637
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006638static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6639{
6640 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006641 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006642 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6643 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006644 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006645 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006646}
6647
6648static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6649{
6650 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006651 struct nfs_server *server = data->server;
6652
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006653 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006654
6655 switch (task->tk_status) {
6656 case 0:
6657 renew_lease(server, data->timestamp);
6658 break;
6659 case -NFS4ERR_STALE_CLIENTID:
6660 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006661 nfs4_schedule_lease_recovery(server->nfs_client);
6662 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006663 case -NFS4ERR_LEASE_MOVED:
6664 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006665 if (nfs4_async_handle_error(task, server,
6666 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006667 rpc_restart_call_prepare(task);
6668 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006669}
6670
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006671static void nfs4_release_lockowner_release(void *calldata)
6672{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006673 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006674 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006675 kfree(calldata);
6676}
6677
Trond Myklebust17280172012-03-11 13:11:00 -04006678static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006679 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6680 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006681 .rpc_release = nfs4_release_lockowner_release,
6682};
6683
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006684static void
6685nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006686{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006687 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006688 struct rpc_message msg = {
6689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6690 };
6691
6692 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006693 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006694
Trond Myklebustcf470c32012-03-07 13:49:12 -05006695 data = kmalloc(sizeof(*data), GFP_NOFS);
6696 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006697 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006698 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006699 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006700 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6701 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6702 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006703
Trond Myklebustcf470c32012-03-07 13:49:12 -05006704 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006705 msg.rpc_resp = &data->res;
6706 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006707 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006708}
6709
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006710#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6711
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006712static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006713 struct dentry *unused, struct inode *inode,
6714 const char *key, const void *buf,
6715 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006716{
Al Viro59301222016-05-27 10:19:30 -04006717 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006718}
6719
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006720static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006721 struct dentry *unused, struct inode *inode,
6722 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006723{
Al Virob2968212016-04-10 20:48:24 -04006724 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006725}
6726
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006727static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006728{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006729 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006730}
6731
David Quigleyc9bccef2013-05-22 12:50:45 -04006732#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006733
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006734static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006735 struct dentry *unused, struct inode *inode,
6736 const char *key, const void *buf,
6737 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006738{
6739 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006740 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006741
6742 return -EOPNOTSUPP;
6743}
6744
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006745static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006746 struct dentry *unused, struct inode *inode,
6747 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006748{
6749 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006750 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006751 return -EOPNOTSUPP;
6752}
6753
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006754static ssize_t
6755nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006756{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006757 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006758
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006759 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6760 len = security_inode_listsecurity(inode, list, list_len);
6761 if (list_len && len > list_len)
6762 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006763 }
6764 return len;
6765}
6766
6767static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6768 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006769 .get = nfs4_xattr_get_nfs4_label,
6770 .set = nfs4_xattr_set_nfs4_label,
6771};
David Quigleyc9bccef2013-05-22 12:50:45 -04006772
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006773#else
6774
6775static ssize_t
6776nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6777{
6778 return 0;
6779}
6780
6781#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006782
Andy Adamson533eb462011-06-13 18:25:56 -04006783/*
6784 * nfs_fhget will use either the mounted_on_fileid or the fileid
6785 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006786static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6787{
Andy Adamson533eb462011-06-13 18:25:56 -04006788 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6789 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6790 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006791 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006792 return;
6793
6794 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006795 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006796 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6797 fattr->nlink = 2;
6798}
6799
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006800static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6801 const struct qstr *name,
6802 struct nfs4_fs_locations *fs_locations,
6803 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006804{
6805 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006806 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006807 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006808 };
6809 struct nfs4_fs_locations_arg args = {
6810 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006811 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006812 .page = page,
6813 .bitmask = bitmask,
6814 };
Benny Halevy22958462009-04-01 09:22:02 -04006815 struct nfs4_fs_locations_res res = {
6816 .fs_locations = fs_locations,
6817 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006818 struct rpc_message msg = {
6819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6820 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006821 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006822 };
6823 int status;
6824
Harvey Harrison3110ff82008-05-02 13:42:44 -07006825 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006826
6827 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6828 * is not supported */
6829 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6830 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6831 else
6832 bitmask[0] |= FATTR4_WORD0_FILEID;
6833
Trond Myklebustc228fd32007-01-13 02:28:11 -05006834 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006835 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006836 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006837 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006838 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006839 return status;
6840}
6841
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006842int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6843 const struct qstr *name,
6844 struct nfs4_fs_locations *fs_locations,
6845 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006846{
6847 struct nfs4_exception exception = { };
6848 int err;
6849 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006850 err = _nfs4_proc_fs_locations(client, dir, name,
6851 fs_locations, page);
6852 trace_nfs4_get_fs_locations(dir, name, err);
6853 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006854 &exception);
6855 } while (exception.retry);
6856 return err;
6857}
6858
Chuck Leverb03d7352013-10-17 14:12:50 -04006859/*
6860 * This operation also signals the server that this client is
6861 * performing migration recovery. The server can stop returning
6862 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6863 * appended to this compound to identify the client ID which is
6864 * performing recovery.
6865 */
6866static int _nfs40_proc_get_locations(struct inode *inode,
6867 struct nfs4_fs_locations *locations,
6868 struct page *page, struct rpc_cred *cred)
6869{
6870 struct nfs_server *server = NFS_SERVER(inode);
6871 struct rpc_clnt *clnt = server->client;
6872 u32 bitmask[2] = {
6873 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6874 };
6875 struct nfs4_fs_locations_arg args = {
6876 .clientid = server->nfs_client->cl_clientid,
6877 .fh = NFS_FH(inode),
6878 .page = page,
6879 .bitmask = bitmask,
6880 .migration = 1, /* skip LOOKUP */
6881 .renew = 1, /* append RENEW */
6882 };
6883 struct nfs4_fs_locations_res res = {
6884 .fs_locations = locations,
6885 .migration = 1,
6886 .renew = 1,
6887 };
6888 struct rpc_message msg = {
6889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6890 .rpc_argp = &args,
6891 .rpc_resp = &res,
6892 .rpc_cred = cred,
6893 };
6894 unsigned long now = jiffies;
6895 int status;
6896
6897 nfs_fattr_init(&locations->fattr);
6898 locations->server = server;
6899 locations->nlocations = 0;
6900
6901 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6902 nfs4_set_sequence_privileged(&args.seq_args);
6903 status = nfs4_call_sync_sequence(clnt, server, &msg,
6904 &args.seq_args, &res.seq_res);
6905 if (status)
6906 return status;
6907
6908 renew_lease(server, now);
6909 return 0;
6910}
6911
6912#ifdef CONFIG_NFS_V4_1
6913
6914/*
6915 * This operation also signals the server that this client is
6916 * performing migration recovery. The server can stop asserting
6917 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6918 * performing this operation is identified in the SEQUENCE
6919 * operation in this compound.
6920 *
6921 * When the client supports GETATTR(fs_locations_info), it can
6922 * be plumbed in here.
6923 */
6924static int _nfs41_proc_get_locations(struct inode *inode,
6925 struct nfs4_fs_locations *locations,
6926 struct page *page, struct rpc_cred *cred)
6927{
6928 struct nfs_server *server = NFS_SERVER(inode);
6929 struct rpc_clnt *clnt = server->client;
6930 u32 bitmask[2] = {
6931 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6932 };
6933 struct nfs4_fs_locations_arg args = {
6934 .fh = NFS_FH(inode),
6935 .page = page,
6936 .bitmask = bitmask,
6937 .migration = 1, /* skip LOOKUP */
6938 };
6939 struct nfs4_fs_locations_res res = {
6940 .fs_locations = locations,
6941 .migration = 1,
6942 };
6943 struct rpc_message msg = {
6944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6945 .rpc_argp = &args,
6946 .rpc_resp = &res,
6947 .rpc_cred = cred,
6948 };
6949 int status;
6950
6951 nfs_fattr_init(&locations->fattr);
6952 locations->server = server;
6953 locations->nlocations = 0;
6954
6955 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6956 nfs4_set_sequence_privileged(&args.seq_args);
6957 status = nfs4_call_sync_sequence(clnt, server, &msg,
6958 &args.seq_args, &res.seq_res);
6959 if (status == NFS4_OK &&
6960 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6961 status = -NFS4ERR_LEASE_MOVED;
6962 return status;
6963}
6964
6965#endif /* CONFIG_NFS_V4_1 */
6966
6967/**
6968 * nfs4_proc_get_locations - discover locations for a migrated FSID
6969 * @inode: inode on FSID that is migrating
6970 * @locations: result of query
6971 * @page: buffer
6972 * @cred: credential to use for this operation
6973 *
6974 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6975 * operation failed, or a negative errno if a local error occurred.
6976 *
6977 * On success, "locations" is filled in, but if the server has
6978 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6979 * asserted.
6980 *
6981 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6982 * from this client that require migration recovery.
6983 */
6984int nfs4_proc_get_locations(struct inode *inode,
6985 struct nfs4_fs_locations *locations,
6986 struct page *page, struct rpc_cred *cred)
6987{
6988 struct nfs_server *server = NFS_SERVER(inode);
6989 struct nfs_client *clp = server->nfs_client;
6990 const struct nfs4_mig_recovery_ops *ops =
6991 clp->cl_mvops->mig_recovery_ops;
6992 struct nfs4_exception exception = { };
6993 int status;
6994
6995 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6996 (unsigned long long)server->fsid.major,
6997 (unsigned long long)server->fsid.minor,
6998 clp->cl_hostname);
6999 nfs_display_fhandle(NFS_FH(inode), __func__);
7000
7001 do {
7002 status = ops->get_locations(inode, locations, page, cred);
7003 if (status != -NFS4ERR_DELAY)
7004 break;
7005 nfs4_handle_exception(server, status, &exception);
7006 } while (exception.retry);
7007 return status;
7008}
7009
Chuck Lever44c99932013-10-17 14:13:30 -04007010/*
7011 * This operation also signals the server that this client is
7012 * performing "lease moved" recovery. The server can stop
7013 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7014 * is appended to this compound to identify the client ID which is
7015 * performing recovery.
7016 */
7017static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7018{
7019 struct nfs_server *server = NFS_SERVER(inode);
7020 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7021 struct rpc_clnt *clnt = server->client;
7022 struct nfs4_fsid_present_arg args = {
7023 .fh = NFS_FH(inode),
7024 .clientid = clp->cl_clientid,
7025 .renew = 1, /* append RENEW */
7026 };
7027 struct nfs4_fsid_present_res res = {
7028 .renew = 1,
7029 };
7030 struct rpc_message msg = {
7031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7032 .rpc_argp = &args,
7033 .rpc_resp = &res,
7034 .rpc_cred = cred,
7035 };
7036 unsigned long now = jiffies;
7037 int status;
7038
7039 res.fh = nfs_alloc_fhandle();
7040 if (res.fh == NULL)
7041 return -ENOMEM;
7042
7043 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7044 nfs4_set_sequence_privileged(&args.seq_args);
7045 status = nfs4_call_sync_sequence(clnt, server, &msg,
7046 &args.seq_args, &res.seq_res);
7047 nfs_free_fhandle(res.fh);
7048 if (status)
7049 return status;
7050
7051 do_renew_lease(clp, now);
7052 return 0;
7053}
7054
7055#ifdef CONFIG_NFS_V4_1
7056
7057/*
7058 * This operation also signals the server that this client is
7059 * performing "lease moved" recovery. The server can stop asserting
7060 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7061 * this operation is identified in the SEQUENCE operation in this
7062 * compound.
7063 */
7064static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7065{
7066 struct nfs_server *server = NFS_SERVER(inode);
7067 struct rpc_clnt *clnt = server->client;
7068 struct nfs4_fsid_present_arg args = {
7069 .fh = NFS_FH(inode),
7070 };
7071 struct nfs4_fsid_present_res res = {
7072 };
7073 struct rpc_message msg = {
7074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7075 .rpc_argp = &args,
7076 .rpc_resp = &res,
7077 .rpc_cred = cred,
7078 };
7079 int status;
7080
7081 res.fh = nfs_alloc_fhandle();
7082 if (res.fh == NULL)
7083 return -ENOMEM;
7084
7085 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7086 nfs4_set_sequence_privileged(&args.seq_args);
7087 status = nfs4_call_sync_sequence(clnt, server, &msg,
7088 &args.seq_args, &res.seq_res);
7089 nfs_free_fhandle(res.fh);
7090 if (status == NFS4_OK &&
7091 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7092 status = -NFS4ERR_LEASE_MOVED;
7093 return status;
7094}
7095
7096#endif /* CONFIG_NFS_V4_1 */
7097
7098/**
7099 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7100 * @inode: inode on FSID to check
7101 * @cred: credential to use for this operation
7102 *
7103 * Server indicates whether the FSID is present, moved, or not
7104 * recognized. This operation is necessary to clear a LEASE_MOVED
7105 * condition for this client ID.
7106 *
7107 * Returns NFS4_OK if the FSID is present on this server,
7108 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7109 * NFS4ERR code if some error occurred on the server, or a
7110 * negative errno if a local failure occurred.
7111 */
7112int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7113{
7114 struct nfs_server *server = NFS_SERVER(inode);
7115 struct nfs_client *clp = server->nfs_client;
7116 const struct nfs4_mig_recovery_ops *ops =
7117 clp->cl_mvops->mig_recovery_ops;
7118 struct nfs4_exception exception = { };
7119 int status;
7120
7121 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7122 (unsigned long long)server->fsid.major,
7123 (unsigned long long)server->fsid.minor,
7124 clp->cl_hostname);
7125 nfs_display_fhandle(NFS_FH(inode), __func__);
7126
7127 do {
7128 status = ops->fsid_present(inode, cred);
7129 if (status != -NFS4ERR_DELAY)
7130 break;
7131 nfs4_handle_exception(server, status, &exception);
7132 } while (exception.retry);
7133 return status;
7134}
7135
Andy Adamson5ec16a82013-08-08 10:57:55 -04007136/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007137 * If 'use_integrity' is true and the state managment nfs_client
7138 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7139 * and the machine credential as per RFC3530bis and RFC5661 Security
7140 * Considerations sections. Otherwise, just use the user cred with the
7141 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007142 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007143static 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 +00007144{
7145 int status;
7146 struct nfs4_secinfo_arg args = {
7147 .dir_fh = NFS_FH(dir),
7148 .name = name,
7149 };
7150 struct nfs4_secinfo_res res = {
7151 .flavors = flavors,
7152 };
7153 struct rpc_message msg = {
7154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7155 .rpc_argp = &args,
7156 .rpc_resp = &res,
7157 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007158 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007159 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007160
7161 if (use_integrity) {
7162 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007163 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7164 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007165 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007166
7167 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007168
7169 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7170 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7171
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007172 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7173 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007174 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007175
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007176 if (cred)
7177 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007178
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007179 return status;
7180}
7181
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007182int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7183 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007184{
7185 struct nfs4_exception exception = { };
7186 int err;
7187 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007188 err = -NFS4ERR_WRONGSEC;
7189
7190 /* try to use integrity protection with machine cred */
7191 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7192 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7193
7194 /*
7195 * if unable to use integrity protection, or SECINFO with
7196 * integrity protection returns NFS4ERR_WRONGSEC (which is
7197 * disallowed by spec, but exists in deployed servers) use
7198 * the current filesystem's rpc_client and the user cred.
7199 */
7200 if (err == -NFS4ERR_WRONGSEC)
7201 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7202
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007203 trace_nfs4_secinfo(dir, name, err);
7204 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007205 &exception);
7206 } while (exception.retry);
7207 return err;
7208}
7209
Andy Adamson557134a2009-04-01 09:21:53 -04007210#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007211/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007212 * Check the exchange flags returned by the server for invalid flags, having
7213 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7214 * DS flags set.
7215 */
7216static int nfs4_check_cl_exchange_flags(u32 flags)
7217{
7218 if (flags & ~EXCHGID4_FLAG_MASK_R)
7219 goto out_inval;
7220 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7221 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7222 goto out_inval;
7223 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7224 goto out_inval;
7225 return NFS_OK;
7226out_inval:
7227 return -NFS4ERR_INVAL;
7228}
7229
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007230static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007231nfs41_same_server_scope(struct nfs41_server_scope *a,
7232 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007233{
7234 if (a->server_scope_sz == b->server_scope_sz &&
7235 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7236 return true;
7237
7238 return false;
7239}
7240
Andy Adamson02a95de2016-02-05 16:08:37 -05007241static void
7242nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7243{
7244}
7245
7246static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7247 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7248};
7249
Andy Adamson357f54d2010-12-14 10:11:57 -05007250/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007251 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007252 *
7253 * The 4.1 client currently uses the same TCP connection for the
7254 * fore and backchannel.
7255 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007256static
7257int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7258 struct rpc_xprt *xprt,
7259 struct nfs_client *clp,
7260 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007261{
7262 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007263 struct nfs41_bind_conn_to_session_args args = {
7264 .client = clp,
7265 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7266 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267 struct nfs41_bind_conn_to_session_res res;
7268 struct rpc_message msg = {
7269 .rpc_proc =
7270 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007271 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007272 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007273 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007274 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007275 struct rpc_task_setup task_setup_data = {
7276 .rpc_client = clnt,
7277 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007278 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007279 .rpc_message = &msg,
7280 .flags = RPC_TASK_TIMEOUT,
7281 };
7282 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007283
7284 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007285
Trond Myklebust71a097c2015-02-18 09:27:18 -08007286 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7287 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7288 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007289
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007290 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7291 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7292 args.dir = NFS4_CDFC4_FORE;
7293
7294 task = rpc_run_task(&task_setup_data);
7295 if (!IS_ERR(task)) {
7296 status = task->tk_status;
7297 rpc_put_task(task);
7298 } else
7299 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007300 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007301 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007302 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007303 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7304 dprintk("NFS: %s: Session ID mismatch\n", __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.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007309 dprintk("NFS: %s: Unexpected direction from server\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 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007314 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007315 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7316 __func__);
7317 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007318 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007319 }
7320 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007321out:
7322 dprintk("<-- %s status= %d\n", __func__, status);
7323 return status;
7324}
7325
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007326struct rpc_bind_conn_calldata {
7327 struct nfs_client *clp;
7328 struct rpc_cred *cred;
7329};
7330
7331static int
7332nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7333 struct rpc_xprt *xprt,
7334 void *calldata)
7335{
7336 struct rpc_bind_conn_calldata *p = calldata;
7337
7338 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7339}
7340
7341int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7342{
7343 struct rpc_bind_conn_calldata data = {
7344 .clp = clp,
7345 .cred = cred,
7346 };
7347 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7348 nfs4_proc_bind_conn_to_session_callback, &data);
7349}
7350
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007351/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007352 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7353 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007354 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007355static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7356 .how = SP4_MACH_CRED,
7357 .enforce.u.words = {
7358 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7359 1 << (OP_EXCHANGE_ID - 32) |
7360 1 << (OP_CREATE_SESSION - 32) |
7361 1 << (OP_DESTROY_SESSION - 32) |
7362 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007363 },
7364 .allow.u.words = {
7365 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007366 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007367 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007368 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007369 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007370 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007371 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007372 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007373 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007374 1 << (OP_FREE_STATEID - 32) |
7375 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007376 }
7377};
7378
7379/*
7380 * Select the state protection mode for client `clp' given the server results
7381 * from exchange_id in `sp'.
7382 *
7383 * Returns 0 on success, negative errno otherwise.
7384 */
7385static int nfs4_sp4_select_mode(struct nfs_client *clp,
7386 struct nfs41_state_protection *sp)
7387{
7388 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7389 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7390 1 << (OP_EXCHANGE_ID - 32) |
7391 1 << (OP_CREATE_SESSION - 32) |
7392 1 << (OP_DESTROY_SESSION - 32) |
7393 1 << (OP_DESTROY_CLIENTID - 32)
7394 };
7395 unsigned int i;
7396
7397 if (sp->how == SP4_MACH_CRED) {
7398 /* Print state protect result */
7399 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7400 for (i = 0; i <= LAST_NFS4_OP; i++) {
7401 if (test_bit(i, sp->enforce.u.longs))
7402 dfprintk(MOUNT, " enforce op %d\n", i);
7403 if (test_bit(i, sp->allow.u.longs))
7404 dfprintk(MOUNT, " allow op %d\n", i);
7405 }
7406
7407 /* make sure nothing is on enforce list that isn't supported */
7408 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7409 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7410 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7411 return -EINVAL;
7412 }
7413 }
7414
7415 /*
7416 * Minimal mode - state operations are allowed to use machine
7417 * credential. Note this already happens by default, so the
7418 * client doesn't have to do anything more than the negotiation.
7419 *
7420 * NOTE: we don't care if EXCHANGE_ID is in the list -
7421 * we're already using the machine cred for exchange_id
7422 * and will never use a different cred.
7423 */
7424 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7425 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7426 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7427 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7428 dfprintk(MOUNT, "sp4_mach_cred:\n");
7429 dfprintk(MOUNT, " minimal mode enabled\n");
7430 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7431 } else {
7432 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7433 return -EINVAL;
7434 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007435
7436 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007437 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7438 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007439 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7440 dfprintk(MOUNT, " cleanup mode enabled\n");
7441 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7442 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007443
Andrew Elble99ade3c2015-12-02 09:39:51 -05007444 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7445 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7446 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7447 &clp->cl_sp4_flags);
7448 }
7449
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007450 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7451 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7452 dfprintk(MOUNT, " secinfo mode enabled\n");
7453 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7454 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007455
7456 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7457 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7458 dfprintk(MOUNT, " stateid mode enabled\n");
7459 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7460 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007461
7462 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7463 dfprintk(MOUNT, " write mode enabled\n");
7464 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7465 }
7466
7467 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7468 dfprintk(MOUNT, " commit mode enabled\n");
7469 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7470 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007471 }
7472
7473 return 0;
7474}
7475
Andy Adamson8d89bd72016-09-09 09:22:18 -04007476struct nfs41_exchange_id_data {
7477 struct nfs41_exchange_id_res res;
7478 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007479 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 int rpc_status;
7481};
7482
7483static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 struct nfs41_exchange_id_data *cdata =
7486 (struct nfs41_exchange_id_data *)data;
7487 struct nfs_client *clp = cdata->args.client;
7488 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007489
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007490 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007491
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007492 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007493 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007494
7495 if (cdata->xprt && status == 0) {
7496 status = nfs4_detect_session_trunking(clp, &cdata->res,
7497 cdata->xprt);
7498 goto out;
7499 }
7500
Andy Adamson8d89bd72016-09-09 09:22:18 -04007501 if (status == 0)
7502 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007503
Chuck Lever177313f2012-05-21 22:44:58 -04007504 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 clp->cl_clientid = cdata->res.clientid;
7506 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007507 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007508 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007509 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 &clp->cl_session->session_state);
7511 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007512 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007513
Chuck Leveracdeb692012-05-21 22:46:16 -04007514 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 clp->cl_serverowner = cdata->res.server_owner;
7516 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007517
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007518 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007519 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 clp->cl_implid = cdata->res.impl_id;
7521 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007522
Chuck Lever177313f2012-05-21 22:44:58 -04007523 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007524 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007526 dprintk("%s: server_scope mismatch detected\n",
7527 __func__);
7528 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007529 kfree(clp->cl_serverscope);
7530 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007531 }
7532
Chuck Lever177313f2012-05-21 22:44:58 -04007533 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534 clp->cl_serverscope = cdata->res.server_scope;
7535 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007536 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007537 /* Save the EXCHANGE_ID verifier session trunk tests */
7538 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7539 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007540 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007541out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007542 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007543 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007544}
7545
7546static void nfs4_exchange_id_release(void *data)
7547{
7548 struct nfs41_exchange_id_data *cdata =
7549 (struct nfs41_exchange_id_data *)data;
7550
7551 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007552 if (cdata->xprt) {
7553 xprt_put(cdata->xprt);
7554 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7555 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007556 kfree(cdata->res.impl_id);
7557 kfree(cdata->res.server_scope);
7558 kfree(cdata->res.server_owner);
7559 kfree(cdata);
7560}
7561
7562static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7563 .rpc_call_done = nfs4_exchange_id_done,
7564 .rpc_release = nfs4_exchange_id_release,
7565};
7566
Benny Halevy99fe60d2009-04-01 09:22:29 -04007567/*
7568 * _nfs4_proc_exchange_id()
7569 *
7570 * Wrapper for EXCHANGE_ID operation.
7571 */
7572static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007573 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007574{
7575 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007576 struct rpc_message msg = {
7577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007578 .rpc_cred = cred,
7579 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007580 struct rpc_task_setup task_setup_data = {
7581 .rpc_client = clp->cl_rpcclient,
7582 .callback_ops = &nfs4_exchange_id_call_ops,
7583 .rpc_message = &msg,
7584 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7585 };
7586 struct nfs41_exchange_id_data *calldata;
7587 struct rpc_task *task;
7588 int status = -EIO;
7589
7590 if (!atomic_inc_not_zero(&clp->cl_count))
7591 goto out;
7592
7593 status = -ENOMEM;
7594 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7595 if (!calldata)
7596 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007597
Andy Adamsonad0849a2016-09-09 09:22:28 -04007598 if (!xprt)
7599 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007600
7601 status = nfs4_init_uniform_client_string(clp);
7602 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007603 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007604
7605 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7606 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7607 clp->cl_owner_id);
7608
Andy Adamson8d89bd72016-09-09 09:22:18 -04007609 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7610 GFP_NOFS);
7611 status = -ENOMEM;
7612 if (unlikely(calldata->res.server_owner == NULL))
7613 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007614
Andy Adamson8d89bd72016-09-09 09:22:18 -04007615 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007616 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007617 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007618 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007619
Andy Adamson8d89bd72016-09-09 09:22:18 -04007620 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7621 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007622 goto out_server_scope;
7623
7624 switch (sp4_how) {
7625 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007626 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007627 break;
7628
7629 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007631 break;
7632
7633 default:
7634 /* unsupported! */
7635 WARN_ON_ONCE(1);
7636 status = -EINVAL;
7637 goto out_impl_id;
7638 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007639 if (xprt) {
7640 calldata->xprt = xprt;
7641 task_setup_data.rpc_xprt = xprt;
7642 task_setup_data.flags =
7643 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7644 calldata->args.verifier = &clp->cl_confirm;
7645 } else {
7646 calldata->args.verifier = &verifier;
7647 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007648 calldata->args.client = clp;
7649#ifdef CONFIG_NFS_V4_1_MIGRATION
7650 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7651 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7652 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7653#else
7654 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7655 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7656#endif
7657 msg.rpc_argp = &calldata->args;
7658 msg.rpc_resp = &calldata->res;
7659 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007660
Andy Adamson8d89bd72016-09-09 09:22:18 -04007661 task = rpc_run_task(&task_setup_data);
7662 if (IS_ERR(task)) {
7663 status = PTR_ERR(task);
7664 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007665 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007666
Andy Adamsonad0849a2016-09-09 09:22:28 -04007667 if (!xprt) {
7668 status = rpc_wait_for_completion_task(task);
7669 if (!status)
7670 status = calldata->rpc_status;
7671 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007672 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007673
Andy Adamson8d89bd72016-09-09 09:22:18 -04007674 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007675out:
Chuck Lever177313f2012-05-21 22:44:58 -04007676 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007677 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007678 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007679 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007680 clp->cl_implid->date.seconds,
7681 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007682 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007683 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007684
7685out_impl_id:
7686 kfree(calldata->res.impl_id);
7687out_server_scope:
7688 kfree(calldata->res.server_scope);
7689out_server_owner:
7690 kfree(calldata->res.server_owner);
7691out_calldata:
7692 kfree(calldata);
7693 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007694}
7695
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007696/*
7697 * nfs4_proc_exchange_id()
7698 *
7699 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7700 *
7701 * Since the clientid has expired, all compounds using sessions
7702 * associated with the stale clientid will be returning
7703 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7704 * be in some phase of session reset.
7705 *
7706 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7707 */
7708int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7709{
7710 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7711 int status;
7712
7713 /* try SP4_MACH_CRED if krb5i/p */
7714 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7715 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007716 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007717 if (!status)
7718 return 0;
7719 }
7720
7721 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007722 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007723}
7724
Andy Adamson04fa2c62016-09-09 09:22:29 -04007725/**
7726 * nfs4_test_session_trunk
7727 *
7728 * This is an add_xprt_test() test function called from
7729 * rpc_clnt_setup_test_and_add_xprt.
7730 *
7731 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7732 * and is dereferrenced in nfs4_exchange_id_release
7733 *
7734 * Upon success, add the new transport to the rpc_clnt
7735 *
7736 * @clnt: struct rpc_clnt to get new transport
7737 * @xprt: the rpc_xprt to test
7738 * @data: call data for _nfs4_proc_exchange_id.
7739 */
7740int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7741 void *data)
7742{
7743 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7744 u32 sp4_how;
7745
7746 dprintk("--> %s try %s\n", __func__,
7747 xprt->address_strings[RPC_DISPLAY_ADDR]);
7748
7749 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7750
7751 /* Test connection for session trunking. Async exchange_id call */
7752 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7753}
7754EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7755
Trond Myklebust66245532012-05-25 17:18:09 -04007756static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7757 struct rpc_cred *cred)
7758{
7759 struct rpc_message msg = {
7760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7761 .rpc_argp = clp,
7762 .rpc_cred = cred,
7763 };
7764 int status;
7765
7766 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007767 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007768 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007769 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007770 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7771 return status;
7772}
7773
7774static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7775 struct rpc_cred *cred)
7776{
7777 unsigned int loop;
7778 int ret;
7779
7780 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7781 ret = _nfs4_proc_destroy_clientid(clp, cred);
7782 switch (ret) {
7783 case -NFS4ERR_DELAY:
7784 case -NFS4ERR_CLIENTID_BUSY:
7785 ssleep(1);
7786 break;
7787 default:
7788 return ret;
7789 }
7790 }
7791 return 0;
7792}
7793
7794int nfs4_destroy_clientid(struct nfs_client *clp)
7795{
7796 struct rpc_cred *cred;
7797 int ret = 0;
7798
7799 if (clp->cl_mvops->minor_version < 1)
7800 goto out;
7801 if (clp->cl_exchange_flags == 0)
7802 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007803 if (clp->cl_preserve_clid)
7804 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007805 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007806 ret = nfs4_proc_destroy_clientid(clp, cred);
7807 if (cred)
7808 put_rpccred(cred);
7809 switch (ret) {
7810 case 0:
7811 case -NFS4ERR_STALE_CLIENTID:
7812 clp->cl_exchange_flags = 0;
7813 }
7814out:
7815 return ret;
7816}
7817
Andy Adamson2050f0c2009-04-01 09:22:30 -04007818struct nfs4_get_lease_time_data {
7819 struct nfs4_get_lease_time_args *args;
7820 struct nfs4_get_lease_time_res *res;
7821 struct nfs_client *clp;
7822};
7823
7824static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7825 void *calldata)
7826{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007827 struct nfs4_get_lease_time_data *data =
7828 (struct nfs4_get_lease_time_data *)calldata;
7829
7830 dprintk("--> %s\n", __func__);
7831 /* just setup sequence, do not trigger session recovery
7832 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007833 nfs41_setup_sequence(data->clp->cl_session,
7834 &data->args->la_seq_args,
7835 &data->res->lr_seq_res,
7836 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007837 dprintk("<-- %s\n", __func__);
7838}
7839
7840/*
7841 * Called from nfs4_state_manager thread for session setup, so don't recover
7842 * from sequence operation or clientid errors.
7843 */
7844static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7845{
7846 struct nfs4_get_lease_time_data *data =
7847 (struct nfs4_get_lease_time_data *)calldata;
7848
7849 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007850 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7851 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007852 switch (task->tk_status) {
7853 case -NFS4ERR_DELAY:
7854 case -NFS4ERR_GRACE:
7855 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7856 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7857 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007858 /* fall through */
7859 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007860 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007861 return;
7862 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007863 dprintk("<-- %s\n", __func__);
7864}
7865
Trond Myklebust17280172012-03-11 13:11:00 -04007866static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007867 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7868 .rpc_call_done = nfs4_get_lease_time_done,
7869};
7870
7871int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7872{
7873 struct rpc_task *task;
7874 struct nfs4_get_lease_time_args args;
7875 struct nfs4_get_lease_time_res res = {
7876 .lr_fsinfo = fsinfo,
7877 };
7878 struct nfs4_get_lease_time_data data = {
7879 .args = &args,
7880 .res = &res,
7881 .clp = clp,
7882 };
7883 struct rpc_message msg = {
7884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7885 .rpc_argp = &args,
7886 .rpc_resp = &res,
7887 };
7888 struct rpc_task_setup task_setup = {
7889 .rpc_client = clp->cl_rpcclient,
7890 .rpc_message = &msg,
7891 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007892 .callback_data = &data,
7893 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007894 };
7895 int status;
7896
Chuck Levera9c92d62013-08-09 12:48:18 -04007897 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007898 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007899 dprintk("--> %s\n", __func__);
7900 task = rpc_run_task(&task_setup);
7901
7902 if (IS_ERR(task))
7903 status = PTR_ERR(task);
7904 else {
7905 status = task->tk_status;
7906 rpc_put_task(task);
7907 }
7908 dprintk("<-- %s return %d\n", __func__, status);
7909
7910 return status;
7911}
7912
Andy Adamsonfc931582009-04-01 09:22:31 -04007913/*
7914 * Initialize the values to be used by the client in CREATE_SESSION
7915 * If nfs4_init_session set the fore channel request and response sizes,
7916 * use them.
7917 *
7918 * Set the back channel max_resp_sz_cached to zero to force the client to
7919 * always set csa_cachethis to FALSE because the current implementation
7920 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7921 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007922static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7923 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007924{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007925 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007926 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007927
Andy Adamson18aad3d2013-06-26 12:21:49 -04007928 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7929 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7930
Andy Adamsonfc931582009-04-01 09:22:31 -04007931 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007932 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7933 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007934 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007935 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007936
7937 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007938 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007939 __func__,
7940 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007941 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007942
7943 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007944 args->bc_attrs.max_rqst_sz = max_bc_payload;
7945 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007946 args->bc_attrs.max_resp_sz_cached = 0;
7947 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007948 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007949
7950 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7951 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7952 __func__,
7953 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7954 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7955 args->bc_attrs.max_reqs);
7956}
7957
Trond Myklebust79969dd2015-02-18 11:30:18 -08007958static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7959 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007960{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007961 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007962 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007963
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007964 if (rcvd->max_resp_sz > sent->max_resp_sz)
7965 return -EINVAL;
7966 /*
7967 * Our requested max_ops is the minimum we need; we're not
7968 * prepared to break up compounds into smaller pieces than that.
7969 * So, no point even trying to continue if the server won't
7970 * cooperate:
7971 */
7972 if (rcvd->max_ops < sent->max_ops)
7973 return -EINVAL;
7974 if (rcvd->max_reqs == 0)
7975 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007976 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7977 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007978 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007979}
7980
Trond Myklebust79969dd2015-02-18 11:30:18 -08007981static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7982 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007983{
7984 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007985 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007986
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007987 if (!(res->flags & SESSION4_BACK_CHAN))
7988 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007989 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7990 return -EINVAL;
7991 if (rcvd->max_resp_sz < sent->max_resp_sz)
7992 return -EINVAL;
7993 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7994 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007995 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007996 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007997 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007998 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007999out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008000 return 0;
8001}
Andy Adamson8d353012009-04-01 09:22:32 -04008002
Andy Adamson8d353012009-04-01 09:22:32 -04008003static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008004 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008005{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008006 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008007
Trond Myklebust79969dd2015-02-18 11:30:18 -08008008 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008009 if (ret)
8010 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008011 return nfs4_verify_back_channel_attrs(args, res);
8012}
8013
8014static void nfs4_update_session(struct nfs4_session *session,
8015 struct nfs41_create_session_res *res)
8016{
8017 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008018 /* Mark client id and session as being confirmed */
8019 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8020 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008021 session->flags = res->flags;
8022 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008023 if (res->flags & SESSION4_BACK_CHAN)
8024 memcpy(&session->bc_attrs, &res->bc_attrs,
8025 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008026}
8027
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008028static int _nfs4_proc_create_session(struct nfs_client *clp,
8029 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008030{
8031 struct nfs4_session *session = clp->cl_session;
8032 struct nfs41_create_session_args args = {
8033 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008034 .clientid = clp->cl_clientid,
8035 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008036 .cb_program = NFS4_CALLBACK,
8037 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008038 struct nfs41_create_session_res res;
8039
Andy Adamsonfc931582009-04-01 09:22:31 -04008040 struct rpc_message msg = {
8041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8042 .rpc_argp = &args,
8043 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008044 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008045 };
8046 int status;
8047
Chuck Lever6b26cc82016-05-02 14:40:40 -04008048 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008049 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008050
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008051 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008052 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008053
Trond Myklebustb519d402016-09-11 14:50:01 -04008054 switch (status) {
8055 case -NFS4ERR_STALE_CLIENTID:
8056 case -NFS4ERR_DELAY:
8057 case -ETIMEDOUT:
8058 case -EACCES:
8059 case -EAGAIN:
8060 goto out;
8061 };
8062
8063 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008064 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008065 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008066 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008067 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008068 if (status)
8069 goto out;
8070 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008071 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008072out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008073 return status;
8074}
8075
8076/*
8077 * Issues a CREATE_SESSION operation to the server.
8078 * It is the responsibility of the caller to verify the session is
8079 * expired before calling this routine.
8080 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008081int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008082{
8083 int status;
8084 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008085 struct nfs4_session *session = clp->cl_session;
8086
8087 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8088
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008089 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008090 if (status)
8091 goto out;
8092
Andy Adamsonaacd5532011-11-09 13:58:21 -05008093 /* Init or reset the session slot tables */
8094 status = nfs4_setup_session_slot_tables(session);
8095 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008096 if (status)
8097 goto out;
8098
8099 ptr = (unsigned *)&session->sess_id.data[0];
8100 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8101 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008102out:
8103 dprintk("<-- %s\n", __func__);
8104 return status;
8105}
8106
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008107/*
8108 * Issue the over-the-wire RPC DESTROY_SESSION.
8109 * The caller must serialize access to this routine.
8110 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008111int nfs4_proc_destroy_session(struct nfs4_session *session,
8112 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008113{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008114 struct rpc_message msg = {
8115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8116 .rpc_argp = session,
8117 .rpc_cred = cred,
8118 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008119 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008120
8121 dprintk("--> nfs4_proc_destroy_session\n");
8122
8123 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008124 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8125 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008126
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008127 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008128 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008129
8130 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008131 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008132 "Session has been destroyed regardless...\n", status);
8133
8134 dprintk("<-- nfs4_proc_destroy_session\n");
8135 return status;
8136}
8137
Trond Myklebust7b38c362012-05-23 13:23:31 -04008138/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008139 * Renew the cl_session lease.
8140 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008141struct nfs4_sequence_data {
8142 struct nfs_client *clp;
8143 struct nfs4_sequence_args args;
8144 struct nfs4_sequence_res res;
8145};
8146
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008147static void nfs41_sequence_release(void *data)
8148{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008149 struct nfs4_sequence_data *calldata = data;
8150 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008151
Alexandros Batsakis71358402010-02-05 03:45:05 -08008152 if (atomic_read(&clp->cl_count) > 1)
8153 nfs4_schedule_state_renewal(clp);
8154 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008155 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008156}
8157
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008158static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8159{
8160 switch(task->tk_status) {
8161 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008162 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8163 return -EAGAIN;
8164 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008165 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008166 }
8167 return 0;
8168}
8169
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008170static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008171{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008172 struct nfs4_sequence_data *calldata = data;
8173 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008174
Trond Myklebust14516c32010-07-31 14:29:06 -04008175 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8176 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008177
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008178 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008179 if (task->tk_status < 0) {
8180 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008181 if (atomic_read(&clp->cl_count) == 1)
8182 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008183
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008184 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8185 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008186 return;
8187 }
8188 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008189 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008190out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008191 dprintk("<-- %s\n", __func__);
8192}
8193
8194static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8195{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008196 struct nfs4_sequence_data *calldata = data;
8197 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008198 struct nfs4_sequence_args *args;
8199 struct nfs4_sequence_res *res;
8200
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008201 args = task->tk_msg.rpc_argp;
8202 res = task->tk_msg.rpc_resp;
8203
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008204 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008205}
8206
8207static const struct rpc_call_ops nfs41_sequence_ops = {
8208 .rpc_call_done = nfs41_sequence_call_done,
8209 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008210 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008211};
8212
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008213static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8214 struct rpc_cred *cred,
8215 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008216{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008217 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008218 struct rpc_message msg = {
8219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8220 .rpc_cred = cred,
8221 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008222 struct rpc_task_setup task_setup_data = {
8223 .rpc_client = clp->cl_rpcclient,
8224 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008225 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008226 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008227 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008228
Alexandros Batsakis71358402010-02-05 03:45:05 -08008229 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008230 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008231 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008232 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008233 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008234 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008235 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008236 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008237 if (is_privileged)
8238 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008239 msg.rpc_argp = &calldata->args;
8240 msg.rpc_resp = &calldata->res;
8241 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008242 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008243
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008244 return rpc_run_task(&task_setup_data);
8245}
8246
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008247static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008248{
8249 struct rpc_task *task;
8250 int ret = 0;
8251
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008252 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008253 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008254 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008255 if (IS_ERR(task))
8256 ret = PTR_ERR(task);
8257 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008258 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008259 dprintk("<-- %s status=%d\n", __func__, ret);
8260 return ret;
8261}
8262
8263static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8264{
8265 struct rpc_task *task;
8266 int ret;
8267
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008268 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008269 if (IS_ERR(task)) {
8270 ret = PTR_ERR(task);
8271 goto out;
8272 }
8273 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008274 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008275 ret = task->tk_status;
8276 rpc_put_task(task);
8277out:
8278 dprintk("<-- %s status=%d\n", __func__, ret);
8279 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008280}
8281
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282struct nfs4_reclaim_complete_data {
8283 struct nfs_client *clp;
8284 struct nfs41_reclaim_complete_args arg;
8285 struct nfs41_reclaim_complete_res res;
8286};
8287
8288static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8289{
8290 struct nfs4_reclaim_complete_data *calldata = data;
8291
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008292 nfs41_setup_sequence(calldata->clp->cl_session,
8293 &calldata->arg.seq_args,
8294 &calldata->res.seq_res,
8295 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008296}
8297
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008298static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8299{
8300 switch(task->tk_status) {
8301 case 0:
8302 case -NFS4ERR_COMPLETE_ALREADY:
8303 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8304 break;
8305 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008306 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008307 /* fall through */
8308 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008309 return -EAGAIN;
8310 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008311 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008312 }
8313 return 0;
8314}
8315
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008316static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8317{
8318 struct nfs4_reclaim_complete_data *calldata = data;
8319 struct nfs_client *clp = calldata->clp;
8320 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8321
8322 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008323 if (!nfs41_sequence_done(task, res))
8324 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008325
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008326 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008327 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8328 rpc_restart_call_prepare(task);
8329 return;
8330 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008331 dprintk("<-- %s\n", __func__);
8332}
8333
8334static void nfs4_free_reclaim_complete_data(void *data)
8335{
8336 struct nfs4_reclaim_complete_data *calldata = data;
8337
8338 kfree(calldata);
8339}
8340
8341static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8342 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8343 .rpc_call_done = nfs4_reclaim_complete_done,
8344 .rpc_release = nfs4_free_reclaim_complete_data,
8345};
8346
8347/*
8348 * Issue a global reclaim complete.
8349 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008350static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8351 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008352{
8353 struct nfs4_reclaim_complete_data *calldata;
8354 struct rpc_task *task;
8355 struct rpc_message msg = {
8356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008357 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008358 };
8359 struct rpc_task_setup task_setup_data = {
8360 .rpc_client = clp->cl_rpcclient,
8361 .rpc_message = &msg,
8362 .callback_ops = &nfs4_reclaim_complete_call_ops,
8363 .flags = RPC_TASK_ASYNC,
8364 };
8365 int status = -ENOMEM;
8366
8367 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008368 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008369 if (calldata == NULL)
8370 goto out;
8371 calldata->clp = clp;
8372 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008373
Chuck Levera9c92d62013-08-09 12:48:18 -04008374 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008375 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008376 msg.rpc_argp = &calldata->arg;
8377 msg.rpc_resp = &calldata->res;
8378 task_setup_data.callback_data = calldata;
8379 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008380 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008381 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008382 goto out;
8383 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008384 status = nfs4_wait_for_completion_rpc_task(task);
8385 if (status == 0)
8386 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008387 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008388 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008389out:
8390 dprintk("<-- %s status=%d\n", __func__, status);
8391 return status;
8392}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008393
8394static void
8395nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8396{
8397 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008398 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Anna Schumaker172d9de2015-05-14 10:56:10 -04008399 struct nfs4_session *session = nfs4_get_session(server->nfs_client);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008400
8401 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008402 nfs41_setup_sequence(session, &lgp->args.seq_args,
8403 &lgp->res.seq_res, task);
8404 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008405}
8406
8407static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8408{
8409 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008410
8411 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008412 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008413 dprintk("<-- %s\n", __func__);
8414}
8415
8416static int
8417nfs4_layoutget_handle_exception(struct rpc_task *task,
8418 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8419{
Trond Myklebustee314c22012-10-01 17:25:48 -07008420 struct inode *inode = lgp->args.inode;
8421 struct nfs_server *server = NFS_SERVER(inode);
8422 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008423 int nfs4err = task->tk_status;
8424 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008425 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008426
Boaz Harroshed7e5422014-01-22 20:34:54 +02008427 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008428
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008429 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008430 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008431 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008432
8433 /*
8434 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8435 * on the file. set tk_status to -ENODATA to tell upper layer to
8436 * retry go inband.
8437 */
8438 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008439 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008440 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008441 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008442 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8443 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8444 */
8445 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008446 status = -EOVERFLOW;
8447 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008448 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008449 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008450 * (or clients) writing to the same RAID stripe except when
8451 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008452 *
8453 * Treat it like we would RECALLCONFLICT -- we retry for a little
8454 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008455 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008456 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008457 if (lgp->args.minlength == 0) {
8458 status = -EOVERFLOW;
8459 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008460 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008461 status = -EBUSY;
8462 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008463 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008464 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008465 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008466 case -NFS4ERR_DELEG_REVOKED:
8467 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008468 case -NFS4ERR_EXPIRED:
8469 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008470 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008471 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008472 lo = NFS_I(inode)->layout;
8473 /* If the open stateid was bad, then recover it. */
8474 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8475 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008476 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008477 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008478 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008479 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008480 break;
8481 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008482
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008483 /*
8484 * Mark the bad layout state as invalid, then retry
8485 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008486 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008487 spin_unlock(&inode->i_lock);
8488 pnfs_free_lseg_list(&head);
8489 status = -EAGAIN;
8490 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008491 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008492
Trond Myklebust8ac09252017-01-23 22:44:12 -05008493 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008494 err = nfs4_handle_exception(server, nfs4err, exception);
8495 if (!status) {
8496 if (exception->retry)
8497 status = -EAGAIN;
8498 else
8499 status = err;
8500 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008501out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008502 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008503 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008504}
8505
Idan Kedar85541162012-08-02 11:47:10 +03008506static size_t max_response_pages(struct nfs_server *server)
8507{
8508 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8509 return nfs_page_array_len(0, max_resp_sz);
8510}
8511
8512static void nfs4_free_pages(struct page **pages, size_t size)
8513{
8514 int i;
8515
8516 if (!pages)
8517 return;
8518
8519 for (i = 0; i < size; i++) {
8520 if (!pages[i])
8521 break;
8522 __free_page(pages[i]);
8523 }
8524 kfree(pages);
8525}
8526
8527static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8528{
8529 struct page **pages;
8530 int i;
8531
8532 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8533 if (!pages) {
8534 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8535 return NULL;
8536 }
8537
8538 for (i = 0; i < size; i++) {
8539 pages[i] = alloc_page(gfp_flags);
8540 if (!pages[i]) {
8541 dprintk("%s: failed to allocate page\n", __func__);
8542 nfs4_free_pages(pages, size);
8543 return NULL;
8544 }
8545 }
8546
8547 return pages;
8548}
8549
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008550static void nfs4_layoutget_release(void *calldata)
8551{
8552 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008553 struct inode *inode = lgp->args.inode;
8554 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008555 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008556
8557 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008558 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008559 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008560 put_nfs_open_context(lgp->args.ctx);
8561 kfree(calldata);
8562 dprintk("<-- %s\n", __func__);
8563}
8564
8565static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8566 .rpc_call_prepare = nfs4_layoutget_prepare,
8567 .rpc_call_done = nfs4_layoutget_done,
8568 .rpc_release = nfs4_layoutget_release,
8569};
8570
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008571struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008572nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008573{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008574 struct inode *inode = lgp->args.inode;
8575 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008576 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008577 struct rpc_task *task;
8578 struct rpc_message msg = {
8579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8580 .rpc_argp = &lgp->args,
8581 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008582 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008583 };
8584 struct rpc_task_setup task_setup_data = {
8585 .rpc_client = server->client,
8586 .rpc_message = &msg,
8587 .callback_ops = &nfs4_layoutget_call_ops,
8588 .callback_data = lgp,
8589 .flags = RPC_TASK_ASYNC,
8590 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008591 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008592 struct nfs4_exception exception = {
8593 .inode = inode,
8594 .timeout = *timeout,
8595 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008596 int status = 0;
8597
8598 dprintk("--> %s\n", __func__);
8599
Peng Tao4bd5a9802014-11-17 11:05:17 +08008600 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8601 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8602
Idan Kedar85541162012-08-02 11:47:10 +03008603 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8604 if (!lgp->args.layout.pages) {
8605 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008606 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008607 }
8608 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8609
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008610 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008612 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008613
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008614 task = rpc_run_task(&task_setup_data);
8615 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008616 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008617 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008618 if (status == 0) {
8619 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8620 *timeout = exception.timeout;
8621 }
8622
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008623 trace_nfs4_layoutget(lgp->args.ctx,
8624 &lgp->args.range,
8625 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008626 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008627 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008628
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008629 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8630 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008631 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008632 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008633 rpc_put_task(task);
8634 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008635 if (status)
8636 return ERR_PTR(status);
8637 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008638}
8639
Benny Halevycbe82602011-05-22 19:52:37 +03008640static void
8641nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8642{
8643 struct nfs4_layoutreturn *lrp = calldata;
8644
8645 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008646 nfs41_setup_sequence(lrp->clp->cl_session,
8647 &lrp->args.seq_args,
8648 &lrp->res.seq_res,
8649 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008650}
8651
8652static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8653{
8654 struct nfs4_layoutreturn *lrp = calldata;
8655 struct nfs_server *server;
8656
8657 dprintk("--> %s\n", __func__);
8658
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008659 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008660 return;
8661
8662 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008663 switch (task->tk_status) {
8664 default:
8665 task->tk_status = 0;
8666 case 0:
8667 break;
8668 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008669 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008670 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008671 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008672 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008673 return;
8674 }
Benny Halevycbe82602011-05-22 19:52:37 +03008675 dprintk("<-- %s\n", __func__);
8676}
8677
8678static void nfs4_layoutreturn_release(void *calldata)
8679{
8680 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008681 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008682
8683 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008684 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008685 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008686 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008687 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8688 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008689 pnfs_put_layout_hdr(lrp->args.layout);
8690 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008691 kfree(calldata);
8692 dprintk("<-- %s\n", __func__);
8693}
8694
8695static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8696 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8697 .rpc_call_done = nfs4_layoutreturn_done,
8698 .rpc_release = nfs4_layoutreturn_release,
8699};
8700
Peng Tao6c166052014-11-17 09:30:40 +08008701int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008702{
8703 struct rpc_task *task;
8704 struct rpc_message msg = {
8705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8706 .rpc_argp = &lrp->args,
8707 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008708 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008709 };
8710 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008711 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008712 .rpc_message = &msg,
8713 .callback_ops = &nfs4_layoutreturn_call_ops,
8714 .callback_data = lrp,
8715 };
Peng Tao6c166052014-11-17 09:30:40 +08008716 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008717
Andrew Elble99ade3c2015-12-02 09:39:51 -05008718 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8719 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8720 &task_setup_data.rpc_client, &msg);
8721
Benny Halevycbe82602011-05-22 19:52:37 +03008722 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008723 if (!sync) {
8724 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8725 if (!lrp->inode) {
8726 nfs4_layoutreturn_release(lrp);
8727 return -EAGAIN;
8728 }
8729 task_setup_data.flags |= RPC_TASK_ASYNC;
8730 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008731 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008732 task = rpc_run_task(&task_setup_data);
8733 if (IS_ERR(task))
8734 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008735 if (sync)
8736 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008737 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008738 dprintk("<-- %s status=%d\n", __func__, status);
8739 rpc_put_task(task);
8740 return status;
8741}
8742
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008743static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008744_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8745 struct pnfs_device *pdev,
8746 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008747{
8748 struct nfs4_getdeviceinfo_args args = {
8749 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008750 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8751 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008752 };
8753 struct nfs4_getdeviceinfo_res res = {
8754 .pdev = pdev,
8755 };
8756 struct rpc_message msg = {
8757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8758 .rpc_argp = &args,
8759 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008760 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008761 };
8762 int status;
8763
8764 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008765 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008766 if (res.notification & ~args.notify_types)
8767 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008768 if (res.notification != args.notify_types)
8769 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008770
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008771 dprintk("<-- %s status=%d\n", __func__, status);
8772
8773 return status;
8774}
8775
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008776int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8777 struct pnfs_device *pdev,
8778 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008779{
8780 struct nfs4_exception exception = { };
8781 int err;
8782
8783 do {
8784 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008785 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008786 &exception);
8787 } while (exception.retry);
8788 return err;
8789}
8790EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8791
Andy Adamson863a3c62011-03-23 13:27:54 +00008792static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8793{
8794 struct nfs4_layoutcommit_data *data = calldata;
8795 struct nfs_server *server = NFS_SERVER(data->args.inode);
Anna Schumaker172d9de2015-05-14 10:56:10 -04008796 struct nfs4_session *session = nfs4_get_session(server->nfs_client);
Andy Adamson863a3c62011-03-23 13:27:54 +00008797
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008798 nfs41_setup_sequence(session,
8799 &data->args.seq_args,
8800 &data->res.seq_res,
8801 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008802}
8803
8804static void
8805nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8806{
8807 struct nfs4_layoutcommit_data *data = calldata;
8808 struct nfs_server *server = NFS_SERVER(data->args.inode);
8809
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008810 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008811 return;
8812
8813 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008814 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8815 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8816 case -NFS4ERR_BADLAYOUT: /* no layout */
8817 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008818 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008819 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008820 break;
8821 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008822 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008823 rpc_restart_call_prepare(task);
8824 return;
8825 }
8826 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008827}
8828
8829static void nfs4_layoutcommit_release(void *calldata)
8830{
8831 struct nfs4_layoutcommit_data *data = calldata;
8832
Andy Adamsondb29c082011-07-30 20:52:38 -04008833 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008834 nfs_post_op_update_inode_force_wcc(data->args.inode,
8835 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008836 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008837 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008838 kfree(data);
8839}
8840
8841static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8842 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8843 .rpc_call_done = nfs4_layoutcommit_done,
8844 .rpc_release = nfs4_layoutcommit_release,
8845};
8846
8847int
Andy Adamsonef311532011-03-12 02:58:10 -05008848nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008849{
8850 struct rpc_message msg = {
8851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8852 .rpc_argp = &data->args,
8853 .rpc_resp = &data->res,
8854 .rpc_cred = data->cred,
8855 };
8856 struct rpc_task_setup task_setup_data = {
8857 .task = &data->task,
8858 .rpc_client = NFS_CLIENT(data->args.inode),
8859 .rpc_message = &msg,
8860 .callback_ops = &nfs4_layoutcommit_ops,
8861 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008862 };
8863 struct rpc_task *task;
8864 int status = 0;
8865
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008866 dprintk("NFS: initiating layoutcommit call. sync %d "
8867 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008868 data->args.lastbytewritten,
8869 data->args.inode->i_ino);
8870
Trond Myklebust472e2592015-02-05 16:50:30 -05008871 if (!sync) {
8872 data->inode = nfs_igrab_and_active(data->args.inode);
8873 if (data->inode == NULL) {
8874 nfs4_layoutcommit_release(data);
8875 return -EAGAIN;
8876 }
8877 task_setup_data.flags = RPC_TASK_ASYNC;
8878 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008879 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008880 task = rpc_run_task(&task_setup_data);
8881 if (IS_ERR(task))
8882 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008883 if (sync)
8884 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008885 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008886 dprintk("%s: status %d\n", __func__, status);
8887 rpc_put_task(task);
8888 return status;
8889}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008890
Andy Adamson97431202013-08-08 10:57:56 -04008891/**
8892 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8893 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8894 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008895static int
8896_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008897 struct nfs_fsinfo *info,
8898 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008899{
8900 struct nfs41_secinfo_no_name_args args = {
8901 .style = SECINFO_STYLE_CURRENT_FH,
8902 };
8903 struct nfs4_secinfo_res res = {
8904 .flavors = flavors,
8905 };
8906 struct rpc_message msg = {
8907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8908 .rpc_argp = &args,
8909 .rpc_resp = &res,
8910 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008911 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008912 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008913 int status;
8914
8915 if (use_integrity) {
8916 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008917 cred = nfs4_get_clid_cred(server->nfs_client);
8918 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008919 }
8920
8921 dprintk("--> %s\n", __func__);
8922 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8923 &res.seq_res, 0);
8924 dprintk("<-- %s status=%d\n", __func__, status);
8925
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008926 if (cred)
8927 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008928
8929 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008930}
8931
8932static int
8933nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8934 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8935{
8936 struct nfs4_exception exception = { };
8937 int err;
8938 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008939 /* first try using integrity protection */
8940 err = -NFS4ERR_WRONGSEC;
8941
8942 /* try to use integrity protection with machine cred */
8943 if (_nfs4_is_integrity_protected(server->nfs_client))
8944 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8945 flavors, true);
8946
8947 /*
8948 * if unable to use integrity protection, or SECINFO with
8949 * integrity protection returns NFS4ERR_WRONGSEC (which is
8950 * disallowed by spec, but exists in deployed servers) use
8951 * the current filesystem's rpc_client and the user cred.
8952 */
8953 if (err == -NFS4ERR_WRONGSEC)
8954 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8955 flavors, false);
8956
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008957 switch (err) {
8958 case 0:
8959 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008960 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008961 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008962 default:
8963 err = nfs4_handle_exception(server, err, &exception);
8964 }
8965 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008966out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008967 return err;
8968}
8969
8970static int
8971nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8972 struct nfs_fsinfo *info)
8973{
8974 int err;
8975 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008976 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008977 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008978 struct nfs4_secinfo4 *secinfo;
8979 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008980
8981 page = alloc_page(GFP_KERNEL);
8982 if (!page) {
8983 err = -ENOMEM;
8984 goto out;
8985 }
8986
8987 flavors = page_address(page);
8988 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8989
8990 /*
8991 * Fall back on "guess and check" method if
8992 * the server doesn't support SECINFO_NO_NAME
8993 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008994 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008995 err = nfs4_find_root_sec(server, fhandle, info);
8996 goto out_freepage;
8997 }
8998 if (err)
8999 goto out_freepage;
9000
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009001 for (i = 0; i < flavors->num_flavors; i++) {
9002 secinfo = &flavors->flavors[i];
9003
9004 switch (secinfo->flavor) {
9005 case RPC_AUTH_NULL:
9006 case RPC_AUTH_UNIX:
9007 case RPC_AUTH_GSS:
9008 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9009 &secinfo->flavor_info);
9010 break;
9011 default:
9012 flavor = RPC_AUTH_MAXFLAVOR;
9013 break;
9014 }
9015
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009016 if (!nfs_auth_info_match(&server->auth_info, flavor))
9017 flavor = RPC_AUTH_MAXFLAVOR;
9018
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009019 if (flavor != RPC_AUTH_MAXFLAVOR) {
9020 err = nfs4_lookup_root_sec(server, fhandle,
9021 info, flavor);
9022 if (!err)
9023 break;
9024 }
9025 }
9026
9027 if (flavor == RPC_AUTH_MAXFLAVOR)
9028 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009029
9030out_freepage:
9031 put_page(page);
9032 if (err == -EACCES)
9033 return -EPERM;
9034out:
9035 return err;
9036}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009037
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009038static int _nfs41_test_stateid(struct nfs_server *server,
9039 nfs4_stateid *stateid,
9040 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009041{
9042 int status;
9043 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009044 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009045 };
9046 struct nfs41_test_stateid_res res;
9047 struct rpc_message msg = {
9048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9049 .rpc_argp = &args,
9050 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009051 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009052 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009053 struct rpc_clnt *rpc_client = server->client;
9054
9055 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9056 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009057
Chuck Lever38527b12012-07-11 16:30:23 -04009058 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009059 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009060 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009061 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009062 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009063 if (status != NFS_OK) {
9064 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009065 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009066 }
9067 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009068 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009069}
9070
Trond Myklebust43912bb2016-09-22 13:38:56 -04009071static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9072 int err, struct nfs4_exception *exception)
9073{
9074 exception->retry = 0;
9075 switch(err) {
9076 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009077 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009078 nfs4_handle_exception(server, err, exception);
9079 break;
9080 case -NFS4ERR_BADSESSION:
9081 case -NFS4ERR_BADSLOT:
9082 case -NFS4ERR_BAD_HIGH_SLOT:
9083 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9084 case -NFS4ERR_DEADSESSION:
9085 nfs4_do_handle_exception(server, err, exception);
9086 }
9087}
9088
Chuck Lever38527b12012-07-11 16:30:23 -04009089/**
9090 * nfs41_test_stateid - perform a TEST_STATEID operation
9091 *
9092 * @server: server / transport on which to perform the operation
9093 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009094 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009095 *
9096 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9097 * Otherwise a negative NFS4ERR value is returned if the operation
9098 * failed or the state ID is not currently valid.
9099 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009100static int nfs41_test_stateid(struct nfs_server *server,
9101 nfs4_stateid *stateid,
9102 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009103{
9104 struct nfs4_exception exception = { };
9105 int err;
9106 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009107 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009108 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009109 } while (exception.retry);
9110 return err;
9111}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009112
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009113struct nfs_free_stateid_data {
9114 struct nfs_server *server;
9115 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009116 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009117};
9118
9119static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9120{
9121 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker172d9de2015-05-14 10:56:10 -04009122 nfs41_setup_sequence(nfs4_get_session(data->server->nfs_client),
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009123 &data->args.seq_args,
9124 &data->res.seq_res,
9125 task);
9126}
9127
9128static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9129{
9130 struct nfs_free_stateid_data *data = calldata;
9131
9132 nfs41_sequence_done(task, &data->res.seq_res);
9133
9134 switch (task->tk_status) {
9135 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009136 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009137 rpc_restart_call_prepare(task);
9138 }
9139}
9140
9141static void nfs41_free_stateid_release(void *calldata)
9142{
9143 kfree(calldata);
9144}
9145
Trond Myklebust17f26b12013-08-21 15:48:42 -04009146static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009147 .rpc_call_prepare = nfs41_free_stateid_prepare,
9148 .rpc_call_done = nfs41_free_stateid_done,
9149 .rpc_release = nfs41_free_stateid_release,
9150};
9151
9152static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009153 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009154 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009155 bool privileged)
9156{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009157 struct rpc_message msg = {
9158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009159 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009160 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009161 struct rpc_task_setup task_setup = {
9162 .rpc_client = server->client,
9163 .rpc_message = &msg,
9164 .callback_ops = &nfs41_free_stateid_ops,
9165 .flags = RPC_TASK_ASYNC,
9166 };
9167 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009168
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009169 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9170 &task_setup.rpc_client, &msg);
9171
Chuck Lever38527b12012-07-11 16:30:23 -04009172 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009173 data = kmalloc(sizeof(*data), GFP_NOFS);
9174 if (!data)
9175 return ERR_PTR(-ENOMEM);
9176 data->server = server;
9177 nfs4_stateid_copy(&data->args.stateid, stateid);
9178
9179 task_setup.callback_data = data;
9180
9181 msg.rpc_argp = &data->args;
9182 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009183 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009184 if (privileged)
9185 nfs4_set_sequence_privileged(&data->args.seq_args);
9186
9187 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009188}
9189
Chuck Lever38527b12012-07-11 16:30:23 -04009190/**
9191 * nfs41_free_stateid - perform a FREE_STATEID operation
9192 *
9193 * @server: server / transport on which to perform the operation
9194 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009195 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009196 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009197 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009198 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009199 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009200static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009201 const nfs4_stateid *stateid,
9202 struct rpc_cred *cred,
9203 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009204{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009205 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009206
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009207 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009208 if (IS_ERR(task))
9209 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009210 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009211 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009212}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009213
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009214static void
9215nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009216{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009217 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009218
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009219 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009220 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009221}
9222
Trond Myklebust36281ca2012-03-04 18:13:56 -05009223static bool nfs41_match_stateid(const nfs4_stateid *s1,
9224 const nfs4_stateid *s2)
9225{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009226 if (s1->type != s2->type)
9227 return false;
9228
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009229 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009230 return false;
9231
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009232 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009233 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009234 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009235 return true;
9236
9237 return false;
9238}
9239
Andy Adamson557134a2009-04-01 09:21:53 -04009240#endif /* CONFIG_NFS_V4_1 */
9241
Trond Myklebust36281ca2012-03-04 18:13:56 -05009242static bool nfs4_match_stateid(const nfs4_stateid *s1,
9243 const nfs4_stateid *s2)
9244{
Trond Myklebustf597c532012-03-04 18:13:56 -05009245 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009246}
9247
9248
Trond Myklebust17280172012-03-11 13:11:00 -04009249static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009250 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009251 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 .recover_open = nfs4_open_reclaim,
9253 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009254 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009255 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256};
9257
Andy Adamson591d71c2009-04-01 09:22:47 -04009258#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009259static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009260 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9261 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9262 .recover_open = nfs4_open_reclaim,
9263 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009264 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009265 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009266 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009267};
9268#endif /* CONFIG_NFS_V4_1 */
9269
Trond Myklebust17280172012-03-11 13:11:00 -04009270static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009271 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009272 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009273 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009275 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276};
9277
Andy Adamson591d71c2009-04-01 09:22:47 -04009278#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009279static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009280 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9281 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009282 .recover_open = nfs41_open_expired,
9283 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009284 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009285};
9286#endif /* CONFIG_NFS_V4_1 */
9287
Trond Myklebust17280172012-03-11 13:11:00 -04009288static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009289 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009290 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009291 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009292};
9293
9294#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009295static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009296 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009297 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009298 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009299};
9300#endif
9301
Chuck Leverec011fe2013-10-17 14:12:39 -04009302static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009303 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009304 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009305};
9306
9307#if defined(CONFIG_NFS_V4_1)
9308static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009309 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009310 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009311};
9312#endif /* CONFIG_NFS_V4_1 */
9313
Trond Myklebust97dc1352010-06-16 09:52:26 -04009314static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9315 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009316 .init_caps = NFS_CAP_READDIRPLUS
9317 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009318 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009319 .init_client = nfs40_init_client,
9320 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009321 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009322 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009323 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009324 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009325 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009326 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009327 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9328 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9329 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009330 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009331};
9332
9333#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009334static struct nfs_seqid *
9335nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9336{
9337 return NULL;
9338}
9339
Trond Myklebust97dc1352010-06-16 09:52:26 -04009340static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9341 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009342 .init_caps = NFS_CAP_READDIRPLUS
9343 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009344 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009345 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009346 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009347 .init_client = nfs41_init_client,
9348 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009349 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009350 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009351 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009352 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009353 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009354 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009355 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009356 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9357 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9358 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009359 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009360};
9361#endif
9362
Steve Dickson42c2c422013-05-22 12:50:38 -04009363#if defined(CONFIG_NFS_V4_2)
9364static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9365 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009366 .init_caps = NFS_CAP_READDIRPLUS
9367 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009368 | NFS_CAP_POSIX_LOCK
9369 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009370 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009371 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009372 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009373 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009374 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009375 | NFS_CAP_LAYOUTSTATS
9376 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009377 .init_client = nfs41_init_client,
9378 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009379 .match_stateid = nfs41_match_stateid,
9380 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009381 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009382 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009383 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009384 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009385 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009386 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9387 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9388 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009389 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009390};
9391#endif
9392
Trond Myklebust97dc1352010-06-16 09:52:26 -04009393const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9394 [0] = &nfs_v4_0_minor_ops,
9395#if defined(CONFIG_NFS_V4_1)
9396 [1] = &nfs_v4_1_minor_ops,
9397#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009398#if defined(CONFIG_NFS_V4_2)
9399 [2] = &nfs_v4_2_minor_ops,
9400#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009401};
9402
Trond Myklebust13997822016-07-24 17:10:52 -04009403static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009404{
9405 ssize_t error, error2;
9406
9407 error = generic_listxattr(dentry, list, size);
9408 if (error < 0)
9409 return error;
9410 if (list) {
9411 list += error;
9412 size -= error;
9413 }
9414
9415 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9416 if (error2 < 0)
9417 return error2;
9418 return error + error2;
9419}
9420
Trond Myklebust17f26b12013-08-21 15:48:42 -04009421static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009422 .create = nfs_create,
9423 .lookup = nfs_lookup,
9424 .atomic_open = nfs_atomic_open,
9425 .link = nfs_link,
9426 .unlink = nfs_unlink,
9427 .symlink = nfs_symlink,
9428 .mkdir = nfs_mkdir,
9429 .rmdir = nfs_rmdir,
9430 .mknod = nfs_mknod,
9431 .rename = nfs_rename,
9432 .permission = nfs_permission,
9433 .getattr = nfs_getattr,
9434 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009435 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009436};
9437
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009438static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009439 .permission = nfs_permission,
9440 .getattr = nfs_getattr,
9441 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009442 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009443};
9444
David Howells509de812006-08-22 20:06:11 -04009445const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 .version = 4, /* protocol version */
9447 .dentry_ops = &nfs4_dentry_operations,
9448 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009449 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009450 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009452 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009453 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 .getattr = nfs4_proc_getattr,
9455 .setattr = nfs4_proc_setattr,
9456 .lookup = nfs4_proc_lookup,
9457 .access = nfs4_proc_access,
9458 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459 .create = nfs4_proc_create,
9460 .remove = nfs4_proc_remove,
9461 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009462 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009464 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009465 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009466 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467 .link = nfs4_proc_link,
9468 .symlink = nfs4_proc_symlink,
9469 .mkdir = nfs4_proc_mkdir,
9470 .rmdir = nfs4_proc_remove,
9471 .readdir = nfs4_proc_readdir,
9472 .mknod = nfs4_proc_mknod,
9473 .statfs = nfs4_proc_statfs,
9474 .fsinfo = nfs4_proc_fsinfo,
9475 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009476 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009478 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009480 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009482 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009483 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009484 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009485 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009487 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009488 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009489 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009490 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009491 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009492 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009493 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009494 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009495 .create_server = nfs4_create_server,
9496 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009497};
9498
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009499static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009500 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009501 .list = nfs4_xattr_list_nfs4_acl,
9502 .get = nfs4_xattr_get_nfs4_acl,
9503 .set = nfs4_xattr_set_nfs4_acl,
9504};
9505
9506const struct xattr_handler *nfs4_xattr_handlers[] = {
9507 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009508#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9509 &nfs4_xattr_nfs4_label_handler,
9510#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009511 NULL
9512};
9513
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514/*
9515 * Local variables:
9516 * c-basic-offset: 8
9517 * End:
9518 */