blob: 46cb55a81dbffa38da821cfe78fd065934a69c8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400228 | FATTR4_WORD0_FILEID,
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_MAXLINK
242 | FATTR4_WORD0_MAXNAME,
243 0
244};
245
Fred Isamandae100c2011-07-30 20:52:37 -0400246const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700250 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800253 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400257 FATTR4_WORD0_TYPE
258 | FATTR4_WORD0_CHANGE
259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
263 FATTR4_WORD1_MODE
264 | FATTR4_WORD1_NUMLINKS
265 | FATTR4_WORD1_OWNER
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400273};
274
Al Virobc4785c2006-10-19 23:28:51 -0700275static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct nfs4_readdir_arg *readdir)
277{
Al Viro0dbb4c62006-10-19 23:28:49 -0700278 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000281 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
283 return;
284 }
285
286 readdir->cookie = 0;
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
288 if (cookie == 2)
289 return;
290
291 /*
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
296 * instead of 1 or 2.
297 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800298 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (cookie == 0) {
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
318 p++;
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800326 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust26d36302016-09-22 13:39:05 -0400329static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
332{
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
334
335 ops->test_and_free_expired(server, stateid, cred);
336}
337
338static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
341{
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
344}
345
346static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
349{
350 nfs4_stateid tmp;
351
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
354}
355
NeilBrown8478eaa2014-09-18 16:09:27 +1000356static long nfs4_update_delay(long *timeout)
357{
358 long ret;
359 if (!timeout)
360 return NFS4_POLL_RETRY_MAX;
361 if (*timeout <= 0)
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
365 ret = *timeout;
366 *timeout <<= 1;
367 return ret;
368}
369
Trond Myklebust65de8722008-12-23 15:21:44 -0500370static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
371{
372 int res = 0;
373
374 might_sleep();
375
NeilBrown8478eaa2014-09-18 16:09:27 +1000376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 if (fatal_signal_pending(current))
379 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 return res;
381}
382
383/* This is the error handling routine for processes that are allowed
384 * to sleep.
385 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400386static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500388{
389 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500390 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400391 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500393 int ret = errorcode;
394
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400395 exception->delay = 0;
396 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400398
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
401
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 switch(errorcode) {
403 case 0:
404 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
411 stateid);
412 goto wait_on_recovery;
413 }
414 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400415 if (inode) {
416 int err;
417
418 err = nfs_async_inode_return_delegation(inode,
419 stateid);
420 if (err == 0)
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
424 break;
425 }
426 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500427 if (state == NULL)
428 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400429 ret = nfs4_schedule_stateid_recovery(server, state);
430 if (ret < 0)
431 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500434 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400437 case -NFS4ERR_MOVED:
438 ret = nfs4_schedule_migration_recovery(server);
439 if (ret < 0)
440 break;
441 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500445#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
454 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400456 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500457#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
461 * fail
462 */
463 ret = -EBUSY;
464 break;
465 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400467 nfs_inc_server_stats(server, NFSIOS_DELAY);
468 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400469 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400470 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400471 exception->delay = 1;
472 return 0;
473
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400474 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800477 break;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
486 "uid/gids. "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
489 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500490 }
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500493wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400494 exception->recovering = 1;
495 return 0;
496}
497
498/* This is the error handling routine for processes that are allowed
499 * to sleep.
500 */
501int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
502{
503 struct nfs_client *clp = server->nfs_client;
504 int ret;
505
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
509 goto out_retry;
510 }
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 return -EIO;
515 goto out_retry;
516 }
517 return ret;
518out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500519 if (ret == 0)
520 exception->retry = 1;
521 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500522}
523
Trond Myklebust037fc982015-09-20 15:51:00 -0400524static int
525nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
527{
528 struct nfs_client *clp = server->nfs_client;
529 int ret;
530
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
534 goto out_retry;
535 }
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
540 goto out_retry;
541 }
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
543 ret = -EIO;
544 return ret;
545out_retry:
546 if (ret == 0)
547 exception->retry = 1;
548 return ret;
549}
550
551static int
552nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
554{
555 struct nfs4_exception exception = {
556 .state = state,
557 };
558
559 if (task->tk_status >= 0)
560 return 0;
561 if (timeout)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
564 task->tk_status,
565 &exception);
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
568 if (exception.retry)
569 return -EAGAIN;
570 return 0;
571}
572
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400573/*
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
576 */
577static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
578{
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
580
581 if (flavor == RPC_AUTH_GSS_KRB5I ||
582 flavor == RPC_AUTH_GSS_KRB5P)
583 return true;
584
585 return false;
586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Peng Taocb04ad22014-06-11 05:24:15 +0800625int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400629{
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 struct nfs4_slot *slot;
631
632 /* slot already allocated? */
633 if (res->sr_slot != NULL)
634 goto out_start;
635
636 spin_lock(&tbl->slot_tbl_lock);
637 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
638 goto out_sleep;
639
640 slot = nfs4_alloc_slot(tbl);
641 if (IS_ERR(slot)) {
642 if (slot == ERR_PTR(-ENOMEM))
643 task->tk_timeout = HZ >> 2;
644 goto out_sleep;
645 }
646 spin_unlock(&tbl->slot_tbl_lock);
647
Trond Myklebust0a014a42016-09-22 13:38:51 -0400648 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400649 args->sa_slot = slot;
650 res->sr_slot = slot;
651
652out_start:
653 rpc_call_start(task);
654 return 0;
655
656out_sleep:
657 if (args->sa_privileged)
658 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
659 NULL, RPC_PRIORITY_PRIVILEGED);
660 else
661 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
662 spin_unlock(&tbl->slot_tbl_lock);
663 return -EAGAIN;
664}
Peng Taocb04ad22014-06-11 05:24:15 +0800665EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400666
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400667static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400668{
669 struct nfs4_slot *slot = res->sr_slot;
670 struct nfs4_slot_table *tbl;
671
Chuck Lever3bd23842013-08-09 12:49:19 -0400672 tbl = slot->table;
673 spin_lock(&tbl->slot_tbl_lock);
674 if (!nfs41_wake_and_assign_slot(tbl, slot))
675 nfs4_free_slot(tbl, slot);
676 spin_unlock(&tbl->slot_tbl_lock);
677
678 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400679}
680
681static int nfs40_sequence_done(struct rpc_task *task,
682 struct nfs4_sequence_res *res)
683{
684 if (res->sr_slot != NULL)
685 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400686 return 1;
687}
688
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689#if defined(CONFIG_NFS_V4_1)
690
Trond Myklebustd185a332010-06-16 09:52:25 -0400691static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400692{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500693 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400694 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500695 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500696 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400697
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500698 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500699 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500700
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400701 /* Bump the slot sequence number */
702 if (slot->seq_done)
703 slot->seq_nr++;
704 slot->seq_done = 0;
705
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500706 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500707 /* Be nice to the server: try to ensure that the last transmitted
708 * value for highest_user_slotid <= target_highest_slotid
709 */
710 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
711 send_new_highest_used_slotid = true;
712
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500713 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500714 send_new_highest_used_slotid = false;
715 goto out_unlock;
716 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500717 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500718
719 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
720 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500721out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500722 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400723 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500724 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400725 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400726 if (waitqueue_active(&tbl->slot_waitq))
727 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400728}
729
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400730static int nfs41_sequence_process(struct rpc_task *task,
731 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400732{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500733 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500734 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500736 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500737 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400738
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 if (slot == NULL)
740 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400741 /* don't increment the sequence number if the task wasn't sent */
742 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743 goto out;
744
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500745 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500746
Trond Myklebustac20d162012-12-15 15:36:07 -0500747 if (slot->interrupted) {
748 slot->interrupted = 0;
749 interrupted = true;
750 }
751
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400752 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500753 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400754 switch (res->sr_status) {
755 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400756 /* If previous op on slot was interrupted and we reused
757 * the seq# and got a reply from the cache, then retry
758 */
759 if (task->tk_status == -EREMOTEIO && interrupted) {
760 ++slot->seq_nr;
761 goto retry_nowait;
762 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400763 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400764 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500765 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500766 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500767 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400768 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
769 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500770 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500772 case 1:
773 /*
774 * sr_status remains 1 if an RPC level error occurred.
775 * The server may or may not have processed the sequence
776 * operation..
777 * Mark the slot as having hosted an interrupted RPC call.
778 */
779 slot->interrupted = 1;
780 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400781 case -NFS4ERR_DELAY:
782 /* The server detected a resend of the RPC call and
783 * returned NFS4ERR_DELAY as per Section 2.10.6.2
784 * of RFC5661.
785 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500786 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400787 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500788 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500789 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400790 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500791 case -NFS4ERR_BADSLOT:
792 /*
793 * The slot id we used was probably retired. Try again
794 * using a different slot id.
795 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500796 goto retry_nowait;
797 case -NFS4ERR_SEQ_MISORDERED:
798 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500799 * Was the last operation on this sequence interrupted?
800 * If so, retry after bumping the sequence number.
801 */
802 if (interrupted) {
803 ++slot->seq_nr;
804 goto retry_nowait;
805 }
806 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500807 * Could this slot have been previously retired?
808 * If so, then the server may be expecting seq_nr = 1!
809 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500810 if (slot->seq_nr != 1) {
811 slot->seq_nr = 1;
812 goto retry_nowait;
813 }
814 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500815 case -NFS4ERR_SEQ_FALSE_RETRY:
816 ++slot->seq_nr;
817 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500818 case -NFS4ERR_DEADSESSION:
819 case -NFS4ERR_BADSESSION:
820 nfs4_schedule_session_recovery(session, res->sr_status);
821 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400822 default:
823 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400824 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400825 }
826out:
827 /* The session may be reset by one of the error handlers. */
828 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500829out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500830 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500831retry_nowait:
832 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400833 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500834 task->tk_status = 0;
835 ret = 0;
836 }
837 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400838out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400839 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 goto out;
841 rpc_delay(task, NFS4_POLL_RETRY_MAX);
842 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400843}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400844
845int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
846{
847 if (!nfs41_sequence_process(task, res))
848 return 0;
849 if (res->sr_slot != NULL)
850 nfs41_sequence_free_slot(res);
851 return 1;
852
853}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500854EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400855
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400856static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
857{
858 if (res->sr_slot == NULL)
859 return 1;
860 if (res->sr_slot->table->session != NULL)
861 return nfs41_sequence_process(task, res);
862 return nfs40_sequence_done(task, res);
863}
864
865static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
866{
867 if (res->sr_slot != NULL) {
868 if (res->sr_slot->table->session != NULL)
869 nfs41_sequence_free_slot(res);
870 else
871 nfs40_sequence_free_slot(res);
872 }
873}
874
Peng Tao2c4b1312014-06-11 05:24:16 +0800875int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400876{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500877 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400878 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400879 if (!res->sr_slot->table->session)
880 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400881 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400882}
Peng Tao2c4b1312014-06-11 05:24:16 +0800883EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400884
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000885int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400886 struct nfs4_sequence_args *args,
887 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888 struct rpc_task *task)
889{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400890 struct nfs4_slot *slot;
891 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400892
893 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400895 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400896 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400898 tbl = &session->fc_slot_table;
899
Trond Myklebust69d206b2012-11-22 13:21:02 -0500900 task->tk_timeout = 0;
901
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400902 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400903 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400904 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500905 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400907 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400908 }
909
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500910 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500911 if (IS_ERR(slot)) {
912 /* If out of memory, try again in 1/4 second */
913 if (slot == ERR_PTR(-ENOMEM))
914 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400915 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400916 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400917 }
918 spin_unlock(&tbl->slot_tbl_lock);
919
Trond Myklebust0a014a42016-09-22 13:38:51 -0400920 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500921 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400922
Chuck Levere8d92382013-08-09 12:47:51 -0400923 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500924 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400925
Benny Halevydfb4f3092010-09-24 09:17:01 -0400926 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500927 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400928 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400929 /*
930 * sr_status is only set in decode_sequence, and so will remain
931 * set to 1 if an rpc level failure occurs.
932 */
933 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400934 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400935out_success:
936 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400938out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400939 /* Privileged tasks are queued with top priority */
940 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400941 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
942 NULL, RPC_PRIORITY_PRIVILEGED);
943 else
944 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400945 spin_unlock(&tbl->slot_tbl_lock);
946 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000948EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400949
Chuck Lever5a580e02013-08-09 12:48:09 -0400950static int nfs4_setup_sequence(const struct nfs_server *server,
951 struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400954{
Anna Schumaker172d9de2015-05-14 10:56:10 -0400955 struct nfs4_session *session = nfs4_get_session(server->nfs_client);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400956 int ret = 0;
957
Chuck Lever3bd23842013-08-09 12:49:19 -0400958 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800959 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
960 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400961
Chuck Levere8d92382013-08-09 12:47:51 -0400962 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400963 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400964 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400965
Trond Myklebust9d12b212012-01-17 22:04:25 -0500966 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400967
Andy Adamsonce5039c2009-04-01 09:22:13 -0400968 dprintk("<-- %s status=%d\n", __func__, ret);
969 return ret;
970}
971
Andy Adamsonce5039c2009-04-01 09:22:13 -0400972static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
973{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400974 struct nfs4_call_sync_data *data = calldata;
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
Chuck Lever5a580e02013-08-09 12:48:09 -0400996static int nfs4_setup_sequence(const struct nfs_server *server,
997 struct nfs4_sequence_args *args,
998 struct nfs4_sequence_res *res,
999 struct rpc_task *task)
1000{
Peng Taocb04ad22014-06-11 05:24:15 +08001001 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1002 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001003}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001004
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001005static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1006{
1007 return nfs40_sequence_done(task, res);
1008}
1009
1010static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1011{
1012 if (res->sr_slot != NULL)
1013 nfs40_sequence_free_slot(res);
1014}
1015
Peng Tao2c4b1312014-06-11 05:24:16 +08001016int nfs4_sequence_done(struct rpc_task *task,
1017 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001018{
Chuck Lever3bd23842013-08-09 12:49:19 -04001019 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001020}
Peng Tao2c4b1312014-06-11 05:24:16 +08001021EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001022
1023#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001024
Chuck Lever9915ea72013-08-09 12:48:27 -04001025static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1026{
1027 struct nfs4_call_sync_data *data = calldata;
1028 nfs4_setup_sequence(data->seq_server,
1029 data->seq_args, data->seq_res, task);
1030}
1031
1032static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1033{
1034 struct nfs4_call_sync_data *data = calldata;
1035 nfs4_sequence_done(task, data->seq_res);
1036}
1037
1038static const struct rpc_call_ops nfs40_call_sync_ops = {
1039 .rpc_call_prepare = nfs40_call_sync_prepare,
1040 .rpc_call_done = nfs40_call_sync_done,
1041};
1042
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001043static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001044 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001046 struct nfs4_sequence_args *args,
1047 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001048{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001049 int ret;
1050 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 struct nfs_client *clp = server->nfs_client;
1052 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001053 .seq_server = server,
1054 .seq_args = args,
1055 .seq_res = res,
1056 };
1057 struct rpc_task_setup task_setup = {
1058 .rpc_client = clnt,
1059 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001060 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001061 .callback_data = &data
1062 };
1063
1064 task = rpc_run_task(&task_setup);
1065 if (IS_ERR(task))
1066 ret = PTR_ERR(task);
1067 else {
1068 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001069 rpc_put_task(task);
1070 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001071 return ret;
1072}
1073
Bryan Schumaker7c513052011-03-24 17:12:24 +00001074int nfs4_call_sync(struct rpc_clnt *clnt,
1075 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001076 struct rpc_message *msg,
1077 struct nfs4_sequence_args *args,
1078 struct nfs4_sequence_res *res,
1079 int cache_reply)
1080{
Chuck Levera9c92d62013-08-09 12:48:18 -04001081 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001082 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001085static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1086 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001092 if (cinfo->atomic && cinfo->before == dir->i_version) {
1093 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1094 nfsi->attrtimeo_timestamp = jiffies;
1095 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001097 if (cinfo->before != dir->i_version)
1098 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1099 NFS_INO_INVALID_ACL;
1100 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001101 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001102 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001103 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001104 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 spin_unlock(&dir->i_lock);
1106}
1107
1108struct nfs4_opendata {
1109 struct kref kref;
1110 struct nfs_openargs o_arg;
1111 struct nfs_openres o_res;
1112 struct nfs_open_confirmargs c_arg;
1113 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001114 struct nfs4_string owner_name;
1115 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001116 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001118 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001120 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct nfs4_state_owner *owner;
1122 struct nfs4_state *state;
1123 struct iattr attrs;
1124 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001125 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001126 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001127 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001128 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int cancelled;
1130};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001131
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001132static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1133 int err, struct nfs4_exception *exception)
1134{
1135 if (err != -EINVAL)
1136 return false;
1137 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1138 return false;
1139 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1140 exception->retry = 1;
1141 return true;
1142}
1143
Trond Myklebust6ae37332015-01-30 14:21:14 -05001144static u32
1145nfs4_map_atomic_open_share(struct nfs_server *server,
1146 fmode_t fmode, int openflags)
1147{
1148 u32 res = 0;
1149
1150 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1151 case FMODE_READ:
1152 res = NFS4_SHARE_ACCESS_READ;
1153 break;
1154 case FMODE_WRITE:
1155 res = NFS4_SHARE_ACCESS_WRITE;
1156 break;
1157 case FMODE_READ|FMODE_WRITE:
1158 res = NFS4_SHARE_ACCESS_BOTH;
1159 }
1160 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1161 goto out;
1162 /* Want no delegation if we're using O_DIRECT */
1163 if (openflags & O_DIRECT)
1164 res |= NFS4_SHARE_WANT_NO_DELEG;
1165out:
1166 return res;
1167}
1168
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001169static enum open_claim_type4
1170nfs4_map_atomic_open_claim(struct nfs_server *server,
1171 enum open_claim_type4 claim)
1172{
1173 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1174 return claim;
1175 switch (claim) {
1176 default:
1177 return claim;
1178 case NFS4_OPEN_CLAIM_FH:
1179 return NFS4_OPEN_CLAIM_NULL;
1180 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1182 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1183 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1184 }
1185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188{
1189 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001190 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001191 p->o_res.seqid = p->o_arg.seqid;
1192 p->c_res.seqid = p->c_arg.seqid;
1193 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001194 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001196 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197}
1198
Al Viro82a2c1b2011-06-22 18:30:55 -04001199static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001202 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001203 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001204 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205{
Al Viro82a2c1b2011-06-22 18:30:55 -04001206 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001207 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001209 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001210 struct nfs4_opendata *p;
1211
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001212 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 if (p == NULL)
1214 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001215
1216 p->f_label = nfs4_label_alloc(server, gfp_mask);
1217 if (IS_ERR(p->f_label))
1218 goto err_free_p;
1219
Kinglong Meea49c2692015-07-27 15:31:38 +08001220 p->a_label = nfs4_label_alloc(server, gfp_mask);
1221 if (IS_ERR(p->a_label))
1222 goto err_free_f;
1223
Trond Myklebust63f5f792015-01-23 19:19:25 -05001224 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1225 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001226 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001227 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001228 nfs_sb_active(dentry->d_sb);
1229 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001230 p->dir = parent;
1231 p->owner = sp;
1232 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001233 p->o_arg.open_flags = flags;
1234 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001235 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001236 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001237 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1238 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001239 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1240 * will return permission denied for all bits until close */
1241 if (!(flags & O_EXCL)) {
1242 /* ask server to check for all possible rights as results
1243 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001244 switch (p->o_arg.claim) {
1245 default:
1246 break;
1247 case NFS4_OPEN_CLAIM_NULL:
1248 case NFS4_OPEN_CLAIM_FH:
1249 p->o_arg.access = NFS4_ACCESS_READ |
1250 NFS4_ACCESS_MODIFY |
1251 NFS4_ACCESS_EXTEND |
1252 NFS4_ACCESS_EXECUTE;
1253 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001254 }
David Howells7539bba2006-08-22 20:06:09 -04001255 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001256 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1257 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001258 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001260 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001261 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001262 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001263 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001264 case NFS4_OPEN_CLAIM_NULL:
1265 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1266 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1267 p->o_arg.fh = NFS_FH(dir);
1268 break;
1269 case NFS4_OPEN_CLAIM_PREVIOUS:
1270 case NFS4_OPEN_CLAIM_FH:
1271 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1272 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001273 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001274 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001275 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001276 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001277
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 p->o_arg.u.attrs = &p->attrs;
1279 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001280
1281 verf[0] = jiffies;
1282 verf[1] = current->pid;
1283 memcpy(p->o_arg.u.verifier.data, verf,
1284 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001286 p->c_arg.fh = &p->o_res.fh;
1287 p->c_arg.stateid = &p->o_res.stateid;
1288 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001289 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001290 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001291 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001292
1293err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001294 nfs4_label_free(p->a_label);
1295err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001296 nfs4_label_free(p->f_label);
1297err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001298 kfree(p);
1299err:
1300 dput(parent);
1301 return NULL;
1302}
1303
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 struct nfs4_opendata *p = container_of(kref,
1307 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001308 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309
1310 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001311 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001312 if (p->state != NULL)
1313 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001315
Kinglong Meea49c2692015-07-27 15:31:38 +08001316 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001317 nfs4_label_free(p->f_label);
1318
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001319 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001320 dput(p->dentry);
1321 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001322 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001323 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001324 kfree(p);
1325}
1326
1327static void nfs4_opendata_put(struct nfs4_opendata *p)
1328{
1329 if (p != NULL)
1330 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331}
1332
Trond Myklebust06f814a2006-01-03 09:55:07 +01001333static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1334{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001335 int ret;
1336
Trond Myklebust06f814a2006-01-03 09:55:07 +01001337 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001338 return ret;
1339}
1340
Trond Myklebust24311f82015-09-20 10:50:17 -04001341static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1342 fmode_t fmode)
1343{
1344 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1345 case FMODE_READ|FMODE_WRITE:
1346 return state->n_rdwr != 0;
1347 case FMODE_WRITE:
1348 return state->n_wronly != 0;
1349 case FMODE_READ:
1350 return state->n_rdonly != 0;
1351 }
1352 WARN_ON_ONCE(1);
1353 return false;
1354}
1355
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357{
1358 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001359
Trond Myklebust536e43d2012-01-17 22:04:26 -05001360 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001361 goto out;
1362 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001363 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001364 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1365 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 break;
1367 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001368 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1369 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 break;
1371 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001372 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1373 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001374 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001376 return ret;
1377}
1378
Trond Myklebust2a606182015-08-19 22:30:00 -05001379static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1380 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001381{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001382 if (delegation == NULL)
1383 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001384 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001386 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1387 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001388 switch (claim) {
1389 case NFS4_OPEN_CLAIM_NULL:
1390 case NFS4_OPEN_CLAIM_FH:
1391 break;
1392 case NFS4_OPEN_CLAIM_PREVIOUS:
1393 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1394 break;
1395 default:
1396 return 0;
1397 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001398 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001399 return 1;
1400}
1401
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001402static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001403{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001404 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001405 case FMODE_WRITE:
1406 state->n_wronly++;
1407 break;
1408 case FMODE_READ:
1409 state->n_rdonly++;
1410 break;
1411 case FMODE_READ|FMODE_WRITE:
1412 state->n_rdwr++;
1413 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001414 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001415}
1416
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001417#ifdef CONFIG_NFS_V4_1
1418static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1419{
1420 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1421 return true;
1422 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1423 return true;
1424 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1425 return true;
1426 return false;
1427}
1428#endif /* CONFIG_NFS_V4_1 */
1429
Trond Myklebust4f14c192014-02-12 19:15:06 -05001430static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001431{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001432 struct nfs_client *clp = state->owner->so_server->nfs_client;
1433 bool need_recover = false;
1434
1435 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1436 need_recover = true;
1437 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1438 need_recover = true;
1439 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1440 need_recover = true;
1441 if (need_recover)
1442 nfs4_state_mark_reclaim_nograce(clp, state);
1443}
1444
Trond Myklebuste999e802014-02-10 18:20:47 -05001445static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001446 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001447{
1448 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1449 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001450 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001451 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001452 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001453 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001454 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001455 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1456 return true;
1457 return false;
1458}
1459
Trond Myklebustf95549c2015-01-23 18:06:09 -05001460static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1461{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001462 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1463 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001464 if (state->n_wronly)
1465 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 if (state->n_rdonly)
1467 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1468 if (state->n_rdwr)
1469 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001470 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001471}
1472
Trond Myklebust226056c2014-02-11 10:41:07 -05001473static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1474 nfs4_stateid *stateid, fmode_t fmode)
1475{
1476 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1477 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1478 case FMODE_WRITE:
1479 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1480 break;
1481 case FMODE_READ:
1482 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1483 break;
1484 case 0:
1485 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1486 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1487 clear_bit(NFS_OPEN_STATE, &state->flags);
1488 }
1489 if (stateid == NULL)
1490 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001491 /* Handle OPEN+OPEN_DOWNGRADE races */
1492 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1493 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001494 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001495 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001496 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001497 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001498 nfs4_stateid_copy(&state->stateid, stateid);
1499 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001500}
1501
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001502static void nfs_clear_open_stateid(struct nfs4_state *state,
1503 nfs4_stateid *arg_stateid,
1504 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001505{
1506 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001507 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1508 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1509 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001510 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001511 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1512 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001513}
1514
Trond Myklebust1393d962016-09-22 13:39:13 -04001515static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1516 const nfs4_stateid *stateid, fmode_t fmode,
1517 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001518{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001519 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001520 case FMODE_READ:
1521 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1522 break;
1523 case FMODE_WRITE:
1524 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1525 break;
1526 case FMODE_READ|FMODE_WRITE:
1527 set_bit(NFS_O_RDWR_STATE, &state->flags);
1528 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001529 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001530 return;
1531 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1532 nfs4_stateid_copy(&state->stateid, stateid);
1533 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001534}
1535
Trond Myklebust1393d962016-09-22 13:39:13 -04001536static void __update_open_stateid(struct nfs4_state *state,
1537 const nfs4_stateid *open_stateid,
1538 const nfs4_stateid *deleg_stateid,
1539 fmode_t fmode,
1540 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001542 /*
1543 * Protect the call to nfs4_state_set_mode_locked and
1544 * serialise the stateid update
1545 */
Andrew Elble361cad32015-12-02 09:20:57 -05001546 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001547 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001548 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001549 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001550 set_bit(NFS_DELEGATED_STATE, &state->flags);
1551 }
1552 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001553 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001554 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001555 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001556 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Trond Myklebust1393d962016-09-22 13:39:13 -04001559static int update_open_stateid(struct nfs4_state *state,
1560 const nfs4_stateid *open_stateid,
1561 const nfs4_stateid *delegation,
1562 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001563{
Trond Myklebust1393d962016-09-22 13:39:13 -04001564 struct nfs_server *server = NFS_SERVER(state->inode);
1565 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001566 struct nfs_inode *nfsi = NFS_I(state->inode);
1567 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001568 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001569 int ret = 0;
1570
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001571 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001572
1573 rcu_read_lock();
1574 deleg_cur = rcu_dereference(nfsi->delegation);
1575 if (deleg_cur == NULL)
1576 goto no_delegation;
1577
1578 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001579 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001580 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001581 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001582 goto no_delegation_unlock;
1583
1584 if (delegation == NULL)
1585 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001586 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001587 goto no_delegation_unlock;
1588
Trond Myklebustb7391f42008-12-23 15:21:52 -05001589 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001590 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1591 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001592 ret = 1;
1593no_delegation_unlock:
1594 spin_unlock(&deleg_cur->lock);
1595no_delegation:
1596 rcu_read_unlock();
1597
1598 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001599 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001600 ret = 1;
1601 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001602 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001603 nfs4_schedule_state_manager(clp);
1604 if (freeme.type != 0)
1605 nfs4_test_and_free_stateid(server, &freeme,
1606 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001607
1608 return ret;
1609}
1610
Trond Myklebust39071e62015-01-24 15:07:56 -05001611static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1612 const nfs4_stateid *stateid)
1613{
1614 struct nfs4_state *state = lsp->ls_state;
1615 bool ret = false;
1616
1617 spin_lock(&state->state_lock);
1618 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1619 goto out_noupdate;
1620 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1621 goto out_noupdate;
1622 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1623 ret = true;
1624out_noupdate:
1625 spin_unlock(&state->state_lock);
1626 return ret;
1627}
Trond Myklebust34310432008-12-23 15:21:38 -05001628
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001630{
1631 struct nfs_delegation *delegation;
1632
1633 rcu_read_lock();
1634 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001635 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636 rcu_read_unlock();
1637 return;
1638 }
1639 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001640 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001641}
1642
Trond Myklebust6ee41262007-07-08 14:11:36 -04001643static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001644{
1645 struct nfs4_state *state = opendata->state;
1646 struct nfs_inode *nfsi = NFS_I(state->inode);
1647 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001648 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001650 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 nfs4_stateid stateid;
1652 int ret = -EAGAIN;
1653
Trond Myklebustaac00a82007-07-05 19:02:21 -04001654 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001655 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001656 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001657 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001658 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001659 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001660 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001661 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001662 rcu_read_lock();
1663 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001664 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001665 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001666 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001667 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001669 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001670 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001671 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001672 if (!opendata->is_recover) {
1673 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1674 if (ret != 0)
1675 goto out;
1676 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001677 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001678
1679 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001680 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001681 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001683out:
1684 return ERR_PTR(ret);
1685out_return_state:
1686 atomic_inc(&state->count);
1687 return state;
1688}
1689
Andy Adamsone23008e2012-10-02 21:07:32 -04001690static void
1691nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1692{
1693 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1694 struct nfs_delegation *delegation;
1695 int delegation_flags = 0;
1696
1697 rcu_read_lock();
1698 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1699 if (delegation)
1700 delegation_flags = delegation->flags;
1701 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001702 switch (data->o_arg.claim) {
1703 default:
1704 break;
1705 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1706 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001707 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1708 "returning a delegation for "
1709 "OPEN(CLAIM_DELEGATE_CUR)\n",
1710 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001711 return;
1712 }
1713 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001714 nfs_inode_set_delegation(state->inode,
1715 data->owner->so_cred,
1716 &data->o_res);
1717 else
1718 nfs_inode_reclaim_delegation(state->inode,
1719 data->owner->so_cred,
1720 &data->o_res);
1721}
1722
1723/*
1724 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1725 * and update the nfs4_state.
1726 */
1727static struct nfs4_state *
1728_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1729{
1730 struct inode *inode = data->state->inode;
1731 struct nfs4_state *state = data->state;
1732 int ret;
1733
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001734 if (!data->rpc_done) {
1735 if (data->rpc_status) {
1736 ret = data->rpc_status;
1737 goto err;
1738 }
1739 /* cached opens have already been processed */
1740 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001741 }
1742
Andy Adamsone23008e2012-10-02 21:07:32 -04001743 ret = nfs_refresh_inode(inode, &data->f_attr);
1744 if (ret)
1745 goto err;
1746
1747 if (data->o_res.delegation_type != 0)
1748 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001749update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001750 update_open_stateid(state, &data->o_res.stateid, NULL,
1751 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001752 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001753
1754 return state;
1755err:
1756 return ERR_PTR(ret);
1757
1758}
1759
1760static struct nfs4_state *
1761_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762{
1763 struct inode *inode;
1764 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001765 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766
Trond Myklebustaac00a82007-07-05 19:02:21 -04001767 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001768 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001769 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001770 goto out;
1771 }
1772
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001773 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001775 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001776 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001777 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001778 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001779 goto err;
1780 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 state = nfs4_get_open_state(inode, data->owner);
1782 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001783 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001784 if (data->o_res.delegation_type != 0)
1785 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001786 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001787 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001789out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001790 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001792err_put_inode:
1793 iput(inode);
1794err:
1795 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001796}
1797
Andy Adamsone23008e2012-10-02 21:07:32 -04001798static struct nfs4_state *
1799nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1800{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001801 struct nfs4_state *ret;
1802
Andy Adamsone23008e2012-10-02 21:07:32 -04001803 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001804 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1805 else
1806 ret = _nfs4_opendata_to_nfs4_state(data);
1807 nfs4_sequence_free_slot(&data->o_res.seq_res);
1808 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001809}
1810
Trond Myklebust864472e2006-01-03 09:55:15 +01001811static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1812{
1813 struct nfs_inode *nfsi = NFS_I(state->inode);
1814 struct nfs_open_context *ctx;
1815
1816 spin_lock(&state->inode->i_lock);
1817 list_for_each_entry(ctx, &nfsi->open_files, list) {
1818 if (ctx->state != state)
1819 continue;
1820 get_nfs_open_context(ctx);
1821 spin_unlock(&state->inode->i_lock);
1822 return ctx;
1823 }
1824 spin_unlock(&state->inode->i_lock);
1825 return ERR_PTR(-ENOENT);
1826}
1827
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001828static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1829 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001830{
1831 struct nfs4_opendata *opendata;
1832
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001833 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001834 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001835 if (opendata == NULL)
1836 return ERR_PTR(-ENOMEM);
1837 opendata->state = state;
1838 atomic_inc(&state->count);
1839 return opendata;
1840}
1841
Trond Myklebust24311f82015-09-20 10:50:17 -04001842static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1843 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001844{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001845 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001846 int ret;
1847
Trond Myklebust24311f82015-09-20 10:50:17 -04001848 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001849 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001850 opendata->o_arg.open_flags = 0;
1851 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001852 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1853 NFS_SB(opendata->dentry->d_sb),
1854 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001855 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1856 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1857 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001858 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 if (ret != 0)
1860 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001861 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001862 if (IS_ERR(newstate))
1863 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001864 if (newstate != opendata->state)
1865 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001866 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001867 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001868}
1869
1870static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1871{
Trond Myklebust864472e2006-01-03 09:55:15 +01001872 int ret;
1873
Trond Myklebust4f14c192014-02-12 19:15:06 -05001874 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1875 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1876 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1877 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001878 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001879 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001880 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001881 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001882 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1883 if (ret != 0)
1884 return ret;
1885 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1886 if (ret != 0)
1887 return ret;
1888 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1889 if (ret != 0)
1890 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001891 /*
1892 * We may have performed cached opens for all three recoveries.
1893 * Check if we need to update the current stateid.
1894 */
1895 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001896 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001897 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001898 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001899 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001900 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001901 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001902 return 0;
1903}
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905/*
1906 * OPEN_RECLAIM:
1907 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001909static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001911 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001912 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001913 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 int status;
1915
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001916 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1917 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001918 if (IS_ERR(opendata))
1919 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001920 rcu_read_lock();
1921 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001922 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001923 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001924 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001925 opendata->o_arg.u.delegation_type = delegation_type;
1926 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001927 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return status;
1929}
1930
Trond Myklebust539cd032007-06-05 11:46:42 -04001931static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 struct nfs_server *server = NFS_SERVER(state->inode);
1934 struct nfs4_exception exception = { };
1935 int err;
1936 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001937 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001938 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001939 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1940 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001941 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001942 break;
1943 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 } while (exception.retry);
1945 return err;
1946}
1947
Trond Myklebust864472e2006-01-03 09:55:15 +01001948static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1949{
1950 struct nfs_open_context *ctx;
1951 int ret;
1952
1953 ctx = nfs4_state_find_open_context(state);
1954 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001955 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001956 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001957 put_nfs_open_context(ctx);
1958 return ret;
1959}
1960
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001961static 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 -07001962{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001963 switch (err) {
1964 default:
1965 printk(KERN_ERR "NFS: %s: unhandled error "
1966 "%d.\n", __func__, err);
1967 case 0:
1968 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001969 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 case -ESTALE:
1971 break;
1972 case -NFS4ERR_BADSESSION:
1973 case -NFS4ERR_BADSLOT:
1974 case -NFS4ERR_BAD_HIGH_SLOT:
1975 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1976 case -NFS4ERR_DEADSESSION:
1977 set_bit(NFS_DELEGATED_STATE, &state->flags);
1978 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1979 return -EAGAIN;
1980 case -NFS4ERR_STALE_CLIENTID:
1981 case -NFS4ERR_STALE_STATEID:
1982 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001983 /* Don't recall a delegation if it was lost */
1984 nfs4_schedule_lease_recovery(server->nfs_client);
1985 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001986 case -NFS4ERR_MOVED:
1987 nfs4_schedule_migration_recovery(server);
1988 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001989 case -NFS4ERR_LEASE_MOVED:
1990 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1991 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001992 case -NFS4ERR_DELEG_REVOKED:
1993 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001994 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001995 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001996 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001997 nfs_inode_find_state_and_recover(state->inode,
1998 stateid);
1999 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002000 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002001 case -NFS4ERR_DELAY:
2002 case -NFS4ERR_GRACE:
2003 set_bit(NFS_DELEGATED_STATE, &state->flags);
2004 ssleep(1);
2005 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002006 case -ENOMEM:
2007 case -NFS4ERR_DENIED:
2008 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2009 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return err;
2012}
2013
Trond Myklebust24311f82015-09-20 10:50:17 -04002014int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2015 struct nfs4_state *state, const nfs4_stateid *stateid,
2016 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002017{
2018 struct nfs_server *server = NFS_SERVER(state->inode);
2019 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002020 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002021
2022 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2023 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2024 if (IS_ERR(opendata))
2025 return PTR_ERR(opendata);
2026 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002027 write_seqlock(&state->seqlock);
2028 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2029 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002030 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2031 switch (type & (FMODE_READ|FMODE_WRITE)) {
2032 case FMODE_READ|FMODE_WRITE:
2033 case FMODE_WRITE:
2034 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2035 if (err)
2036 break;
2037 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2038 if (err)
2039 break;
2040 case FMODE_READ:
2041 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2042 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002043 nfs4_opendata_put(opendata);
2044 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2045}
2046
Chuck Leverbe05c862013-08-09 12:49:47 -04002047static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2048{
2049 struct nfs4_opendata *data = calldata;
2050
Peng Taocb04ad22014-06-11 05:24:15 +08002051 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2052 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002053}
2054
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002055static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2056{
2057 struct nfs4_opendata *data = calldata;
2058
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002059 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002060
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002061 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002062 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002063 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002064 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002065 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002066 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002067 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002068}
2069
2070static void nfs4_open_confirm_release(void *calldata)
2071{
2072 struct nfs4_opendata *data = calldata;
2073 struct nfs4_state *state = NULL;
2074
2075 /* If this request hasn't been cancelled, do nothing */
2076 if (data->cancelled == 0)
2077 goto out_free;
2078 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002079 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002080 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002081 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002082 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002083 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002084out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002085 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002086}
2087
2088static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002089 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002090 .rpc_call_done = nfs4_open_confirm_done,
2091 .rpc_release = nfs4_open_confirm_release,
2092};
2093
2094/*
2095 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2096 */
2097static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2098{
David Howells2b0143b2015-03-17 22:25:59 +00002099 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002100 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002101 struct rpc_message msg = {
2102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2103 .rpc_argp = &data->c_arg,
2104 .rpc_resp = &data->c_res,
2105 .rpc_cred = data->owner->so_cred,
2106 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002107 struct rpc_task_setup task_setup_data = {
2108 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002109 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002110 .callback_ops = &nfs4_open_confirm_ops,
2111 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002112 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002113 .flags = RPC_TASK_ASYNC,
2114 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 int status;
2116
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002117 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002118 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002119 data->rpc_done = 0;
2120 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002121 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002122 if (data->is_recover)
2123 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002124 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002125 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002126 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002127 status = nfs4_wait_for_completion_rpc_task(task);
2128 if (status != 0) {
2129 data->cancelled = 1;
2130 smp_wmb();
2131 } else
2132 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002133 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return status;
2135}
2136
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002137static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002139 struct nfs4_opendata *data = calldata;
2140 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002141 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002142 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002143
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002144 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002145 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146 /*
2147 * Check if we still need to send an OPEN call, or if we can use
2148 * a delegation instead.
2149 */
2150 if (data->state != NULL) {
2151 struct nfs_delegation *delegation;
2152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002153 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002154 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002155 rcu_read_lock();
2156 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002157 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002158 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002159 rcu_read_unlock();
2160 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002161 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002162 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002163 switch (claim) {
2164 default:
2165 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002166 case NFS4_OPEN_CLAIM_PREVIOUS:
2167 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2168 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002169 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002170 case NFS4_OPEN_CLAIM_FH:
2171 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002172 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2173 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002174 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002175 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002176 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002177 &data->o_res.seq_res,
2178 task) != 0)
2179 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002180
2181 /* Set the create mode (note dependency on the session type) */
2182 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2183 if (data->o_arg.open_flags & O_EXCL) {
2184 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2185 if (nfs4_has_persistent_session(clp))
2186 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2187 else if (clp->cl_mvops->minor_version > 0)
2188 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2189 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002190 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002191unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002192 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002193 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002194out_no_action:
2195 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002196out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002197 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002198}
2199
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200static void nfs4_open_done(struct rpc_task *task, void *calldata)
2201{
2202 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002205
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002206 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002207 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002208
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002209 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002210 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2211 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002212 case S_IFREG:
2213 break;
2214 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002215 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002216 break;
2217 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002219 break;
2220 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002222 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002223 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002224 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002225 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2226 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002227 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002228 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002229}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002230
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002231static void nfs4_open_release(void *calldata)
2232{
2233 struct nfs4_opendata *data = calldata;
2234 struct nfs4_state *state = NULL;
2235
2236 /* If this request hasn't been cancelled, do nothing */
2237 if (data->cancelled == 0)
2238 goto out_free;
2239 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002240 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002241 goto out_free;
2242 /* In case we need an open_confirm, no cleanup! */
2243 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2244 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002245 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002246 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002247 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002249 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002250}
2251
2252static const struct rpc_call_ops nfs4_open_ops = {
2253 .rpc_call_prepare = nfs4_open_prepare,
2254 .rpc_call_done = nfs4_open_done,
2255 .rpc_release = nfs4_open_release,
2256};
2257
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002258static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002259{
David Howells2b0143b2015-03-17 22:25:59 +00002260 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002261 struct nfs_server *server = NFS_SERVER(dir);
2262 struct nfs_openargs *o_arg = &data->o_arg;
2263 struct nfs_openres *o_res = &data->o_res;
2264 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002265 struct rpc_message msg = {
2266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2267 .rpc_argp = o_arg,
2268 .rpc_resp = o_res,
2269 .rpc_cred = data->owner->so_cred,
2270 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002271 struct rpc_task_setup task_setup_data = {
2272 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002273 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002274 .callback_ops = &nfs4_open_ops,
2275 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002276 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002277 .flags = RPC_TASK_ASYNC,
2278 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002279 int status;
2280
Chuck Levera9c92d62013-08-09 12:48:18 -04002281 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002282 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002283 data->rpc_done = 0;
2284 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002285 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002286 data->is_recover = 0;
2287 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002288 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002289 data->is_recover = 1;
2290 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002291 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002292 if (IS_ERR(task))
2293 return PTR_ERR(task);
2294 status = nfs4_wait_for_completion_rpc_task(task);
2295 if (status != 0) {
2296 data->cancelled = 1;
2297 smp_wmb();
2298 } else
2299 status = data->rpc_status;
2300 rpc_put_task(task);
2301
2302 return status;
2303}
2304
2305static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2306{
David Howells2b0143b2015-03-17 22:25:59 +00002307 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002308 struct nfs_openres *o_res = &data->o_res;
2309 int status;
2310
2311 status = nfs4_run_open_task(data, 1);
2312 if (status != 0 || !data->rpc_done)
2313 return status;
2314
Trond Myklebust6926afd2012-01-07 13:22:46 -05002315 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2316
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002317 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2318 status = _nfs4_proc_open_confirm(data);
2319 if (status != 0)
2320 return status;
2321 }
2322
2323 return status;
2324}
2325
Trond Myklebustf3792d62014-07-10 08:54:32 -04002326/*
2327 * Additional permission checks in order to distinguish between an
2328 * open for read, and an open for execute. This works around the
2329 * fact that NFSv4 OPEN treats read and execute permissions as being
2330 * the same.
2331 * Note that in the non-execute case, we want to turn off permission
2332 * checking if we just created a new file (POSIX open() semantics).
2333 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002334static int nfs4_opendata_access(struct rpc_cred *cred,
2335 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002336 struct nfs4_state *state, fmode_t fmode,
2337 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002338{
2339 struct nfs_access_entry cache;
2340 u32 mask;
2341
2342 /* access call failed or for some reason the server doesn't
2343 * support any access modes -- defer access call until later */
2344 if (opendata->o_res.access_supported == 0)
2345 return 0;
2346
2347 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002348 /*
2349 * Use openflags to check for exec, because fmode won't
2350 * always have FMODE_EXEC set when file open for exec.
2351 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002352 if (openflags & __FMODE_EXEC) {
2353 /* ONLY check for exec rights */
2354 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002355 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002356 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002357
2358 cache.cred = cred;
2359 cache.jiffies = jiffies;
2360 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2361 nfs_access_add_cache(state->inode, &cache);
2362
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002363 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002364 return 0;
2365
2366 /* even though OPEN succeeded, access is denied. Close the file */
2367 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002368 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002369}
2370
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002371/*
2372 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2373 */
2374static int _nfs4_proc_open(struct nfs4_opendata *data)
2375{
David Howells2b0143b2015-03-17 22:25:59 +00002376 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002377 struct nfs_server *server = NFS_SERVER(dir);
2378 struct nfs_openargs *o_arg = &data->o_arg;
2379 struct nfs_openres *o_res = &data->o_res;
2380 int status;
2381
2382 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002383 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002384 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002385 if (status != 0) {
2386 if (status == -NFS4ERR_BADNAME &&
2387 !(o_arg->open_flags & O_CREAT))
2388 return -ENOENT;
2389 return status;
2390 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002391
Trond Myklebust6926afd2012-01-07 13:22:46 -05002392 nfs_fattr_map_and_free_names(server, &data->f_attr);
2393
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002394 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002395 if (o_arg->open_flags & O_EXCL)
2396 data->file_created = 1;
2397 else if (o_res->cinfo.before != o_res->cinfo.after)
2398 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002399 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002400 update_changeattr(dir, &o_res->cinfo,
2401 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002402 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002403 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2404 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002406 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002408 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002411 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Andy Adamsond83217c2011-03-01 01:34:17 +00002415static int nfs4_recover_expired_lease(struct nfs_server *server)
2416{
2417 return nfs4_client_recover_expired_lease(server->nfs_client);
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420/*
2421 * OPEN_EXPIRED:
2422 * reclaim state on the server after a network partition.
2423 * Assumes caller holds the appropriate lock
2424 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002425static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002427 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002428 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002430 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002431 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002432 if (IS_ERR(opendata))
2433 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002434 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002435 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002436 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002437 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002438 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002441static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002442{
Trond Myklebust539cd032007-06-05 11:46:42 -04002443 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002444 struct nfs4_exception exception = { };
2445 int err;
2446
2447 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002448 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002449 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002450 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2451 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002452 switch (err) {
2453 default:
2454 goto out;
2455 case -NFS4ERR_GRACE:
2456 case -NFS4ERR_DELAY:
2457 nfs4_handle_exception(server, err, &exception);
2458 err = 0;
2459 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002460 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002461out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002462 return err;
2463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2466{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002468 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Trond Myklebust864472e2006-01-03 09:55:15 +01002470 ctx = nfs4_state_find_open_context(state);
2471 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002472 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002473 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002474 put_nfs_open_context(ctx);
2475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Trond Myklebust41020b62016-09-22 13:38:58 -04002478static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2479 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002480{
Trond Myklebust41020b62016-09-22 13:38:58 -04002481 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002482 write_seqlock(&state->seqlock);
2483 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2484 write_sequnlock(&state->seqlock);
2485 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2486}
2487
2488static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2489{
2490 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002491 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002492}
2493
2494static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2495{
2496 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2497 nfs40_clear_delegation_stateid(state);
2498 return nfs4_open_expired(sp, state);
2499}
2500
Trond Myklebust45870d62016-09-22 13:38:59 -04002501static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2502 nfs4_stateid *stateid,
2503 struct rpc_cred *cred)
2504{
2505 return -NFS4ERR_BAD_STATEID;
2506}
2507
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002508#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2510 nfs4_stateid *stateid,
2511 struct rpc_cred *cred)
2512{
2513 int status;
2514
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515 switch (stateid->type) {
2516 default:
2517 break;
2518 case NFS4_INVALID_STATEID_TYPE:
2519 case NFS4_SPECIAL_STATEID_TYPE:
2520 return -NFS4ERR_BAD_STATEID;
2521 case NFS4_REVOKED_STATEID_TYPE:
2522 goto out_free;
2523 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002524
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002525 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002526 switch (status) {
2527 case -NFS4ERR_EXPIRED:
2528 case -NFS4ERR_ADMIN_REVOKED:
2529 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002530 break;
2531 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002532 return status;
2533 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002534out_free:
2535 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002536 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002537 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002538}
2539
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002541{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002542 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002543 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002544 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002545 struct rpc_cred *cred;
2546 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002547
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002548 /* Get the delegation credential for use by test/free_stateid */
2549 rcu_read_lock();
2550 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002551 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002552 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002553 return;
2554 }
2555
2556 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002557 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2558 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002559 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002560 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002561 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002562
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002563 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2564 rcu_read_unlock();
2565 return;
2566 }
2567
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002568 cred = get_rpccred(delegation->cred);
2569 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002570 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002571 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002572 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002573 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002574
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002575 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002576}
2577
2578/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002579 * nfs41_check_expired_locks - possibly free a lock stateid
2580 *
2581 * @state: NFSv4 state for an inode
2582 *
2583 * Returns NFS_OK if recovery for this stateid is now finished.
2584 * Otherwise a negative NFS4ERR value is returned.
2585 */
2586static int nfs41_check_expired_locks(struct nfs4_state *state)
2587{
2588 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002589 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002590 struct nfs_server *server = NFS_SERVER(state->inode);
2591
2592 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2593 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002594
2595 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002596 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2597 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2598 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2599
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002600 atomic_inc(&lsp->ls_count);
2601 spin_unlock(&state->state_lock);
2602
2603 nfs4_put_lock_state(prev);
2604 prev = lsp;
2605
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002606 status = nfs41_test_and_free_expired_stateid(server,
2607 &lsp->ls_stateid,
2608 cred);
2609 trace_nfs4_test_lock_stateid(state, lsp, status);
2610 if (status == -NFS4ERR_EXPIRED ||
2611 status == -NFS4ERR_BAD_STATEID) {
2612 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002613 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002614 if (!recover_lost_locks)
2615 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2616 } else if (status != NFS_OK) {
2617 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002618 nfs4_put_lock_state(prev);
2619 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002620 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002621 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002622 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002623 }
2624 spin_unlock(&state->state_lock);
2625 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002626out:
2627 return ret;
2628}
2629
2630/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002631 * nfs41_check_open_stateid - possibly free an open stateid
2632 *
2633 * @state: NFSv4 state for an inode
2634 *
2635 * Returns NFS_OK if recovery for this stateid is now finished.
2636 * Otherwise a negative NFS4ERR value is returned.
2637 */
2638static int nfs41_check_open_stateid(struct nfs4_state *state)
2639{
2640 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002641 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002642 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002643 int status;
2644
Trond Myklebustb134fc42016-09-22 13:39:16 -04002645 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002646 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2647 if (nfs4_have_delegation(state->inode, state->state))
2648 return NFS_OK;
2649 return -NFS4ERR_OPENMODE;
2650 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002651 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002652 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002653 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002654 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002655 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002656 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2657 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2658 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002659 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002660 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002661 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002662 if (status != NFS_OK)
2663 return status;
2664 if (nfs_open_stateid_recover_openmode(state))
2665 return -NFS4ERR_OPENMODE;
2666 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002667}
2668
2669static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2670{
Chuck Levereb64cf92012-07-11 16:30:05 -04002671 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002672
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002673 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 status = nfs41_check_expired_locks(state);
2675 if (status != NFS_OK)
2676 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002677 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002678 if (status != NFS_OK)
2679 status = nfs4_open_expired(sp, state);
2680 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002681}
2682#endif
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002685 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2686 * fields corresponding to attributes that were used to store the verifier.
2687 * Make sure we clobber those fields in the later setattr call
2688 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002689static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2690 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002691{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002692 const u32 *attrset = opendata->o_res.attrset;
2693
2694 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002695 !(sattr->ia_valid & ATTR_ATIME_SET))
2696 sattr->ia_valid |= ATTR_ATIME;
2697
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002698 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002699 !(sattr->ia_valid & ATTR_MTIME_SET))
2700 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002701
2702 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002703 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2704 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002705 sattr->ia_valid &= ~ATTR_MODE;
2706
2707 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2708 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002709}
2710
Trond Myklebustc21443c2013-02-07 14:26:21 -05002711static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2712 fmode_t fmode,
2713 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002714 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715{
2716 struct nfs4_state_owner *sp = opendata->owner;
2717 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002718 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002719 struct nfs4_state *state;
2720 unsigned int seq;
2721 int ret;
2722
2723 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2724
2725 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002726 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002727 goto out;
2728
2729 state = nfs4_opendata_to_nfs4_state(opendata);
2730 ret = PTR_ERR(state);
2731 if (IS_ERR(state))
2732 goto out;
2733 if (server->caps & NFS_CAP_POSIX_LOCK)
2734 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002735 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2736 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002737
Trond Myklebust275bb302013-05-29 13:11:28 -04002738 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002739 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002740 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002741 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002742 alias = d_exact_alias(dentry, state->inode);
2743 if (!alias)
2744 alias = d_splice_alias(igrab(state->inode), dentry);
2745 /* d_splice_alias() can't fail here - it's a non-directory */
2746 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002747 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002748 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002749 }
2750 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002751 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002752 }
2753
Trond Myklebustc21443c2013-02-07 14:26:21 -05002754 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2755 if (ret != 0)
2756 goto out;
2757
Trond Myklebust3efb9722013-05-29 13:17:04 -04002758 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002759 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002760 nfs_inode_attach_open_context(ctx);
2761 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2762 nfs4_schedule_stateid_recovery(server, state);
2763 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002764out:
2765 return ret;
2766}
2767
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002768/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002769 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 */
Andy Adamson82be4172012-05-23 05:02:35 -04002771static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002772 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002773 int flags,
2774 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002775 struct nfs4_label *label,
2776 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 struct nfs4_state_owner *sp;
2779 struct nfs4_state *state = NULL;
2780 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002781 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002782 struct dentry *dentry = ctx->dentry;
2783 struct rpc_cred *cred = ctx->cred;
2784 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2785 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002786 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002787 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002788 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002792 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2793 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2795 goto out_err;
2796 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002797 status = nfs4_recover_expired_lease(server);
2798 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002799 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002800 if (d_really_is_positive(dentry))
2801 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002802 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002803 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002804 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002805 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002806 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002807 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002808 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
David Quigley14c43f72013-05-22 12:50:43 -04002810 if (label) {
2811 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2812 if (IS_ERR(olabel)) {
2813 status = PTR_ERR(olabel);
2814 goto err_opendata_put;
2815 }
2816 }
2817
Trond Myklebuste911b812014-03-26 13:24:37 -07002818 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2819 if (!opendata->f_attr.mdsthreshold) {
2820 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2821 if (!opendata->f_attr.mdsthreshold)
2822 goto err_free_label;
2823 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002824 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002825 }
David Howells2b0143b2015-03-17 22:25:59 +00002826 if (d_really_is_positive(dentry))
2827 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002828
Trond Myklebust3efb9722013-05-29 13:17:04 -04002829 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002830 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002831 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002832 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002833
NeilBrownefcbc042015-07-30 13:00:56 +10002834 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002835 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002836 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002837 /*
2838 * send create attributes which was not set by open
2839 * with an extra setattr.
2840 */
2841 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2842 nfs_fattr_init(opendata->o_res.f_attr);
2843 status = nfs4_do_setattr(state->inode, cred,
2844 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002845 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002846 if (status == 0) {
2847 nfs_setattr_update_inode(state->inode, sattr,
2848 opendata->o_res.f_attr);
2849 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2850 }
David Quigley1775fd32013-05-22 12:50:42 -04002851 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002852 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002853 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002854 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002855
Trond Myklebuste911b812014-03-26 13:24:37 -07002856 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002857 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002858 opendata->f_attr.mdsthreshold = NULL;
2859 }
Andy Adamson82be4172012-05-23 05:02:35 -04002860
David Quigley14c43f72013-05-22 12:50:43 -04002861 nfs4_label_free(olabel);
2862
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002863 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002866err_free_label:
2867 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002868err_opendata_put:
2869 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002870err_put_state_owner:
2871 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return status;
2874}
2875
2876
Andy Adamson82be4172012-05-23 05:02:35 -04002877static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002878 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002879 int flags,
2880 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002881 struct nfs4_label *label,
2882 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002884 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 struct nfs4_exception exception = { };
2886 struct nfs4_state *res;
2887 int status;
2888
2889 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002890 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002891 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002892 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (status == 0)
2894 break;
2895 /* NOTE: BAD_SEQID means the server and client disagree about the
2896 * book-keeping w.r.t. state-changing operations
2897 * (OPEN/CLOSE/LOCK/LOCKU...)
2898 * It is actually a sign of a bug on the client or on the server.
2899 *
2900 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002901 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 * have unhashed the old state_owner for us, and that we can
2903 * therefore safely retry using a new one. We should still warn
2904 * the user though...
2905 */
2906 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002907 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002908 " returned a bad sequence-id error!\n",
2909 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 exception.retry = 1;
2911 continue;
2912 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002913 /*
2914 * BAD_STATEID on OPEN means that the server cancelled our
2915 * state before it received the OPEN_CONFIRM.
2916 * Recover by retrying the request as per the discussion
2917 * on Page 181 of RFC3530.
2918 */
2919 if (status == -NFS4ERR_BAD_STATEID) {
2920 exception.retry = 1;
2921 continue;
2922 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002923 if (status == -EAGAIN) {
2924 /* We must have found a delegation */
2925 exception.retry = 1;
2926 continue;
2927 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002928 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2929 continue;
2930 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 status, &exception));
2932 } while (exception.retry);
2933 return res;
2934}
2935
Trond Myklebust8487c472016-06-26 08:44:35 -04002936static int _nfs4_do_setattr(struct inode *inode,
2937 struct nfs_setattrargs *arg,
2938 struct nfs_setattrres *res,
2939 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002940 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002941{
2942 struct nfs_server *server = NFS_SERVER(inode);
2943 struct rpc_message msg = {
2944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2945 .rpc_argp = arg,
2946 .rpc_resp = res,
2947 .rpc_cred = cred,
2948 };
2949 struct rpc_cred *delegation_cred = NULL;
2950 unsigned long timestamp = jiffies;
2951 fmode_t fmode;
2952 bool truncate;
2953 int status;
2954
2955 nfs_fattr_init(res->fattr);
2956
2957 /* Servers should only apply open mode checks for file size changes */
2958 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2959 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2960
2961 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2962 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002963 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002964 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002965 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002966 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002967 l_ctx = nfs_get_lock_context(ctx);
2968 if (IS_ERR(l_ctx))
2969 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002970 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2971 &arg->stateid, &delegation_cred);
2972 nfs_put_lock_context(l_ctx);
2973 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002974 return -EBADF;
2975 } else
2976 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2977 if (delegation_cred)
2978 msg.rpc_cred = delegation_cred;
2979
2980 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2981
2982 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002983 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002984 renew_lease(server, timestamp);
2985 trace_nfs4_setattr(inode, &arg->stateid, status);
2986 return status;
2987}
2988
2989static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2990 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002991 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002992 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002994 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002995 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002997 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 .iap = sattr,
2999 .server = server,
3000 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003001 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
3003 struct nfs_setattrres res = {
3004 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003005 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 .server = server,
3007 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003008 struct nfs4_exception exception = {
3009 .state = state,
3010 .inode = inode,
3011 .stateid = &arg.stateid,
3012 };
3013 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
David Quigleyaa9c2662013-05-22 12:50:44 -04003015 arg.bitmask = nfs4_bitmask(server, ilabel);
3016 if (ilabel)
3017 arg.bitmask = nfs4_bitmask(server, olabel);
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003020 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003021 switch (err) {
3022 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003023 if (!(sattr->ia_valid & ATTR_SIZE)) {
3024 pr_warn_once("NFSv4: server %s is incorrectly "
3025 "applying open mode checks to "
3026 "a SETATTR that is not "
3027 "changing file size.\n",
3028 server->nfs_client->cl_hostname);
3029 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003030 if (state && !(state->state & FMODE_WRITE)) {
3031 err = -EBADF;
3032 if (sattr->ia_valid & ATTR_OPEN)
3033 err = -EACCES;
3034 goto out;
3035 }
3036 }
3037 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003039out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return err;
3041}
3042
Peng Tao500d7012015-09-22 11:35:22 +08003043static bool
3044nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3045{
3046 if (inode == NULL || !nfs_have_layout(inode))
3047 return false;
3048
3049 return pnfs_wait_on_layoutreturn(inode, task);
3050}
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052struct nfs4_closedata {
3053 struct inode *inode;
3054 struct nfs4_state *state;
3055 struct nfs_closeargs arg;
3056 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003057 struct {
3058 struct nfs4_layoutreturn_args arg;
3059 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003060 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003061 u32 roc_barrier;
3062 bool roc;
3063 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003064 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003065 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066};
3067
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003068static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003069{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003070 struct nfs4_closedata *calldata = data;
3071 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003072 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003073
Trond Myklebustcf805162016-11-15 14:56:07 -05003074 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003075 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3076 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003077 nfs4_put_open_state(calldata->state);
3078 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003079 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003080 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003081 kfree(calldata);
3082}
3083
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003084static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003086 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003089 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Chuck Levera3ca5652012-03-01 17:00:40 -05003091 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003092 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3093 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003094 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003095
3096 /* Handle Layoutreturn errors */
3097 if (calldata->arg.lr_args && task->tk_status != 0) {
3098 switch (calldata->res.lr_ret) {
3099 default:
3100 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3101 break;
3102 case 0:
3103 calldata->arg.lr_args = NULL;
3104 calldata->res.lr_res = NULL;
3105 break;
3106 case -NFS4ERR_ADMIN_REVOKED:
3107 case -NFS4ERR_DELEG_REVOKED:
3108 case -NFS4ERR_EXPIRED:
3109 case -NFS4ERR_BAD_STATEID:
3110 case -NFS4ERR_OLD_STATEID:
3111 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3112 case -NFS4ERR_WRONG_CRED:
3113 calldata->arg.lr_args = NULL;
3114 calldata->res.lr_res = NULL;
3115 calldata->res.lr_ret = 0;
3116 rpc_restart_call_prepare(task);
3117 return;
3118 }
3119 }
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 /* hmm. we are done with the inode, and in the process of freeing
3122 * the state_owner. we keep this around to process errors
3123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 switch (task->tk_status) {
3125 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003126 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003127 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003128 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003129 case -NFS4ERR_ACCESS:
3130 if (calldata->arg.bitmask != NULL) {
3131 calldata->arg.bitmask = NULL;
3132 calldata->res.fattr = NULL;
3133 task->tk_status = 0;
3134 rpc_restart_call_prepare(task);
3135 goto out_release;
3136
3137 }
3138 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003139 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003141 case -NFS4ERR_EXPIRED:
3142 nfs4_free_revoked_stateid(server,
3143 &calldata->arg.stateid,
3144 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003145 case -NFS4ERR_OLD_STATEID:
3146 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003147 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003148 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003149 rpc_restart_call_prepare(task);
3150 goto out_release;
3151 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003152 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003155 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003156 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003157 goto out_release;
3158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003160 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3161 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003162out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003163 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003164 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003165 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003168static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003170 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003171 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003172 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003173 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003174 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003175
Chuck Levera3ca5652012-03-01 17:00:40 -05003176 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003178 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003179
Trond Myklebust88069f72009-12-08 08:33:16 -05003180 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003181 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003182 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3183 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3184 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003185 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003186 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003187 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003188 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003189 if (state->n_rdonly == 0)
3190 call_close |= is_rdonly;
3191 else if (is_rdonly)
3192 calldata->arg.fmode |= FMODE_READ;
3193 if (state->n_wronly == 0)
3194 call_close |= is_wronly;
3195 else if (is_wronly)
3196 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003197 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3198 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003199 } else if (is_rdwr)
3200 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3201
Trond Myklebust5cc78612016-11-14 11:19:56 -05003202 if (!nfs4_valid_open_stateid(state) ||
3203 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003204 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003205 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003206
3207 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003208 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003209 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003210 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003211
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003212 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003213 nfs_release_seqid(calldata->arg.seqid);
3214 goto out_wait;
3215 }
3216
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003217 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003218 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003219
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003220 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003221 /* Close-to-open cache consistency revalidation */
3222 if (!nfs4_have_delegation(inode, FMODE_READ))
3223 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3224 else
3225 calldata->arg.bitmask = NULL;
3226 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003227
Trond Myklebust6ae37332015-01-30 14:21:14 -05003228 calldata->arg.share_access =
3229 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3230 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003231
Trond Myklebustd8d84982016-12-19 12:14:44 -05003232 if (calldata->res.fattr == NULL)
3233 calldata->arg.bitmask = NULL;
3234 else if (calldata->arg.bitmask == NULL)
3235 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003236 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003237 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003238 &calldata->arg.seq_args,
3239 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003240 task) != 0)
3241 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003242 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003243 return;
3244out_no_action:
3245 task->tk_action = NULL;
3246out_wait:
3247 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003250static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003251 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003252 .rpc_call_done = nfs4_close_done,
3253 .rpc_release = nfs4_free_closedata,
3254};
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256/*
3257 * It is possible for data to be read/written from a mem-mapped file
3258 * after the sys_close call (which hits the vfs layer as a flush).
3259 * This means that we can't safely call nfsv4 close on a file until
3260 * the inode is cleared. This in turn means that we are not good
3261 * NFSv4 citizens - we do not indicate to the server to update the file's
3262 * share state even when we are done with one of the three share
3263 * stateid's in the inode.
3264 *
3265 * NOTE: Caller must be holding the sp->so_owner semaphore!
3266 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003267int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003269 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003270 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003272 struct nfs4_state_owner *sp = state->owner;
3273 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003274 struct rpc_message msg = {
3275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3276 .rpc_cred = state->owner->so_cred,
3277 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003278 struct rpc_task_setup task_setup_data = {
3279 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003280 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003281 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003282 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003283 .flags = RPC_TASK_ASYNC,
3284 };
Trond Myklebust95121352005-10-18 14:20:12 -07003285 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003287 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3288 &task_setup_data.rpc_client, &msg);
3289
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003290 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003292 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003293 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003294 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003296 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003298 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3299 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003300 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003301 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003302 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003303 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003304 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003305 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003306 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003307 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003308 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003309 calldata->lr.roc = pnfs_roc(state->inode,
3310 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3311 if (calldata->lr.roc) {
3312 calldata->arg.lr_args = &calldata->lr.arg;
3313 calldata->res.lr_res = &calldata->lr.res;
3314 }
Al Viro643168c2011-06-22 18:20:23 -04003315 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003316
Trond Myklebust1174dd12010-12-21 10:52:24 -05003317 msg.rpc_argp = &calldata->arg;
3318 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003319 task_setup_data.callback_data = calldata;
3320 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003321 if (IS_ERR(task))
3322 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003323 status = 0;
3324 if (wait)
3325 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003326 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003327 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003328out_free_calldata:
3329 kfree(calldata);
3330out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003331 nfs4_put_open_state(state);
3332 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003333 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Trond Myklebust2b484292010-09-17 10:56:51 -04003336static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003337nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3338 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003341 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3342
3343 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Trond Myklebust565277f2007-10-15 18:17:53 -04003345 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003346 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003347
3348 nfs4_label_release_security(label);
3349
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003350 if (IS_ERR(state))
3351 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003352 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Trond Myklebust1185a552009-12-03 15:54:02 -05003355static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003356{
3357 if (ctx->state == NULL)
3358 return;
3359 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003360 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003361 else
Al Viro643168c2011-06-22 18:20:23 -04003362 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Trond Myklebustb944dba2013-11-04 15:20:20 -05003365#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3366#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003367#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3370{
Kinglong Mee8c612822015-08-26 21:12:58 +08003371 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003372 struct nfs4_server_caps_arg args = {
3373 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003374 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003375 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 struct nfs4_server_caps_res res = {};
3377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003379 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 .rpc_resp = &res,
3381 };
3382 int status;
3383
Kinglong Mee8c612822015-08-26 21:12:58 +08003384 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3385 FATTR4_WORD0_FH_EXPIRE_TYPE |
3386 FATTR4_WORD0_LINK_SUPPORT |
3387 FATTR4_WORD0_SYMLINK_SUPPORT |
3388 FATTR4_WORD0_ACLSUPPORT;
3389 if (minorversion)
3390 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3391
Bryan Schumaker7c513052011-03-24 17:12:24 +00003392 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003394 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003395 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003396 case 0:
3397 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3398 res.attr_bitmask[2] = 0;
3399 break;
3400 case 1:
3401 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3402 break;
3403 case 2:
3404 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003407 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3408 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3409 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3410 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003411 NFS_CAP_CTIME|NFS_CAP_MTIME|
3412 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003413 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3414 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 server->caps |= NFS_CAP_ACLS;
3416 if (res.has_links != 0)
3417 server->caps |= NFS_CAP_HARDLINKS;
3418 if (res.has_symlinks != 0)
3419 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003420 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3421 server->caps |= NFS_CAP_FILEID;
3422 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3423 server->caps |= NFS_CAP_MODE;
3424 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3425 server->caps |= NFS_CAP_NLINK;
3426 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3427 server->caps |= NFS_CAP_OWNER;
3428 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3429 server->caps |= NFS_CAP_OWNER_GROUP;
3430 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3431 server->caps |= NFS_CAP_ATIME;
3432 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3433 server->caps |= NFS_CAP_CTIME;
3434 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3435 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003436#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3437 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3438 server->caps |= NFS_CAP_SECURITY_LABEL;
3439#endif
3440 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3441 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003442 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003443
Trond Myklebusta65318b2009-03-11 14:10:28 -04003444 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3445 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3446 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003447 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003448 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3449 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003451 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return status;
3455}
3456
Trond Myklebust55a97592006-06-09 09:34:19 -04003457int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 struct nfs4_exception exception = { };
3460 int err;
3461 do {
3462 err = nfs4_handle_exception(server,
3463 _nfs4_server_capabilities(server, fhandle),
3464 &exception);
3465 } while (exception.retry);
3466 return err;
3467}
3468
3469static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3470 struct nfs_fsinfo *info)
3471{
David Quigleyaa9c2662013-05-22 12:50:44 -04003472 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003474 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 };
3476 struct nfs4_lookup_res res = {
3477 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003478 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 .fh = fhandle,
3480 };
3481 struct rpc_message msg = {
3482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3483 .rpc_argp = &args,
3484 .rpc_resp = &res,
3485 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003486
David Quigleyaa9c2662013-05-22 12:50:44 -04003487 bitmask[0] = nfs4_fattr_bitmap[0];
3488 bitmask[1] = nfs4_fattr_bitmap[1];
3489 /*
3490 * Process the label in the upcoming getfattr
3491 */
3492 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3493
Trond Myklebust0e574af2005-10-27 22:12:38 -04003494 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003495 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
3498static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3499 struct nfs_fsinfo *info)
3500{
3501 struct nfs4_exception exception = { };
3502 int err;
3503 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003504 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003505 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003506 switch (err) {
3507 case 0:
3508 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003509 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003510 default:
3511 err = nfs4_handle_exception(server, err, &exception);
3512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return err;
3516}
3517
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003518static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3519 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3520{
Trond Myklebustc2190662013-08-26 19:23:04 -04003521 struct rpc_auth_create_args auth_args = {
3522 .pseudoflavor = flavor,
3523 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003524 struct rpc_auth *auth;
3525 int ret;
3526
Trond Myklebustc2190662013-08-26 19:23:04 -04003527 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003528 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003529 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003530 goto out;
3531 }
3532 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003533out:
3534 return ret;
3535}
3536
Chuck Lever9a744ba2013-03-16 15:56:02 -04003537/*
3538 * Retry pseudoroot lookup with various security flavors. We do this when:
3539 *
3540 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3541 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3542 *
3543 * Returns zero on success, or a negative NFS4ERR value, or a
3544 * negative errno value.
3545 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003546static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003547 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003549 /* Per 3530bis 15.33.5 */
3550 static const rpc_authflavor_t flav_array[] = {
3551 RPC_AUTH_GSS_KRB5P,
3552 RPC_AUTH_GSS_KRB5I,
3553 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003554 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003555 RPC_AUTH_NULL,
3556 };
3557 int status = -EPERM;
3558 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003560 if (server->auth_info.flavor_len > 0) {
3561 /* try each flavor specified by user */
3562 for (i = 0; i < server->auth_info.flavor_len; i++) {
3563 status = nfs4_lookup_root_sec(server, fhandle, info,
3564 server->auth_info.flavors[i]);
3565 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3566 continue;
3567 break;
3568 }
3569 } else {
3570 /* no flavors specified by user, try default list */
3571 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3572 status = nfs4_lookup_root_sec(server, fhandle, info,
3573 flav_array[i]);
3574 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3575 continue;
3576 break;
3577 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003578 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003579
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003580 /*
3581 * -EACCESS could mean that the user doesn't have correct permissions
3582 * to access the mount. It could also mean that we tried to mount
3583 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3584 * existing mount programs don't handle -EACCES very well so it should
3585 * be mapped to -EPERM instead.
3586 */
3587 if (status == -EACCES)
3588 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003589 return status;
3590}
3591
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003592/**
3593 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3594 * @server: initialized nfs_server handle
3595 * @fhandle: we fill in the pseudo-fs root file handle
3596 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003597 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003598 *
3599 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003600 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003601int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003602 struct nfs_fsinfo *info,
3603 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003604{
Andre Przywarac7757072015-04-23 17:17:40 +01003605 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003606
Andre Przywarac7757072015-04-23 17:17:40 +01003607 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003608 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003609
3610 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003611 status = server->nfs_client->cl_mvops->find_root_sec(server,
3612 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (status == 0)
3615 status = nfs4_server_capabilities(server, fhandle);
3616 if (status == 0)
3617 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003618
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003619 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Bryan Schumakerbae36242012-05-10 15:07:31 -04003622static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3623 struct nfs_fsinfo *info)
3624{
3625 int error;
3626 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003627 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003628
3629 error = nfs4_server_capabilities(server, mntfh);
3630 if (error < 0) {
3631 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3632 return error;
3633 }
3634
David Quigley14c43f72013-05-22 12:50:43 -04003635 label = nfs4_label_alloc(server, GFP_KERNEL);
3636 if (IS_ERR(label))
3637 return PTR_ERR(label);
3638
David Quigley1775fd32013-05-22 12:50:42 -04003639 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003640 if (error < 0) {
3641 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003642 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003643 }
3644
3645 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3646 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3647 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3648
David Quigley14c43f72013-05-22 12:50:43 -04003649err_free_label:
3650 nfs4_label_free(label);
3651
Bryan Schumakerbae36242012-05-10 15:07:31 -04003652 return error;
3653}
3654
Manoj Naik6b97fd32006-06-09 09:34:29 -04003655/*
3656 * Get locations and (maybe) other attributes of a referral.
3657 * Note that we'll actually follow the referral later when
3658 * we detect fsid mismatch in inode revalidation
3659 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003660static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3661 const struct qstr *name, struct nfs_fattr *fattr,
3662 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003663{
3664 int status = -ENOMEM;
3665 struct page *page = NULL;
3666 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003667
3668 page = alloc_page(GFP_KERNEL);
3669 if (page == NULL)
3670 goto out;
3671 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3672 if (locations == NULL)
3673 goto out;
3674
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003675 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003676 if (status != 0)
3677 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003678
3679 /*
3680 * If the fsid didn't change, this is a migration event, not a
3681 * referral. Cause us to drop into the exception handler, which
3682 * will kick off migration recovery.
3683 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003684 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003685 dprintk("%s: server did not return a different fsid for"
3686 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003687 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003688 goto out;
3689 }
Andy Adamson533eb462011-06-13 18:25:56 -04003690 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3691 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003692
Andy Adamson533eb462011-06-13 18:25:56 -04003693 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003694 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003695 memset(fhandle, 0, sizeof(struct nfs_fh));
3696out:
3697 if (page)
3698 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003699 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003700 return status;
3701}
3702
David Quigley1775fd32013-05-22 12:50:42 -04003703static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3704 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
3706 struct nfs4_getattr_arg args = {
3707 .fh = fhandle,
3708 .bitmask = server->attr_bitmask,
3709 };
3710 struct nfs4_getattr_res res = {
3711 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003712 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 .server = server,
3714 };
3715 struct rpc_message msg = {
3716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3717 .rpc_argp = &args,
3718 .rpc_resp = &res,
3719 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003720
3721 args.bitmask = nfs4_bitmask(server, label);
3722
Trond Myklebust0e574af2005-10-27 22:12:38 -04003723 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
David Quigley1775fd32013-05-22 12:50:42 -04003727static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3728 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
3730 struct nfs4_exception exception = { };
3731 int err;
3732 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003733 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3734 trace_nfs4_getattr(server, fhandle, fattr, err);
3735 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 &exception);
3737 } while (exception.retry);
3738 return err;
3739}
3740
3741/*
3742 * The file is not closed if it is opened due to the a request to change
3743 * the size of the file. The open call will not be needed once the
3744 * VFS layer lookup-intents are implemented.
3745 *
3746 * Close is called when the inode is destroyed.
3747 * If we haven't opened the file for O_WRONLY, we
3748 * need to in the size_change case to obtain a stateid.
3749 *
3750 * Got race?
3751 * Because OPEN is always done by name in nfsv4, it is
3752 * possible that we opened a different file by the same
3753 * name. We can recognize this race condition, but we
3754 * can't do anything about it besides returning an error.
3755 *
3756 * This will be fixed with VFS changes (lookup-intent).
3757 */
3758static int
3759nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3760 struct iattr *sattr)
3761{
David Howells2b0143b2015-03-17 22:25:59 +00003762 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003763 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003764 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003765 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 int status;
3767
Peng Tao88ac8152014-09-12 11:04:10 +08003768 if (pnfs_ld_layoutret_on_setattr(inode) &&
3769 sattr->ia_valid & ATTR_SIZE &&
3770 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003771 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003772
Trond Myklebust0e574af2005-10-27 22:12:38 -04003773 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Andy Adamson26699402012-05-30 16:12:24 -04003775 /* Deal with open(O_TRUNC) */
3776 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003777 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003778
3779 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003780 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003781 return 0;
3782
Trond Myklebustd5308382005-11-04 15:33:38 -05003783 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003784 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003785
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003786 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003787 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003788 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003789 }
3790
David Quigley14c43f72013-05-22 12:50:43 -04003791 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3792 if (IS_ERR(label))
3793 return PTR_ERR(label);
3794
NeilBrown29b59f92016-10-13 15:26:47 +11003795 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003796 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003797 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003798 nfs_setsecurity(inode, fattr, label);
3799 }
David Quigley14c43f72013-05-22 12:50:43 -04003800 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return status;
3802}
3803
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003804static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3805 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003806 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003807{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003808 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003809 int status;
3810 struct nfs4_lookup_arg args = {
3811 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003812 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003813 .name = name,
3814 };
3815 struct nfs4_lookup_res res = {
3816 .server = server,
3817 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003818 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003819 .fh = fhandle,
3820 };
3821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3823 .rpc_argp = &args,
3824 .rpc_resp = &res,
3825 };
3826
David Quigleyaa9c2662013-05-22 12:50:44 -04003827 args.bitmask = nfs4_bitmask(server, label);
3828
David Howells2b3de442006-08-22 20:06:09 -04003829 nfs_fattr_init(fattr);
3830
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003832 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 dprintk("NFS reply lookup: %d\n", status);
3834 return status;
3835}
3836
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003837static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003838{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003839 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003840 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003841 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3842 fattr->nlink = 2;
3843}
3844
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003845static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003846 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003847 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003848{
3849 struct nfs4_exception exception = { };
3850 struct rpc_clnt *client = *clnt;
3851 int err;
3852 do {
David Quigley1775fd32013-05-22 12:50:42 -04003853 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003854 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003855 switch (err) {
3856 case -NFS4ERR_BADNAME:
3857 err = -ENOENT;
3858 goto out;
3859 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003860 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003861 if (err == -NFS4ERR_MOVED)
3862 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003863 goto out;
3864 case -NFS4ERR_WRONGSEC:
3865 err = -EPERM;
3866 if (client != *clnt)
3867 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003868 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003869 if (IS_ERR(client))
3870 return PTR_ERR(client);
3871
3872 exception.retry = 1;
3873 break;
3874 default:
3875 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3876 }
3877 } while (exception.retry);
3878
3879out:
3880 if (err == 0)
3881 *clnt = client;
3882 else if (client != *clnt)
3883 rpc_shutdown_client(client);
3884
3885 return err;
3886}
3887
Al Virobeffb8f2016-07-20 16:34:42 -04003888static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003889 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3890 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003892 int status;
3893 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003894
David Quigley1775fd32013-05-22 12:50:42 -04003895 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003896 if (client != NFS_CLIENT(dir)) {
3897 rpc_shutdown_client(client);
3898 nfs_fixup_secinfo_attributes(fattr);
3899 }
3900 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901}
3902
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003903struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003904nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003905 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3906{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003907 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003908 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003909
David Quigley1775fd32013-05-22 12:50:42 -04003910 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003911 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003912 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003913 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003914}
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3917{
Trond Myklebust76b32992007-08-10 17:45:11 -04003918 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct nfs4_accessargs args = {
3920 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003921 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003923 struct nfs4_accessres res = {
3924 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003925 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3928 .rpc_argp = &args,
3929 .rpc_resp = &res,
3930 .rpc_cred = entry->cred,
3931 };
3932 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003933 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 /*
3936 * Determine which access bits we want to ask for...
3937 */
3938 if (mode & MAY_READ)
3939 args.access |= NFS4_ACCESS_READ;
3940 if (S_ISDIR(inode->i_mode)) {
3941 if (mode & MAY_WRITE)
3942 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3943 if (mode & MAY_EXEC)
3944 args.access |= NFS4_ACCESS_LOOKUP;
3945 } else {
3946 if (mode & MAY_WRITE)
3947 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3948 if (mode & MAY_EXEC)
3949 args.access |= NFS4_ACCESS_EXECUTE;
3950 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003951
3952 res.fattr = nfs_alloc_fattr();
3953 if (res.fattr == NULL)
3954 return -ENOMEM;
3955
Bryan Schumaker7c513052011-03-24 17:12:24 +00003956 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003958 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003959 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003961 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 return status;
3963}
3964
3965static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3966{
3967 struct nfs4_exception exception = { };
3968 int err;
3969 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003970 err = _nfs4_proc_access(inode, entry);
3971 trace_nfs4_access(inode, err);
3972 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 &exception);
3974 } while (exception.retry);
3975 return err;
3976}
3977
3978/*
3979 * TODO: For the time being, we don't try to get any attributes
3980 * along with any of the zero-copy operations READ, READDIR,
3981 * READLINK, WRITE.
3982 *
3983 * In the case of the first three, we want to put the GETATTR
3984 * after the read-type operation -- this is because it is hard
3985 * to predict the length of a GETATTR response in v4, and thus
3986 * align the READ data correctly. This means that the GETATTR
3987 * may end up partially falling into the page cache, and we should
3988 * shift it into the 'tail' of the xdr_buf before processing.
3989 * To do this efficiently, we need to know the total length
3990 * of data received, which doesn't seem to be available outside
3991 * of the RPC layer.
3992 *
3993 * In the case of WRITE, we also want to put the GETATTR after
3994 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003995 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 *
3997 * Both of these changes to the XDR layer would in fact be quite
3998 * minor, but I decided to leave them for a subsequent patch.
3999 */
4000static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4001 unsigned int pgbase, unsigned int pglen)
4002{
4003 struct nfs4_readlink args = {
4004 .fh = NFS_FH(inode),
4005 .pgbase = pgbase,
4006 .pglen = pglen,
4007 .pages = &page,
4008 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004009 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 struct rpc_message msg = {
4011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4012 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004013 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 };
4015
Bryan Schumaker7c513052011-03-24 17:12:24 +00004016 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 -07004017}
4018
4019static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4020 unsigned int pgbase, unsigned int pglen)
4021{
4022 struct nfs4_exception exception = { };
4023 int err;
4024 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004025 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4026 trace_nfs4_readlink(inode, err);
4027 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 &exception);
4029 } while (exception.retry);
4030 return err;
4031}
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004034 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036static int
4037nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004038 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004040 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004041 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004042 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 int status = 0;
4045
NeilBrown532d4de2016-10-13 15:26:47 +11004046 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004047 if (IS_ERR(ctx))
4048 return PTR_ERR(ctx);
4049
David Quigleyaa9c2662013-05-22 12:50:44 -04004050 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4051
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004052 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4053 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004054 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 if (IS_ERR(state)) {
4056 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004060 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004061 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return status;
4063}
4064
Al Virobeffb8f2016-07-20 16:34:42 -04004065static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
Trond Myklebust16e42952005-10-27 22:12:44 -04004067 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004068 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004070 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004072 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004073 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4077 .rpc_argp = &args,
4078 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004080 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004081 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004082
Bryan Schumaker7c513052011-03-24 17:12:24 +00004083 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004084 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004085 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return status;
4087}
4088
Al Virobeffb8f2016-07-20 16:34:42 -04004089static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
4091 struct nfs4_exception exception = { };
4092 int err;
4093 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004094 err = _nfs4_proc_remove(dir, name);
4095 trace_nfs4_remove(dir, name, err);
4096 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 &exception);
4098 } while (exception.retry);
4099 return err;
4100}
4101
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004102static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004104 struct nfs_server *server = NFS_SERVER(dir);
4105 struct nfs_removeargs *args = msg->rpc_argp;
4106 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004108 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004110 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004111
4112 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113}
4114
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004115static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4116{
Al Viro884be172016-04-28 23:56:31 -04004117 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004118 &data->args.seq_args,
4119 &data->res.seq_res,
4120 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004123static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004125 struct nfs_unlinkdata *data = task->tk_calldata;
4126 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004127
Trond Myklebust14516c32010-07-31 14:29:06 -04004128 if (!nfs4_sequence_done(task, &res->seq_res))
4129 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004130 if (nfs4_async_handle_error(task, res->server, NULL,
4131 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004132 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004133 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004134 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004135 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Jeff Laytond3d41522010-09-17 17:31:57 -04004138static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4139{
4140 struct nfs_server *server = NFS_SERVER(dir);
4141 struct nfs_renameargs *arg = msg->rpc_argp;
4142 struct nfs_renameres *res = msg->rpc_resp;
4143
4144 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004145 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004146 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004147}
4148
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004149static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4150{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004151 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4152 &data->args.seq_args,
4153 &data->res.seq_res,
4154 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004155}
4156
4157static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4158 struct inode *new_dir)
4159{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004160 struct nfs_renamedata *data = task->tk_calldata;
4161 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004162
4163 if (!nfs4_sequence_done(task, &res->seq_res))
4164 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004165 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004166 return 0;
4167
Trond Myklebustc733c492017-01-11 12:32:26 -05004168 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004169 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004170 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004171 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004172 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004173 return 1;
4174}
4175
Al Virobeffb8f2016-07-20 16:34:42 -04004176static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004178 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 struct nfs4_link_arg arg = {
4180 .fh = NFS_FH(inode),
4181 .dir_fh = NFS_FH(dir),
4182 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004183 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004185 struct nfs4_link_res res = {
4186 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004187 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004188 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 struct rpc_message msg = {
4190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4191 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004192 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004194 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Trond Myklebust136f2622010-04-16 16:22:49 -04004196 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004197 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004198 goto out;
4199
David Quigley14c43f72013-05-22 12:50:43 -04004200 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4201 if (IS_ERR(res.label)) {
4202 status = PTR_ERR(res.label);
4203 goto out;
4204 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004205 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004206
Bryan Schumaker7c513052011-03-24 17:12:24 +00004207 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004208 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004209 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004210 status = nfs_post_op_update_inode(inode, res.fattr);
4211 if (!status)
4212 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004213 }
David Quigley14c43f72013-05-22 12:50:43 -04004214
4215
4216 nfs4_label_free(res.label);
4217
Trond Myklebust136f2622010-04-16 16:22:49 -04004218out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004219 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return status;
4221}
4222
Al Virobeffb8f2016-07-20 16:34:42 -04004223static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225 struct nfs4_exception exception = { };
4226 int err;
4227 do {
4228 err = nfs4_handle_exception(NFS_SERVER(inode),
4229 _nfs4_proc_link(inode, dir, name),
4230 &exception);
4231 } while (exception.retry);
4232 return err;
4233}
4234
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235struct nfs4_createdata {
4236 struct rpc_message msg;
4237 struct nfs4_create_arg arg;
4238 struct nfs4_create_res res;
4239 struct nfs_fh fh;
4240 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004241 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242};
4243
4244static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004245 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246{
4247 struct nfs4_createdata *data;
4248
4249 data = kzalloc(sizeof(*data), GFP_KERNEL);
4250 if (data != NULL) {
4251 struct nfs_server *server = NFS_SERVER(dir);
4252
David Quigley14c43f72013-05-22 12:50:43 -04004253 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4254 if (IS_ERR(data->label))
4255 goto out_free;
4256
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4258 data->msg.rpc_argp = &data->arg;
4259 data->msg.rpc_resp = &data->res;
4260 data->arg.dir_fh = NFS_FH(dir);
4261 data->arg.server = server;
4262 data->arg.name = name;
4263 data->arg.attrs = sattr;
4264 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004265 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004266 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267 data->res.server = server;
4268 data->res.fh = &data->fh;
4269 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004270 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004271 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004272 }
4273 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004274out_free:
4275 kfree(data);
4276 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004277}
4278
4279static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4280{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004281 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004282 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004283 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004284 update_changeattr(dir, &data->res.dir_cinfo,
4285 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004286 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004287 }
4288 return status;
4289}
4290
4291static void nfs4_free_createdata(struct nfs4_createdata *data)
4292{
David Quigley14c43f72013-05-22 12:50:43 -04004293 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004294 kfree(data);
4295}
4296
Chuck Lever4f390c12006-08-22 20:06:22 -04004297static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004298 struct page *page, unsigned int len, struct iattr *sattr,
4299 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004301 struct nfs4_createdata *data;
4302 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Chuck Lever94a6d752006-08-22 20:06:23 -04004304 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004305 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004306
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004307 status = -ENOMEM;
4308 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4309 if (data == NULL)
4310 goto out;
4311
4312 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4313 data->arg.u.symlink.pages = &page;
4314 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004315 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004317 status = nfs4_do_create(dir, dentry, data);
4318
4319 nfs4_free_createdata(data);
4320out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return status;
4322}
4323
Chuck Lever4f390c12006-08-22 20:06:22 -04004324static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004325 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004328 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004330
4331 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004334 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4335 trace_nfs4_symlink(dir, &dentry->d_name, err);
4336 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 &exception);
4338 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004339
4340 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return err;
4342}
4343
4344static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004345 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004347 struct nfs4_createdata *data;
4348 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4351 if (data == NULL)
4352 goto out;
4353
David Quigley1775fd32013-05-22 12:50:42 -04004354 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004355 status = nfs4_do_create(dir, dentry, data);
4356
4357 nfs4_free_createdata(data);
4358out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return status;
4360}
4361
4362static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4363 struct iattr *sattr)
4364{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004365 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004367 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004369
David Quigleyaa9c2662013-05-22 12:50:44 -04004370 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4371
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004372 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4373 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004375 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4376 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4377 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 &exception);
4379 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004380 nfs4_label_release_security(label);
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return err;
4383}
4384
4385static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004386 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
David Howells2b0143b2015-03-17 22:25:59 +00004388 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 struct nfs4_readdir_arg args = {
4390 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004391 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 .pgbase = 0,
4393 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004394 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004395 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 };
4397 struct nfs4_readdir_res res;
4398 struct rpc_message msg = {
4399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4400 .rpc_argp = &args,
4401 .rpc_resp = &res,
4402 .rpc_cred = cred,
4403 };
4404 int status;
4405
Al Viro6de14722013-09-16 10:53:17 -04004406 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4407 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004408 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004409 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004411 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 -05004412 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004413 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004414 status += args.pgbase;
4415 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004416
4417 nfs_invalidate_atime(dir);
4418
Harvey Harrison3110ff82008-05-02 13:42:44 -07004419 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 return status;
4421}
4422
4423static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004424 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
4426 struct nfs4_exception exception = { };
4427 int err;
4428 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004429 err = _nfs4_proc_readdir(dentry, cred, cookie,
4430 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004431 trace_nfs4_readdir(d_inode(dentry), err);
4432 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 &exception);
4434 } while (exception.retry);
4435 return err;
4436}
4437
4438static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004439 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004441 struct nfs4_createdata *data;
4442 int mode = sattr->ia_mode;
4443 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4446 if (data == NULL)
4447 goto out;
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004452 data->arg.ftype = NF4BLK;
4453 data->arg.u.device.specdata1 = MAJOR(rdev);
4454 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
4456 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 data->arg.ftype = NF4CHR;
4458 data->arg.u.device.specdata1 = MAJOR(rdev);
4459 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004460 } else if (!S_ISSOCK(mode)) {
4461 status = -EINVAL;
4462 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
David Quigley1775fd32013-05-22 12:50:42 -04004464
David Quigleyaa9c2662013-05-22 12:50:44 -04004465 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004466 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004467out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004468 nfs4_free_createdata(data);
4469out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 return status;
4471}
4472
4473static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4474 struct iattr *sattr, dev_t rdev)
4475{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004476 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004478 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004480
David Quigleyaa9c2662013-05-22 12:50:44 -04004481 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4482
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004483 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4484 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004486 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4487 trace_nfs4_mknod(dir, &dentry->d_name, err);
4488 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 &exception);
4490 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004491
4492 nfs4_label_release_security(label);
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 return err;
4495}
4496
4497static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4498 struct nfs_fsstat *fsstat)
4499{
4500 struct nfs4_statfs_arg args = {
4501 .fh = fhandle,
4502 .bitmask = server->attr_bitmask,
4503 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004504 struct nfs4_statfs_res res = {
4505 .fsstat = fsstat,
4506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4509 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004510 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 };
4512
Trond Myklebust0e574af2005-10-27 22:12:38 -04004513 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
4517static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4518{
4519 struct nfs4_exception exception = { };
4520 int err;
4521 do {
4522 err = nfs4_handle_exception(server,
4523 _nfs4_proc_statfs(server, fhandle, fsstat),
4524 &exception);
4525 } while (exception.retry);
4526 return err;
4527}
4528
4529static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4530 struct nfs_fsinfo *fsinfo)
4531{
4532 struct nfs4_fsinfo_arg args = {
4533 .fh = fhandle,
4534 .bitmask = server->attr_bitmask,
4535 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004536 struct nfs4_fsinfo_res res = {
4537 .fsinfo = fsinfo,
4538 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4541 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004542 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 };
4544
Bryan Schumaker7c513052011-03-24 17:12:24 +00004545 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
4548static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4549{
4550 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004551 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 int err;
4553
4554 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004555 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004556 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004557 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004558 nfs4_set_lease_period(server->nfs_client,
4559 fsinfo->lease_time * HZ,
4560 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004561 break;
4562 }
4563 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 } while (exception.retry);
4565 return err;
4566}
4567
4568static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4569{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004570 int error;
4571
Trond Myklebust0e574af2005-10-27 22:12:38 -04004572 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004573 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004574 if (error == 0) {
4575 /* block layout checks this! */
4576 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004577 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004578 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004579
4580 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581}
4582
4583static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4584 struct nfs_pathconf *pathconf)
4585{
4586 struct nfs4_pathconf_arg args = {
4587 .fh = fhandle,
4588 .bitmask = server->attr_bitmask,
4589 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004590 struct nfs4_pathconf_res res = {
4591 .pathconf = pathconf,
4592 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 struct rpc_message msg = {
4594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4595 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004596 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 };
4598
4599 /* None of the pathconf attributes are mandatory to implement */
4600 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4601 memset(pathconf, 0, sizeof(*pathconf));
4602 return 0;
4603 }
4604
Trond Myklebust0e574af2005-10-27 22:12:38 -04004605 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004606 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607}
4608
4609static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4610 struct nfs_pathconf *pathconf)
4611{
4612 struct nfs4_exception exception = { };
4613 int err;
4614
4615 do {
4616 err = nfs4_handle_exception(server,
4617 _nfs4_proc_pathconf(server, fhandle, pathconf),
4618 &exception);
4619 } while (exception.retry);
4620 return err;
4621}
4622
Trond Myklebust5521abf2013-03-16 20:54:34 -04004623int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004624 const struct nfs_open_context *ctx,
4625 const struct nfs_lock_context *l_ctx,
4626 fmode_t fmode)
4627{
NeilBrown17393472016-10-13 15:26:47 +11004628 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004629}
4630EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4631
Trond Myklebust5521abf2013-03-16 20:54:34 -04004632static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4633 const struct nfs_open_context *ctx,
4634 const struct nfs_lock_context *l_ctx,
4635 fmode_t fmode)
4636{
4637 nfs4_stateid current_stateid;
4638
Trond Myklebuste1253be2014-03-05 08:44:23 -05004639 /* If the current stateid represents a lost lock, then exit */
4640 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4641 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004642 return nfs4_stateid_match(stateid, &current_stateid);
4643}
4644
4645static bool nfs4_error_stateid_expired(int err)
4646{
4647 switch (err) {
4648 case -NFS4ERR_DELEG_REVOKED:
4649 case -NFS4ERR_ADMIN_REVOKED:
4650 case -NFS4ERR_BAD_STATEID:
4651 case -NFS4ERR_STALE_STATEID:
4652 case -NFS4ERR_OLD_STATEID:
4653 case -NFS4ERR_OPENMODE:
4654 case -NFS4ERR_EXPIRED:
4655 return true;
4656 }
4657 return false;
4658}
4659
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004662 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004665 if (task->tk_status < 0) {
4666 struct nfs4_exception exception = {
4667 .inode = hdr->inode,
4668 .state = hdr->args.context->state,
4669 .stateid = &hdr->args.stateid,
4670 };
4671 task->tk_status = nfs4_async_handle_exception(task,
4672 server, task->tk_status, &exception);
4673 if (exception.retry) {
4674 rpc_restart_call_prepare(task);
4675 return -EAGAIN;
4676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
Trond Myklebust5521abf2013-03-16 20:54:34 -04004684static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004685 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004686{
4687
4688 if (!nfs4_error_stateid_expired(task->tk_status) ||
4689 nfs4_stateid_is_current(&args->stateid,
4690 args->context,
4691 args->lock_context,
4692 FMODE_READ))
4693 return false;
4694 rpc_restart_call_prepare(task);
4695 return true;
4696}
4697
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004699{
4700
4701 dprintk("--> %s\n", __func__);
4702
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004704 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004706 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004707 if (task->tk_status > 0)
4708 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4710 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004711}
4712
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4714 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004716 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004717 if (!hdr->pgio_done_cb)
4718 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004719 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004720 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004723static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4724 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004725{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4727 &hdr->args.seq_args,
4728 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004729 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004730 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4732 hdr->args.lock_context,
4733 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004734 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004735 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004736 return -EIO;
4737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740static int nfs4_write_done_cb(struct rpc_task *task,
4741 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004744
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004745 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004746 if (task->tk_status < 0) {
4747 struct nfs4_exception exception = {
4748 .inode = hdr->inode,
4749 .state = hdr->args.context->state,
4750 .stateid = &hdr->args.stateid,
4751 };
4752 task->tk_status = nfs4_async_handle_exception(task,
4753 NFS_SERVER(inode), task->tk_status,
4754 &exception);
4755 if (exception.retry) {
4756 rpc_restart_call_prepare(task);
4757 return -EAGAIN;
4758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004760 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004762 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004763 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
Trond Myklebust5521abf2013-03-16 20:54:34 -04004767static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004768 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004769{
4770
4771 if (!nfs4_error_stateid_expired(task->tk_status) ||
4772 nfs4_stateid_is_current(&args->stateid,
4773 args->context,
4774 args->lock_context,
4775 FMODE_WRITE))
4776 return false;
4777 rpc_restart_call_prepare(task);
4778 return true;
4779}
4780
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004781static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004782{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004783 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004784 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004785 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004787 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4788 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004789}
4790
Trond Myklebust5a37f852012-04-28 14:55:16 -04004791static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004792bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004793{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004794 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004795 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004796 return false;
4797 /* Otherwise, request attributes if and only if we don't hold
4798 * a delegation
4799 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004800 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004801}
Fred Isamana69aef12011-03-03 15:13:47 +00004802
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004803static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4804 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004806 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004807
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4809 hdr->args.bitmask = NULL;
4810 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004811 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004812 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004813
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004814 if (!hdr->pgio_done_cb)
4815 hdr->pgio_done_cb = nfs4_write_done_cb;
4816 hdr->res.server = server;
4817 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004819 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004820 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Fred Isaman0b7c0152012-04-20 14:47:39 -04004823static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4824{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004825 nfs4_setup_sequence(NFS_SERVER(data->inode),
4826 &data->args.seq_args,
4827 &data->res.seq_res,
4828 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
Fred Isaman0b7c0152012-04-20 14:47:39 -04004831static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004834
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004835 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004836 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4837 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004838 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004839 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842}
4843
Fred Isaman0b7c0152012-04-20 14:47:39 -04004844static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004845{
4846 if (!nfs4_sequence_done(task, &data->res.seq_res))
4847 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004848 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004849}
4850
Fred Isaman0b7c0152012-04-20 14:47:39 -04004851static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004853 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004854
Fred Isaman0b7c0152012-04-20 14:47:39 -04004855 if (data->commit_done_cb == NULL)
4856 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004857 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004858 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004859 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004862struct nfs4_renewdata {
4863 struct nfs_client *client;
4864 unsigned long timestamp;
4865};
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867/*
4868 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4869 * standalone procedure for queueing an asynchronous RENEW.
4870 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004871static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004872{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004873 struct nfs4_renewdata *data = calldata;
4874 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004875
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004876 if (atomic_read(&clp->cl_count) > 1)
4877 nfs4_schedule_state_renewal(clp);
4878 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004879 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004880}
4881
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004882static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004884 struct nfs4_renewdata *data = calldata;
4885 struct nfs_client *clp = data->client;
4886 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004888 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004889 switch (task->tk_status) {
4890 case 0:
4891 break;
4892 case -NFS4ERR_LEASE_MOVED:
4893 nfs4_schedule_lease_moved_recovery(clp);
4894 break;
4895 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004896 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004897 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4898 return;
4899 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004900 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004901 return;
4902 }
4903 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004905 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004908static const struct rpc_call_ops nfs4_renew_ops = {
4909 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004910 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004911};
4912
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004913static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
4915 struct rpc_message msg = {
4916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4917 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004918 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004920 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004922 if (renew_flags == 0)
4923 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004924 if (!atomic_inc_not_zero(&clp->cl_count))
4925 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004926 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004927 if (data == NULL)
4928 return -ENOMEM;
4929 data->client = clp;
4930 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004931 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004932 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004935static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
4937 struct rpc_message msg = {
4938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4939 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004940 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 };
4942 unsigned long now = jiffies;
4943 int status;
4944
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004945 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (status < 0)
4947 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004948 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
4950}
4951
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004952static inline int nfs4_server_supports_acls(struct nfs_server *server)
4953{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004954 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004955}
4956
Trond Myklebust21f498c2012-08-24 10:59:25 -04004957/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4958 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004959 * the stack.
4960 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004961#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004962
Neil Hormane9e3d722011-03-04 19:26:03 -05004963static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004964 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004965{
4966 struct page *newpage, **spages;
4967 int rc = 0;
4968 size_t len;
4969 spages = pages;
4970
4971 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004972 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004973 newpage = alloc_page(GFP_KERNEL);
4974
4975 if (newpage == NULL)
4976 goto unwind;
4977 memcpy(page_address(newpage), buf, len);
4978 buf += len;
4979 buflen -= len;
4980 *pages++ = newpage;
4981 rc++;
4982 } while (buflen != 0);
4983
4984 return rc;
4985
4986unwind:
4987 for(; rc > 0; rc--)
4988 __free_page(spages[rc-1]);
4989 return -ENOMEM;
4990}
4991
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004992struct nfs4_cached_acl {
4993 int cached;
4994 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004995 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004996};
4997
4998static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004999{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005000 struct nfs_inode *nfsi = NFS_I(inode);
5001
5002 spin_lock(&inode->i_lock);
5003 kfree(nfsi->nfs4_acl);
5004 nfsi->nfs4_acl = acl;
5005 spin_unlock(&inode->i_lock);
5006}
5007
5008static void nfs4_zap_acl_attr(struct inode *inode)
5009{
5010 nfs4_set_cached_acl(inode, NULL);
5011}
5012
5013static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5014{
5015 struct nfs_inode *nfsi = NFS_I(inode);
5016 struct nfs4_cached_acl *acl;
5017 int ret = -ENOENT;
5018
5019 spin_lock(&inode->i_lock);
5020 acl = nfsi->nfs4_acl;
5021 if (acl == NULL)
5022 goto out;
5023 if (buf == NULL) /* user is just asking for length */
5024 goto out_len;
5025 if (acl->cached == 0)
5026 goto out;
5027 ret = -ERANGE; /* see getxattr(2) man page */
5028 if (acl->len > buflen)
5029 goto out;
5030 memcpy(buf, acl->data, acl->len);
5031out_len:
5032 ret = acl->len;
5033out:
5034 spin_unlock(&inode->i_lock);
5035 return ret;
5036}
5037
Sachin Prabhu5794d212012-04-17 14:36:40 +01005038static 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 +00005039{
5040 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005041 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005042
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005043 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005044 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045 if (acl == NULL)
5046 goto out;
5047 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005048 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005049 } else {
5050 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5051 if (acl == NULL)
5052 goto out;
5053 acl->cached = 0;
5054 }
5055 acl->len = acl_len;
5056out:
5057 nfs4_set_cached_acl(inode, acl);
5058}
5059
Andy Adamsonbf118a32011-12-07 11:55:27 -05005060/*
5061 * The getxattr API returns the required buffer length when called with a
5062 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5063 * the required buf. On a NULL buf, we send a page of data to the server
5064 * guessing that the ACL request can be serviced by a page. If so, we cache
5065 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5066 * the cache. If not so, we throw away the page, and cache the required
5067 * length. The next getxattr call will then produce another round trip to
5068 * the server, this time with the input buf of the required size.
5069 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005070static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005071{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005072 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005073 struct nfs_getaclargs args = {
5074 .fh = NFS_FH(inode),
5075 .acl_pages = pages,
5076 .acl_len = buflen,
5077 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005078 struct nfs_getaclres res = {
5079 .acl_len = buflen,
5080 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005081 struct rpc_message msg = {
5082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5083 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005084 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005085 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005086 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5087 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005088
Andy Adamsonbf118a32011-12-07 11:55:27 -05005089 /* As long as we're doing a round trip to the server anyway,
5090 * let's be prepared for a page of acl data. */
5091 if (npages == 0)
5092 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005093 if (npages > ARRAY_SIZE(pages))
5094 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005095
Andy Adamsonbf118a32011-12-07 11:55:27 -05005096 for (i = 0; i < npages; i++) {
5097 pages[i] = alloc_page(GFP_KERNEL);
5098 if (!pages[i])
5099 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005100 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005101
5102 /* for decoding across pages */
5103 res.acl_scratch = alloc_page(GFP_KERNEL);
5104 if (!res.acl_scratch)
5105 goto out_free;
5106
Andy Adamsonbf118a32011-12-07 11:55:27 -05005107 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005108
Peng Taode040be2012-01-10 22:42:47 +08005109 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005110 __func__, buf, buflen, npages, args.acl_len);
5111 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5112 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005113 if (ret)
5114 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005115
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005116 /* Handle the case where the passed-in buffer is too short */
5117 if (res.acl_flags & NFS4_ACL_TRUNC) {
5118 /* Did the user only issue a request for the acl length? */
5119 if (buf == NULL)
5120 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005121 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005122 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005123 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005124 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005125 if (buf) {
5126 if (res.acl_len > buflen) {
5127 ret = -ERANGE;
5128 goto out_free;
5129 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005130 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005131 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005132out_ok:
5133 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005134out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005135 for (i = 0; i < npages; i++)
5136 if (pages[i])
5137 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005138 if (res.acl_scratch)
5139 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005140 return ret;
5141}
5142
Trond Myklebust16b42892006-08-24 12:27:15 -04005143static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5144{
5145 struct nfs4_exception exception = { };
5146 ssize_t ret;
5147 do {
5148 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005149 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005150 if (ret >= 0)
5151 break;
5152 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5153 } while (exception.retry);
5154 return ret;
5155}
5156
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005157static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5158{
5159 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005160 int ret;
5161
5162 if (!nfs4_server_supports_acls(server))
5163 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005164 ret = nfs_revalidate_inode(server, inode);
5165 if (ret < 0)
5166 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005167 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5168 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005169 ret = nfs4_read_cached_acl(inode, buf, buflen);
5170 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005171 /* -ENOENT is returned if there is no ACL or if there is an ACL
5172 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005173 return ret;
5174 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005175}
5176
Trond Myklebust16b42892006-08-24 12:27:15 -04005177static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005178{
5179 struct nfs_server *server = NFS_SERVER(inode);
5180 struct page *pages[NFS4ACL_MAXPAGES];
5181 struct nfs_setaclargs arg = {
5182 .fh = NFS_FH(inode),
5183 .acl_pages = pages,
5184 .acl_len = buflen,
5185 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005186 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005187 struct rpc_message msg = {
5188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5189 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005190 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005191 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005192 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005193 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005194
5195 if (!nfs4_server_supports_acls(server))
5196 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005197 if (npages > ARRAY_SIZE(pages))
5198 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005199 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005200 if (i < 0)
5201 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005202 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005203 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005204
5205 /*
5206 * Free each page after tx, so the only ref left is
5207 * held by the network stack
5208 */
5209 for (; i > 0; i--)
5210 put_page(pages[i-1]);
5211
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005212 /*
5213 * Acl update can result in inode attribute update.
5214 * so mark the attribute cache invalid.
5215 */
5216 spin_lock(&inode->i_lock);
5217 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5218 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005219 nfs_access_zap_cache(inode);
5220 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005221 return ret;
5222}
5223
Trond Myklebust16b42892006-08-24 12:27:15 -04005224static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5225{
5226 struct nfs4_exception exception = { };
5227 int err;
5228 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005229 err = __nfs4_proc_set_acl(inode, buf, buflen);
5230 trace_nfs4_set_acl(inode, err);
5231 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005232 &exception);
5233 } while (exception.retry);
5234 return err;
5235}
5236
David Quigleyaa9c2662013-05-22 12:50:44 -04005237#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5238static int _nfs4_get_security_label(struct inode *inode, void *buf,
5239 size_t buflen)
5240{
5241 struct nfs_server *server = NFS_SERVER(inode);
5242 struct nfs_fattr fattr;
5243 struct nfs4_label label = {0, 0, buflen, buf};
5244
5245 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005246 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005247 .fh = NFS_FH(inode),
5248 .bitmask = bitmask,
5249 };
5250 struct nfs4_getattr_res res = {
5251 .fattr = &fattr,
5252 .label = &label,
5253 .server = server,
5254 };
5255 struct rpc_message msg = {
5256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005257 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005258 .rpc_resp = &res,
5259 };
5260 int ret;
5261
5262 nfs_fattr_init(&fattr);
5263
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005264 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005265 if (ret)
5266 return ret;
5267 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5268 return -ENOENT;
5269 if (buflen < label.len)
5270 return -ERANGE;
5271 return 0;
5272}
5273
5274static int nfs4_get_security_label(struct inode *inode, void *buf,
5275 size_t buflen)
5276{
5277 struct nfs4_exception exception = { };
5278 int err;
5279
5280 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5281 return -EOPNOTSUPP;
5282
5283 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005284 err = _nfs4_get_security_label(inode, buf, buflen);
5285 trace_nfs4_get_security_label(inode, err);
5286 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005287 &exception);
5288 } while (exception.retry);
5289 return err;
5290}
5291
5292static int _nfs4_do_set_security_label(struct inode *inode,
5293 struct nfs4_label *ilabel,
5294 struct nfs_fattr *fattr,
5295 struct nfs4_label *olabel)
5296{
5297
5298 struct iattr sattr = {0};
5299 struct nfs_server *server = NFS_SERVER(inode);
5300 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005301 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005302 .fh = NFS_FH(inode),
5303 .iap = &sattr,
5304 .server = server,
5305 .bitmask = bitmask,
5306 .label = ilabel,
5307 };
5308 struct nfs_setattrres res = {
5309 .fattr = fattr,
5310 .label = olabel,
5311 .server = server,
5312 };
5313 struct rpc_message msg = {
5314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005315 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005316 .rpc_resp = &res,
5317 };
5318 int status;
5319
Jeff Layton12207f62013-11-01 10:49:32 -04005320 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005321
Jeff Layton12207f62013-11-01 10:49:32 -04005322 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005323 if (status)
5324 dprintk("%s failed: %d\n", __func__, status);
5325
5326 return status;
5327}
5328
5329static int nfs4_do_set_security_label(struct inode *inode,
5330 struct nfs4_label *ilabel,
5331 struct nfs_fattr *fattr,
5332 struct nfs4_label *olabel)
5333{
5334 struct nfs4_exception exception = { };
5335 int err;
5336
5337 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005338 err = _nfs4_do_set_security_label(inode, ilabel,
5339 fattr, olabel);
5340 trace_nfs4_set_security_label(inode, err);
5341 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005342 &exception);
5343 } while (exception.retry);
5344 return err;
5345}
5346
5347static int
Al Viro59301222016-05-27 10:19:30 -04005348nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005349{
5350 struct nfs4_label ilabel, *olabel = NULL;
5351 struct nfs_fattr fattr;
5352 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005353 int status;
5354
5355 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5356 return -EOPNOTSUPP;
5357
5358 nfs_fattr_init(&fattr);
5359
5360 ilabel.pi = 0;
5361 ilabel.lfs = 0;
5362 ilabel.label = (char *)buf;
5363 ilabel.len = buflen;
5364
5365 cred = rpc_lookup_cred();
5366 if (IS_ERR(cred))
5367 return PTR_ERR(cred);
5368
5369 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5370 if (IS_ERR(olabel)) {
5371 status = -PTR_ERR(olabel);
5372 goto out;
5373 }
5374
5375 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5376 if (status == 0)
5377 nfs_setsecurity(inode, &fattr, olabel);
5378
5379 nfs4_label_free(olabel);
5380out:
5381 put_rpccred(cred);
5382 return status;
5383}
5384#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5385
5386
Chuck Leverf0920752012-05-21 22:45:41 -04005387static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5388 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005389{
5390 __be32 verf[2];
5391
Chuck Lever2c820d92012-05-21 22:45:33 -04005392 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5393 /* An impossible timestamp guarantees this value
5394 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005395 verf[0] = cpu_to_be32(U32_MAX);
5396 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005397 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005398 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005399 u64 ns = ktime_to_ns(nn->boot_time);
5400
5401 verf[0] = cpu_to_be32(ns >> 32);
5402 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005403 }
Chuck Levercd937102012-03-02 17:14:31 -05005404 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5405}
5406
Jeff Laytona3192682015-06-09 19:43:59 -04005407static int
5408nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005409{
Jeff Laytona3192682015-06-09 19:43:59 -04005410 size_t len;
5411 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005412
Trond Myklebustceb3a162015-01-03 15:16:04 -05005413 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005414 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005415
Jeff Laytona3192682015-06-09 19:43:59 -04005416 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005417 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005418 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5419 1 +
5420 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5421 1;
5422 rcu_read_unlock();
5423
5424 if (len > NFS4_OPAQUE_LIMIT + 1)
5425 return -EINVAL;
5426
5427 /*
5428 * Since this string is allocated at mount time, and held until the
5429 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5430 * about a memory-reclaim deadlock.
5431 */
5432 str = kmalloc(len, GFP_KERNEL);
5433 if (!str)
5434 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005435
Chuck Levere984a552012-09-14 17:24:21 -04005436 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005437 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005438 clp->cl_ipaddr,
5439 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5440 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005441 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005442
Jeff Laytona3192682015-06-09 19:43:59 -04005443 clp->cl_owner_id = str;
5444 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005445}
5446
Jeff Layton873e3852015-06-09 19:44:00 -04005447static int
5448nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005449{
Jeff Layton873e3852015-06-09 19:44:00 -04005450 size_t len;
5451 char *str;
5452
5453 len = 10 + 10 + 1 + 10 + 1 +
5454 strlen(nfs4_client_id_uniquifier) + 1 +
5455 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5456
5457 if (len > NFS4_OPAQUE_LIMIT + 1)
5458 return -EINVAL;
5459
5460 /*
5461 * Since this string is allocated at mount time, and held until the
5462 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5463 * about a memory-reclaim deadlock.
5464 */
5465 str = kmalloc(len, GFP_KERNEL);
5466 if (!str)
5467 return -ENOMEM;
5468
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005469 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005470 clp->rpc_ops->version, clp->cl_minorversion,
5471 nfs4_client_id_uniquifier,
5472 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005473 clp->cl_owner_id = str;
5474 return 0;
5475}
5476
5477static int
5478nfs4_init_uniform_client_string(struct nfs_client *clp)
5479{
Jeff Layton873e3852015-06-09 19:44:00 -04005480 size_t len;
5481 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005482
5483 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005484 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005485
5486 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005487 return nfs4_init_uniquifier_client_string(clp);
5488
5489 len = 10 + 10 + 1 + 10 + 1 +
5490 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5491
5492 if (len > NFS4_OPAQUE_LIMIT + 1)
5493 return -EINVAL;
5494
5495 /*
5496 * Since this string is allocated at mount time, and held until the
5497 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5498 * about a memory-reclaim deadlock.
5499 */
5500 str = kmalloc(len, GFP_KERNEL);
5501 if (!str)
5502 return -ENOMEM;
5503
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005504 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005505 clp->rpc_ops->version, clp->cl_minorversion,
5506 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005507 clp->cl_owner_id = str;
5508 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005509}
5510
Chuck Lever706cb8d2014-03-12 12:51:47 -04005511/*
5512 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5513 * services. Advertise one based on the address family of the
5514 * clientaddr.
5515 */
5516static unsigned int
5517nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5518{
5519 if (strchr(clp->cl_ipaddr, ':') != NULL)
5520 return scnprintf(buf, len, "tcp6");
5521 else
5522 return scnprintf(buf, len, "tcp");
5523}
5524
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005525static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5526{
5527 struct nfs4_setclientid *sc = calldata;
5528
5529 if (task->tk_status == 0)
5530 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5531}
5532
5533static const struct rpc_call_ops nfs4_setclientid_ops = {
5534 .rpc_call_done = nfs4_setclientid_done,
5535};
5536
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005537/**
5538 * nfs4_proc_setclientid - Negotiate client ID
5539 * @clp: state data structure
5540 * @program: RPC program for NFSv4 callback service
5541 * @port: IP port number for NFS4 callback service
5542 * @cred: RPC credential to use for this call
5543 * @res: where to place the result
5544 *
5545 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5546 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005547int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5548 unsigned short port, struct rpc_cred *cred,
5549 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
5551 nfs4_verifier sc_verifier;
5552 struct nfs4_setclientid setclientid = {
5553 .sc_verifier = &sc_verifier,
5554 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005555 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 };
5557 struct rpc_message msg = {
5558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5559 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005560 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005561 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005563 struct rpc_task *task;
5564 struct rpc_task_setup task_setup_data = {
5565 .rpc_client = clp->cl_rpcclient,
5566 .rpc_message = &msg,
5567 .callback_ops = &nfs4_setclientid_ops,
5568 .callback_data = &setclientid,
5569 .flags = RPC_TASK_TIMEOUT,
5570 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005571 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Chuck Leverde734832012-07-11 16:30:50 -04005573 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005574 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005575
5576 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5577 status = nfs4_init_uniform_client_string(clp);
5578 else
Jeff Laytona3192682015-06-09 19:43:59 -04005579 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005580
5581 if (status)
5582 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005583
Chuck Leverde734832012-07-11 16:30:50 -04005584 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005585 setclientid.sc_netid_len =
5586 nfs4_init_callback_netid(clp,
5587 setclientid.sc_netid,
5588 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005589 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005590 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 clp->cl_ipaddr, port >> 8, port & 255);
5592
Jeff Layton3a6bb732015-06-09 19:43:57 -04005593 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005594 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005595 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005596 task = rpc_run_task(&task_setup_data);
5597 if (IS_ERR(task)) {
5598 status = PTR_ERR(task);
5599 goto out;
5600 }
5601 status = task->tk_status;
5602 if (setclientid.sc_cred) {
5603 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5604 put_rpccred(setclientid.sc_cred);
5605 }
5606 rpc_put_task(task);
5607out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005608 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005609 dprintk("NFS reply setclientid: %d\n", status);
5610 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611}
5612
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005613/**
5614 * nfs4_proc_setclientid_confirm - Confirm client ID
5615 * @clp: state data structure
5616 * @res: result of a previous SETCLIENTID
5617 * @cred: RPC credential to use for this call
5618 *
5619 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5620 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005621int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005622 struct nfs4_setclientid_res *arg,
5623 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 struct rpc_message msg = {
5626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005627 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005628 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 int status;
5631
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005632 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5633 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5634 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005635 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005636 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005637 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 return status;
5639}
5640
Trond Myklebustfe650402006-01-03 09:55:18 +01005641struct nfs4_delegreturndata {
5642 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005643 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005644 struct nfs_fh fh;
5645 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005646 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005647 struct {
5648 struct nfs4_layoutreturn_args arg;
5649 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005650 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005651 u32 roc_barrier;
5652 bool roc;
5653 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005654 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005655 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005656 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005657};
5658
Trond Myklebustfe650402006-01-03 09:55:18 +01005659static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5660{
5661 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005662
Trond Myklebust14516c32010-07-31 14:29:06 -04005663 if (!nfs4_sequence_done(task, &data->res.seq_res))
5664 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005665
Trond Myklebustca8acf82013-08-13 10:36:56 -04005666 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005667
5668 /* Handle Layoutreturn errors */
5669 if (data->args.lr_args && task->tk_status != 0) {
5670 switch(data->res.lr_ret) {
5671 default:
5672 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5673 break;
5674 case 0:
5675 data->args.lr_args = NULL;
5676 data->res.lr_res = NULL;
5677 break;
5678 case -NFS4ERR_ADMIN_REVOKED:
5679 case -NFS4ERR_DELEG_REVOKED:
5680 case -NFS4ERR_EXPIRED:
5681 case -NFS4ERR_BAD_STATEID:
5682 case -NFS4ERR_OLD_STATEID:
5683 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5684 case -NFS4ERR_WRONG_CRED:
5685 data->args.lr_args = NULL;
5686 data->res.lr_res = NULL;
5687 data->res.lr_ret = 0;
5688 rpc_restart_call_prepare(task);
5689 return;
5690 }
5691 }
5692
Ricardo Labiaga79708862009-12-07 09:23:21 -05005693 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005694 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005695 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005696 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005697 case -NFS4ERR_ADMIN_REVOKED:
5698 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005699 case -NFS4ERR_EXPIRED:
5700 nfs4_free_revoked_stateid(data->res.server,
5701 data->args.stateid,
5702 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005703 case -NFS4ERR_BAD_STATEID:
5704 case -NFS4ERR_OLD_STATEID:
5705 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005706 task->tk_status = 0;
5707 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005708 case -NFS4ERR_ACCESS:
5709 if (data->args.bitmask) {
5710 data->args.bitmask = NULL;
5711 data->res.fattr = NULL;
5712 task->tk_status = 0;
5713 rpc_restart_call_prepare(task);
5714 return;
5715 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005716 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005717 if (nfs4_async_handle_error(task, data->res.server,
5718 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005719 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005720 return;
5721 }
5722 }
5723 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005724}
5725
5726static void nfs4_delegreturn_release(void *calldata)
5727{
Peng Tao039b7562014-07-03 13:05:02 +08005728 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005729 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005730
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005731 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005732 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005733 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5734 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005735 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005736 nfs_iput_and_deactive(inode);
5737 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005738 kfree(calldata);
5739}
5740
Andy Adamson938e1012009-04-01 09:22:28 -04005741static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5742{
5743 struct nfs4_delegreturndata *d_data;
5744
5745 d_data = (struct nfs4_delegreturndata *)data;
5746
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005747 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005748 return;
5749
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005750 nfs4_setup_sequence(d_data->res.server,
5751 &d_data->args.seq_args,
5752 &d_data->res.seq_res,
5753 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005754}
Andy Adamson938e1012009-04-01 09:22:28 -04005755
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005756static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005757 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005758 .rpc_call_done = nfs4_delegreturn_done,
5759 .rpc_release = nfs4_delegreturn_release,
5760};
5761
Trond Myklebuste6f81072008-01-24 18:14:34 -05005762static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005763{
5764 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005765 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005766 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005767 struct rpc_message msg = {
5768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5769 .rpc_cred = cred,
5770 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005771 struct rpc_task_setup task_setup_data = {
5772 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005773 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005774 .callback_ops = &nfs4_delegreturn_ops,
5775 .flags = RPC_TASK_ASYNC,
5776 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005777 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005778
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005779 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005780 if (data == NULL)
5781 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005782 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005783
5784 nfs4_state_protect(server->nfs_client,
5785 NFS_SP4_MACH_CRED_CLEANUP,
5786 &task_setup_data.rpc_client, &msg);
5787
Trond Myklebustfe650402006-01-03 09:55:18 +01005788 data->args.fhandle = &data->fh;
5789 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005790 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005791 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005792 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005793 data->res.fattr = &data->fattr;
5794 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005795 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005796 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005797 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005798 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005799 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005800 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005801 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005802 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005803 if (data->lr.roc) {
5804 data->args.lr_args = &data->lr.arg;
5805 data->res.lr_res = &data->lr.res;
5806 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005807 } else if (data->lr.roc) {
5808 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5809 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005810 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005811
Trond Myklebustc970aa82007-07-14 15:39:59 -04005812 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005813 msg.rpc_argp = &data->args;
5814 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005815 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005816 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005817 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005818 if (!issync)
5819 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005820 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005821 if (status != 0)
5822 goto out;
5823 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005824out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005825 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005826 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827}
5828
Trond Myklebuste6f81072008-01-24 18:14:34 -05005829int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
5831 struct nfs_server *server = NFS_SERVER(inode);
5832 struct nfs4_exception exception = { };
5833 int err;
5834 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005835 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005836 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 switch (err) {
5838 case -NFS4ERR_STALE_STATEID:
5839 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 case 0:
5841 return 0;
5842 }
5843 err = nfs4_handle_exception(server, err, &exception);
5844 } while (exception.retry);
5845 return err;
5846}
5847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5849{
5850 struct inode *inode = state->inode;
5851 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005852 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005853 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005855 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005857 struct nfs_lockt_res res = {
5858 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 };
5860 struct rpc_message msg = {
5861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5862 .rpc_argp = &arg,
5863 .rpc_resp = &res,
5864 .rpc_cred = state->owner->so_cred,
5865 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 struct nfs4_lock_state *lsp;
5867 int status;
5868
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005869 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005870 status = nfs4_set_lock_state(state, request);
5871 if (status != 0)
5872 goto out;
5873 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005874 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005875 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005876 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005877 switch (status) {
5878 case 0:
5879 request->fl_type = F_UNLCK;
5880 break;
5881 case -NFS4ERR_DENIED:
5882 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005884 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005885 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005886out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 return status;
5888}
5889
5890static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5891{
5892 struct nfs4_exception exception = { };
5893 int err;
5894
5895 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005896 err = _nfs4_proc_getlk(state, cmd, request);
5897 trace_nfs4_get_lock(request, state, cmd, err);
5898 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 &exception);
5900 } while (exception.retry);
5901 return err;
5902}
5903
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005904struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005905 struct nfs_locku_args arg;
5906 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005907 struct nfs4_lock_state *lsp;
5908 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005909 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005910 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005911 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005912};
5913
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005914static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5915 struct nfs_open_context *ctx,
5916 struct nfs4_lock_state *lsp,
5917 struct nfs_seqid *seqid)
5918{
5919 struct nfs4_unlockdata *p;
5920 struct inode *inode = lsp->ls_state->inode;
5921
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005922 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005923 if (p == NULL)
5924 return NULL;
5925 p->arg.fh = NFS_FH(inode);
5926 p->arg.fl = &p->fl;
5927 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005928 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005929 p->lsp = lsp;
5930 atomic_inc(&lsp->ls_count);
5931 /* Ensure we don't close file until we're done freeing locks! */
5932 p->ctx = get_nfs_open_context(ctx);
5933 memcpy(&p->fl, fl, sizeof(p->fl));
5934 p->server = NFS_SERVER(inode);
5935 return p;
5936}
5937
Trond Myklebust06f814a2006-01-03 09:55:07 +01005938static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005940 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005941 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005942 nfs4_put_lock_state(calldata->lsp);
5943 put_nfs_open_context(calldata->ctx);
5944 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005945}
5946
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005947static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005948{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005949 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005950
Trond Myklebust14516c32010-07-31 14:29:06 -04005951 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5952 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005953 switch (task->tk_status) {
5954 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005955 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005956 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005957 if (nfs4_update_lock_stateid(calldata->lsp,
5958 &calldata->res.stateid))
5959 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005960 case -NFS4ERR_ADMIN_REVOKED:
5961 case -NFS4ERR_EXPIRED:
5962 nfs4_free_revoked_stateid(calldata->server,
5963 &calldata->arg.stateid,
5964 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005965 case -NFS4ERR_BAD_STATEID:
5966 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005967 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005968 if (!nfs4_stateid_match(&calldata->arg.stateid,
5969 &calldata->lsp->ls_stateid))
5970 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005971 break;
5972 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005973 if (nfs4_async_handle_error(task, calldata->server,
5974 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005975 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005976 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005977 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005978}
5979
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005980static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005981{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005982 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005984 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005985 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005986 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005987 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005988 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005989 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005990 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005991 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005992 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005993 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005994 &calldata->res.seq_res,
5995 task) != 0)
5996 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005997 return;
5998out_no_action:
5999 task->tk_action = NULL;
6000out_wait:
6001 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002}
6003
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006004static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006005 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006006 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006007 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006008};
6009
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6011 struct nfs_open_context *ctx,
6012 struct nfs4_lock_state *lsp,
6013 struct nfs_seqid *seqid)
6014{
6015 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006016 struct rpc_message msg = {
6017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6018 .rpc_cred = ctx->cred,
6019 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006020 struct rpc_task_setup task_setup_data = {
6021 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006022 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006023 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006024 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006025 .flags = RPC_TASK_ASYNC,
6026 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006027
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006028 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6029 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6030
Frank Filz137d6ac2007-07-09 15:32:29 -07006031 /* Ensure this is an unlock - when canceling a lock, the
6032 * canceled lock is passed in, and it won't be an unlock.
6033 */
6034 fl->fl_type = F_UNLCK;
6035
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006036 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6037 if (data == NULL) {
6038 nfs_free_seqid(seqid);
6039 return ERR_PTR(-ENOMEM);
6040 }
6041
Chuck Levera9c92d62013-08-09 12:48:18 -04006042 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006043 msg.rpc_argp = &data->arg;
6044 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006045 task_setup_data.callback_data = data;
6046 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047}
6048
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6050{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006051 struct inode *inode = state->inode;
6052 struct nfs4_state_owner *sp = state->owner;
6053 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006054 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006055 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006056 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006057 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006058 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006059 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Trond Myklebust9b073572006-06-29 16:38:34 -04006061 status = nfs4_set_lock_state(state, request);
6062 /* Unlock _before_ we do the RPC call */
6063 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006064 /* Exclude nfs_delegation_claim_locks() */
6065 mutex_lock(&sp->so_delegreturn_mutex);
6066 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006067 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006068 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006069 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006070 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006071 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006072 }
6073 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006074 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006075 if (status != 0)
6076 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006077 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006078 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006079 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6080 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006081 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6082 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006083 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006084 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006085 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006086 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 status = PTR_ERR(task);
6088 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006089 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006091 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006092out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006093 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006094 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006095 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096}
6097
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006098struct nfs4_lockdata {
6099 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101 struct nfs4_lock_state *lsp;
6102 struct nfs_open_context *ctx;
6103 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006104 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006105 int rpc_status;
6106 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006107 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006108};
6109
6110static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006111 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6112 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113{
6114 struct nfs4_lockdata *p;
6115 struct inode *inode = lsp->ls_state->inode;
6116 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006117 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006118
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006119 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006120 if (p == NULL)
6121 return NULL;
6122
6123 p->arg.fh = NFS_FH(inode);
6124 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006125 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006126 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006127 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006128 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6129 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006130 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006131 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006132 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006133 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006134 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006135 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006136 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006137 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 atomic_inc(&lsp->ls_count);
6139 p->ctx = get_nfs_open_context(ctx);
6140 memcpy(&p->fl, fl, sizeof(p->fl));
6141 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006142out_free_seqid:
6143 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006144out_free:
6145 kfree(p);
6146 return NULL;
6147}
6148
6149static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6150{
6151 struct nfs4_lockdata *data = calldata;
6152 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
Harvey Harrison3110ff82008-05-02 13:42:44 -07006154 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006155 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006156 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006158 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006159 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006160 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006161 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006162 nfs4_stateid_copy(&data->arg.open_stateid,
6163 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006164 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006165 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006166 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006167 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006168 nfs4_stateid_copy(&data->arg.lock_stateid,
6169 &data->lsp->ls_stateid);
6170 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006171 if (!nfs4_valid_open_stateid(state)) {
6172 data->rpc_status = -EBADF;
6173 task->tk_action = NULL;
6174 goto out_release_open_seqid;
6175 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006176 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006177 if (nfs4_setup_sequence(data->server,
6178 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006179 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006180 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006181 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006182out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006183 nfs_release_seqid(data->arg.open_seqid);
6184out_release_lock_seqid:
6185 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006186out_wait:
6187 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006188 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006189}
6190
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006191static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6192{
6193 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006194 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006195
Harvey Harrison3110ff82008-05-02 13:42:44 -07006196 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006197
Trond Myklebust14516c32010-07-31 14:29:06 -04006198 if (!nfs4_sequence_done(task, &data->res.seq_res))
6199 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006200
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006201 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006202 switch (task->tk_status) {
6203 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006204 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006205 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006206 if (data->arg.new_lock) {
6207 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006208 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006209 rpc_restart_call_prepare(task);
6210 break;
6211 }
6212 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006213 if (data->arg.new_lock_owner != 0) {
6214 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6215 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6216 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6217 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6218 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006219 break;
6220 case -NFS4ERR_BAD_STATEID:
6221 case -NFS4ERR_OLD_STATEID:
6222 case -NFS4ERR_STALE_STATEID:
6223 case -NFS4ERR_EXPIRED:
6224 if (data->arg.new_lock_owner != 0) {
6225 if (!nfs4_stateid_match(&data->arg.open_stateid,
6226 &lsp->ls_state->open_stateid))
6227 rpc_restart_call_prepare(task);
6228 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6229 &lsp->ls_stateid))
6230 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006232 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006233}
6234
6235static void nfs4_lock_release(void *calldata)
6236{
6237 struct nfs4_lockdata *data = calldata;
6238
Harvey Harrison3110ff82008-05-02 13:42:44 -07006239 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006240 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 if (data->cancelled != 0) {
6242 struct rpc_task *task;
6243 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6244 data->arg.lock_seqid);
6245 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006246 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006247 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006248 } else
6249 nfs_free_seqid(data->arg.lock_seqid);
6250 nfs4_put_lock_state(data->lsp);
6251 put_nfs_open_context(data->ctx);
6252 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006253 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254}
6255
6256static const struct rpc_call_ops nfs4_lock_ops = {
6257 .rpc_call_prepare = nfs4_lock_prepare,
6258 .rpc_call_done = nfs4_lock_done,
6259 .rpc_release = nfs4_lock_release,
6260};
6261
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006262static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6263{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006264 switch (error) {
6265 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006266 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006267 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006268 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006269 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006270 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006271 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006272 break;
6273 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006274 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006275 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006276 };
6277}
6278
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006279static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006280{
6281 struct nfs4_lockdata *data;
6282 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006283 struct rpc_message msg = {
6284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6285 .rpc_cred = state->owner->so_cred,
6286 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006287 struct rpc_task_setup task_setup_data = {
6288 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006289 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006290 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006291 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006292 .flags = RPC_TASK_ASYNC,
6293 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006294 int ret;
6295
Harvey Harrison3110ff82008-05-02 13:42:44 -07006296 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006297 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006298 fl->fl_u.nfs4_fl.owner,
6299 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006300 if (data == NULL)
6301 return -ENOMEM;
6302 if (IS_SETLKW(cmd))
6303 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006304 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006305 msg.rpc_argp = &data->arg;
6306 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006307 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006308 if (recovery_type > NFS_LOCK_NEW) {
6309 if (recovery_type == NFS_LOCK_RECLAIM)
6310 data->arg.reclaim = NFS_LOCK_RECLAIM;
6311 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006312 } else
6313 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006314 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006315 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006316 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006317 ret = nfs4_wait_for_completion_rpc_task(task);
6318 if (ret == 0) {
6319 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006320 if (ret)
6321 nfs4_handle_setlk_error(data->server, data->lsp,
6322 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006323 } else
6324 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006325 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006326 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006327 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329}
6330
6331static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6332{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006333 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006334 struct nfs4_exception exception = {
6335 .inode = state->inode,
6336 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006337 int err;
6338
6339 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006340 /* Cache the lock if possible... */
6341 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6342 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006343 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006344 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006345 break;
6346 nfs4_handle_exception(server, err, &exception);
6347 } while (exception.retry);
6348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349}
6350
6351static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6352{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006353 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006354 struct nfs4_exception exception = {
6355 .inode = state->inode,
6356 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006357 int err;
6358
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006359 err = nfs4_set_lock_state(state, request);
6360 if (err != 0)
6361 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006362 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006363 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6364 return 0;
6365 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006366 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006367 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6368 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006369 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006370 switch (err) {
6371 default:
6372 goto out;
6373 case -NFS4ERR_GRACE:
6374 case -NFS4ERR_DELAY:
6375 nfs4_handle_exception(server, err, &exception);
6376 err = 0;
6377 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006378 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006379out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381}
6382
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006383#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006384static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6385{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006386 struct nfs4_lock_state *lsp;
6387 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006388
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006389 status = nfs4_set_lock_state(state, request);
6390 if (status != 0)
6391 return status;
6392 lsp = request->fl_u.nfs4_fl.owner;
6393 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6394 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6395 return 0;
6396 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006397 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006398}
6399#endif
6400
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6402{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006403 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006404 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006405 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006406 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
Trond Myklebust01c3b862006-06-29 16:38:39 -04006408 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006409 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006410 if (status < 0)
6411 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006412 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006413 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006414 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006415 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006416 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006417 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006418 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006419 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006420 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006421 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006422 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006423 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006424 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006425 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006426out:
6427 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 return status;
6429}
6430
6431static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6432{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006433 struct nfs4_exception exception = {
6434 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006435 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006436 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 int err;
6438
6439 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006440 err = _nfs4_proc_setlk(state, cmd, request);
6441 if (err == -NFS4ERR_DENIED)
6442 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006444 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 } while (exception.retry);
6446 return err;
6447}
6448
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006449#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6450#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6451
6452static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006453nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6454 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006455{
6456 int status = -ERESTARTSYS;
6457 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6458
6459 while(!signalled()) {
6460 status = nfs4_proc_setlk(state, cmd, request);
6461 if ((status != -EAGAIN) || IS_SETLK(cmd))
6462 break;
6463 freezable_schedule_timeout_interruptible(timeout);
6464 timeout *= 2;
6465 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6466 status = -ERESTARTSYS;
6467 }
6468 return status;
6469}
6470
Jeff Laytona1d617d82016-09-17 18:17:39 -04006471#ifdef CONFIG_NFS_V4_1
6472struct nfs4_lock_waiter {
6473 struct task_struct *task;
6474 struct inode *inode;
6475 struct nfs_lowner *owner;
6476 bool notified;
6477};
6478
6479static int
6480nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6481{
6482 int ret;
6483 struct cb_notify_lock_args *cbnl = key;
6484 struct nfs4_lock_waiter *waiter = wait->private;
6485 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6486 *wowner = waiter->owner;
6487
6488 /* Only wake if the callback was for the same owner */
6489 if (lowner->clientid != wowner->clientid ||
6490 lowner->id != wowner->id ||
6491 lowner->s_dev != wowner->s_dev)
6492 return 0;
6493
6494 /* Make sure it's for the right inode */
6495 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6496 return 0;
6497
6498 waiter->notified = true;
6499
6500 /* override "private" so we can use default_wake_function */
6501 wait->private = waiter->task;
6502 ret = autoremove_wake_function(wait, mode, flags, key);
6503 wait->private = waiter;
6504 return ret;
6505}
6506
6507static int
6508nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6509{
6510 int status = -ERESTARTSYS;
6511 unsigned long flags;
6512 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6513 struct nfs_server *server = NFS_SERVER(state->inode);
6514 struct nfs_client *clp = server->nfs_client;
6515 wait_queue_head_t *q = &clp->cl_lock_waitq;
6516 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6517 .id = lsp->ls_seqid.owner_id,
6518 .s_dev = server->s_dev };
6519 struct nfs4_lock_waiter waiter = { .task = current,
6520 .inode = state->inode,
6521 .owner = &owner,
6522 .notified = false };
6523 wait_queue_t wait;
6524
6525 /* Don't bother with waitqueue if we don't expect a callback */
6526 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6527 return nfs4_retry_setlk_simple(state, cmd, request);
6528
6529 init_wait(&wait);
6530 wait.private = &waiter;
6531 wait.func = nfs4_wake_lock_waiter;
6532 add_wait_queue(q, &wait);
6533
6534 while(!signalled()) {
6535 status = nfs4_proc_setlk(state, cmd, request);
6536 if ((status != -EAGAIN) || IS_SETLK(cmd))
6537 break;
6538
6539 status = -ERESTARTSYS;
6540 spin_lock_irqsave(&q->lock, flags);
6541 if (waiter.notified) {
6542 spin_unlock_irqrestore(&q->lock, flags);
6543 continue;
6544 }
6545 set_current_state(TASK_INTERRUPTIBLE);
6546 spin_unlock_irqrestore(&q->lock, flags);
6547
6548 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6549 }
6550
6551 finish_wait(q, &wait);
6552 return status;
6553}
6554#else /* !CONFIG_NFS_V4_1 */
6555static inline int
6556nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6557{
6558 return nfs4_retry_setlk_simple(state, cmd, request);
6559}
6560#endif
6561
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562static int
6563nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6564{
6565 struct nfs_open_context *ctx;
6566 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 int status;
6568
6569 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006570 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 state = ctx->state;
6572
6573 if (request->fl_start < 0 || request->fl_end < 0)
6574 return -EINVAL;
6575
Trond Myklebustd9531262009-07-21 19:22:38 -04006576 if (IS_GETLK(cmd)) {
6577 if (state != NULL)
6578 return nfs4_proc_getlk(state, F_GETLK, request);
6579 return 0;
6580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581
6582 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6583 return -EINVAL;
6584
Trond Myklebustd9531262009-07-21 19:22:38 -04006585 if (request->fl_type == F_UNLCK) {
6586 if (state != NULL)
6587 return nfs4_proc_unlck(state, cmd, request);
6588 return 0;
6589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
Trond Myklebustd9531262009-07-21 19:22:38 -04006591 if (state == NULL)
6592 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006593
6594 if ((request->fl_flags & FL_POSIX) &&
6595 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6596 return -ENOLCK;
6597
Trond Myklebust55725512012-04-18 12:48:35 -04006598 /*
6599 * Don't rely on the VFS having checked the file open mode,
6600 * since it won't do this for flock() locks.
6601 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006602 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006603 case F_RDLCK:
6604 if (!(filp->f_mode & FMODE_READ))
6605 return -EBADF;
6606 break;
6607 case F_WRLCK:
6608 if (!(filp->f_mode & FMODE_WRITE))
6609 return -EBADF;
6610 }
6611
Jeff Layton1ea67db2016-09-17 18:17:37 -04006612 status = nfs4_set_lock_state(state, request);
6613 if (status != 0)
6614 return status;
6615
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006616 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617}
6618
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006619int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006620{
6621 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006622 int err;
6623
6624 err = nfs4_set_lock_state(state, fl);
6625 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006626 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006627 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006628 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006629}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006630
Trond Myklebustcf470c32012-03-07 13:49:12 -05006631struct nfs_release_lockowner_data {
6632 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006633 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006634 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006635 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006636 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006637};
6638
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006639static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6640{
6641 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006642 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006643 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6644 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006645 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006646 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006647}
6648
6649static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6650{
6651 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006652 struct nfs_server *server = data->server;
6653
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006654 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006655
6656 switch (task->tk_status) {
6657 case 0:
6658 renew_lease(server, data->timestamp);
6659 break;
6660 case -NFS4ERR_STALE_CLIENTID:
6661 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006662 nfs4_schedule_lease_recovery(server->nfs_client);
6663 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006664 case -NFS4ERR_LEASE_MOVED:
6665 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006666 if (nfs4_async_handle_error(task, server,
6667 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006668 rpc_restart_call_prepare(task);
6669 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006670}
6671
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006672static void nfs4_release_lockowner_release(void *calldata)
6673{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006674 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006675 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006676 kfree(calldata);
6677}
6678
Trond Myklebust17280172012-03-11 13:11:00 -04006679static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006680 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6681 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006682 .rpc_release = nfs4_release_lockowner_release,
6683};
6684
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006685static void
6686nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006687{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006688 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006689 struct rpc_message msg = {
6690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6691 };
6692
6693 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006694 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006695
Trond Myklebustcf470c32012-03-07 13:49:12 -05006696 data = kmalloc(sizeof(*data), GFP_NOFS);
6697 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006698 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006699 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006700 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006701 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6702 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6703 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006704
Trond Myklebustcf470c32012-03-07 13:49:12 -05006705 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006706 msg.rpc_resp = &data->res;
6707 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006708 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006709}
6710
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006711#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6712
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006713static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006714 struct dentry *unused, struct inode *inode,
6715 const char *key, const void *buf,
6716 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006717{
Al Viro59301222016-05-27 10:19:30 -04006718 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006719}
6720
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006721static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006722 struct dentry *unused, struct inode *inode,
6723 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006724{
Al Virob2968212016-04-10 20:48:24 -04006725 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006726}
6727
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006728static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006729{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006730 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006731}
6732
David Quigleyc9bccef2013-05-22 12:50:45 -04006733#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006734
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006735static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006736 struct dentry *unused, struct inode *inode,
6737 const char *key, const void *buf,
6738 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006739{
6740 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006741 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006742
6743 return -EOPNOTSUPP;
6744}
6745
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006746static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006747 struct dentry *unused, struct inode *inode,
6748 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006749{
6750 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006751 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006752 return -EOPNOTSUPP;
6753}
6754
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006755static ssize_t
6756nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006757{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006758 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006759
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006760 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6761 len = security_inode_listsecurity(inode, list, list_len);
6762 if (list_len && len > list_len)
6763 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006764 }
6765 return len;
6766}
6767
6768static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6769 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006770 .get = nfs4_xattr_get_nfs4_label,
6771 .set = nfs4_xattr_set_nfs4_label,
6772};
David Quigleyc9bccef2013-05-22 12:50:45 -04006773
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006774#else
6775
6776static ssize_t
6777nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6778{
6779 return 0;
6780}
6781
6782#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006783
Andy Adamson533eb462011-06-13 18:25:56 -04006784/*
6785 * nfs_fhget will use either the mounted_on_fileid or the fileid
6786 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006787static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6788{
Andy Adamson533eb462011-06-13 18:25:56 -04006789 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6790 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6791 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006792 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006793 return;
6794
6795 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006796 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006797 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6798 fattr->nlink = 2;
6799}
6800
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006801static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6802 const struct qstr *name,
6803 struct nfs4_fs_locations *fs_locations,
6804 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006805{
6806 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006807 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006808 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006809 };
6810 struct nfs4_fs_locations_arg args = {
6811 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006812 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006813 .page = page,
6814 .bitmask = bitmask,
6815 };
Benny Halevy22958462009-04-01 09:22:02 -04006816 struct nfs4_fs_locations_res res = {
6817 .fs_locations = fs_locations,
6818 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006819 struct rpc_message msg = {
6820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6821 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006822 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006823 };
6824 int status;
6825
Harvey Harrison3110ff82008-05-02 13:42:44 -07006826 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006827
6828 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6829 * is not supported */
6830 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6831 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6832 else
6833 bitmask[0] |= FATTR4_WORD0_FILEID;
6834
Trond Myklebustc228fd32007-01-13 02:28:11 -05006835 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006836 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006837 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006838 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006839 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006840 return status;
6841}
6842
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006843int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6844 const struct qstr *name,
6845 struct nfs4_fs_locations *fs_locations,
6846 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006847{
6848 struct nfs4_exception exception = { };
6849 int err;
6850 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006851 err = _nfs4_proc_fs_locations(client, dir, name,
6852 fs_locations, page);
6853 trace_nfs4_get_fs_locations(dir, name, err);
6854 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006855 &exception);
6856 } while (exception.retry);
6857 return err;
6858}
6859
Chuck Leverb03d7352013-10-17 14:12:50 -04006860/*
6861 * This operation also signals the server that this client is
6862 * performing migration recovery. The server can stop returning
6863 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6864 * appended to this compound to identify the client ID which is
6865 * performing recovery.
6866 */
6867static int _nfs40_proc_get_locations(struct inode *inode,
6868 struct nfs4_fs_locations *locations,
6869 struct page *page, struct rpc_cred *cred)
6870{
6871 struct nfs_server *server = NFS_SERVER(inode);
6872 struct rpc_clnt *clnt = server->client;
6873 u32 bitmask[2] = {
6874 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6875 };
6876 struct nfs4_fs_locations_arg args = {
6877 .clientid = server->nfs_client->cl_clientid,
6878 .fh = NFS_FH(inode),
6879 .page = page,
6880 .bitmask = bitmask,
6881 .migration = 1, /* skip LOOKUP */
6882 .renew = 1, /* append RENEW */
6883 };
6884 struct nfs4_fs_locations_res res = {
6885 .fs_locations = locations,
6886 .migration = 1,
6887 .renew = 1,
6888 };
6889 struct rpc_message msg = {
6890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6891 .rpc_argp = &args,
6892 .rpc_resp = &res,
6893 .rpc_cred = cred,
6894 };
6895 unsigned long now = jiffies;
6896 int status;
6897
6898 nfs_fattr_init(&locations->fattr);
6899 locations->server = server;
6900 locations->nlocations = 0;
6901
6902 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6903 nfs4_set_sequence_privileged(&args.seq_args);
6904 status = nfs4_call_sync_sequence(clnt, server, &msg,
6905 &args.seq_args, &res.seq_res);
6906 if (status)
6907 return status;
6908
6909 renew_lease(server, now);
6910 return 0;
6911}
6912
6913#ifdef CONFIG_NFS_V4_1
6914
6915/*
6916 * This operation also signals the server that this client is
6917 * performing migration recovery. The server can stop asserting
6918 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6919 * performing this operation is identified in the SEQUENCE
6920 * operation in this compound.
6921 *
6922 * When the client supports GETATTR(fs_locations_info), it can
6923 * be plumbed in here.
6924 */
6925static int _nfs41_proc_get_locations(struct inode *inode,
6926 struct nfs4_fs_locations *locations,
6927 struct page *page, struct rpc_cred *cred)
6928{
6929 struct nfs_server *server = NFS_SERVER(inode);
6930 struct rpc_clnt *clnt = server->client;
6931 u32 bitmask[2] = {
6932 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6933 };
6934 struct nfs4_fs_locations_arg args = {
6935 .fh = NFS_FH(inode),
6936 .page = page,
6937 .bitmask = bitmask,
6938 .migration = 1, /* skip LOOKUP */
6939 };
6940 struct nfs4_fs_locations_res res = {
6941 .fs_locations = locations,
6942 .migration = 1,
6943 };
6944 struct rpc_message msg = {
6945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6946 .rpc_argp = &args,
6947 .rpc_resp = &res,
6948 .rpc_cred = cred,
6949 };
6950 int status;
6951
6952 nfs_fattr_init(&locations->fattr);
6953 locations->server = server;
6954 locations->nlocations = 0;
6955
6956 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6957 nfs4_set_sequence_privileged(&args.seq_args);
6958 status = nfs4_call_sync_sequence(clnt, server, &msg,
6959 &args.seq_args, &res.seq_res);
6960 if (status == NFS4_OK &&
6961 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6962 status = -NFS4ERR_LEASE_MOVED;
6963 return status;
6964}
6965
6966#endif /* CONFIG_NFS_V4_1 */
6967
6968/**
6969 * nfs4_proc_get_locations - discover locations for a migrated FSID
6970 * @inode: inode on FSID that is migrating
6971 * @locations: result of query
6972 * @page: buffer
6973 * @cred: credential to use for this operation
6974 *
6975 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6976 * operation failed, or a negative errno if a local error occurred.
6977 *
6978 * On success, "locations" is filled in, but if the server has
6979 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6980 * asserted.
6981 *
6982 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6983 * from this client that require migration recovery.
6984 */
6985int nfs4_proc_get_locations(struct inode *inode,
6986 struct nfs4_fs_locations *locations,
6987 struct page *page, struct rpc_cred *cred)
6988{
6989 struct nfs_server *server = NFS_SERVER(inode);
6990 struct nfs_client *clp = server->nfs_client;
6991 const struct nfs4_mig_recovery_ops *ops =
6992 clp->cl_mvops->mig_recovery_ops;
6993 struct nfs4_exception exception = { };
6994 int status;
6995
6996 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6997 (unsigned long long)server->fsid.major,
6998 (unsigned long long)server->fsid.minor,
6999 clp->cl_hostname);
7000 nfs_display_fhandle(NFS_FH(inode), __func__);
7001
7002 do {
7003 status = ops->get_locations(inode, locations, page, cred);
7004 if (status != -NFS4ERR_DELAY)
7005 break;
7006 nfs4_handle_exception(server, status, &exception);
7007 } while (exception.retry);
7008 return status;
7009}
7010
Chuck Lever44c99932013-10-17 14:13:30 -04007011/*
7012 * This operation also signals the server that this client is
7013 * performing "lease moved" recovery. The server can stop
7014 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7015 * is appended to this compound to identify the client ID which is
7016 * performing recovery.
7017 */
7018static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7019{
7020 struct nfs_server *server = NFS_SERVER(inode);
7021 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7022 struct rpc_clnt *clnt = server->client;
7023 struct nfs4_fsid_present_arg args = {
7024 .fh = NFS_FH(inode),
7025 .clientid = clp->cl_clientid,
7026 .renew = 1, /* append RENEW */
7027 };
7028 struct nfs4_fsid_present_res res = {
7029 .renew = 1,
7030 };
7031 struct rpc_message msg = {
7032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7033 .rpc_argp = &args,
7034 .rpc_resp = &res,
7035 .rpc_cred = cred,
7036 };
7037 unsigned long now = jiffies;
7038 int status;
7039
7040 res.fh = nfs_alloc_fhandle();
7041 if (res.fh == NULL)
7042 return -ENOMEM;
7043
7044 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7045 nfs4_set_sequence_privileged(&args.seq_args);
7046 status = nfs4_call_sync_sequence(clnt, server, &msg,
7047 &args.seq_args, &res.seq_res);
7048 nfs_free_fhandle(res.fh);
7049 if (status)
7050 return status;
7051
7052 do_renew_lease(clp, now);
7053 return 0;
7054}
7055
7056#ifdef CONFIG_NFS_V4_1
7057
7058/*
7059 * This operation also signals the server that this client is
7060 * performing "lease moved" recovery. The server can stop asserting
7061 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7062 * this operation is identified in the SEQUENCE operation in this
7063 * compound.
7064 */
7065static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7066{
7067 struct nfs_server *server = NFS_SERVER(inode);
7068 struct rpc_clnt *clnt = server->client;
7069 struct nfs4_fsid_present_arg args = {
7070 .fh = NFS_FH(inode),
7071 };
7072 struct nfs4_fsid_present_res res = {
7073 };
7074 struct rpc_message msg = {
7075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7076 .rpc_argp = &args,
7077 .rpc_resp = &res,
7078 .rpc_cred = cred,
7079 };
7080 int status;
7081
7082 res.fh = nfs_alloc_fhandle();
7083 if (res.fh == NULL)
7084 return -ENOMEM;
7085
7086 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7087 nfs4_set_sequence_privileged(&args.seq_args);
7088 status = nfs4_call_sync_sequence(clnt, server, &msg,
7089 &args.seq_args, &res.seq_res);
7090 nfs_free_fhandle(res.fh);
7091 if (status == NFS4_OK &&
7092 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7093 status = -NFS4ERR_LEASE_MOVED;
7094 return status;
7095}
7096
7097#endif /* CONFIG_NFS_V4_1 */
7098
7099/**
7100 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7101 * @inode: inode on FSID to check
7102 * @cred: credential to use for this operation
7103 *
7104 * Server indicates whether the FSID is present, moved, or not
7105 * recognized. This operation is necessary to clear a LEASE_MOVED
7106 * condition for this client ID.
7107 *
7108 * Returns NFS4_OK if the FSID is present on this server,
7109 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7110 * NFS4ERR code if some error occurred on the server, or a
7111 * negative errno if a local failure occurred.
7112 */
7113int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7114{
7115 struct nfs_server *server = NFS_SERVER(inode);
7116 struct nfs_client *clp = server->nfs_client;
7117 const struct nfs4_mig_recovery_ops *ops =
7118 clp->cl_mvops->mig_recovery_ops;
7119 struct nfs4_exception exception = { };
7120 int status;
7121
7122 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7123 (unsigned long long)server->fsid.major,
7124 (unsigned long long)server->fsid.minor,
7125 clp->cl_hostname);
7126 nfs_display_fhandle(NFS_FH(inode), __func__);
7127
7128 do {
7129 status = ops->fsid_present(inode, cred);
7130 if (status != -NFS4ERR_DELAY)
7131 break;
7132 nfs4_handle_exception(server, status, &exception);
7133 } while (exception.retry);
7134 return status;
7135}
7136
Andy Adamson5ec16a82013-08-08 10:57:55 -04007137/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007138 * If 'use_integrity' is true and the state managment nfs_client
7139 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7140 * and the machine credential as per RFC3530bis and RFC5661 Security
7141 * Considerations sections. Otherwise, just use the user cred with the
7142 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007143 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007144static 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 +00007145{
7146 int status;
7147 struct nfs4_secinfo_arg args = {
7148 .dir_fh = NFS_FH(dir),
7149 .name = name,
7150 };
7151 struct nfs4_secinfo_res res = {
7152 .flavors = flavors,
7153 };
7154 struct rpc_message msg = {
7155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7156 .rpc_argp = &args,
7157 .rpc_resp = &res,
7158 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007159 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007160 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007161
7162 if (use_integrity) {
7163 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007164 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7165 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007166 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007167
7168 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007169
7170 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7171 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7172
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007173 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7174 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007175 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007176
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007177 if (cred)
7178 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007179
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007180 return status;
7181}
7182
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007183int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7184 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007185{
7186 struct nfs4_exception exception = { };
7187 int err;
7188 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007189 err = -NFS4ERR_WRONGSEC;
7190
7191 /* try to use integrity protection with machine cred */
7192 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7193 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7194
7195 /*
7196 * if unable to use integrity protection, or SECINFO with
7197 * integrity protection returns NFS4ERR_WRONGSEC (which is
7198 * disallowed by spec, but exists in deployed servers) use
7199 * the current filesystem's rpc_client and the user cred.
7200 */
7201 if (err == -NFS4ERR_WRONGSEC)
7202 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7203
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007204 trace_nfs4_secinfo(dir, name, err);
7205 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007206 &exception);
7207 } while (exception.retry);
7208 return err;
7209}
7210
Andy Adamson557134a2009-04-01 09:21:53 -04007211#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007212/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007213 * Check the exchange flags returned by the server for invalid flags, having
7214 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7215 * DS flags set.
7216 */
7217static int nfs4_check_cl_exchange_flags(u32 flags)
7218{
7219 if (flags & ~EXCHGID4_FLAG_MASK_R)
7220 goto out_inval;
7221 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7222 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7223 goto out_inval;
7224 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7225 goto out_inval;
7226 return NFS_OK;
7227out_inval:
7228 return -NFS4ERR_INVAL;
7229}
7230
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007231static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007232nfs41_same_server_scope(struct nfs41_server_scope *a,
7233 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007234{
7235 if (a->server_scope_sz == b->server_scope_sz &&
7236 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7237 return true;
7238
7239 return false;
7240}
7241
Andy Adamson02a95de2016-02-05 16:08:37 -05007242static void
7243nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7244{
7245}
7246
7247static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7248 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7249};
7250
Andy Adamson357f54d2010-12-14 10:11:57 -05007251/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007252 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007253 *
7254 * The 4.1 client currently uses the same TCP connection for the
7255 * fore and backchannel.
7256 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007257static
7258int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7259 struct rpc_xprt *xprt,
7260 struct nfs_client *clp,
7261 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007262{
7263 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007264 struct nfs41_bind_conn_to_session_args args = {
7265 .client = clp,
7266 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7267 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007268 struct nfs41_bind_conn_to_session_res res;
7269 struct rpc_message msg = {
7270 .rpc_proc =
7271 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007272 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007273 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007274 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007275 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007276 struct rpc_task_setup task_setup_data = {
7277 .rpc_client = clnt,
7278 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007279 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007280 .rpc_message = &msg,
7281 .flags = RPC_TASK_TIMEOUT,
7282 };
7283 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007284
7285 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007286
Trond Myklebust71a097c2015-02-18 09:27:18 -08007287 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7288 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7289 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007290
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007291 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7292 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7293 args.dir = NFS4_CDFC4_FORE;
7294
7295 task = rpc_run_task(&task_setup_data);
7296 if (!IS_ERR(task)) {
7297 status = task->tk_status;
7298 rpc_put_task(task);
7299 } else
7300 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007301 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007302 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007303 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007304 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7305 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7306 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007307 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007308 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007309 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007310 dprintk("NFS: %s: Unexpected direction from server\n",
7311 __func__);
7312 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007313 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007314 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007315 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007316 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7317 __func__);
7318 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007319 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007320 }
7321 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007322out:
7323 dprintk("<-- %s status= %d\n", __func__, status);
7324 return status;
7325}
7326
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007327struct rpc_bind_conn_calldata {
7328 struct nfs_client *clp;
7329 struct rpc_cred *cred;
7330};
7331
7332static int
7333nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7334 struct rpc_xprt *xprt,
7335 void *calldata)
7336{
7337 struct rpc_bind_conn_calldata *p = calldata;
7338
7339 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7340}
7341
7342int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7343{
7344 struct rpc_bind_conn_calldata data = {
7345 .clp = clp,
7346 .cred = cred,
7347 };
7348 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7349 nfs4_proc_bind_conn_to_session_callback, &data);
7350}
7351
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007352/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007353 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7354 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007355 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007356static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7357 .how = SP4_MACH_CRED,
7358 .enforce.u.words = {
7359 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7360 1 << (OP_EXCHANGE_ID - 32) |
7361 1 << (OP_CREATE_SESSION - 32) |
7362 1 << (OP_DESTROY_SESSION - 32) |
7363 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007364 },
7365 .allow.u.words = {
7366 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007367 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007368 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007369 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007370 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007371 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007372 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007373 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007374 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007375 1 << (OP_FREE_STATEID - 32) |
7376 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007377 }
7378};
7379
7380/*
7381 * Select the state protection mode for client `clp' given the server results
7382 * from exchange_id in `sp'.
7383 *
7384 * Returns 0 on success, negative errno otherwise.
7385 */
7386static int nfs4_sp4_select_mode(struct nfs_client *clp,
7387 struct nfs41_state_protection *sp)
7388{
7389 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7390 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7391 1 << (OP_EXCHANGE_ID - 32) |
7392 1 << (OP_CREATE_SESSION - 32) |
7393 1 << (OP_DESTROY_SESSION - 32) |
7394 1 << (OP_DESTROY_CLIENTID - 32)
7395 };
7396 unsigned int i;
7397
7398 if (sp->how == SP4_MACH_CRED) {
7399 /* Print state protect result */
7400 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7401 for (i = 0; i <= LAST_NFS4_OP; i++) {
7402 if (test_bit(i, sp->enforce.u.longs))
7403 dfprintk(MOUNT, " enforce op %d\n", i);
7404 if (test_bit(i, sp->allow.u.longs))
7405 dfprintk(MOUNT, " allow op %d\n", i);
7406 }
7407
7408 /* make sure nothing is on enforce list that isn't supported */
7409 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7410 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7411 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7412 return -EINVAL;
7413 }
7414 }
7415
7416 /*
7417 * Minimal mode - state operations are allowed to use machine
7418 * credential. Note this already happens by default, so the
7419 * client doesn't have to do anything more than the negotiation.
7420 *
7421 * NOTE: we don't care if EXCHANGE_ID is in the list -
7422 * we're already using the machine cred for exchange_id
7423 * and will never use a different cred.
7424 */
7425 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7426 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7427 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7428 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7429 dfprintk(MOUNT, "sp4_mach_cred:\n");
7430 dfprintk(MOUNT, " minimal mode enabled\n");
7431 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7432 } else {
7433 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7434 return -EINVAL;
7435 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007436
7437 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007438 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7439 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007440 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7441 dfprintk(MOUNT, " cleanup mode enabled\n");
7442 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7443 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007444
Andrew Elble99ade3c2015-12-02 09:39:51 -05007445 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7446 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7447 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7448 &clp->cl_sp4_flags);
7449 }
7450
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007451 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7452 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7453 dfprintk(MOUNT, " secinfo mode enabled\n");
7454 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7455 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007456
7457 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7458 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7459 dfprintk(MOUNT, " stateid mode enabled\n");
7460 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7461 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007462
7463 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7464 dfprintk(MOUNT, " write mode enabled\n");
7465 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7466 }
7467
7468 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7469 dfprintk(MOUNT, " commit mode enabled\n");
7470 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7471 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007472 }
7473
7474 return 0;
7475}
7476
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477struct nfs41_exchange_id_data {
7478 struct nfs41_exchange_id_res res;
7479 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007480 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 int rpc_status;
7482};
7483
7484static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007485{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007486 struct nfs41_exchange_id_data *cdata =
7487 (struct nfs41_exchange_id_data *)data;
7488 struct nfs_client *clp = cdata->args.client;
7489 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007491 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007492
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007493 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007495
7496 if (cdata->xprt && status == 0) {
7497 status = nfs4_detect_session_trunking(clp, &cdata->res,
7498 cdata->xprt);
7499 goto out;
7500 }
7501
Andy Adamson8d89bd72016-09-09 09:22:18 -04007502 if (status == 0)
7503 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007504
Chuck Lever177313f2012-05-21 22:44:58 -04007505 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 clp->cl_clientid = cdata->res.clientid;
7507 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007508 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007510 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511 &clp->cl_session->session_state);
7512 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007513 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007514
Chuck Leveracdeb692012-05-21 22:46:16 -04007515 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007516 clp->cl_serverowner = cdata->res.server_owner;
7517 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007518
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007519 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007520 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007521 clp->cl_implid = cdata->res.impl_id;
7522 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007523
Chuck Lever177313f2012-05-21 22:44:58 -04007524 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007525 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007527 dprintk("%s: server_scope mismatch detected\n",
7528 __func__);
7529 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007530 kfree(clp->cl_serverscope);
7531 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007532 }
7533
Chuck Lever177313f2012-05-21 22:44:58 -04007534 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007535 clp->cl_serverscope = cdata->res.server_scope;
7536 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007537 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007538 /* Save the EXCHANGE_ID verifier session trunk tests */
7539 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7540 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007541 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007542out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007543 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007544 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007545}
7546
7547static void nfs4_exchange_id_release(void *data)
7548{
7549 struct nfs41_exchange_id_data *cdata =
7550 (struct nfs41_exchange_id_data *)data;
7551
7552 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007553 if (cdata->xprt) {
7554 xprt_put(cdata->xprt);
7555 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7556 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007557 kfree(cdata->res.impl_id);
7558 kfree(cdata->res.server_scope);
7559 kfree(cdata->res.server_owner);
7560 kfree(cdata);
7561}
7562
7563static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7564 .rpc_call_done = nfs4_exchange_id_done,
7565 .rpc_release = nfs4_exchange_id_release,
7566};
7567
Benny Halevy99fe60d2009-04-01 09:22:29 -04007568/*
7569 * _nfs4_proc_exchange_id()
7570 *
7571 * Wrapper for EXCHANGE_ID operation.
7572 */
7573static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007574 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007575{
7576 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007577 struct rpc_message msg = {
7578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007579 .rpc_cred = cred,
7580 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007581 struct rpc_task_setup task_setup_data = {
7582 .rpc_client = clp->cl_rpcclient,
7583 .callback_ops = &nfs4_exchange_id_call_ops,
7584 .rpc_message = &msg,
7585 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7586 };
7587 struct nfs41_exchange_id_data *calldata;
7588 struct rpc_task *task;
7589 int status = -EIO;
7590
7591 if (!atomic_inc_not_zero(&clp->cl_count))
7592 goto out;
7593
7594 status = -ENOMEM;
7595 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7596 if (!calldata)
7597 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007598
Andy Adamsonad0849a2016-09-09 09:22:28 -04007599 if (!xprt)
7600 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007601
7602 status = nfs4_init_uniform_client_string(clp);
7603 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007604 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007605
7606 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7607 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7608 clp->cl_owner_id);
7609
Andy Adamson8d89bd72016-09-09 09:22:18 -04007610 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7611 GFP_NOFS);
7612 status = -ENOMEM;
7613 if (unlikely(calldata->res.server_owner == NULL))
7614 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007615
Andy Adamson8d89bd72016-09-09 09:22:18 -04007616 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007617 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007618 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007619 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007620
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7622 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007623 goto out_server_scope;
7624
7625 switch (sp4_how) {
7626 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007627 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007628 break;
7629
7630 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007631 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007632 break;
7633
7634 default:
7635 /* unsupported! */
7636 WARN_ON_ONCE(1);
7637 status = -EINVAL;
7638 goto out_impl_id;
7639 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007640 if (xprt) {
7641 calldata->xprt = xprt;
7642 task_setup_data.rpc_xprt = xprt;
7643 task_setup_data.flags =
7644 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7645 calldata->args.verifier = &clp->cl_confirm;
7646 } else {
7647 calldata->args.verifier = &verifier;
7648 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007649 calldata->args.client = clp;
7650#ifdef CONFIG_NFS_V4_1_MIGRATION
7651 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7652 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7653 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7654#else
7655 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7656 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7657#endif
7658 msg.rpc_argp = &calldata->args;
7659 msg.rpc_resp = &calldata->res;
7660 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007661
Andy Adamson8d89bd72016-09-09 09:22:18 -04007662 task = rpc_run_task(&task_setup_data);
7663 if (IS_ERR(task)) {
7664 status = PTR_ERR(task);
7665 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007666 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007667
Andy Adamsonad0849a2016-09-09 09:22:28 -04007668 if (!xprt) {
7669 status = rpc_wait_for_completion_task(task);
7670 if (!status)
7671 status = calldata->rpc_status;
7672 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007673 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007674
Andy Adamson8d89bd72016-09-09 09:22:18 -04007675 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007676out:
Chuck Lever177313f2012-05-21 22:44:58 -04007677 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007678 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007679 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007680 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007681 clp->cl_implid->date.seconds,
7682 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007683 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007684 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007685
7686out_impl_id:
7687 kfree(calldata->res.impl_id);
7688out_server_scope:
7689 kfree(calldata->res.server_scope);
7690out_server_owner:
7691 kfree(calldata->res.server_owner);
7692out_calldata:
7693 kfree(calldata);
7694 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007695}
7696
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007697/*
7698 * nfs4_proc_exchange_id()
7699 *
7700 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7701 *
7702 * Since the clientid has expired, all compounds using sessions
7703 * associated with the stale clientid will be returning
7704 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7705 * be in some phase of session reset.
7706 *
7707 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7708 */
7709int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7710{
7711 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7712 int status;
7713
7714 /* try SP4_MACH_CRED if krb5i/p */
7715 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7716 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007717 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007718 if (!status)
7719 return 0;
7720 }
7721
7722 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007723 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007724}
7725
Andy Adamson04fa2c62016-09-09 09:22:29 -04007726/**
7727 * nfs4_test_session_trunk
7728 *
7729 * This is an add_xprt_test() test function called from
7730 * rpc_clnt_setup_test_and_add_xprt.
7731 *
7732 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7733 * and is dereferrenced in nfs4_exchange_id_release
7734 *
7735 * Upon success, add the new transport to the rpc_clnt
7736 *
7737 * @clnt: struct rpc_clnt to get new transport
7738 * @xprt: the rpc_xprt to test
7739 * @data: call data for _nfs4_proc_exchange_id.
7740 */
7741int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7742 void *data)
7743{
7744 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7745 u32 sp4_how;
7746
7747 dprintk("--> %s try %s\n", __func__,
7748 xprt->address_strings[RPC_DISPLAY_ADDR]);
7749
7750 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7751
7752 /* Test connection for session trunking. Async exchange_id call */
7753 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7754}
7755EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7756
Trond Myklebust66245532012-05-25 17:18:09 -04007757static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7758 struct rpc_cred *cred)
7759{
7760 struct rpc_message msg = {
7761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7762 .rpc_argp = clp,
7763 .rpc_cred = cred,
7764 };
7765 int status;
7766
7767 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007768 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007769 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007770 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007771 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7772 return status;
7773}
7774
7775static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7776 struct rpc_cred *cred)
7777{
7778 unsigned int loop;
7779 int ret;
7780
7781 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7782 ret = _nfs4_proc_destroy_clientid(clp, cred);
7783 switch (ret) {
7784 case -NFS4ERR_DELAY:
7785 case -NFS4ERR_CLIENTID_BUSY:
7786 ssleep(1);
7787 break;
7788 default:
7789 return ret;
7790 }
7791 }
7792 return 0;
7793}
7794
7795int nfs4_destroy_clientid(struct nfs_client *clp)
7796{
7797 struct rpc_cred *cred;
7798 int ret = 0;
7799
7800 if (clp->cl_mvops->minor_version < 1)
7801 goto out;
7802 if (clp->cl_exchange_flags == 0)
7803 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007804 if (clp->cl_preserve_clid)
7805 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007806 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007807 ret = nfs4_proc_destroy_clientid(clp, cred);
7808 if (cred)
7809 put_rpccred(cred);
7810 switch (ret) {
7811 case 0:
7812 case -NFS4ERR_STALE_CLIENTID:
7813 clp->cl_exchange_flags = 0;
7814 }
7815out:
7816 return ret;
7817}
7818
Andy Adamson2050f0c2009-04-01 09:22:30 -04007819struct nfs4_get_lease_time_data {
7820 struct nfs4_get_lease_time_args *args;
7821 struct nfs4_get_lease_time_res *res;
7822 struct nfs_client *clp;
7823};
7824
7825static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7826 void *calldata)
7827{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007828 struct nfs4_get_lease_time_data *data =
7829 (struct nfs4_get_lease_time_data *)calldata;
7830
7831 dprintk("--> %s\n", __func__);
7832 /* just setup sequence, do not trigger session recovery
7833 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007834 nfs41_setup_sequence(data->clp->cl_session,
7835 &data->args->la_seq_args,
7836 &data->res->lr_seq_res,
7837 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007838 dprintk("<-- %s\n", __func__);
7839}
7840
7841/*
7842 * Called from nfs4_state_manager thread for session setup, so don't recover
7843 * from sequence operation or clientid errors.
7844 */
7845static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7846{
7847 struct nfs4_get_lease_time_data *data =
7848 (struct nfs4_get_lease_time_data *)calldata;
7849
7850 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007851 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7852 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007853 switch (task->tk_status) {
7854 case -NFS4ERR_DELAY:
7855 case -NFS4ERR_GRACE:
7856 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7857 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7858 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007859 /* fall through */
7860 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007861 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007862 return;
7863 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007864 dprintk("<-- %s\n", __func__);
7865}
7866
Trond Myklebust17280172012-03-11 13:11:00 -04007867static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007868 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7869 .rpc_call_done = nfs4_get_lease_time_done,
7870};
7871
7872int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7873{
7874 struct rpc_task *task;
7875 struct nfs4_get_lease_time_args args;
7876 struct nfs4_get_lease_time_res res = {
7877 .lr_fsinfo = fsinfo,
7878 };
7879 struct nfs4_get_lease_time_data data = {
7880 .args = &args,
7881 .res = &res,
7882 .clp = clp,
7883 };
7884 struct rpc_message msg = {
7885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7886 .rpc_argp = &args,
7887 .rpc_resp = &res,
7888 };
7889 struct rpc_task_setup task_setup = {
7890 .rpc_client = clp->cl_rpcclient,
7891 .rpc_message = &msg,
7892 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007893 .callback_data = &data,
7894 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007895 };
7896 int status;
7897
Chuck Levera9c92d62013-08-09 12:48:18 -04007898 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007899 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007900 dprintk("--> %s\n", __func__);
7901 task = rpc_run_task(&task_setup);
7902
7903 if (IS_ERR(task))
7904 status = PTR_ERR(task);
7905 else {
7906 status = task->tk_status;
7907 rpc_put_task(task);
7908 }
7909 dprintk("<-- %s return %d\n", __func__, status);
7910
7911 return status;
7912}
7913
Andy Adamsonfc931582009-04-01 09:22:31 -04007914/*
7915 * Initialize the values to be used by the client in CREATE_SESSION
7916 * If nfs4_init_session set the fore channel request and response sizes,
7917 * use them.
7918 *
7919 * Set the back channel max_resp_sz_cached to zero to force the client to
7920 * always set csa_cachethis to FALSE because the current implementation
7921 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7922 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007923static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7924 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007925{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007926 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007927 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007928
Andy Adamson18aad3d2013-06-26 12:21:49 -04007929 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7930 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7931
Andy Adamsonfc931582009-04-01 09:22:31 -04007932 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007933 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7934 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007935 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007936 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007937
7938 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007939 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 __func__,
7941 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007942 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007943
7944 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007945 args->bc_attrs.max_rqst_sz = max_bc_payload;
7946 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007947 args->bc_attrs.max_resp_sz_cached = 0;
7948 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007949 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007950
7951 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7952 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7953 __func__,
7954 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7955 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7956 args->bc_attrs.max_reqs);
7957}
7958
Trond Myklebust79969dd2015-02-18 11:30:18 -08007959static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7960 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007961{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007962 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007963 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007964
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007965 if (rcvd->max_resp_sz > sent->max_resp_sz)
7966 return -EINVAL;
7967 /*
7968 * Our requested max_ops is the minimum we need; we're not
7969 * prepared to break up compounds into smaller pieces than that.
7970 * So, no point even trying to continue if the server won't
7971 * cooperate:
7972 */
7973 if (rcvd->max_ops < sent->max_ops)
7974 return -EINVAL;
7975 if (rcvd->max_reqs == 0)
7976 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007977 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7978 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007979 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007980}
7981
Trond Myklebust79969dd2015-02-18 11:30:18 -08007982static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7983 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007984{
7985 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007986 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007987
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007988 if (!(res->flags & SESSION4_BACK_CHAN))
7989 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007990 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7991 return -EINVAL;
7992 if (rcvd->max_resp_sz < sent->max_resp_sz)
7993 return -EINVAL;
7994 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7995 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007996 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007997 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007998 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007999 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008000out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008001 return 0;
8002}
Andy Adamson8d353012009-04-01 09:22:32 -04008003
Andy Adamson8d353012009-04-01 09:22:32 -04008004static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008005 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008006{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008007 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008008
Trond Myklebust79969dd2015-02-18 11:30:18 -08008009 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008010 if (ret)
8011 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008012 return nfs4_verify_back_channel_attrs(args, res);
8013}
8014
8015static void nfs4_update_session(struct nfs4_session *session,
8016 struct nfs41_create_session_res *res)
8017{
8018 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008019 /* Mark client id and session as being confirmed */
8020 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8021 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008022 session->flags = res->flags;
8023 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008024 if (res->flags & SESSION4_BACK_CHAN)
8025 memcpy(&session->bc_attrs, &res->bc_attrs,
8026 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008027}
8028
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008029static int _nfs4_proc_create_session(struct nfs_client *clp,
8030 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008031{
8032 struct nfs4_session *session = clp->cl_session;
8033 struct nfs41_create_session_args args = {
8034 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008035 .clientid = clp->cl_clientid,
8036 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008037 .cb_program = NFS4_CALLBACK,
8038 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008039 struct nfs41_create_session_res res;
8040
Andy Adamsonfc931582009-04-01 09:22:31 -04008041 struct rpc_message msg = {
8042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8043 .rpc_argp = &args,
8044 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008045 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008046 };
8047 int status;
8048
Chuck Lever6b26cc82016-05-02 14:40:40 -04008049 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008050 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008051
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008052 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008053 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008054
Trond Myklebustb519d402016-09-11 14:50:01 -04008055 switch (status) {
8056 case -NFS4ERR_STALE_CLIENTID:
8057 case -NFS4ERR_DELAY:
8058 case -ETIMEDOUT:
8059 case -EACCES:
8060 case -EAGAIN:
8061 goto out;
8062 };
8063
8064 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008065 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008066 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008067 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008068 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008069 if (status)
8070 goto out;
8071 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008072 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008073out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008074 return status;
8075}
8076
8077/*
8078 * Issues a CREATE_SESSION operation to the server.
8079 * It is the responsibility of the caller to verify the session is
8080 * expired before calling this routine.
8081 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008082int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008083{
8084 int status;
8085 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008086 struct nfs4_session *session = clp->cl_session;
8087
8088 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8089
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008090 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008091 if (status)
8092 goto out;
8093
Andy Adamsonaacd5532011-11-09 13:58:21 -05008094 /* Init or reset the session slot tables */
8095 status = nfs4_setup_session_slot_tables(session);
8096 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008097 if (status)
8098 goto out;
8099
8100 ptr = (unsigned *)&session->sess_id.data[0];
8101 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8102 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008103out:
8104 dprintk("<-- %s\n", __func__);
8105 return status;
8106}
8107
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008108/*
8109 * Issue the over-the-wire RPC DESTROY_SESSION.
8110 * The caller must serialize access to this routine.
8111 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008112int nfs4_proc_destroy_session(struct nfs4_session *session,
8113 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008114{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008115 struct rpc_message msg = {
8116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8117 .rpc_argp = session,
8118 .rpc_cred = cred,
8119 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008120 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008121
8122 dprintk("--> nfs4_proc_destroy_session\n");
8123
8124 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008125 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8126 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008127
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008128 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008129 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008130
8131 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008132 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008133 "Session has been destroyed regardless...\n", status);
8134
8135 dprintk("<-- nfs4_proc_destroy_session\n");
8136 return status;
8137}
8138
Trond Myklebust7b38c362012-05-23 13:23:31 -04008139/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008140 * Renew the cl_session lease.
8141 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008142struct nfs4_sequence_data {
8143 struct nfs_client *clp;
8144 struct nfs4_sequence_args args;
8145 struct nfs4_sequence_res res;
8146};
8147
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008148static void nfs41_sequence_release(void *data)
8149{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008150 struct nfs4_sequence_data *calldata = data;
8151 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008152
Alexandros Batsakis71358402010-02-05 03:45:05 -08008153 if (atomic_read(&clp->cl_count) > 1)
8154 nfs4_schedule_state_renewal(clp);
8155 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008156 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008157}
8158
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008159static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8160{
8161 switch(task->tk_status) {
8162 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008163 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8164 return -EAGAIN;
8165 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008166 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008167 }
8168 return 0;
8169}
8170
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008171static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008172{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008173 struct nfs4_sequence_data *calldata = data;
8174 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008175
Trond Myklebust14516c32010-07-31 14:29:06 -04008176 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8177 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008178
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008179 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180 if (task->tk_status < 0) {
8181 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008182 if (atomic_read(&clp->cl_count) == 1)
8183 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008184
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008185 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8186 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187 return;
8188 }
8189 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008190 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008191out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008192 dprintk("<-- %s\n", __func__);
8193}
8194
8195static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8196{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008197 struct nfs4_sequence_data *calldata = data;
8198 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008199 struct nfs4_sequence_args *args;
8200 struct nfs4_sequence_res *res;
8201
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008202 args = task->tk_msg.rpc_argp;
8203 res = task->tk_msg.rpc_resp;
8204
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008205 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008206}
8207
8208static const struct rpc_call_ops nfs41_sequence_ops = {
8209 .rpc_call_done = nfs41_sequence_call_done,
8210 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008211 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008212};
8213
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008214static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8215 struct rpc_cred *cred,
8216 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008217{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008218 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008219 struct rpc_message msg = {
8220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8221 .rpc_cred = cred,
8222 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008223 struct rpc_task_setup task_setup_data = {
8224 .rpc_client = clp->cl_rpcclient,
8225 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008226 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008227 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008228 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008229
Alexandros Batsakis71358402010-02-05 03:45:05 -08008230 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008231 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008232 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008233 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008234 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008235 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008236 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008237 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008238 if (is_privileged)
8239 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008240 msg.rpc_argp = &calldata->args;
8241 msg.rpc_resp = &calldata->res;
8242 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008243 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008244
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008245 return rpc_run_task(&task_setup_data);
8246}
8247
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008248static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008249{
8250 struct rpc_task *task;
8251 int ret = 0;
8252
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008253 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008254 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008255 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008256 if (IS_ERR(task))
8257 ret = PTR_ERR(task);
8258 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008259 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008260 dprintk("<-- %s status=%d\n", __func__, ret);
8261 return ret;
8262}
8263
8264static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8265{
8266 struct rpc_task *task;
8267 int ret;
8268
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008269 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008270 if (IS_ERR(task)) {
8271 ret = PTR_ERR(task);
8272 goto out;
8273 }
8274 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008275 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008276 ret = task->tk_status;
8277 rpc_put_task(task);
8278out:
8279 dprintk("<-- %s status=%d\n", __func__, ret);
8280 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008281}
8282
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008283struct nfs4_reclaim_complete_data {
8284 struct nfs_client *clp;
8285 struct nfs41_reclaim_complete_args arg;
8286 struct nfs41_reclaim_complete_res res;
8287};
8288
8289static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8290{
8291 struct nfs4_reclaim_complete_data *calldata = data;
8292
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008293 nfs41_setup_sequence(calldata->clp->cl_session,
8294 &calldata->arg.seq_args,
8295 &calldata->res.seq_res,
8296 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008297}
8298
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008299static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8300{
8301 switch(task->tk_status) {
8302 case 0:
8303 case -NFS4ERR_COMPLETE_ALREADY:
8304 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8305 break;
8306 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008307 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008308 /* fall through */
8309 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008310 return -EAGAIN;
8311 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008312 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008313 }
8314 return 0;
8315}
8316
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008317static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8318{
8319 struct nfs4_reclaim_complete_data *calldata = data;
8320 struct nfs_client *clp = calldata->clp;
8321 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8322
8323 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008324 if (!nfs41_sequence_done(task, res))
8325 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008326
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008327 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008328 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8329 rpc_restart_call_prepare(task);
8330 return;
8331 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008332 dprintk("<-- %s\n", __func__);
8333}
8334
8335static void nfs4_free_reclaim_complete_data(void *data)
8336{
8337 struct nfs4_reclaim_complete_data *calldata = data;
8338
8339 kfree(calldata);
8340}
8341
8342static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8343 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8344 .rpc_call_done = nfs4_reclaim_complete_done,
8345 .rpc_release = nfs4_free_reclaim_complete_data,
8346};
8347
8348/*
8349 * Issue a global reclaim complete.
8350 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008351static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8352 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008353{
8354 struct nfs4_reclaim_complete_data *calldata;
8355 struct rpc_task *task;
8356 struct rpc_message msg = {
8357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008358 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008359 };
8360 struct rpc_task_setup task_setup_data = {
8361 .rpc_client = clp->cl_rpcclient,
8362 .rpc_message = &msg,
8363 .callback_ops = &nfs4_reclaim_complete_call_ops,
8364 .flags = RPC_TASK_ASYNC,
8365 };
8366 int status = -ENOMEM;
8367
8368 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008369 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008370 if (calldata == NULL)
8371 goto out;
8372 calldata->clp = clp;
8373 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008374
Chuck Levera9c92d62013-08-09 12:48:18 -04008375 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008376 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008377 msg.rpc_argp = &calldata->arg;
8378 msg.rpc_resp = &calldata->res;
8379 task_setup_data.callback_data = calldata;
8380 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008381 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008382 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008383 goto out;
8384 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008385 status = nfs4_wait_for_completion_rpc_task(task);
8386 if (status == 0)
8387 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008388 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008389 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008390out:
8391 dprintk("<-- %s status=%d\n", __func__, status);
8392 return status;
8393}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008394
8395static void
8396nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8397{
8398 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008399 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Anna Schumaker172d9de2015-05-14 10:56:10 -04008400 struct nfs4_session *session = nfs4_get_session(server->nfs_client);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008401
8402 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008403 nfs41_setup_sequence(session, &lgp->args.seq_args,
8404 &lgp->res.seq_res, task);
8405 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008406}
8407
8408static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8409{
8410 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008411
8412 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008413 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008414 dprintk("<-- %s\n", __func__);
8415}
8416
8417static int
8418nfs4_layoutget_handle_exception(struct rpc_task *task,
8419 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8420{
Trond Myklebustee314c22012-10-01 17:25:48 -07008421 struct inode *inode = lgp->args.inode;
8422 struct nfs_server *server = NFS_SERVER(inode);
8423 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008424 int nfs4err = task->tk_status;
8425 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008426 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008427
Boaz Harroshed7e5422014-01-22 20:34:54 +02008428 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008429
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008430 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008431 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008432 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008433
8434 /*
8435 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8436 * on the file. set tk_status to -ENODATA to tell upper layer to
8437 * retry go inband.
8438 */
8439 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008440 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008441 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008442 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008443 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8444 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8445 */
8446 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008447 status = -EOVERFLOW;
8448 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008449 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008450 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008451 * (or clients) writing to the same RAID stripe except when
8452 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008453 *
8454 * Treat it like we would RECALLCONFLICT -- we retry for a little
8455 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008456 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008458 if (lgp->args.minlength == 0) {
8459 status = -EOVERFLOW;
8460 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008461 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008462 status = -EBUSY;
8463 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008464 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008465 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008466 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008467 case -NFS4ERR_DELEG_REVOKED:
8468 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008469 case -NFS4ERR_EXPIRED:
8470 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008471 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008472 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008473 lo = NFS_I(inode)->layout;
8474 /* If the open stateid was bad, then recover it. */
8475 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8476 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008477 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008478 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008479 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008480 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008481 break;
8482 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008483
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008484 /*
8485 * Mark the bad layout state as invalid, then retry
8486 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008487 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008488 spin_unlock(&inode->i_lock);
8489 pnfs_free_lseg_list(&head);
8490 status = -EAGAIN;
8491 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008492 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008493
Trond Myklebust8ac09252017-01-23 22:44:12 -05008494 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008495 err = nfs4_handle_exception(server, nfs4err, exception);
8496 if (!status) {
8497 if (exception->retry)
8498 status = -EAGAIN;
8499 else
8500 status = err;
8501 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008502out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008503 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008504 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505}
8506
Idan Kedar85541162012-08-02 11:47:10 +03008507static size_t max_response_pages(struct nfs_server *server)
8508{
8509 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8510 return nfs_page_array_len(0, max_resp_sz);
8511}
8512
8513static void nfs4_free_pages(struct page **pages, size_t size)
8514{
8515 int i;
8516
8517 if (!pages)
8518 return;
8519
8520 for (i = 0; i < size; i++) {
8521 if (!pages[i])
8522 break;
8523 __free_page(pages[i]);
8524 }
8525 kfree(pages);
8526}
8527
8528static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8529{
8530 struct page **pages;
8531 int i;
8532
8533 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8534 if (!pages) {
8535 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8536 return NULL;
8537 }
8538
8539 for (i = 0; i < size; i++) {
8540 pages[i] = alloc_page(gfp_flags);
8541 if (!pages[i]) {
8542 dprintk("%s: failed to allocate page\n", __func__);
8543 nfs4_free_pages(pages, size);
8544 return NULL;
8545 }
8546 }
8547
8548 return pages;
8549}
8550
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008551static void nfs4_layoutget_release(void *calldata)
8552{
8553 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008554 struct inode *inode = lgp->args.inode;
8555 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008556 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008557
8558 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008559 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008560 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008561 put_nfs_open_context(lgp->args.ctx);
8562 kfree(calldata);
8563 dprintk("<-- %s\n", __func__);
8564}
8565
8566static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8567 .rpc_call_prepare = nfs4_layoutget_prepare,
8568 .rpc_call_done = nfs4_layoutget_done,
8569 .rpc_release = nfs4_layoutget_release,
8570};
8571
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008572struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008573nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008574{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008575 struct inode *inode = lgp->args.inode;
8576 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008577 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008578 struct rpc_task *task;
8579 struct rpc_message msg = {
8580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8581 .rpc_argp = &lgp->args,
8582 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008583 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008584 };
8585 struct rpc_task_setup task_setup_data = {
8586 .rpc_client = server->client,
8587 .rpc_message = &msg,
8588 .callback_ops = &nfs4_layoutget_call_ops,
8589 .callback_data = lgp,
8590 .flags = RPC_TASK_ASYNC,
8591 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008592 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008593 struct nfs4_exception exception = {
8594 .inode = inode,
8595 .timeout = *timeout,
8596 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008597 int status = 0;
8598
8599 dprintk("--> %s\n", __func__);
8600
Peng Tao4bd5a9802014-11-17 11:05:17 +08008601 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8602 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8603
Idan Kedar85541162012-08-02 11:47:10 +03008604 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8605 if (!lgp->args.layout.pages) {
8606 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008607 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008608 }
8609 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8610
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008611 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008612 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008613 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008614
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615 task = rpc_run_task(&task_setup_data);
8616 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008617 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008618 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008619 if (status == 0) {
8620 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8621 *timeout = exception.timeout;
8622 }
8623
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008624 trace_nfs4_layoutget(lgp->args.ctx,
8625 &lgp->args.range,
8626 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008627 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008628 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008629
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008630 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8631 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008632 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008633 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008634 rpc_put_task(task);
8635 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008636 if (status)
8637 return ERR_PTR(status);
8638 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008639}
8640
Benny Halevycbe82602011-05-22 19:52:37 +03008641static void
8642nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8643{
8644 struct nfs4_layoutreturn *lrp = calldata;
8645
8646 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008647 nfs41_setup_sequence(lrp->clp->cl_session,
8648 &lrp->args.seq_args,
8649 &lrp->res.seq_res,
8650 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008651}
8652
8653static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8654{
8655 struct nfs4_layoutreturn *lrp = calldata;
8656 struct nfs_server *server;
8657
8658 dprintk("--> %s\n", __func__);
8659
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008660 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008661 return;
8662
8663 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008664 switch (task->tk_status) {
8665 default:
8666 task->tk_status = 0;
8667 case 0:
8668 break;
8669 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008670 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008671 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008672 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008673 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008674 return;
8675 }
Benny Halevycbe82602011-05-22 19:52:37 +03008676 dprintk("<-- %s\n", __func__);
8677}
8678
8679static void nfs4_layoutreturn_release(void *calldata)
8680{
8681 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008682 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008683
8684 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008685 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008686 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008687 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008688 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8689 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008690 pnfs_put_layout_hdr(lrp->args.layout);
8691 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008692 kfree(calldata);
8693 dprintk("<-- %s\n", __func__);
8694}
8695
8696static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8697 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8698 .rpc_call_done = nfs4_layoutreturn_done,
8699 .rpc_release = nfs4_layoutreturn_release,
8700};
8701
Peng Tao6c166052014-11-17 09:30:40 +08008702int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008703{
8704 struct rpc_task *task;
8705 struct rpc_message msg = {
8706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8707 .rpc_argp = &lrp->args,
8708 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008709 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008710 };
8711 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008712 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008713 .rpc_message = &msg,
8714 .callback_ops = &nfs4_layoutreturn_call_ops,
8715 .callback_data = lrp,
8716 };
Peng Tao6c166052014-11-17 09:30:40 +08008717 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008718
Andrew Elble99ade3c2015-12-02 09:39:51 -05008719 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8720 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8721 &task_setup_data.rpc_client, &msg);
8722
Benny Halevycbe82602011-05-22 19:52:37 +03008723 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008724 if (!sync) {
8725 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8726 if (!lrp->inode) {
8727 nfs4_layoutreturn_release(lrp);
8728 return -EAGAIN;
8729 }
8730 task_setup_data.flags |= RPC_TASK_ASYNC;
8731 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008732 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008733 task = rpc_run_task(&task_setup_data);
8734 if (IS_ERR(task))
8735 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008736 if (sync)
8737 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008738 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008739 dprintk("<-- %s status=%d\n", __func__, status);
8740 rpc_put_task(task);
8741 return status;
8742}
8743
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008744static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008745_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8746 struct pnfs_device *pdev,
8747 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008748{
8749 struct nfs4_getdeviceinfo_args args = {
8750 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008751 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8752 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008753 };
8754 struct nfs4_getdeviceinfo_res res = {
8755 .pdev = pdev,
8756 };
8757 struct rpc_message msg = {
8758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8759 .rpc_argp = &args,
8760 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008761 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008762 };
8763 int status;
8764
8765 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008766 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008767 if (res.notification & ~args.notify_types)
8768 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008769 if (res.notification != args.notify_types)
8770 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008771
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008772 dprintk("<-- %s status=%d\n", __func__, status);
8773
8774 return status;
8775}
8776
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008777int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8778 struct pnfs_device *pdev,
8779 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008780{
8781 struct nfs4_exception exception = { };
8782 int err;
8783
8784 do {
8785 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008786 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008787 &exception);
8788 } while (exception.retry);
8789 return err;
8790}
8791EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8792
Andy Adamson863a3c62011-03-23 13:27:54 +00008793static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8794{
8795 struct nfs4_layoutcommit_data *data = calldata;
8796 struct nfs_server *server = NFS_SERVER(data->args.inode);
Anna Schumaker172d9de2015-05-14 10:56:10 -04008797 struct nfs4_session *session = nfs4_get_session(server->nfs_client);
Andy Adamson863a3c62011-03-23 13:27:54 +00008798
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008799 nfs41_setup_sequence(session,
8800 &data->args.seq_args,
8801 &data->res.seq_res,
8802 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008803}
8804
8805static void
8806nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8807{
8808 struct nfs4_layoutcommit_data *data = calldata;
8809 struct nfs_server *server = NFS_SERVER(data->args.inode);
8810
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008811 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008812 return;
8813
8814 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008815 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8816 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8817 case -NFS4ERR_BADLAYOUT: /* no layout */
8818 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008819 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008820 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008821 break;
8822 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008823 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008824 rpc_restart_call_prepare(task);
8825 return;
8826 }
8827 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008828}
8829
8830static void nfs4_layoutcommit_release(void *calldata)
8831{
8832 struct nfs4_layoutcommit_data *data = calldata;
8833
Andy Adamsondb29c082011-07-30 20:52:38 -04008834 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008835 nfs_post_op_update_inode_force_wcc(data->args.inode,
8836 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008837 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008838 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008839 kfree(data);
8840}
8841
8842static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8843 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8844 .rpc_call_done = nfs4_layoutcommit_done,
8845 .rpc_release = nfs4_layoutcommit_release,
8846};
8847
8848int
Andy Adamsonef311532011-03-12 02:58:10 -05008849nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008850{
8851 struct rpc_message msg = {
8852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8853 .rpc_argp = &data->args,
8854 .rpc_resp = &data->res,
8855 .rpc_cred = data->cred,
8856 };
8857 struct rpc_task_setup task_setup_data = {
8858 .task = &data->task,
8859 .rpc_client = NFS_CLIENT(data->args.inode),
8860 .rpc_message = &msg,
8861 .callback_ops = &nfs4_layoutcommit_ops,
8862 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008863 };
8864 struct rpc_task *task;
8865 int status = 0;
8866
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008867 dprintk("NFS: initiating layoutcommit call. sync %d "
8868 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008869 data->args.lastbytewritten,
8870 data->args.inode->i_ino);
8871
Trond Myklebust472e2592015-02-05 16:50:30 -05008872 if (!sync) {
8873 data->inode = nfs_igrab_and_active(data->args.inode);
8874 if (data->inode == NULL) {
8875 nfs4_layoutcommit_release(data);
8876 return -EAGAIN;
8877 }
8878 task_setup_data.flags = RPC_TASK_ASYNC;
8879 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008880 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008881 task = rpc_run_task(&task_setup_data);
8882 if (IS_ERR(task))
8883 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008884 if (sync)
8885 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008886 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008887 dprintk("%s: status %d\n", __func__, status);
8888 rpc_put_task(task);
8889 return status;
8890}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008891
Andy Adamson97431202013-08-08 10:57:56 -04008892/**
8893 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8894 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8895 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008896static int
8897_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008898 struct nfs_fsinfo *info,
8899 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008900{
8901 struct nfs41_secinfo_no_name_args args = {
8902 .style = SECINFO_STYLE_CURRENT_FH,
8903 };
8904 struct nfs4_secinfo_res res = {
8905 .flavors = flavors,
8906 };
8907 struct rpc_message msg = {
8908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8909 .rpc_argp = &args,
8910 .rpc_resp = &res,
8911 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008912 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008913 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008914 int status;
8915
8916 if (use_integrity) {
8917 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008918 cred = nfs4_get_clid_cred(server->nfs_client);
8919 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008920 }
8921
8922 dprintk("--> %s\n", __func__);
8923 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8924 &res.seq_res, 0);
8925 dprintk("<-- %s status=%d\n", __func__, status);
8926
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008927 if (cred)
8928 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008929
8930 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008931}
8932
8933static int
8934nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8935 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8936{
8937 struct nfs4_exception exception = { };
8938 int err;
8939 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008940 /* first try using integrity protection */
8941 err = -NFS4ERR_WRONGSEC;
8942
8943 /* try to use integrity protection with machine cred */
8944 if (_nfs4_is_integrity_protected(server->nfs_client))
8945 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8946 flavors, true);
8947
8948 /*
8949 * if unable to use integrity protection, or SECINFO with
8950 * integrity protection returns NFS4ERR_WRONGSEC (which is
8951 * disallowed by spec, but exists in deployed servers) use
8952 * the current filesystem's rpc_client and the user cred.
8953 */
8954 if (err == -NFS4ERR_WRONGSEC)
8955 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8956 flavors, false);
8957
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008958 switch (err) {
8959 case 0:
8960 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008961 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008962 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008963 default:
8964 err = nfs4_handle_exception(server, err, &exception);
8965 }
8966 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008967out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008968 return err;
8969}
8970
8971static int
8972nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8973 struct nfs_fsinfo *info)
8974{
8975 int err;
8976 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008977 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008978 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008979 struct nfs4_secinfo4 *secinfo;
8980 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008981
8982 page = alloc_page(GFP_KERNEL);
8983 if (!page) {
8984 err = -ENOMEM;
8985 goto out;
8986 }
8987
8988 flavors = page_address(page);
8989 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8990
8991 /*
8992 * Fall back on "guess and check" method if
8993 * the server doesn't support SECINFO_NO_NAME
8994 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008995 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008996 err = nfs4_find_root_sec(server, fhandle, info);
8997 goto out_freepage;
8998 }
8999 if (err)
9000 goto out_freepage;
9001
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009002 for (i = 0; i < flavors->num_flavors; i++) {
9003 secinfo = &flavors->flavors[i];
9004
9005 switch (secinfo->flavor) {
9006 case RPC_AUTH_NULL:
9007 case RPC_AUTH_UNIX:
9008 case RPC_AUTH_GSS:
9009 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9010 &secinfo->flavor_info);
9011 break;
9012 default:
9013 flavor = RPC_AUTH_MAXFLAVOR;
9014 break;
9015 }
9016
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009017 if (!nfs_auth_info_match(&server->auth_info, flavor))
9018 flavor = RPC_AUTH_MAXFLAVOR;
9019
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009020 if (flavor != RPC_AUTH_MAXFLAVOR) {
9021 err = nfs4_lookup_root_sec(server, fhandle,
9022 info, flavor);
9023 if (!err)
9024 break;
9025 }
9026 }
9027
9028 if (flavor == RPC_AUTH_MAXFLAVOR)
9029 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009030
9031out_freepage:
9032 put_page(page);
9033 if (err == -EACCES)
9034 return -EPERM;
9035out:
9036 return err;
9037}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009038
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009039static int _nfs41_test_stateid(struct nfs_server *server,
9040 nfs4_stateid *stateid,
9041 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009042{
9043 int status;
9044 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009045 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009046 };
9047 struct nfs41_test_stateid_res res;
9048 struct rpc_message msg = {
9049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9050 .rpc_argp = &args,
9051 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009052 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009053 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009054 struct rpc_clnt *rpc_client = server->client;
9055
9056 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9057 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009058
Chuck Lever38527b12012-07-11 16:30:23 -04009059 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009060 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009061 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009062 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009063 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009064 if (status != NFS_OK) {
9065 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009066 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009067 }
9068 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009069 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009070}
9071
Trond Myklebust43912bb2016-09-22 13:38:56 -04009072static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9073 int err, struct nfs4_exception *exception)
9074{
9075 exception->retry = 0;
9076 switch(err) {
9077 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009078 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009079 nfs4_handle_exception(server, err, exception);
9080 break;
9081 case -NFS4ERR_BADSESSION:
9082 case -NFS4ERR_BADSLOT:
9083 case -NFS4ERR_BAD_HIGH_SLOT:
9084 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9085 case -NFS4ERR_DEADSESSION:
9086 nfs4_do_handle_exception(server, err, exception);
9087 }
9088}
9089
Chuck Lever38527b12012-07-11 16:30:23 -04009090/**
9091 * nfs41_test_stateid - perform a TEST_STATEID operation
9092 *
9093 * @server: server / transport on which to perform the operation
9094 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009095 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009096 *
9097 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9098 * Otherwise a negative NFS4ERR value is returned if the operation
9099 * failed or the state ID is not currently valid.
9100 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009101static int nfs41_test_stateid(struct nfs_server *server,
9102 nfs4_stateid *stateid,
9103 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009104{
9105 struct nfs4_exception exception = { };
9106 int err;
9107 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009108 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009109 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009110 } while (exception.retry);
9111 return err;
9112}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009113
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009114struct nfs_free_stateid_data {
9115 struct nfs_server *server;
9116 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009117 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009118};
9119
9120static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9121{
9122 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker172d9de2015-05-14 10:56:10 -04009123 nfs41_setup_sequence(nfs4_get_session(data->server->nfs_client),
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009124 &data->args.seq_args,
9125 &data->res.seq_res,
9126 task);
9127}
9128
9129static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9130{
9131 struct nfs_free_stateid_data *data = calldata;
9132
9133 nfs41_sequence_done(task, &data->res.seq_res);
9134
9135 switch (task->tk_status) {
9136 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009137 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009138 rpc_restart_call_prepare(task);
9139 }
9140}
9141
9142static void nfs41_free_stateid_release(void *calldata)
9143{
9144 kfree(calldata);
9145}
9146
Trond Myklebust17f26b12013-08-21 15:48:42 -04009147static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009148 .rpc_call_prepare = nfs41_free_stateid_prepare,
9149 .rpc_call_done = nfs41_free_stateid_done,
9150 .rpc_release = nfs41_free_stateid_release,
9151};
9152
9153static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009154 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009155 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009156 bool privileged)
9157{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009158 struct rpc_message msg = {
9159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009160 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009161 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009162 struct rpc_task_setup task_setup = {
9163 .rpc_client = server->client,
9164 .rpc_message = &msg,
9165 .callback_ops = &nfs41_free_stateid_ops,
9166 .flags = RPC_TASK_ASYNC,
9167 };
9168 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009169
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009170 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9171 &task_setup.rpc_client, &msg);
9172
Chuck Lever38527b12012-07-11 16:30:23 -04009173 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009174 data = kmalloc(sizeof(*data), GFP_NOFS);
9175 if (!data)
9176 return ERR_PTR(-ENOMEM);
9177 data->server = server;
9178 nfs4_stateid_copy(&data->args.stateid, stateid);
9179
9180 task_setup.callback_data = data;
9181
9182 msg.rpc_argp = &data->args;
9183 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009184 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009185 if (privileged)
9186 nfs4_set_sequence_privileged(&data->args.seq_args);
9187
9188 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009189}
9190
Chuck Lever38527b12012-07-11 16:30:23 -04009191/**
9192 * nfs41_free_stateid - perform a FREE_STATEID operation
9193 *
9194 * @server: server / transport on which to perform the operation
9195 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009196 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009197 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009198 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009199 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009200 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009201static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009202 const nfs4_stateid *stateid,
9203 struct rpc_cred *cred,
9204 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009205{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009206 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009207
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009208 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009209 if (IS_ERR(task))
9210 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009211 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009212 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009213}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009214
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009215static void
9216nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009217{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009218 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009219
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009220 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009221 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009222}
9223
Trond Myklebust36281ca2012-03-04 18:13:56 -05009224static bool nfs41_match_stateid(const nfs4_stateid *s1,
9225 const nfs4_stateid *s2)
9226{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009227 if (s1->type != s2->type)
9228 return false;
9229
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009230 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009231 return false;
9232
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009233 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009234 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009235 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009236 return true;
9237
9238 return false;
9239}
9240
Andy Adamson557134a2009-04-01 09:21:53 -04009241#endif /* CONFIG_NFS_V4_1 */
9242
Trond Myklebust36281ca2012-03-04 18:13:56 -05009243static bool nfs4_match_stateid(const nfs4_stateid *s1,
9244 const nfs4_stateid *s2)
9245{
Trond Myklebustf597c532012-03-04 18:13:56 -05009246 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009247}
9248
9249
Trond Myklebust17280172012-03-11 13:11:00 -04009250static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009251 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009252 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 .recover_open = nfs4_open_reclaim,
9254 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009255 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009256 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257};
9258
Andy Adamson591d71c2009-04-01 09:22:47 -04009259#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009260static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009261 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9262 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9263 .recover_open = nfs4_open_reclaim,
9264 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009265 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009266 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009267 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009268};
9269#endif /* CONFIG_NFS_V4_1 */
9270
Trond Myklebust17280172012-03-11 13:11:00 -04009271static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009272 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009273 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009274 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009276 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277};
9278
Andy Adamson591d71c2009-04-01 09:22:47 -04009279#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009280static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009281 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9282 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009283 .recover_open = nfs41_open_expired,
9284 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009285 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009286};
9287#endif /* CONFIG_NFS_V4_1 */
9288
Trond Myklebust17280172012-03-11 13:11:00 -04009289static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009290 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009291 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009292 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009293};
9294
9295#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009296static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009297 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009298 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009299 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009300};
9301#endif
9302
Chuck Leverec011fe2013-10-17 14:12:39 -04009303static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009304 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009305 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009306};
9307
9308#if defined(CONFIG_NFS_V4_1)
9309static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009310 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009311 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009312};
9313#endif /* CONFIG_NFS_V4_1 */
9314
Trond Myklebust97dc1352010-06-16 09:52:26 -04009315static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9316 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009317 .init_caps = NFS_CAP_READDIRPLUS
9318 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009319 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009320 .init_client = nfs40_init_client,
9321 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009322 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009323 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009324 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009325 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009326 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009327 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009328 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9329 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9330 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009331 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009332};
9333
9334#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009335static struct nfs_seqid *
9336nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9337{
9338 return NULL;
9339}
9340
Trond Myklebust97dc1352010-06-16 09:52:26 -04009341static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9342 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009343 .init_caps = NFS_CAP_READDIRPLUS
9344 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009345 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009346 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009347 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009348 .init_client = nfs41_init_client,
9349 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009350 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009351 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009352 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009353 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009354 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009355 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009356 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009357 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9358 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9359 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009360 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009361};
9362#endif
9363
Steve Dickson42c2c422013-05-22 12:50:38 -04009364#if defined(CONFIG_NFS_V4_2)
9365static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9366 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009367 .init_caps = NFS_CAP_READDIRPLUS
9368 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009369 | NFS_CAP_POSIX_LOCK
9370 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009371 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009372 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009373 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009374 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009375 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009376 | NFS_CAP_LAYOUTSTATS
9377 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009378 .init_client = nfs41_init_client,
9379 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009380 .match_stateid = nfs41_match_stateid,
9381 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009382 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009383 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009384 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009385 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009386 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009387 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9388 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9389 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009390 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009391};
9392#endif
9393
Trond Myklebust97dc1352010-06-16 09:52:26 -04009394const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9395 [0] = &nfs_v4_0_minor_ops,
9396#if defined(CONFIG_NFS_V4_1)
9397 [1] = &nfs_v4_1_minor_ops,
9398#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009399#if defined(CONFIG_NFS_V4_2)
9400 [2] = &nfs_v4_2_minor_ops,
9401#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009402};
9403
Trond Myklebust13997822016-07-24 17:10:52 -04009404static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009405{
9406 ssize_t error, error2;
9407
9408 error = generic_listxattr(dentry, list, size);
9409 if (error < 0)
9410 return error;
9411 if (list) {
9412 list += error;
9413 size -= error;
9414 }
9415
9416 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9417 if (error2 < 0)
9418 return error2;
9419 return error + error2;
9420}
9421
Trond Myklebust17f26b12013-08-21 15:48:42 -04009422static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009423 .create = nfs_create,
9424 .lookup = nfs_lookup,
9425 .atomic_open = nfs_atomic_open,
9426 .link = nfs_link,
9427 .unlink = nfs_unlink,
9428 .symlink = nfs_symlink,
9429 .mkdir = nfs_mkdir,
9430 .rmdir = nfs_rmdir,
9431 .mknod = nfs_mknod,
9432 .rename = nfs_rename,
9433 .permission = nfs_permission,
9434 .getattr = nfs_getattr,
9435 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009436 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009437};
9438
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009439static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009440 .permission = nfs_permission,
9441 .getattr = nfs_getattr,
9442 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009443 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009444};
9445
David Howells509de812006-08-22 20:06:11 -04009446const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 .version = 4, /* protocol version */
9448 .dentry_ops = &nfs4_dentry_operations,
9449 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009450 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009451 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009453 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009454 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 .getattr = nfs4_proc_getattr,
9456 .setattr = nfs4_proc_setattr,
9457 .lookup = nfs4_proc_lookup,
9458 .access = nfs4_proc_access,
9459 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460 .create = nfs4_proc_create,
9461 .remove = nfs4_proc_remove,
9462 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009463 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009465 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009466 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009467 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468 .link = nfs4_proc_link,
9469 .symlink = nfs4_proc_symlink,
9470 .mkdir = nfs4_proc_mkdir,
9471 .rmdir = nfs4_proc_remove,
9472 .readdir = nfs4_proc_readdir,
9473 .mknod = nfs4_proc_mknod,
9474 .statfs = nfs4_proc_statfs,
9475 .fsinfo = nfs4_proc_fsinfo,
9476 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009477 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009479 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009481 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009483 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009485 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009486 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009488 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009489 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009490 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009491 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009492 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009493 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009494 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009495 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009496 .create_server = nfs4_create_server,
9497 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498};
9499
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009500static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009501 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009502 .list = nfs4_xattr_list_nfs4_acl,
9503 .get = nfs4_xattr_get_nfs4_acl,
9504 .set = nfs4_xattr_set_nfs4_acl,
9505};
9506
9507const struct xattr_handler *nfs4_xattr_handlers[] = {
9508 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009509#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9510 &nfs4_xattr_nfs4_label_handler,
9511#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009512 NULL
9513};
9514
Linus Torvalds1da177e2005-04-16 15:20:36 -07009515/*
9516 * Local variables:
9517 * c-basic-offset: 8
9518 * End:
9519 */