blob: b704e08b390c4af2c0f43d03e0b65c94d3a38ba9 [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 Myklebust3be0f80b2017-10-19 15:46:45 -040099static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
100 struct rpc_cred *cred,
101 struct nfs4_slot *slot,
102 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400103static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
104 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400105static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
106 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400107#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400108
109#ifdef CONFIG_NFS_V4_SECURITY_LABEL
110static inline struct nfs4_label *
111nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
112 struct iattr *sattr, struct nfs4_label *label)
113{
114 int err;
115
116 if (label == NULL)
117 return NULL;
118
119 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
120 return NULL;
121
David Quigleyaa9c2662013-05-22 12:50:44 -0400122 err = security_dentry_init_security(dentry, sattr->ia_mode,
123 &dentry->d_name, (void **)&label->label, &label->len);
124 if (err == 0)
125 return label;
126
127 return NULL;
128}
129static inline void
130nfs4_label_release_security(struct nfs4_label *label)
131{
132 if (label)
133 security_release_secctx(label->label, label->len);
134}
135static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
136{
137 if (label)
138 return server->attr_bitmask;
139
140 return server->attr_bitmask_nl;
141}
142#else
143static inline struct nfs4_label *
144nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
145 struct iattr *sattr, struct nfs4_label *l)
146{ return NULL; }
147static inline void
148nfs4_label_release_security(struct nfs4_label *label)
149{ return; }
150static inline u32 *
151nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
152{ return server->attr_bitmask; }
153#endif
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500156static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Trond Myklebust52567b02009-10-23 14:46:42 -0400158 if (err >= -1000)
159 return err;
160 switch (err) {
161 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500162 case -NFS4ERR_LAYOUTTRYLATER:
163 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000165 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400166 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800168 case -NFS4ERR_BADOWNER:
169 case -NFS4ERR_BADNAME:
170 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400171 case -NFS4ERR_SHARE_DENIED:
172 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400173 case -NFS4ERR_MINOR_VERS_MISMATCH:
174 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400175 case -NFS4ERR_FILE_OPEN:
176 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700179 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * This is our standard bitmap for GETATTR requests.
187 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400188const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID,
194 FATTR4_WORD1_MODE
195 | FATTR4_WORD1_NUMLINKS
196 | FATTR4_WORD1_OWNER
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400204#ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Trond Myklebust1549210f2012-06-05 09:16:47 -0400209static const u32 nfs4_pnfs_open_bitmap[3] = {
210 FATTR4_WORD0_TYPE
211 | FATTR4_WORD0_CHANGE
212 | FATTR4_WORD0_SIZE
213 | FATTR4_WORD0_FSID
214 | FATTR4_WORD0_FILEID,
215 FATTR4_WORD1_MODE
216 | FATTR4_WORD1_NUMLINKS
217 | FATTR4_WORD1_OWNER
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY,
224 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500225#ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
227#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400228};
229
Andy Adamsone23008e2012-10-02 21:07:32 -0400230static const u32 nfs4_open_noattr_bitmap[3] = {
231 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400232 | FATTR4_WORD0_FILEID,
233};
234
David Quigleya09df2c2013-05-22 12:50:41 -0400235const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_MAXLINK
246 | FATTR4_WORD0_MAXNAME,
247 0
248};
249
Fred Isamandae100c2011-07-30 20:52:37 -0400250const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700254 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400255 | FATTR4_WORD1_FS_LAYOUT_TYPES,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800257 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
David Quigleya09df2c2013-05-22 12:50:41 -0400260const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500261 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400262 | FATTR4_WORD0_SIZE
263 | FATTR4_WORD0_FSID
264 | FATTR4_WORD0_FILEID
265 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500266 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400274};
275
Al Virobc4785c2006-10-19 23:28:51 -0700276static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nfs4_readdir_arg *readdir)
278{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400279 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400310 *p++ = htonl(attrs); /* bitmap */
311 *p++ = htonl(12); /* attribute buffer length */
312 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000313 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 *p++ = xdr_one; /* next */
317 *p++ = xdr_zero; /* cookie, first word */
318 *p++ = xdr_two; /* cookie, second word */
319 *p++ = xdr_two; /* entry len */
320 memcpy(p, "..\0\0", 4); /* entry */
321 p++;
322 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400323 *p++ = htonl(attrs); /* bitmap */
324 *p++ = htonl(12); /* attribute buffer length */
325 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000326 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 readdir->pgbase = (char *)p - (char *)start;
329 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800330 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Trond Myklebust26d36302016-09-22 13:39:05 -0400333static void nfs4_test_and_free_stateid(struct nfs_server *server,
334 nfs4_stateid *stateid,
335 struct rpc_cred *cred)
336{
337 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
338
339 ops->test_and_free_expired(server, stateid, cred);
340}
341
342static void __nfs4_free_revoked_stateid(struct nfs_server *server,
343 nfs4_stateid *stateid,
344 struct rpc_cred *cred)
345{
346 stateid->type = NFS4_REVOKED_STATEID_TYPE;
347 nfs4_test_and_free_stateid(server, stateid, cred);
348}
349
350static void nfs4_free_revoked_stateid(struct nfs_server *server,
351 const nfs4_stateid *stateid,
352 struct rpc_cred *cred)
353{
354 nfs4_stateid tmp;
355
356 nfs4_stateid_copy(&tmp, stateid);
357 __nfs4_free_revoked_stateid(server, &tmp, cred);
358}
359
NeilBrown8478eaa2014-09-18 16:09:27 +1000360static long nfs4_update_delay(long *timeout)
361{
362 long ret;
363 if (!timeout)
364 return NFS4_POLL_RETRY_MAX;
365 if (*timeout <= 0)
366 *timeout = NFS4_POLL_RETRY_MIN;
367 if (*timeout > NFS4_POLL_RETRY_MAX)
368 *timeout = NFS4_POLL_RETRY_MAX;
369 ret = *timeout;
370 *timeout <<= 1;
371 return ret;
372}
373
Trond Myklebust65de8722008-12-23 15:21:44 -0500374static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
375{
376 int res = 0;
377
378 might_sleep();
379
NeilBrown8478eaa2014-09-18 16:09:27 +1000380 freezable_schedule_timeout_killable_unsafe(
381 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 if (fatal_signal_pending(current))
383 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500384 return res;
385}
386
387/* This is the error handling routine for processes that are allowed
388 * to sleep.
389 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400390static int nfs4_do_handle_exception(struct nfs_server *server,
391 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500392{
393 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500394 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400395 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500396 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 int ret = errorcode;
398
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400399 exception->delay = 0;
400 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500401 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400402
403 if (stateid == NULL && state != NULL)
404 stateid = &state->stateid;
405
Trond Myklebust65de8722008-12-23 15:21:44 -0500406 switch(errorcode) {
407 case 0:
408 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400409 case -NFS4ERR_DELEG_REVOKED:
410 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400412 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400413 if (inode != NULL && stateid != NULL) {
414 nfs_inode_find_state_and_recover(inode,
415 stateid);
416 goto wait_on_recovery;
417 }
418 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400419 if (inode) {
420 int err;
421
422 err = nfs_async_inode_return_delegation(inode,
423 stateid);
424 if (err == 0)
425 goto wait_on_recovery;
426 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
427 exception->retry = 1;
428 break;
429 }
430 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500431 if (state == NULL)
432 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400433 ret = nfs4_schedule_stateid_recovery(server, state);
434 if (ret < 0)
435 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500436 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500439 nfs4_schedule_lease_recovery(clp);
440 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400441 case -NFS4ERR_MOVED:
442 ret = nfs4_schedule_migration_recovery(server);
443 if (ret < 0)
444 break;
445 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400446 case -NFS4ERR_LEASE_MOVED:
447 nfs4_schedule_lease_moved_recovery(clp);
448 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500449#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400450 case -NFS4ERR_BADSESSION:
451 case -NFS4ERR_BADSLOT:
452 case -NFS4ERR_BAD_HIGH_SLOT:
453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
454 case -NFS4ERR_DEADSESSION:
455 case -NFS4ERR_SEQ_FALSE_RETRY:
456 case -NFS4ERR_SEQ_MISORDERED:
457 dprintk("%s ERROR: %d Reset session\n", __func__,
458 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400459 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400460 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500461#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500462 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500463 if (exception->timeout > HZ) {
464 /* We have retried a decent amount, time to
465 * fail
466 */
467 ret = -EBUSY;
468 break;
469 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500470 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400471 nfs_inc_server_stats(server, NFSIOS_DELAY);
472 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400473 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400474 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400475 exception->delay = 1;
476 return 0;
477
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400478 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500479 case -NFS4ERR_OLD_STATEID:
480 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800481 break;
482 case -NFS4ERR_BADOWNER:
483 /* The following works around a Linux server bug! */
484 case -NFS4ERR_BADNAME:
485 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
486 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
487 exception->retry = 1;
488 printk(KERN_WARNING "NFS: v4 server %s "
489 "does not accept raw "
490 "uid/gids. "
491 "Reenabling the idmapper.\n",
492 server->nfs_client->cl_hostname);
493 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500494 }
495 /* We failed to handle the error */
496 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500497wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400498 exception->recovering = 1;
499 return 0;
500}
501
502/* This is the error handling routine for processes that are allowed
503 * to sleep.
504 */
505int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
506{
507 struct nfs_client *clp = server->nfs_client;
508 int ret;
509
510 ret = nfs4_do_handle_exception(server, errorcode, exception);
511 if (exception->delay) {
512 ret = nfs4_delay(server->client, &exception->timeout);
513 goto out_retry;
514 }
515 if (exception->recovering) {
516 ret = nfs4_wait_clnt_recover(clp);
517 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
518 return -EIO;
519 goto out_retry;
520 }
521 return ret;
522out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500523 if (ret == 0)
524 exception->retry = 1;
525 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500526}
527
Trond Myklebust037fc982015-09-20 15:51:00 -0400528static int
529nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
530 int errorcode, struct nfs4_exception *exception)
531{
532 struct nfs_client *clp = server->nfs_client;
533 int ret;
534
535 ret = nfs4_do_handle_exception(server, errorcode, exception);
536 if (exception->delay) {
537 rpc_delay(task, nfs4_update_delay(&exception->timeout));
538 goto out_retry;
539 }
540 if (exception->recovering) {
541 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
542 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
543 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
544 goto out_retry;
545 }
546 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
547 ret = -EIO;
548 return ret;
549out_retry:
550 if (ret == 0)
551 exception->retry = 1;
552 return ret;
553}
554
555static int
556nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
557 struct nfs4_state *state, long *timeout)
558{
559 struct nfs4_exception exception = {
560 .state = state,
561 };
562
563 if (task->tk_status >= 0)
564 return 0;
565 if (timeout)
566 exception.timeout = *timeout;
567 task->tk_status = nfs4_async_handle_exception(task, server,
568 task->tk_status,
569 &exception);
570 if (exception.delay && timeout)
571 *timeout = exception.timeout;
572 if (exception.retry)
573 return -EAGAIN;
574 return 0;
575}
576
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400577/*
578 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
579 * or 'false' otherwise.
580 */
581static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
582{
583 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500584 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400585}
Trond Myklebust65de8722008-12-23 15:21:44 -0500586
Trond Myklebust452e9352010-07-31 14:29:06 -0400587static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 spin_lock(&clp->cl_lock);
590 if (time_before(clp->cl_last_renewal,timestamp))
591 clp->cl_last_renewal = timestamp;
592 spin_unlock(&clp->cl_lock);
593}
594
Trond Myklebust452e9352010-07-31 14:29:06 -0400595static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
596{
Trond Myklebustbe824162015-07-05 14:50:46 -0400597 struct nfs_client *clp = server->nfs_client;
598
599 if (!nfs4_has_session(clp))
600 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400601}
602
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400603struct nfs4_call_sync_data {
604 const struct nfs_server *seq_server;
605 struct nfs4_sequence_args *seq_args;
606 struct nfs4_sequence_res *seq_res;
607};
608
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800609void nfs4_init_sequence(struct nfs4_sequence_args *args,
610 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400611{
612 args->sa_slot = NULL;
613 args->sa_cache_this = cache_reply;
614 args->sa_privileged = 0;
615
616 res->sr_slot = NULL;
617}
618
619static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
620{
621 args->sa_privileged = 1;
622}
623
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400624static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400625{
626 struct nfs4_slot *slot = res->sr_slot;
627 struct nfs4_slot_table *tbl;
628
Chuck Lever3bd23842013-08-09 12:49:19 -0400629 tbl = slot->table;
630 spin_lock(&tbl->slot_tbl_lock);
631 if (!nfs41_wake_and_assign_slot(tbl, slot))
632 nfs4_free_slot(tbl, slot);
633 spin_unlock(&tbl->slot_tbl_lock);
634
635 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400636}
637
638static int nfs40_sequence_done(struct rpc_task *task,
639 struct nfs4_sequence_res *res)
640{
641 if (res->sr_slot != NULL)
642 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 return 1;
644}
645
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400646#if defined(CONFIG_NFS_V4_1)
647
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400648static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400649{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500650 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400651 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500652 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400653
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400654 if (!slot)
655 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500656 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500657 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500658
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400659 /* Bump the slot sequence number */
660 if (slot->seq_done)
661 slot->seq_nr++;
662 slot->seq_done = 0;
663
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500664 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500665 /* Be nice to the server: try to ensure that the last transmitted
666 * value for highest_user_slotid <= target_highest_slotid
667 */
668 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
669 send_new_highest_used_slotid = true;
670
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500671 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500672 send_new_highest_used_slotid = false;
673 goto out_unlock;
674 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500675 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500676
677 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
678 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500679out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500680 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500681 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400682 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400683 if (waitqueue_active(&tbl->slot_waitq))
684 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400685}
686
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400687static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
688{
689 nfs41_release_slot(res->sr_slot);
690 res->sr_slot = NULL;
691}
692
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400693static int nfs41_sequence_process(struct rpc_task *task,
694 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400695{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500696 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500699 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500700 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400701
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500702 if (slot == NULL)
703 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400704 /* don't increment the sequence number if the task wasn't sent */
705 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400706 goto out;
707
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500708 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500709
Trond Myklebustac20d162012-12-15 15:36:07 -0500710 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400711 if (res->sr_status != -NFS4ERR_DELAY)
712 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500713 interrupted = true;
714 }
715
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400716 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500717 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400718 switch (res->sr_status) {
719 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400720 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400721 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500723 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500724 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
726 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500727 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 case 1:
730 /*
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
733 * operation..
734 * Mark the slot as having hosted an interrupted RPC call.
735 */
736 slot->interrupted = 1;
737 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 case -NFS4ERR_DELAY:
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
741 * of RFC5661.
742 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500746 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400747 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500748 case -NFS4ERR_BADSLOT:
749 /*
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
752 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400753 if (slot->seq_nr < slot->table->target_highest_slotid)
754 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500755 goto retry_nowait;
756 case -NFS4ERR_SEQ_MISORDERED:
757 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500758 * Was the last operation on this sequence interrupted?
759 * If so, retry after bumping the sequence number.
760 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400761 if (interrupted)
762 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
766 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 if (slot->seq_nr != 1) {
768 slot->seq_nr = 1;
769 goto retry_nowait;
770 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400771 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500772 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400773 if (interrupted)
774 goto retry_new_seq;
775 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 default:
777 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400778 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779 }
780out:
781 /* The session may be reset by one of the error handlers. */
782 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500783out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500784 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400785session_recover:
786 nfs4_schedule_session_recovery(session, res->sr_status);
787 goto retry_nowait;
788retry_new_seq:
789 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500790retry_nowait:
791 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400792 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500793 task->tk_status = 0;
794 ret = 0;
795 }
796 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400797out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400798 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400799 goto out;
800 rpc_delay(task, NFS4_POLL_RETRY_MAX);
801 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400802}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400803
804int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
805{
806 if (!nfs41_sequence_process(task, res))
807 return 0;
808 if (res->sr_slot != NULL)
809 nfs41_sequence_free_slot(res);
810 return 1;
811
812}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500813EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400814
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400815static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
816{
817 if (res->sr_slot == NULL)
818 return 1;
819 if (res->sr_slot->table->session != NULL)
820 return nfs41_sequence_process(task, res);
821 return nfs40_sequence_done(task, res);
822}
823
824static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
825{
826 if (res->sr_slot != NULL) {
827 if (res->sr_slot->table->session != NULL)
828 nfs41_sequence_free_slot(res);
829 else
830 nfs40_sequence_free_slot(res);
831 }
832}
833
Peng Tao2c4b1312014-06-11 05:24:16 +0800834int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400835{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500836 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400837 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400838 if (!res->sr_slot->table->session)
839 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400841}
Peng Tao2c4b1312014-06-11 05:24:16 +0800842EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400843
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
845{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400846 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400847
Trond Myklebust035168ab2010-06-16 09:52:26 -0400848 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
849
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500850 nfs4_setup_sequence(data->seq_server->nfs_client,
851 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852}
853
Andy Adamson69ab40c2009-04-01 09:22:19 -0400854static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
855{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400856 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857
Trond Myklebust14516c32010-07-31 14:29:06 -0400858 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400859}
860
Trond Myklebust17280172012-03-11 13:11:00 -0400861static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400862 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400863 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400864};
865
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400866static void
867nfs4_sequence_process_interrupted(struct nfs_client *client,
868 struct nfs4_slot *slot, struct rpc_cred *cred)
869{
870 struct rpc_task *task;
871
872 task = _nfs41_proc_sequence(client, cred, slot, true);
873 if (!IS_ERR(task))
874 rpc_put_task_async(task);
875}
876
Chuck Lever3bd23842013-08-09 12:49:19 -0400877#else /* !CONFIG_NFS_V4_1 */
878
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400879static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
880{
881 return nfs40_sequence_done(task, res);
882}
883
884static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
885{
886 if (res->sr_slot != NULL)
887 nfs40_sequence_free_slot(res);
888}
889
Peng Tao2c4b1312014-06-11 05:24:16 +0800890int nfs4_sequence_done(struct rpc_task *task,
891 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400892{
Chuck Lever3bd23842013-08-09 12:49:19 -0400893 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400894}
Peng Tao2c4b1312014-06-11 05:24:16 +0800895EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400896
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400897static void
898nfs4_sequence_process_interrupted(struct nfs_client *client,
899 struct nfs4_slot *slot, struct rpc_cred *cred)
900{
901 WARN_ON_ONCE(1);
902 slot->interrupted = 0;
903}
904
Chuck Lever3bd23842013-08-09 12:49:19 -0400905#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400906
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400907static
908void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
909 struct nfs4_sequence_res *res,
910 struct nfs4_slot *slot)
911{
912 if (!slot)
913 return;
914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 res->sr_timestamp = jiffies;
919 res->sr_status_flags = 0;
920 res->sr_status = 1;
921
922}
923
924int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500925 struct nfs4_sequence_args *args,
926 struct nfs4_sequence_res *res,
927 struct rpc_task *task)
928{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500929 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500930 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500931 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500932
Anna Schumaker9dd91072017-01-10 12:01:46 -0500933 /* slot already allocated? */
934 if (res->sr_slot != NULL)
935 goto out_start;
936
Anna Schumaker76ee0352017-01-10 16:49:31 -0500937 if (session) {
938 tbl = &session->fc_slot_table;
939 task->tk_timeout = 0;
940 }
941
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400942 for (;;) {
943 spin_lock(&tbl->slot_tbl_lock);
944 /* The state manager will wait until the slot table is empty */
945 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
946 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500947
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400948 slot = nfs4_alloc_slot(tbl);
949 if (IS_ERR(slot)) {
950 /* Try again in 1/4 second */
951 if (slot == ERR_PTR(-ENOMEM))
952 task->tk_timeout = HZ >> 2;
953 goto out_sleep;
954 }
955 spin_unlock(&tbl->slot_tbl_lock);
956
957 if (likely(!slot->interrupted))
958 break;
959 nfs4_sequence_process_interrupted(client,
960 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500961 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500962
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400963 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500964
Anna Schumakerad05cc02017-01-11 13:37:06 -0500965 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500966out_start:
967 rpc_call_start(task);
968 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500969
970out_sleep:
971 if (args->sa_privileged)
972 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
973 NULL, RPC_PRIORITY_PRIVILEGED);
974 else
975 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
976 spin_unlock(&tbl->slot_tbl_lock);
977 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978}
979EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
980
Chuck Lever9915ea72013-08-09 12:48:27 -0400981static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
982{
983 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500984 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400985 data->seq_args, data->seq_res, task);
986}
987
988static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
989{
990 struct nfs4_call_sync_data *data = calldata;
991 nfs4_sequence_done(task, data->seq_res);
992}
993
994static const struct rpc_call_ops nfs40_call_sync_ops = {
995 .rpc_call_prepare = nfs40_call_sync_prepare,
996 .rpc_call_done = nfs40_call_sync_done,
997};
998
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400999static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001000 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001004{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 int ret;
1006 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001007 struct nfs_client *clp = server->nfs_client;
1008 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 .seq_server = server,
1010 .seq_args = args,
1011 .seq_res = res,
1012 };
1013 struct rpc_task_setup task_setup = {
1014 .rpc_client = clnt,
1015 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001016 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001017 .callback_data = &data
1018 };
1019
1020 task = rpc_run_task(&task_setup);
1021 if (IS_ERR(task))
1022 ret = PTR_ERR(task);
1023 else {
1024 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001025 rpc_put_task(task);
1026 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001027 return ret;
1028}
1029
Bryan Schumaker7c513052011-03-24 17:12:24 +00001030int nfs4_call_sync(struct rpc_clnt *clnt,
1031 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001032 struct rpc_message *msg,
1033 struct nfs4_sequence_args *args,
1034 struct nfs4_sequence_res *res,
1035 int cache_reply)
1036{
Chuck Levera9c92d62013-08-09 12:48:18 -04001037 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001038 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001041static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1042 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct nfs_inode *nfsi = NFS_I(dir);
1045
1046 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001047 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001048 if (cinfo->atomic && cinfo->before == dir->i_version) {
1049 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1050 nfsi->attrtimeo_timestamp = jiffies;
1051 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001053 if (cinfo->before != dir->i_version)
1054 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1055 NFS_INO_INVALID_ACL;
1056 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001057 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001058 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001059 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001060 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_unlock(&dir->i_lock);
1062}
1063
1064struct nfs4_opendata {
1065 struct kref kref;
1066 struct nfs_openargs o_arg;
1067 struct nfs_openres o_res;
1068 struct nfs_open_confirmargs c_arg;
1069 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001070 struct nfs4_string owner_name;
1071 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001072 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001074 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001076 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct nfs4_state_owner *owner;
1078 struct nfs4_state *state;
1079 struct iattr attrs;
1080 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001081 bool rpc_done;
1082 bool file_created;
1083 bool is_recover;
1084 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001085 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001087
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001088struct nfs4_open_createattrs {
1089 struct nfs4_label *label;
1090 struct iattr *sattr;
1091 const __u32 verf[2];
1092};
1093
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001094static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1095 int err, struct nfs4_exception *exception)
1096{
1097 if (err != -EINVAL)
1098 return false;
1099 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1100 return false;
1101 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1102 exception->retry = 1;
1103 return true;
1104}
1105
Trond Myklebust6ae37332015-01-30 14:21:14 -05001106static u32
1107nfs4_map_atomic_open_share(struct nfs_server *server,
1108 fmode_t fmode, int openflags)
1109{
1110 u32 res = 0;
1111
1112 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1113 case FMODE_READ:
1114 res = NFS4_SHARE_ACCESS_READ;
1115 break;
1116 case FMODE_WRITE:
1117 res = NFS4_SHARE_ACCESS_WRITE;
1118 break;
1119 case FMODE_READ|FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_BOTH;
1121 }
1122 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1123 goto out;
1124 /* Want no delegation if we're using O_DIRECT */
1125 if (openflags & O_DIRECT)
1126 res |= NFS4_SHARE_WANT_NO_DELEG;
1127out:
1128 return res;
1129}
1130
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001131static enum open_claim_type4
1132nfs4_map_atomic_open_claim(struct nfs_server *server,
1133 enum open_claim_type4 claim)
1134{
1135 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1136 return claim;
1137 switch (claim) {
1138 default:
1139 return claim;
1140 case NFS4_OPEN_CLAIM_FH:
1141 return NFS4_OPEN_CLAIM_NULL;
1142 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1143 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1144 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1145 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1146 }
1147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150{
1151 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001152 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153 p->o_res.seqid = p->o_arg.seqid;
1154 p->c_res.seqid = p->c_arg.seqid;
1155 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001156 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001157 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001158 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159}
1160
Al Viro82a2c1b2011-06-22 18:30:55 -04001161static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001163 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001164 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001165 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166{
Al Viro82a2c1b2011-06-22 18:30:55 -04001167 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001168 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001170 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001171 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs4_opendata *p;
1173
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001174 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 if (p == NULL)
1176 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001177
1178 p->f_label = nfs4_label_alloc(server, gfp_mask);
1179 if (IS_ERR(p->f_label))
1180 goto err_free_p;
1181
Kinglong Meea49c2692015-07-27 15:31:38 +08001182 p->a_label = nfs4_label_alloc(server, gfp_mask);
1183 if (IS_ERR(p->a_label))
1184 goto err_free_f;
1185
Trond Myklebust63f5f792015-01-23 19:19:25 -05001186 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1187 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001188 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001189 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001190 nfs_sb_active(dentry->d_sb);
1191 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 p->dir = parent;
1193 p->owner = sp;
1194 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 p->o_arg.open_flags = flags;
1196 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001197 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001198 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001199 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1200 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001201 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1202 * will return permission denied for all bits until close */
1203 if (!(flags & O_EXCL)) {
1204 /* ask server to check for all possible rights as results
1205 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001206 switch (p->o_arg.claim) {
1207 default:
1208 break;
1209 case NFS4_OPEN_CLAIM_NULL:
1210 case NFS4_OPEN_CLAIM_FH:
1211 p->o_arg.access = NFS4_ACCESS_READ |
1212 NFS4_ACCESS_MODIFY |
1213 NFS4_ACCESS_EXTEND |
1214 NFS4_ACCESS_EXECUTE;
1215 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001216 }
David Howells7539bba2006-08-22 20:06:09 -04001217 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001218 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1219 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001222 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001223 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001224 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001225 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001226 case NFS4_OPEN_CLAIM_NULL:
1227 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1228 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1229 p->o_arg.fh = NFS_FH(dir);
1230 break;
1231 case NFS4_OPEN_CLAIM_PREVIOUS:
1232 case NFS4_OPEN_CLAIM_FH:
1233 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001235 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001236 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001237 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001238 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001239 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001240
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001241 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001242 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001243 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001244 p->c_arg.fh = &p->o_res.fh;
1245 p->c_arg.stateid = &p->o_res.stateid;
1246 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001250
1251err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001252 nfs4_label_free(p->a_label);
1253err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001254 nfs4_label_free(p->f_label);
1255err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001256 kfree(p);
1257err:
1258 dput(parent);
1259 return NULL;
1260}
1261
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001263{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001264 struct nfs4_opendata *p = container_of(kref,
1265 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001266 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267
1268 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001269 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001270 if (p->state != NULL)
1271 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001273
Kinglong Meea49c2692015-07-27 15:31:38 +08001274 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001275 nfs4_label_free(p->f_label);
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001278 dput(p->dentry);
1279 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001280 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001281 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282 kfree(p);
1283}
1284
1285static void nfs4_opendata_put(struct nfs4_opendata *p)
1286{
1287 if (p != NULL)
1288 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289}
1290
Trond Myklebust24311f82015-09-20 10:50:17 -04001291static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1292 fmode_t fmode)
1293{
1294 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1295 case FMODE_READ|FMODE_WRITE:
1296 return state->n_rdwr != 0;
1297 case FMODE_WRITE:
1298 return state->n_wronly != 0;
1299 case FMODE_READ:
1300 return state->n_rdonly != 0;
1301 }
1302 WARN_ON_ONCE(1);
1303 return false;
1304}
1305
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001307{
1308 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309
Trond Myklebust536e43d2012-01-17 22:04:26 -05001310 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 goto out;
1312 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001314 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1315 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 break;
1317 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001318 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1319 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001320 break;
1321 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001322 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1323 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001325out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001326 return ret;
1327}
1328
Trond Myklebust2a606182015-08-19 22:30:00 -05001329static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1330 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001331{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001332 if (delegation == NULL)
1333 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001336 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1337 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001338 switch (claim) {
1339 case NFS4_OPEN_CLAIM_NULL:
1340 case NFS4_OPEN_CLAIM_FH:
1341 break;
1342 case NFS4_OPEN_CLAIM_PREVIOUS:
1343 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1344 break;
1345 default:
1346 return 0;
1347 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001348 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001349 return 1;
1350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001353{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001355 case FMODE_WRITE:
1356 state->n_wronly++;
1357 break;
1358 case FMODE_READ:
1359 state->n_rdonly++;
1360 break;
1361 case FMODE_READ|FMODE_WRITE:
1362 state->n_rdwr++;
1363 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001365}
1366
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001367#ifdef CONFIG_NFS_V4_1
1368static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1369{
1370 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1371 return true;
1372 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1373 return true;
1374 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1375 return true;
1376 return false;
1377}
1378#endif /* CONFIG_NFS_V4_1 */
1379
Trond Myklebustc9399f22017-11-06 15:28:01 -05001380static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1381{
1382 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1383 wake_up_all(&state->waitq);
1384}
1385
1386static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1387 const nfs4_stateid *stateid)
1388{
1389 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1390 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1391
1392 if (stateid_seqid == state_seqid + 1U ||
1393 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1394 nfs_state_log_update_open_stateid(state);
1395 else
1396 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1397}
1398
Trond Myklebust4f14c192014-02-12 19:15:06 -05001399static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001400{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001401 struct nfs_client *clp = state->owner->so_server->nfs_client;
1402 bool need_recover = false;
1403
1404 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1405 need_recover = true;
1406 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1407 need_recover = true;
1408 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1409 need_recover = true;
1410 if (need_recover)
1411 nfs4_state_mark_reclaim_nograce(clp, state);
1412}
1413
Trond Myklebustc9399f22017-11-06 15:28:01 -05001414/*
1415 * Check for whether or not the caller may update the open stateid
1416 * to the value passed in by stateid.
1417 *
1418 * Note: This function relies heavily on the server implementing
1419 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1420 * correctly.
1421 * i.e. The stateid seqids have to be initialised to 1, and
1422 * are then incremented on every state transition.
1423 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001424static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001425 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001426{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001427 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1428 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1429 if (stateid->seqid == cpu_to_be32(1))
1430 nfs_state_log_update_open_stateid(state);
1431 else
1432 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001434 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001435
1436 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1437 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001439 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001440 return false;
1441}
1442
Trond Myklebustf95549c2015-01-23 18:06:09 -05001443static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1444{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001445 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1446 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447 if (state->n_wronly)
1448 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1449 if (state->n_rdonly)
1450 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1451 if (state->n_rdwr)
1452 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001454}
1455
Trond Myklebust226056c2014-02-11 10:41:07 -05001456static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1457 nfs4_stateid *stateid, fmode_t fmode)
1458{
1459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1460 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1461 case FMODE_WRITE:
1462 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 break;
1464 case FMODE_READ:
1465 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 break;
1467 case 0:
1468 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 clear_bit(NFS_OPEN_STATE, &state->flags);
1471 }
1472 if (stateid == NULL)
1473 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001474 /* Handle OPEN+OPEN_DOWNGRADE races */
1475 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1476 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001477 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001478 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 nfs4_stateid_copy(&state->stateid, stateid);
1482 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001483 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001484out:
1485 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486}
1487
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001488static void nfs_clear_open_stateid(struct nfs4_state *state,
1489 nfs4_stateid *arg_stateid,
1490 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001491{
1492 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001493 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1494 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1495 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001496 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001497 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1498 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499}
1500
Trond Myklebust1393d962016-09-22 13:39:13 -04001501static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001502 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001503{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001504 DEFINE_WAIT(wait);
1505 int status = 0;
1506 for (;;) {
1507
1508 if (!nfs_need_update_open_stateid(state, stateid))
1509 return;
1510 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001511 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001512 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001513 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001514 /* Rely on seqids for serialisation with NFSv4.0 */
1515 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1516 break;
1517
1518 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1519 /*
1520 * Ensure we process the state changes in the same order
1521 * in which the server processed them by delaying the
1522 * update of the stateid until we are in sequence.
1523 */
1524 write_sequnlock(&state->seqlock);
1525 spin_unlock(&state->owner->so_lock);
1526 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001527 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001528 if (!signal_pending(current)) {
1529 if (schedule_timeout(5*HZ) == 0)
1530 status = -EAGAIN;
1531 else
1532 status = 0;
1533 } else
1534 status = -EINTR;
1535 finish_wait(&state->waitq, &wait);
1536 rcu_read_lock();
1537 spin_lock(&state->owner->so_lock);
1538 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001539 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001540
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001541 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1542 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543 nfs4_stateid_copy(freeme, &state->open_stateid);
1544 nfs_test_and_clear_all_open_stateid(state);
1545 }
1546
Trond Myklebuste999e802014-02-10 18:20:47 -05001547 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1548 nfs4_stateid_copy(&state->stateid, stateid);
1549 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001550 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001552}
1553
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001556 fmode_t fmode,
1557 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001559 /*
1560 * Protect the call to nfs4_state_set_mode_locked and
1561 * serialise the stateid update
1562 */
1563 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001564 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1565 switch (fmode) {
1566 case FMODE_READ:
1567 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1568 break;
1569 case FMODE_WRITE:
1570 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_READ|FMODE_WRITE:
1573 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001574 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001576 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001577}
1578
1579static void nfs_state_set_delegation(struct nfs4_state *state,
1580 const nfs4_stateid *deleg_stateid,
1581 fmode_t fmode)
1582{
1583 /*
1584 * Protect the call to nfs4_state_set_mode_locked and
1585 * serialise the stateid update
1586 */
1587 write_seqlock(&state->seqlock);
1588 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1589 set_bit(NFS_DELEGATED_STATE, &state->flags);
1590 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Trond Myklebust1393d962016-09-22 13:39:13 -04001593static int update_open_stateid(struct nfs4_state *state,
1594 const nfs4_stateid *open_stateid,
1595 const nfs4_stateid *delegation,
1596 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001597{
Trond Myklebust1393d962016-09-22 13:39:13 -04001598 struct nfs_server *server = NFS_SERVER(state->inode);
1599 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001600 struct nfs_inode *nfsi = NFS_I(state->inode);
1601 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001602 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001603 int ret = 0;
1604
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001605 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001606
1607 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001608 spin_lock(&state->owner->so_lock);
1609 if (open_stateid != NULL) {
1610 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1611 ret = 1;
1612 }
1613
Trond Myklebust34310432008-12-23 15:21:38 -05001614 deleg_cur = rcu_dereference(nfsi->delegation);
1615 if (deleg_cur == NULL)
1616 goto no_delegation;
1617
1618 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001619 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001620 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001621 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001622 goto no_delegation_unlock;
1623
1624 if (delegation == NULL)
1625 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001626 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001627 goto no_delegation_unlock;
1628
Trond Myklebustb7391f42008-12-23 15:21:52 -05001629 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001631 ret = 1;
1632no_delegation_unlock:
1633 spin_unlock(&deleg_cur->lock);
1634no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001635 if (ret)
1636 update_open_stateflags(state, fmode);
1637 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001638 rcu_read_unlock();
1639
Trond Myklebust4f14c192014-02-12 19:15:06 -05001640 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001641 nfs4_schedule_state_manager(clp);
1642 if (freeme.type != 0)
1643 nfs4_test_and_free_stateid(server, &freeme,
1644 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001645
1646 return ret;
1647}
1648
Trond Myklebust39071e62015-01-24 15:07:56 -05001649static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1650 const nfs4_stateid *stateid)
1651{
1652 struct nfs4_state *state = lsp->ls_state;
1653 bool ret = false;
1654
1655 spin_lock(&state->state_lock);
1656 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1657 goto out_noupdate;
1658 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1659 goto out_noupdate;
1660 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1661 ret = true;
1662out_noupdate:
1663 spin_unlock(&state->state_lock);
1664 return ret;
1665}
Trond Myklebust34310432008-12-23 15:21:38 -05001666
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668{
1669 struct nfs_delegation *delegation;
1670
1671 rcu_read_lock();
1672 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 rcu_read_unlock();
1675 return;
1676 }
1677 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001678 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679}
1680
Trond Myklebust6ee41262007-07-08 14:11:36 -04001681static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682{
1683 struct nfs4_state *state = opendata->state;
1684 struct nfs_inode *nfsi = NFS_I(state->inode);
1685 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001686 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001687 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001688 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 nfs4_stateid stateid;
1690 int ret = -EAGAIN;
1691
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001693 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001696 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001700 rcu_read_lock();
1701 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001702 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001703 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001704 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001705 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001707 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001709 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001710 if (!opendata->is_recover) {
1711 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1712 if (ret != 0)
1713 goto out;
1714 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001716
1717 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001719 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721out:
1722 return ERR_PTR(ret);
1723out_return_state:
1724 atomic_inc(&state->count);
1725 return state;
1726}
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728static void
1729nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1730{
1731 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1732 struct nfs_delegation *delegation;
1733 int delegation_flags = 0;
1734
1735 rcu_read_lock();
1736 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1737 if (delegation)
1738 delegation_flags = delegation->flags;
1739 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001740 switch (data->o_arg.claim) {
1741 default:
1742 break;
1743 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1744 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001745 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1746 "returning a delegation for "
1747 "OPEN(CLAIM_DELEGATE_CUR)\n",
1748 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001749 return;
1750 }
1751 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001752 nfs_inode_set_delegation(state->inode,
1753 data->owner->so_cred,
1754 &data->o_res);
1755 else
1756 nfs_inode_reclaim_delegation(state->inode,
1757 data->owner->so_cred,
1758 &data->o_res);
1759}
1760
1761/*
1762 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1763 * and update the nfs4_state.
1764 */
1765static struct nfs4_state *
1766_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1767{
1768 struct inode *inode = data->state->inode;
1769 struct nfs4_state *state = data->state;
1770 int ret;
1771
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001772 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001773 if (data->rpc_status)
1774 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001775 /* cached opens have already been processed */
1776 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001777 }
1778
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 ret = nfs_refresh_inode(inode, &data->f_attr);
1780 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001781 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001782
1783 if (data->o_res.delegation_type != 0)
1784 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001785update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001786 update_open_stateid(state, &data->o_res.stateid, NULL,
1787 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001788 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001789
1790 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001791}
1792
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001793static struct inode *
1794nfs4_opendata_get_inode(struct nfs4_opendata *data)
1795{
1796 struct inode *inode;
1797
1798 switch (data->o_arg.claim) {
1799 case NFS4_OPEN_CLAIM_NULL:
1800 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1801 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1802 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1803 return ERR_PTR(-EAGAIN);
1804 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1805 &data->f_attr, data->f_label);
1806 break;
1807 default:
1808 inode = d_inode(data->dentry);
1809 ihold(inode);
1810 nfs_refresh_inode(inode, &data->f_attr);
1811 }
1812 return inode;
1813}
1814
Andy Adamsone23008e2012-10-02 21:07:32 -04001815static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001816nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1817{
1818 struct nfs4_state *state;
1819 struct inode *inode;
1820
1821 inode = nfs4_opendata_get_inode(data);
1822 if (IS_ERR(inode))
1823 return ERR_CAST(inode);
1824 if (data->state != NULL && data->state->inode == inode) {
1825 state = data->state;
1826 atomic_inc(&state->count);
1827 } else
1828 state = nfs4_get_open_state(inode, data->owner);
1829 iput(inode);
1830 if (state == NULL)
1831 state = ERR_PTR(-ENOMEM);
1832 return state;
1833}
1834
Andy Adamsone23008e2012-10-02 21:07:32 -04001835static struct nfs4_state *
1836_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001838 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001841 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001842 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001843 goto out;
1844 }
1845
Trond Myklebust75e8c482017-08-08 10:38:07 -04001846 state = nfs4_opendata_find_nfs4_state(data);
1847 if (IS_ERR(state))
1848 goto out;
1849
Andy Adamsone23008e2012-10-02 21:07:32 -04001850 if (data->o_res.delegation_type != 0)
1851 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001852 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001853 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001855 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 return state;
1857}
1858
Andy Adamsone23008e2012-10-02 21:07:32 -04001859static struct nfs4_state *
1860nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1861{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001862 struct nfs4_state *ret;
1863
Andy Adamsone23008e2012-10-02 21:07:32 -04001864 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001865 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1866 else
1867 ret = _nfs4_opendata_to_nfs4_state(data);
1868 nfs4_sequence_free_slot(&data->o_res.seq_res);
1869 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001870}
1871
Trond Myklebust864472e2006-01-03 09:55:15 +01001872static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1873{
1874 struct nfs_inode *nfsi = NFS_I(state->inode);
1875 struct nfs_open_context *ctx;
1876
1877 spin_lock(&state->inode->i_lock);
1878 list_for_each_entry(ctx, &nfsi->open_files, list) {
1879 if (ctx->state != state)
1880 continue;
1881 get_nfs_open_context(ctx);
1882 spin_unlock(&state->inode->i_lock);
1883 return ctx;
1884 }
1885 spin_unlock(&state->inode->i_lock);
1886 return ERR_PTR(-ENOENT);
1887}
1888
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001889static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1890 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001891{
1892 struct nfs4_opendata *opendata;
1893
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001894 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001895 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001896 if (opendata == NULL)
1897 return ERR_PTR(-ENOMEM);
1898 opendata->state = state;
1899 atomic_inc(&state->count);
1900 return opendata;
1901}
1902
Trond Myklebust24311f82015-09-20 10:50:17 -04001903static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1904 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001905{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001906 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001907 int ret;
1908
Trond Myklebust24311f82015-09-20 10:50:17 -04001909 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001910 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001911 opendata->o_arg.open_flags = 0;
1912 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001913 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1914 NFS_SB(opendata->dentry->d_sb),
1915 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001916 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1917 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1918 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001919 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001920 if (ret != 0)
1921 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001922 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001923 if (IS_ERR(newstate))
1924 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001925 if (newstate != opendata->state)
1926 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001927 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001928 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001929}
1930
1931static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1932{
Trond Myklebust864472e2006-01-03 09:55:15 +01001933 int ret;
1934
Trond Myklebust4f14c192014-02-12 19:15:06 -05001935 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1936 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1937 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1938 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001939 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001940 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001941 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001943 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1944 if (ret != 0)
1945 return ret;
1946 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1947 if (ret != 0)
1948 return ret;
1949 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1950 if (ret != 0)
1951 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001952 /*
1953 * We may have performed cached opens for all three recoveries.
1954 * Check if we need to update the current stateid.
1955 */
1956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001957 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001958 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001959 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001960 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001961 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001962 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001963 return 0;
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * OPEN_RECLAIM:
1968 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001970static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001972 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001973 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001974 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int status;
1976
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1978 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001981 rcu_read_lock();
1982 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001983 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001984 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001985 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001986 opendata->o_arg.u.delegation_type = delegation_type;
1987 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001988 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return status;
1990}
1991
Trond Myklebust539cd032007-06-05 11:46:42 -04001992static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct nfs_server *server = NFS_SERVER(state->inode);
1995 struct nfs4_exception exception = { };
1996 int err;
1997 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001998 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001999 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002000 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2001 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002002 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002003 break;
2004 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } while (exception.retry);
2006 return err;
2007}
2008
Trond Myklebust864472e2006-01-03 09:55:15 +01002009static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2010{
2011 struct nfs_open_context *ctx;
2012 int ret;
2013
2014 ctx = nfs4_state_find_open_context(state);
2015 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002016 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002017 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002018 put_nfs_open_context(ctx);
2019 return ret;
2020}
2021
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002022static 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 -07002023{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002024 switch (err) {
2025 default:
2026 printk(KERN_ERR "NFS: %s: unhandled error "
2027 "%d.\n", __func__, err);
2028 case 0:
2029 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002030 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002031 case -ESTALE:
2032 break;
2033 case -NFS4ERR_BADSESSION:
2034 case -NFS4ERR_BADSLOT:
2035 case -NFS4ERR_BAD_HIGH_SLOT:
2036 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2037 case -NFS4ERR_DEADSESSION:
2038 set_bit(NFS_DELEGATED_STATE, &state->flags);
2039 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2040 return -EAGAIN;
2041 case -NFS4ERR_STALE_CLIENTID:
2042 case -NFS4ERR_STALE_STATEID:
2043 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002044 /* Don't recall a delegation if it was lost */
2045 nfs4_schedule_lease_recovery(server->nfs_client);
2046 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002047 case -NFS4ERR_MOVED:
2048 nfs4_schedule_migration_recovery(server);
2049 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002050 case -NFS4ERR_LEASE_MOVED:
2051 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2052 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002053 case -NFS4ERR_DELEG_REVOKED:
2054 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002055 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002056 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002057 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 nfs_inode_find_state_and_recover(state->inode,
2059 stateid);
2060 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002061 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002062 case -NFS4ERR_DELAY:
2063 case -NFS4ERR_GRACE:
2064 set_bit(NFS_DELEGATED_STATE, &state->flags);
2065 ssleep(1);
2066 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002067 case -ENOMEM:
2068 case -NFS4ERR_DENIED:
2069 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2070 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return err;
2073}
2074
Trond Myklebust24311f82015-09-20 10:50:17 -04002075int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2076 struct nfs4_state *state, const nfs4_stateid *stateid,
2077 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002078{
2079 struct nfs_server *server = NFS_SERVER(state->inode);
2080 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002081 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002082
2083 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2084 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2085 if (IS_ERR(opendata))
2086 return PTR_ERR(opendata);
2087 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002088 write_seqlock(&state->seqlock);
2089 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2090 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002091 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2092 switch (type & (FMODE_READ|FMODE_WRITE)) {
2093 case FMODE_READ|FMODE_WRITE:
2094 case FMODE_WRITE:
2095 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2096 if (err)
2097 break;
2098 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2099 if (err)
2100 break;
2101 case FMODE_READ:
2102 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2103 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002104 nfs4_opendata_put(opendata);
2105 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2106}
2107
Chuck Leverbe05c862013-08-09 12:49:47 -04002108static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2109{
2110 struct nfs4_opendata *data = calldata;
2111
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002112 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2113 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002114}
2115
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002116static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2117{
2118 struct nfs4_opendata *data = calldata;
2119
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002120 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002121
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002122 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002124 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002125 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002127 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002129}
2130
2131static void nfs4_open_confirm_release(void *calldata)
2132{
2133 struct nfs4_opendata *data = calldata;
2134 struct nfs4_state *state = NULL;
2135
2136 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002137 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002138 goto out_free;
2139 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002140 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002141 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002142 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002143 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002144 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002145out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002146 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147}
2148
2149static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002150 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002151 .rpc_call_done = nfs4_open_confirm_done,
2152 .rpc_release = nfs4_open_confirm_release,
2153};
2154
2155/*
2156 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2157 */
2158static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2159{
David Howells2b0143b2015-03-17 22:25:59 +00002160 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002161 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2164 .rpc_argp = &data->c_arg,
2165 .rpc_resp = &data->c_res,
2166 .rpc_cred = data->owner->so_cred,
2167 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002168 struct rpc_task_setup task_setup_data = {
2169 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002170 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002171 .callback_ops = &nfs4_open_confirm_ops,
2172 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002173 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002174 .flags = RPC_TASK_ASYNC,
2175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int status;
2177
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002178 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002179 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002180 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002181 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002183 if (data->is_recover)
2184 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002185 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002186 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002187 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002188 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002189 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002190 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002191 smp_wmb();
2192 } else
2193 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002194 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return status;
2196}
2197
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200 struct nfs4_opendata *data = calldata;
2201 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002202 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002203 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002204
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002206 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002207 /*
2208 * Check if we still need to send an OPEN call, or if we can use
2209 * a delegation instead.
2210 */
2211 if (data->state != NULL) {
2212 struct nfs_delegation *delegation;
2213
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002214 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002215 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002216 rcu_read_lock();
2217 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002218 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002219 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002220 rcu_read_unlock();
2221 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002222 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002223 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002224 switch (claim) {
2225 default:
2226 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002227 case NFS4_OPEN_CLAIM_PREVIOUS:
2228 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2229 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002230 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002231 case NFS4_OPEN_CLAIM_FH:
2232 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002233 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002234 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002235 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002236 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002237 &data->o_res.seq_res,
2238 task) != 0)
2239 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002240
2241 /* Set the create mode (note dependency on the session type) */
2242 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2243 if (data->o_arg.open_flags & O_EXCL) {
2244 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2245 if (nfs4_has_persistent_session(clp))
2246 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2247 else if (clp->cl_mvops->minor_version > 0)
2248 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2249 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002250 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002251unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002252 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002253 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002254out_no_action:
2255 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002256out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002257 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002258}
2259
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260static void nfs4_open_done(struct rpc_task *task, void *calldata)
2261{
2262 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002264 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002265
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002266 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002267 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002268
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002269 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002270 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2271 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002272 case S_IFREG:
2273 break;
2274 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002276 break;
2277 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002279 break;
2280 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002282 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002283 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002284 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002285 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2286 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002287 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002288 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002289}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002290
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291static void nfs4_open_release(void *calldata)
2292{
2293 struct nfs4_opendata *data = calldata;
2294 struct nfs4_state *state = NULL;
2295
2296 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002297 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298 goto out_free;
2299 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002300 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 goto out_free;
2302 /* In case we need an open_confirm, no cleanup! */
2303 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2304 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002306 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002307 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002309 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310}
2311
2312static const struct rpc_call_ops nfs4_open_ops = {
2313 .rpc_call_prepare = nfs4_open_prepare,
2314 .rpc_call_done = nfs4_open_done,
2315 .rpc_release = nfs4_open_release,
2316};
2317
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002318static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002319{
David Howells2b0143b2015-03-17 22:25:59 +00002320 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321 struct nfs_server *server = NFS_SERVER(dir);
2322 struct nfs_openargs *o_arg = &data->o_arg;
2323 struct nfs_openres *o_res = &data->o_res;
2324 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2327 .rpc_argp = o_arg,
2328 .rpc_resp = o_res,
2329 .rpc_cred = data->owner->so_cred,
2330 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002333 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 .callback_ops = &nfs4_open_ops,
2335 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002336 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 .flags = RPC_TASK_ASYNC,
2338 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002339 int status;
2340
Chuck Levera9c92d62013-08-09 12:48:18 -04002341 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002342 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002343 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002344 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002345 data->cancelled = false;
2346 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002347 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002348 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002350 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002351 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002352 if (IS_ERR(task))
2353 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002354 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002355 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 smp_wmb();
2358 } else
2359 status = data->rpc_status;
2360 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002361
2362 return status;
2363}
2364
2365static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2366{
David Howells2b0143b2015-03-17 22:25:59 +00002367 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002368 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002369 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370
2371 status = nfs4_run_open_task(data, 1);
2372 if (status != 0 || !data->rpc_done)
2373 return status;
2374
Trond Myklebust6926afd2012-01-07 13:22:46 -05002375 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2376
Anna Schumakerd7e98252017-01-11 16:13:29 -05002377 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002378 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002379
2380 return status;
2381}
2382
Trond Myklebustf3792d62014-07-10 08:54:32 -04002383/*
2384 * Additional permission checks in order to distinguish between an
2385 * open for read, and an open for execute. This works around the
2386 * fact that NFSv4 OPEN treats read and execute permissions as being
2387 * the same.
2388 * Note that in the non-execute case, we want to turn off permission
2389 * checking if we just created a new file (POSIX open() semantics).
2390 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002391static int nfs4_opendata_access(struct rpc_cred *cred,
2392 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002393 struct nfs4_state *state, fmode_t fmode,
2394 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002395{
2396 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002397 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002398
2399 /* access call failed or for some reason the server doesn't
2400 * support any access modes -- defer access call until later */
2401 if (opendata->o_res.access_supported == 0)
2402 return 0;
2403
2404 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002405 /*
2406 * Use openflags to check for exec, because fmode won't
2407 * always have FMODE_EXEC set when file open for exec.
2408 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002409 if (openflags & __FMODE_EXEC) {
2410 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002411 if (S_ISDIR(state->inode->i_mode))
2412 mask = NFS4_ACCESS_LOOKUP;
2413 else
2414 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002415 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002416 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002417
2418 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002419 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2420 nfs_access_add_cache(state->inode, &cache);
2421
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002422 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2423 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424 return 0;
2425
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002426 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427}
2428
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002429/*
2430 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2431 */
2432static int _nfs4_proc_open(struct nfs4_opendata *data)
2433{
David Howells2b0143b2015-03-17 22:25:59 +00002434 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002435 struct nfs_server *server = NFS_SERVER(dir);
2436 struct nfs_openargs *o_arg = &data->o_arg;
2437 struct nfs_openres *o_res = &data->o_res;
2438 int status;
2439
2440 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002441 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002442 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002443 if (status != 0) {
2444 if (status == -NFS4ERR_BADNAME &&
2445 !(o_arg->open_flags & O_CREAT))
2446 return -ENOENT;
2447 return status;
2448 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002449
Trond Myklebust6926afd2012-01-07 13:22:46 -05002450 nfs_fattr_map_and_free_names(server, &data->f_attr);
2451
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002452 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002453 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002454 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002455 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002456 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002457 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002458 update_changeattr(dir, &o_res->cinfo,
2459 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002460 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002461 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2462 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002464 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002466 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002468 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2469 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002470 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002471 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475/*
2476 * OPEN_EXPIRED:
2477 * reclaim state on the server after a network partition.
2478 * Assumes caller holds the appropriate lock
2479 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002480static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002482 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002483 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002485 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002486 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002487 if (IS_ERR(opendata))
2488 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002489 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002490 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002491 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002492 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002496static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002497{
Trond Myklebust539cd032007-06-05 11:46:42 -04002498 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002499 struct nfs4_exception exception = { };
2500 int err;
2501
2502 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002503 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002504 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002505 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2506 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002507 switch (err) {
2508 default:
2509 goto out;
2510 case -NFS4ERR_GRACE:
2511 case -NFS4ERR_DELAY:
2512 nfs4_handle_exception(server, err, &exception);
2513 err = 0;
2514 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002515 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002516out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002517 return err;
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002523 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Trond Myklebust864472e2006-01-03 09:55:15 +01002525 ctx = nfs4_state_find_open_context(state);
2526 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002527 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002528 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002529 put_nfs_open_context(ctx);
2530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Trond Myklebust41020b62016-09-22 13:38:58 -04002533static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2534 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002535{
Trond Myklebust41020b62016-09-22 13:38:58 -04002536 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002537 write_seqlock(&state->seqlock);
2538 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2539 write_sequnlock(&state->seqlock);
2540 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2541}
2542
2543static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2544{
2545 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002546 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002547}
2548
2549static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2550{
2551 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2552 nfs40_clear_delegation_stateid(state);
2553 return nfs4_open_expired(sp, state);
2554}
2555
Trond Myklebust45870d62016-09-22 13:38:59 -04002556static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2557 nfs4_stateid *stateid,
2558 struct rpc_cred *cred)
2559{
2560 return -NFS4ERR_BAD_STATEID;
2561}
2562
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002563#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002564static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2565 nfs4_stateid *stateid,
2566 struct rpc_cred *cred)
2567{
2568 int status;
2569
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002570 switch (stateid->type) {
2571 default:
2572 break;
2573 case NFS4_INVALID_STATEID_TYPE:
2574 case NFS4_SPECIAL_STATEID_TYPE:
2575 return -NFS4ERR_BAD_STATEID;
2576 case NFS4_REVOKED_STATEID_TYPE:
2577 goto out_free;
2578 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002579
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002580 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002581 switch (status) {
2582 case -NFS4ERR_EXPIRED:
2583 case -NFS4ERR_ADMIN_REVOKED:
2584 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002585 break;
2586 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002587 return status;
2588 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002589out_free:
2590 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002591 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002592 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002593}
2594
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002595static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002596{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002597 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002598 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002599 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002600 struct rpc_cred *cred;
2601 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002602
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002603 /* Get the delegation credential for use by test/free_stateid */
2604 rcu_read_lock();
2605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002606 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002607 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002608 return;
2609 }
2610
2611 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002612 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2613 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2614 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002615 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002616 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002617 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002618 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002619
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 cred = get_rpccred(delegation->cred);
2621 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002622 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002623 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002624 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002625 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002626
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002627 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628}
2629
2630/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002631 * nfs41_check_expired_locks - possibly free a lock 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_expired_locks(struct nfs4_state *state)
2639{
2640 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002641 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002642 struct nfs_server *server = NFS_SERVER(state->inode);
2643
2644 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2645 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002646
2647 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002648 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2649 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2650 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2651
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002652 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002653 spin_unlock(&state->state_lock);
2654
2655 nfs4_put_lock_state(prev);
2656 prev = lsp;
2657
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002658 status = nfs41_test_and_free_expired_stateid(server,
2659 &lsp->ls_stateid,
2660 cred);
2661 trace_nfs4_test_lock_stateid(state, lsp, status);
2662 if (status == -NFS4ERR_EXPIRED ||
2663 status == -NFS4ERR_BAD_STATEID) {
2664 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002665 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002666 if (!recover_lost_locks)
2667 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2668 } else if (status != NFS_OK) {
2669 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002670 nfs4_put_lock_state(prev);
2671 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002672 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002673 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002675 }
2676 spin_unlock(&state->state_lock);
2677 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002678out:
2679 return ret;
2680}
2681
2682/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002683 * nfs41_check_open_stateid - possibly free an open stateid
2684 *
2685 * @state: NFSv4 state for an inode
2686 *
2687 * Returns NFS_OK if recovery for this stateid is now finished.
2688 * Otherwise a negative NFS4ERR value is returned.
2689 */
2690static int nfs41_check_open_stateid(struct nfs4_state *state)
2691{
2692 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002693 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002694 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002695 int status;
2696
Trond Myklebustb134fc42016-09-22 13:39:16 -04002697 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002698 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2699 if (nfs4_have_delegation(state->inode, state->state))
2700 return NFS_OK;
2701 return -NFS4ERR_OPENMODE;
2702 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002703 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002704 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002705 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002706 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002707 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002708 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2709 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2710 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002711 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002712 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002713 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002714 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002715 if (nfs_open_stateid_recover_openmode(state))
2716 return -NFS4ERR_OPENMODE;
2717 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002718}
2719
2720static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2721{
Chuck Levereb64cf92012-07-11 16:30:05 -04002722 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002723
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002724 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002725 status = nfs41_check_expired_locks(state);
2726 if (status != NFS_OK)
2727 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002728 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002729 if (status != NFS_OK)
2730 status = nfs4_open_expired(sp, state);
2731 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002732}
2733#endif
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002736 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2737 * fields corresponding to attributes that were used to store the verifier.
2738 * Make sure we clobber those fields in the later setattr call
2739 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002740static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2741 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002742{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002743 const u32 *attrset = opendata->o_res.attrset;
2744
2745 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002746 !(sattr->ia_valid & ATTR_ATIME_SET))
2747 sattr->ia_valid |= ATTR_ATIME;
2748
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002749 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002750 !(sattr->ia_valid & ATTR_MTIME_SET))
2751 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752
2753 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002754 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002755 (attrset[1] & FATTR4_WORD1_MODE ||
2756 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002757 sattr->ia_valid &= ~ATTR_MODE;
2758
2759 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2760 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002761}
2762
Trond Myklebustc21443c2013-02-07 14:26:21 -05002763static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2764 fmode_t fmode,
2765 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002766 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002767{
2768 struct nfs4_state_owner *sp = opendata->owner;
2769 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002770 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002771 struct nfs4_state *state;
2772 unsigned int seq;
2773 int ret;
2774
2775 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2776
2777 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002778 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002779 goto out;
2780
2781 state = nfs4_opendata_to_nfs4_state(opendata);
2782 ret = PTR_ERR(state);
2783 if (IS_ERR(state))
2784 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002785 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002786 if (server->caps & NFS_CAP_POSIX_LOCK)
2787 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002788 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2789 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002790
Trond Myklebust275bb302013-05-29 13:11:28 -04002791 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002792 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002793 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002794 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002795 alias = d_exact_alias(dentry, state->inode);
2796 if (!alias)
2797 alias = d_splice_alias(igrab(state->inode), dentry);
2798 /* d_splice_alias() can't fail here - it's a non-directory */
2799 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002800 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002801 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002802 }
2803 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002804 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002805 }
2806
Trond Myklebustc21443c2013-02-07 14:26:21 -05002807 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2808 if (ret != 0)
2809 goto out;
2810
David Howells2b0143b2015-03-17 22:25:59 +00002811 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002812 nfs_inode_attach_open_context(ctx);
2813 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2814 nfs4_schedule_stateid_recovery(server, state);
2815 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002816out:
2817 return ret;
2818}
2819
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002820/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002821 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Andy Adamson82be4172012-05-23 05:02:35 -04002823static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002824 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002825 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002826 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002827 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct nfs4_state_owner *sp;
2830 struct nfs4_state *state = NULL;
2831 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002832 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002833 struct dentry *dentry = ctx->dentry;
2834 struct rpc_cred *cred = ctx->cred;
2835 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2836 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002837 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002838 struct iattr *sattr = c->sattr;
2839 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002840 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002841 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002845 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2846 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2848 goto out_err;
2849 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002850 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002851 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002852 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002853 if (d_really_is_positive(dentry))
2854 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002855 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002856 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002857 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002858 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2859 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002860 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002861 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
David Quigley14c43f72013-05-22 12:50:43 -04002863 if (label) {
2864 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2865 if (IS_ERR(olabel)) {
2866 status = PTR_ERR(olabel);
2867 goto err_opendata_put;
2868 }
2869 }
2870
Trond Myklebuste911b812014-03-26 13:24:37 -07002871 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2872 if (!opendata->f_attr.mdsthreshold) {
2873 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2874 if (!opendata->f_attr.mdsthreshold)
2875 goto err_free_label;
2876 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002877 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002878 }
David Howells2b0143b2015-03-17 22:25:59 +00002879 if (d_really_is_positive(dentry))
2880 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002881
Trond Myklebust3efb9722013-05-29 13:17:04 -04002882 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002883 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002884 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002885 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002886
NeilBrownefcbc042015-07-30 13:00:56 +10002887 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002888 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002889 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002890 /*
2891 * send create attributes which was not set by open
2892 * with an extra setattr.
2893 */
2894 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2895 nfs_fattr_init(opendata->o_res.f_attr);
2896 status = nfs4_do_setattr(state->inode, cred,
2897 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002898 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002899 if (status == 0) {
2900 nfs_setattr_update_inode(state->inode, sattr,
2901 opendata->o_res.f_attr);
2902 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2903 }
David Quigley1775fd32013-05-22 12:50:42 -04002904 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002905 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002906 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002907 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002908
Trond Myklebuste911b812014-03-26 13:24:37 -07002909 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002910 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002911 opendata->f_attr.mdsthreshold = NULL;
2912 }
Andy Adamson82be4172012-05-23 05:02:35 -04002913
David Quigley14c43f72013-05-22 12:50:43 -04002914 nfs4_label_free(olabel);
2915
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002916 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002919err_free_label:
2920 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002921err_opendata_put:
2922 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002923err_put_state_owner:
2924 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
2929
Andy Adamson82be4172012-05-23 05:02:35 -04002930static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002931 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002932 int flags,
2933 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002934 struct nfs4_label *label,
2935 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002937 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct nfs4_exception exception = { };
2939 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002940 struct nfs4_open_createattrs c = {
2941 .label = label,
2942 .sattr = sattr,
2943 .verf = {
2944 [0] = (__u32)jiffies,
2945 [1] = (__u32)current->pid,
2946 },
2947 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 int status;
2949
2950 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002951 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002952 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002953 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (status == 0)
2955 break;
2956 /* NOTE: BAD_SEQID means the server and client disagree about the
2957 * book-keeping w.r.t. state-changing operations
2958 * (OPEN/CLOSE/LOCK/LOCKU...)
2959 * It is actually a sign of a bug on the client or on the server.
2960 *
2961 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002962 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 * have unhashed the old state_owner for us, and that we can
2964 * therefore safely retry using a new one. We should still warn
2965 * the user though...
2966 */
2967 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002968 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002969 " returned a bad sequence-id error!\n",
2970 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 exception.retry = 1;
2972 continue;
2973 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002974 /*
2975 * BAD_STATEID on OPEN means that the server cancelled our
2976 * state before it received the OPEN_CONFIRM.
2977 * Recover by retrying the request as per the discussion
2978 * on Page 181 of RFC3530.
2979 */
2980 if (status == -NFS4ERR_BAD_STATEID) {
2981 exception.retry = 1;
2982 continue;
2983 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002984 if (status == -EAGAIN) {
2985 /* We must have found a delegation */
2986 exception.retry = 1;
2987 continue;
2988 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002989 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2990 continue;
2991 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 status, &exception));
2993 } while (exception.retry);
2994 return res;
2995}
2996
Trond Myklebust8487c472016-06-26 08:44:35 -04002997static int _nfs4_do_setattr(struct inode *inode,
2998 struct nfs_setattrargs *arg,
2999 struct nfs_setattrres *res,
3000 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003001 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003002{
3003 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003004 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3006 .rpc_argp = arg,
3007 .rpc_resp = res,
3008 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003009 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003010 struct rpc_cred *delegation_cred = NULL;
3011 unsigned long timestamp = jiffies;
3012 fmode_t fmode;
3013 bool truncate;
3014 int status;
3015
3016 nfs_fattr_init(res->fattr);
3017
3018 /* Servers should only apply open mode checks for file size changes */
3019 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3020 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3021
3022 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3023 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003024 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003025 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003026 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003027 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003028 l_ctx = nfs_get_lock_context(ctx);
3029 if (IS_ERR(l_ctx))
3030 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003031 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3032 &arg->stateid, &delegation_cred);
3033 nfs_put_lock_context(l_ctx);
3034 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003035 return -EBADF;
3036 } else
3037 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3038 if (delegation_cred)
3039 msg.rpc_cred = delegation_cred;
3040
3041 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3042
3043 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003044 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003045 renew_lease(server, timestamp);
3046 trace_nfs4_setattr(inode, &arg->stateid, status);
3047 return status;
3048}
3049
3050static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3051 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003052 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003053 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003055 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003056 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003057 struct nfs_setattrargs arg = {
3058 .fh = NFS_FH(inode),
3059 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 .server = server,
3061 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003062 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003063 };
3064 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003066 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003068 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003069 struct nfs4_exception exception = {
3070 .state = state,
3071 .inode = inode,
3072 .stateid = &arg.stateid,
3073 };
3074 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
David Quigleyaa9c2662013-05-22 12:50:44 -04003076 arg.bitmask = nfs4_bitmask(server, ilabel);
3077 if (ilabel)
3078 arg.bitmask = nfs4_bitmask(server, olabel);
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003081 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003082 switch (err) {
3083 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003084 if (!(sattr->ia_valid & ATTR_SIZE)) {
3085 pr_warn_once("NFSv4: server %s is incorrectly "
3086 "applying open mode checks to "
3087 "a SETATTR that is not "
3088 "changing file size.\n",
3089 server->nfs_client->cl_hostname);
3090 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003091 if (state && !(state->state & FMODE_WRITE)) {
3092 err = -EBADF;
3093 if (sattr->ia_valid & ATTR_OPEN)
3094 err = -EACCES;
3095 goto out;
3096 }
3097 }
3098 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return err;
3102}
3103
Peng Tao500d7012015-09-22 11:35:22 +08003104static bool
3105nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3106{
3107 if (inode == NULL || !nfs_have_layout(inode))
3108 return false;
3109
3110 return pnfs_wait_on_layoutreturn(inode, task);
3111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113struct nfs4_closedata {
3114 struct inode *inode;
3115 struct nfs4_state *state;
3116 struct nfs_closeargs arg;
3117 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003118 struct {
3119 struct nfs4_layoutreturn_args arg;
3120 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003121 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003122 u32 roc_barrier;
3123 bool roc;
3124 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003125 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003126 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127};
3128
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003129static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003130{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131 struct nfs4_closedata *calldata = data;
3132 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003133 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003134
Trond Myklebustcf805162016-11-15 14:56:07 -05003135 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003136 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3137 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003138 nfs4_put_open_state(calldata->state);
3139 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003140 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003141 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003142 kfree(calldata);
3143}
3144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003145static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003150 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003151 struct nfs4_exception exception = {
3152 .state = state,
3153 .inode = calldata->inode,
3154 .stateid = &calldata->arg.stateid,
3155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Chuck Levera3ca5652012-03-01 17:00:40 -05003157 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003158 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3159 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003160 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003161
3162 /* Handle Layoutreturn errors */
3163 if (calldata->arg.lr_args && task->tk_status != 0) {
3164 switch (calldata->res.lr_ret) {
3165 default:
3166 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3167 break;
3168 case 0:
3169 calldata->arg.lr_args = NULL;
3170 calldata->res.lr_res = NULL;
3171 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003172 case -NFS4ERR_OLD_STATEID:
3173 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003174 calldata->inode))
3175 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003176 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003177 case -NFS4ERR_ADMIN_REVOKED:
3178 case -NFS4ERR_DELEG_REVOKED:
3179 case -NFS4ERR_EXPIRED:
3180 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003181 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3182 case -NFS4ERR_WRONG_CRED:
3183 calldata->arg.lr_args = NULL;
3184 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003185 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003186 }
3187 }
3188
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003189 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 * the state_owner. we keep this around to process errors
3191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 switch (task->tk_status) {
3193 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003194 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003195 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003196 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003197 case -NFS4ERR_ACCESS:
3198 if (calldata->arg.bitmask != NULL) {
3199 calldata->arg.bitmask = NULL;
3200 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003201 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003202
3203 }
3204 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003205 case -NFS4ERR_OLD_STATEID:
3206 /* Did we race with OPEN? */
3207 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003208 state))
3209 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003210 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003211 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003213 case -NFS4ERR_EXPIRED:
3214 nfs4_free_revoked_stateid(server,
3215 &calldata->arg.stateid,
3216 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003217 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003218 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003221 task->tk_status = nfs4_async_handle_exception(task,
3222 server, task->tk_status, &exception);
3223 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003224 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003226 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3227 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003228out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003229 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003230 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003231 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003232 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003233 return;
3234lr_restart:
3235 calldata->res.lr_ret = 0;
3236out_restart:
3237 task->tk_status = 0;
3238 rpc_restart_call_prepare(task);
3239 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003242static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003244 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003245 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003246 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003247 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003248 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003249
Chuck Levera3ca5652012-03-01 17:00:40 -05003250 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003251 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003252 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003253
Trond Myklebust88069f72009-12-08 08:33:16 -05003254 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003255 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003256 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3257 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3258 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003259 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003260 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003261 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003262 if (state->n_rdonly == 0)
3263 call_close |= is_rdonly;
3264 else if (is_rdonly)
3265 calldata->arg.fmode |= FMODE_READ;
3266 if (state->n_wronly == 0)
3267 call_close |= is_wronly;
3268 else if (is_wronly)
3269 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003270 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3271 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003272 } else if (is_rdwr)
3273 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3274
Trond Myklebust5cc78612016-11-14 11:19:56 -05003275 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003276 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003277 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003278 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003279
3280 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003281 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003282 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003283 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003284
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003285 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003286 nfs_release_seqid(calldata->arg.seqid);
3287 goto out_wait;
3288 }
3289
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003290 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003291 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003292
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003293 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003294 /* Close-to-open cache consistency revalidation */
3295 if (!nfs4_have_delegation(inode, FMODE_READ))
3296 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3297 else
3298 calldata->arg.bitmask = NULL;
3299 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003300
Trond Myklebust6ae37332015-01-30 14:21:14 -05003301 calldata->arg.share_access =
3302 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3303 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003304
Trond Myklebustd8d84982016-12-19 12:14:44 -05003305 if (calldata->res.fattr == NULL)
3306 calldata->arg.bitmask = NULL;
3307 else if (calldata->arg.bitmask == NULL)
3308 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003309 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003310 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003311 &calldata->arg.seq_args,
3312 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003313 task) != 0)
3314 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003315 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003316 return;
3317out_no_action:
3318 task->tk_action = NULL;
3319out_wait:
3320 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003323static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003324 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003325 .rpc_call_done = nfs4_close_done,
3326 .rpc_release = nfs4_free_closedata,
3327};
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/*
3330 * It is possible for data to be read/written from a mem-mapped file
3331 * after the sys_close call (which hits the vfs layer as a flush).
3332 * This means that we can't safely call nfsv4 close on a file until
3333 * the inode is cleared. This in turn means that we are not good
3334 * NFSv4 citizens - we do not indicate to the server to update the file's
3335 * share state even when we are done with one of the three share
3336 * stateid's in the inode.
3337 *
3338 * NOTE: Caller must be holding the sp->so_owner semaphore!
3339 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003340int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003342 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003343 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003345 struct nfs4_state_owner *sp = state->owner;
3346 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003347 struct rpc_message msg = {
3348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3349 .rpc_cred = state->owner->so_cred,
3350 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003351 struct rpc_task_setup task_setup_data = {
3352 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003353 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003354 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003355 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003356 .flags = RPC_TASK_ASYNC,
3357 };
Trond Myklebust95121352005-10-18 14:20:12 -07003358 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003360 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3361 &task_setup_data.rpc_client, &msg);
3362
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003363 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003365 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003366 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003367 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003369 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003370 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3371 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003373 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3374 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003375 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003376 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003377 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003378 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003379 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003380 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003381 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003382 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003383 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003384 calldata->lr.roc = pnfs_roc(state->inode,
3385 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3386 if (calldata->lr.roc) {
3387 calldata->arg.lr_args = &calldata->lr.arg;
3388 calldata->res.lr_res = &calldata->lr.res;
3389 }
Al Viro643168c2011-06-22 18:20:23 -04003390 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003391
Trond Myklebust1174dd12010-12-21 10:52:24 -05003392 msg.rpc_argp = &calldata->arg;
3393 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003394 task_setup_data.callback_data = calldata;
3395 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003396 if (IS_ERR(task))
3397 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003398 status = 0;
3399 if (wait)
3400 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003401 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003402 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003403out_free_calldata:
3404 kfree(calldata);
3405out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003406 nfs4_put_open_state(state);
3407 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003408 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
Trond Myklebust2b484292010-09-17 10:56:51 -04003411static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003412nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3413 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003416 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3417
3418 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Trond Myklebust565277f2007-10-15 18:17:53 -04003420 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003421 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003422
3423 nfs4_label_release_security(label);
3424
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003425 if (IS_ERR(state))
3426 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003427 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
Trond Myklebust1185a552009-12-03 15:54:02 -05003430static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003431{
3432 if (ctx->state == NULL)
3433 return;
3434 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003435 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003436 else
Al Viro643168c2011-06-22 18:20:23 -04003437 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003438}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Trond Myklebustb944dba2013-11-04 15:20:20 -05003440#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3441#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003442#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3445{
Kinglong Mee8c612822015-08-26 21:12:58 +08003446 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003447 struct nfs4_server_caps_arg args = {
3448 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003449 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 struct nfs4_server_caps_res res = {};
3452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003454 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 .rpc_resp = &res,
3456 };
3457 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003458 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Kinglong Mee8c612822015-08-26 21:12:58 +08003460 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3461 FATTR4_WORD0_FH_EXPIRE_TYPE |
3462 FATTR4_WORD0_LINK_SUPPORT |
3463 FATTR4_WORD0_SYMLINK_SUPPORT |
3464 FATTR4_WORD0_ACLSUPPORT;
3465 if (minorversion)
3466 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3467
Bryan Schumaker7c513052011-03-24 17:12:24 +00003468 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003470 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003471 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003472 case 0:
3473 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3474 res.attr_bitmask[2] = 0;
3475 break;
3476 case 1:
3477 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3478 break;
3479 case 2:
3480 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003483 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3484 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3485 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3486 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003487 NFS_CAP_CTIME|NFS_CAP_MTIME|
3488 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003489 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3490 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 server->caps |= NFS_CAP_ACLS;
3492 if (res.has_links != 0)
3493 server->caps |= NFS_CAP_HARDLINKS;
3494 if (res.has_symlinks != 0)
3495 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003496 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3497 server->caps |= NFS_CAP_FILEID;
3498 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3499 server->caps |= NFS_CAP_MODE;
3500 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3501 server->caps |= NFS_CAP_NLINK;
3502 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3503 server->caps |= NFS_CAP_OWNER;
3504 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3505 server->caps |= NFS_CAP_OWNER_GROUP;
3506 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3507 server->caps |= NFS_CAP_ATIME;
3508 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3509 server->caps |= NFS_CAP_CTIME;
3510 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3511 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003512#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3513 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3514 server->caps |= NFS_CAP_SECURITY_LABEL;
3515#endif
3516 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3517 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003518 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003519
Trond Myklebusta65318b2009-03-11 14:10:28 -04003520 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3521 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3522 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003523 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003524
3525 /* Avoid a regression due to buggy server */
3526 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3527 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003528 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3529 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003532 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return status;
3536}
3537
Trond Myklebust55a97592006-06-09 09:34:19 -04003538int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
3540 struct nfs4_exception exception = { };
3541 int err;
3542 do {
3543 err = nfs4_handle_exception(server,
3544 _nfs4_server_capabilities(server, fhandle),
3545 &exception);
3546 } while (exception.retry);
3547 return err;
3548}
3549
3550static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3551 struct nfs_fsinfo *info)
3552{
David Quigleyaa9c2662013-05-22 12:50:44 -04003553 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003555 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 };
3557 struct nfs4_lookup_res res = {
3558 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003559 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 .fh = fhandle,
3561 };
3562 struct rpc_message msg = {
3563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3564 .rpc_argp = &args,
3565 .rpc_resp = &res,
3566 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003567
David Quigleyaa9c2662013-05-22 12:50:44 -04003568 bitmask[0] = nfs4_fattr_bitmap[0];
3569 bitmask[1] = nfs4_fattr_bitmap[1];
3570 /*
3571 * Process the label in the upcoming getfattr
3572 */
3573 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3574
Trond Myklebust0e574af2005-10-27 22:12:38 -04003575 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003576 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
3579static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3580 struct nfs_fsinfo *info)
3581{
3582 struct nfs4_exception exception = { };
3583 int err;
3584 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003585 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003586 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003587 switch (err) {
3588 case 0:
3589 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003590 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003591 default:
3592 err = nfs4_handle_exception(server, err, &exception);
3593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003595out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return err;
3597}
3598
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003599static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3600 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3601{
Trond Myklebustc2190662013-08-26 19:23:04 -04003602 struct rpc_auth_create_args auth_args = {
3603 .pseudoflavor = flavor,
3604 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003605 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003606
Trond Myklebustc2190662013-08-26 19:23:04 -04003607 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003608 if (IS_ERR(auth))
3609 return -EACCES;
3610 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003611}
3612
Chuck Lever9a744ba2013-03-16 15:56:02 -04003613/*
3614 * Retry pseudoroot lookup with various security flavors. We do this when:
3615 *
3616 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3617 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3618 *
3619 * Returns zero on success, or a negative NFS4ERR value, or a
3620 * negative errno value.
3621 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003622static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003623 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003625 /* Per 3530bis 15.33.5 */
3626 static const rpc_authflavor_t flav_array[] = {
3627 RPC_AUTH_GSS_KRB5P,
3628 RPC_AUTH_GSS_KRB5I,
3629 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003630 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003631 RPC_AUTH_NULL,
3632 };
3633 int status = -EPERM;
3634 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003636 if (server->auth_info.flavor_len > 0) {
3637 /* try each flavor specified by user */
3638 for (i = 0; i < server->auth_info.flavor_len; i++) {
3639 status = nfs4_lookup_root_sec(server, fhandle, info,
3640 server->auth_info.flavors[i]);
3641 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3642 continue;
3643 break;
3644 }
3645 } else {
3646 /* no flavors specified by user, try default list */
3647 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3648 status = nfs4_lookup_root_sec(server, fhandle, info,
3649 flav_array[i]);
3650 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3651 continue;
3652 break;
3653 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003654 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003655
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003656 /*
3657 * -EACCESS could mean that the user doesn't have correct permissions
3658 * to access the mount. It could also mean that we tried to mount
3659 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3660 * existing mount programs don't handle -EACCES very well so it should
3661 * be mapped to -EPERM instead.
3662 */
3663 if (status == -EACCES)
3664 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003665 return status;
3666}
3667
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003668/**
3669 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3670 * @server: initialized nfs_server handle
3671 * @fhandle: we fill in the pseudo-fs root file handle
3672 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003673 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003674 *
3675 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003676 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003677int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003678 struct nfs_fsinfo *info,
3679 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003680{
Andre Przywarac7757072015-04-23 17:17:40 +01003681 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003682
Andre Przywarac7757072015-04-23 17:17:40 +01003683 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003684 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003685
3686 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003687 status = server->nfs_client->cl_mvops->find_root_sec(server,
3688 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 if (status == 0)
3691 status = nfs4_server_capabilities(server, fhandle);
3692 if (status == 0)
3693 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003694
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003695 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697
Bryan Schumakerbae36242012-05-10 15:07:31 -04003698static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3699 struct nfs_fsinfo *info)
3700{
3701 int error;
3702 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003703 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003704
3705 error = nfs4_server_capabilities(server, mntfh);
3706 if (error < 0) {
3707 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3708 return error;
3709 }
3710
David Quigley14c43f72013-05-22 12:50:43 -04003711 label = nfs4_label_alloc(server, GFP_KERNEL);
3712 if (IS_ERR(label))
3713 return PTR_ERR(label);
3714
David Quigley1775fd32013-05-22 12:50:42 -04003715 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003716 if (error < 0) {
3717 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003718 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003719 }
3720
3721 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3722 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3723 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3724
David Quigley14c43f72013-05-22 12:50:43 -04003725err_free_label:
3726 nfs4_label_free(label);
3727
Bryan Schumakerbae36242012-05-10 15:07:31 -04003728 return error;
3729}
3730
Manoj Naik6b97fd32006-06-09 09:34:29 -04003731/*
3732 * Get locations and (maybe) other attributes of a referral.
3733 * Note that we'll actually follow the referral later when
3734 * we detect fsid mismatch in inode revalidation
3735 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003736static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3737 const struct qstr *name, struct nfs_fattr *fattr,
3738 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003739{
3740 int status = -ENOMEM;
3741 struct page *page = NULL;
3742 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003743
3744 page = alloc_page(GFP_KERNEL);
3745 if (page == NULL)
3746 goto out;
3747 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3748 if (locations == NULL)
3749 goto out;
3750
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003751 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003752 if (status != 0)
3753 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003754
3755 /*
3756 * If the fsid didn't change, this is a migration event, not a
3757 * referral. Cause us to drop into the exception handler, which
3758 * will kick off migration recovery.
3759 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003760 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003761 dprintk("%s: server did not return a different fsid for"
3762 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003763 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764 goto out;
3765 }
Andy Adamson533eb462011-06-13 18:25:56 -04003766 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3767 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003768
Andy Adamson533eb462011-06-13 18:25:56 -04003769 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003770 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003771 memset(fhandle, 0, sizeof(struct nfs_fh));
3772out:
3773 if (page)
3774 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003775 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003776 return status;
3777}
3778
David Quigley1775fd32013-05-22 12:50:42 -04003779static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3780 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
3782 struct nfs4_getattr_arg args = {
3783 .fh = fhandle,
3784 .bitmask = server->attr_bitmask,
3785 };
3786 struct nfs4_getattr_res res = {
3787 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003788 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 .server = server,
3790 };
3791 struct rpc_message msg = {
3792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3793 .rpc_argp = &args,
3794 .rpc_resp = &res,
3795 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003796
3797 args.bitmask = nfs4_bitmask(server, label);
3798
Trond Myklebust0e574af2005-10-27 22:12:38 -04003799 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003800 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
David Quigley1775fd32013-05-22 12:50:42 -04003803static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3804 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
3806 struct nfs4_exception exception = { };
3807 int err;
3808 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003809 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3810 trace_nfs4_getattr(server, fhandle, fattr, err);
3811 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 &exception);
3813 } while (exception.retry);
3814 return err;
3815}
3816
3817/*
3818 * The file is not closed if it is opened due to the a request to change
3819 * the size of the file. The open call will not be needed once the
3820 * VFS layer lookup-intents are implemented.
3821 *
3822 * Close is called when the inode is destroyed.
3823 * If we haven't opened the file for O_WRONLY, we
3824 * need to in the size_change case to obtain a stateid.
3825 *
3826 * Got race?
3827 * Because OPEN is always done by name in nfsv4, it is
3828 * possible that we opened a different file by the same
3829 * name. We can recognize this race condition, but we
3830 * can't do anything about it besides returning an error.
3831 *
3832 * This will be fixed with VFS changes (lookup-intent).
3833 */
3834static int
3835nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3836 struct iattr *sattr)
3837{
David Howells2b0143b2015-03-17 22:25:59 +00003838 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003839 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003840 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003841 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 int status;
3843
Peng Tao88ac8152014-09-12 11:04:10 +08003844 if (pnfs_ld_layoutret_on_setattr(inode) &&
3845 sattr->ia_valid & ATTR_SIZE &&
3846 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003847 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003848
Trond Myklebust0e574af2005-10-27 22:12:38 -04003849 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Andy Adamson26699402012-05-30 16:12:24 -04003851 /* Deal with open(O_TRUNC) */
3852 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003853 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003854
3855 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003856 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003857 return 0;
3858
Trond Myklebustd5308382005-11-04 15:33:38 -05003859 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003860 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003861
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003862 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003863 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003864 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003865 }
3866
David Quigley14c43f72013-05-22 12:50:43 -04003867 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3868 if (IS_ERR(label))
3869 return PTR_ERR(label);
3870
NeilBrown29b59f92016-10-13 15:26:47 +11003871 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003872 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003873 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003874 nfs_setsecurity(inode, fattr, label);
3875 }
David Quigley14c43f72013-05-22 12:50:43 -04003876 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 return status;
3878}
3879
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003880static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3881 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003882 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003883{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003884 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003885 int status;
3886 struct nfs4_lookup_arg args = {
3887 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003888 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003889 .name = name,
3890 };
3891 struct nfs4_lookup_res res = {
3892 .server = server,
3893 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003894 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003895 .fh = fhandle,
3896 };
3897 struct rpc_message msg = {
3898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3899 .rpc_argp = &args,
3900 .rpc_resp = &res,
3901 };
3902
David Quigleyaa9c2662013-05-22 12:50:44 -04003903 args.bitmask = nfs4_bitmask(server, label);
3904
David Howells2b3de442006-08-22 20:06:09 -04003905 nfs_fattr_init(fattr);
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003908 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 dprintk("NFS reply lookup: %d\n", status);
3910 return status;
3911}
3912
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003913static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003914{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003915 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003916 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003917 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3918 fattr->nlink = 2;
3919}
3920
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003921static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003922 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003923 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003924{
3925 struct nfs4_exception exception = { };
3926 struct rpc_clnt *client = *clnt;
3927 int err;
3928 do {
David Quigley1775fd32013-05-22 12:50:42 -04003929 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003930 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003931 switch (err) {
3932 case -NFS4ERR_BADNAME:
3933 err = -ENOENT;
3934 goto out;
3935 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003936 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003937 if (err == -NFS4ERR_MOVED)
3938 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003939 goto out;
3940 case -NFS4ERR_WRONGSEC:
3941 err = -EPERM;
3942 if (client != *clnt)
3943 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003944 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003945 if (IS_ERR(client))
3946 return PTR_ERR(client);
3947
3948 exception.retry = 1;
3949 break;
3950 default:
3951 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3952 }
3953 } while (exception.retry);
3954
3955out:
3956 if (err == 0)
3957 *clnt = client;
3958 else if (client != *clnt)
3959 rpc_shutdown_client(client);
3960
3961 return err;
3962}
3963
Al Virobeffb8f2016-07-20 16:34:42 -04003964static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003965 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3966 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003968 int status;
3969 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003970
David Quigley1775fd32013-05-22 12:50:42 -04003971 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003972 if (client != NFS_CLIENT(dir)) {
3973 rpc_shutdown_client(client);
3974 nfs_fixup_secinfo_attributes(fattr);
3975 }
3976 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003979struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003980nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003981 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3982{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003983 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003984 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003985
David Quigley1775fd32013-05-22 12:50:42 -04003986 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003987 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003988 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003989 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003990}
3991
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003992static int _nfs4_proc_lookupp(struct inode *inode,
3993 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3994 struct nfs4_label *label)
3995{
3996 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3997 struct nfs_server *server = NFS_SERVER(inode);
3998 int status;
3999 struct nfs4_lookupp_arg args = {
4000 .bitmask = server->attr_bitmask,
4001 .fh = NFS_FH(inode),
4002 };
4003 struct nfs4_lookupp_res res = {
4004 .server = server,
4005 .fattr = fattr,
4006 .label = label,
4007 .fh = fhandle,
4008 };
4009 struct rpc_message msg = {
4010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4011 .rpc_argp = &args,
4012 .rpc_resp = &res,
4013 };
4014
4015 args.bitmask = nfs4_bitmask(server, label);
4016
4017 nfs_fattr_init(fattr);
4018
4019 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4020 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4021 &res.seq_res, 0);
4022 dprintk("NFS reply lookupp: %d\n", status);
4023 return status;
4024}
4025
4026static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4027 struct nfs_fattr *fattr, struct nfs4_label *label)
4028{
4029 struct nfs4_exception exception = { };
4030 int err;
4031 do {
4032 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4033 trace_nfs4_lookupp(inode, err);
4034 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4035 &exception);
4036 } while (exception.retry);
4037 return err;
4038}
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4041{
Trond Myklebust76b32992007-08-10 17:45:11 -04004042 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 struct nfs4_accessargs args = {
4044 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004045 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004046 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004048 struct nfs4_accessres res = {
4049 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004050 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 struct rpc_message msg = {
4052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4053 .rpc_argp = &args,
4054 .rpc_resp = &res,
4055 .rpc_cred = entry->cred,
4056 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004057 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Trond Myklebustc407d412010-04-16 16:22:48 -04004059 res.fattr = nfs_alloc_fattr();
4060 if (res.fattr == NULL)
4061 return -ENOMEM;
4062
Bryan Schumaker7c513052011-03-24 17:12:24 +00004063 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004065 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004066 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004068 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return status;
4070}
4071
4072static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4073{
4074 struct nfs4_exception exception = { };
4075 int err;
4076 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004077 err = _nfs4_proc_access(inode, entry);
4078 trace_nfs4_access(inode, err);
4079 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 &exception);
4081 } while (exception.retry);
4082 return err;
4083}
4084
4085/*
4086 * TODO: For the time being, we don't try to get any attributes
4087 * along with any of the zero-copy operations READ, READDIR,
4088 * READLINK, WRITE.
4089 *
4090 * In the case of the first three, we want to put the GETATTR
4091 * after the read-type operation -- this is because it is hard
4092 * to predict the length of a GETATTR response in v4, and thus
4093 * align the READ data correctly. This means that the GETATTR
4094 * may end up partially falling into the page cache, and we should
4095 * shift it into the 'tail' of the xdr_buf before processing.
4096 * To do this efficiently, we need to know the total length
4097 * of data received, which doesn't seem to be available outside
4098 * of the RPC layer.
4099 *
4100 * In the case of WRITE, we also want to put the GETATTR after
4101 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004102 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 *
4104 * Both of these changes to the XDR layer would in fact be quite
4105 * minor, but I decided to leave them for a subsequent patch.
4106 */
4107static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4108 unsigned int pgbase, unsigned int pglen)
4109{
4110 struct nfs4_readlink args = {
4111 .fh = NFS_FH(inode),
4112 .pgbase = pgbase,
4113 .pglen = pglen,
4114 .pages = &page,
4115 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004116 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4119 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004120 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 };
4122
Bryan Schumaker7c513052011-03-24 17:12:24 +00004123 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 -07004124}
4125
4126static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4127 unsigned int pgbase, unsigned int pglen)
4128{
4129 struct nfs4_exception exception = { };
4130 int err;
4131 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004132 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4133 trace_nfs4_readlink(inode, err);
4134 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 &exception);
4136 } while (exception.retry);
4137 return err;
4138}
4139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004141 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143static int
4144nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004145 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004147 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004148 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004149 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 int status = 0;
4152
NeilBrown532d4de2016-10-13 15:26:47 +11004153 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004154 if (IS_ERR(ctx))
4155 return PTR_ERR(ctx);
4156
David Quigleyaa9c2662013-05-22 12:50:44 -04004157 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4158
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004159 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4160 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004161 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 if (IS_ERR(state)) {
4163 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004164 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004167 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004168 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 return status;
4170}
4171
Al Virobeffb8f2016-07-20 16:34:42 -04004172static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Trond Myklebust16e42952005-10-27 22:12:44 -04004174 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004175 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004177 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004179 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004180 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4184 .rpc_argp = &args,
4185 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004187 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004188 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004189
Bryan Schumaker7c513052011-03-24 17:12:24 +00004190 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004191 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004192 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 return status;
4194}
4195
Al Virobeffb8f2016-07-20 16:34:42 -04004196static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
4198 struct nfs4_exception exception = { };
4199 int err;
4200 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004201 err = _nfs4_proc_remove(dir, name);
4202 trace_nfs4_remove(dir, name, err);
4203 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 &exception);
4205 } while (exception.retry);
4206 return err;
4207}
4208
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004209static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004211 struct nfs_server *server = NFS_SERVER(dir);
4212 struct nfs_removeargs *args = msg->rpc_argp;
4213 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004215 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004217 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004218
4219 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004222static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4223{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004224 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004225 &data->args.seq_args,
4226 &data->res.seq_res,
4227 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004230static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004232 struct nfs_unlinkdata *data = task->tk_calldata;
4233 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004234
Trond Myklebust14516c32010-07-31 14:29:06 -04004235 if (!nfs4_sequence_done(task, &res->seq_res))
4236 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004237 if (nfs4_async_handle_error(task, res->server, NULL,
4238 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004239 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004240 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004241 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004242 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243}
4244
Jeff Laytond3d41522010-09-17 17:31:57 -04004245static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4246{
4247 struct nfs_server *server = NFS_SERVER(dir);
4248 struct nfs_renameargs *arg = msg->rpc_argp;
4249 struct nfs_renameres *res = msg->rpc_resp;
4250
4251 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004252 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004253 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004254}
4255
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004256static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4257{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004258 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004259 &data->args.seq_args,
4260 &data->res.seq_res,
4261 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004262}
4263
4264static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4265 struct inode *new_dir)
4266{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004267 struct nfs_renamedata *data = task->tk_calldata;
4268 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004269
4270 if (!nfs4_sequence_done(task, &res->seq_res))
4271 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004272 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004273 return 0;
4274
Trond Myklebustc733c492017-01-11 12:32:26 -05004275 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004276 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004277 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004278 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004279 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004280 return 1;
4281}
4282
Al Virobeffb8f2016-07-20 16:34:42 -04004283static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004285 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct nfs4_link_arg arg = {
4287 .fh = NFS_FH(inode),
4288 .dir_fh = NFS_FH(dir),
4289 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004290 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004292 struct nfs4_link_res res = {
4293 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004294 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4298 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004299 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004301 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Trond Myklebust136f2622010-04-16 16:22:49 -04004303 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004304 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004305 goto out;
4306
David Quigley14c43f72013-05-22 12:50:43 -04004307 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4308 if (IS_ERR(res.label)) {
4309 status = PTR_ERR(res.label);
4310 goto out;
4311 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004312 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004313
Bryan Schumaker7c513052011-03-24 17:12:24 +00004314 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004315 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004316 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004317 status = nfs_post_op_update_inode(inode, res.fattr);
4318 if (!status)
4319 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004320 }
David Quigley14c43f72013-05-22 12:50:43 -04004321
4322
4323 nfs4_label_free(res.label);
4324
Trond Myklebust136f2622010-04-16 16:22:49 -04004325out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004326 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return status;
4328}
4329
Al Virobeffb8f2016-07-20 16:34:42 -04004330static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
4332 struct nfs4_exception exception = { };
4333 int err;
4334 do {
4335 err = nfs4_handle_exception(NFS_SERVER(inode),
4336 _nfs4_proc_link(inode, dir, name),
4337 &exception);
4338 } while (exception.retry);
4339 return err;
4340}
4341
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004342struct nfs4_createdata {
4343 struct rpc_message msg;
4344 struct nfs4_create_arg arg;
4345 struct nfs4_create_res res;
4346 struct nfs_fh fh;
4347 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004348 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349};
4350
4351static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004352 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004353{
4354 struct nfs4_createdata *data;
4355
4356 data = kzalloc(sizeof(*data), GFP_KERNEL);
4357 if (data != NULL) {
4358 struct nfs_server *server = NFS_SERVER(dir);
4359
David Quigley14c43f72013-05-22 12:50:43 -04004360 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4361 if (IS_ERR(data->label))
4362 goto out_free;
4363
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004364 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4365 data->msg.rpc_argp = &data->arg;
4366 data->msg.rpc_resp = &data->res;
4367 data->arg.dir_fh = NFS_FH(dir);
4368 data->arg.server = server;
4369 data->arg.name = name;
4370 data->arg.attrs = sattr;
4371 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004372 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004373 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004374 data->res.server = server;
4375 data->res.fh = &data->fh;
4376 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004377 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004378 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004379 }
4380 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004381out_free:
4382 kfree(data);
4383 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004384}
4385
4386static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4387{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004388 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004389 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004391 update_changeattr(dir, &data->res.dir_cinfo,
4392 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004393 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004394 }
4395 return status;
4396}
4397
4398static void nfs4_free_createdata(struct nfs4_createdata *data)
4399{
David Quigley14c43f72013-05-22 12:50:43 -04004400 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 kfree(data);
4402}
4403
Chuck Lever4f390c12006-08-22 20:06:22 -04004404static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004405 struct page *page, unsigned int len, struct iattr *sattr,
4406 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004408 struct nfs4_createdata *data;
4409 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Chuck Lever94a6d752006-08-22 20:06:23 -04004411 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004413
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 status = -ENOMEM;
4415 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4416 if (data == NULL)
4417 goto out;
4418
4419 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4420 data->arg.u.symlink.pages = &page;
4421 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004422 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004424 status = nfs4_do_create(dir, dentry, data);
4425
4426 nfs4_free_createdata(data);
4427out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 return status;
4429}
4430
Chuck Lever4f390c12006-08-22 20:06:22 -04004431static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004432 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
4434 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004435 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004437
4438 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004441 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4442 trace_nfs4_symlink(dir, &dentry->d_name, err);
4443 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 &exception);
4445 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004446
4447 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return err;
4449}
4450
4451static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004452 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004454 struct nfs4_createdata *data;
4455 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4458 if (data == NULL)
4459 goto out;
4460
David Quigley1775fd32013-05-22 12:50:42 -04004461 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004462 status = nfs4_do_create(dir, dentry, data);
4463
4464 nfs4_free_createdata(data);
4465out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return status;
4467}
4468
4469static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4470 struct iattr *sattr)
4471{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004472 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004474 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004476
David Quigleyaa9c2662013-05-22 12:50:44 -04004477 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4478
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004479 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4480 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004482 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4483 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4484 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 &exception);
4486 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004487 nfs4_label_release_security(label);
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 return err;
4490}
4491
4492static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004493 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
David Howells2b0143b2015-03-17 22:25:59 +00004495 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 struct nfs4_readdir_arg args = {
4497 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004498 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 .pgbase = 0,
4500 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004501 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004502 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 };
4504 struct nfs4_readdir_res res;
4505 struct rpc_message msg = {
4506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4507 .rpc_argp = &args,
4508 .rpc_resp = &res,
4509 .rpc_cred = cred,
4510 };
4511 int status;
4512
Al Viro6de14722013-09-16 10:53:17 -04004513 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4514 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004515 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004516 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004518 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 -05004519 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004520 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004521 status += args.pgbase;
4522 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004523
4524 nfs_invalidate_atime(dir);
4525
Harvey Harrison3110ff82008-05-02 13:42:44 -07004526 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return status;
4528}
4529
4530static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004531 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
4533 struct nfs4_exception exception = { };
4534 int err;
4535 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004536 err = _nfs4_proc_readdir(dentry, cred, cookie,
4537 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004538 trace_nfs4_readdir(d_inode(dentry), err);
4539 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 &exception);
4541 } while (exception.retry);
4542 return err;
4543}
4544
4545static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004546 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004548 struct nfs4_createdata *data;
4549 int mode = sattr->ia_mode;
4550 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4553 if (data == NULL)
4554 goto out;
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004557 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004559 data->arg.ftype = NF4BLK;
4560 data->arg.u.device.specdata1 = MAJOR(rdev);
4561 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 }
4563 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004564 data->arg.ftype = NF4CHR;
4565 data->arg.u.device.specdata1 = MAJOR(rdev);
4566 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004567 } else if (!S_ISSOCK(mode)) {
4568 status = -EINVAL;
4569 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 }
David Quigley1775fd32013-05-22 12:50:42 -04004571
David Quigleyaa9c2662013-05-22 12:50:44 -04004572 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004573 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004574out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004575 nfs4_free_createdata(data);
4576out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return status;
4578}
4579
4580static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4581 struct iattr *sattr, dev_t rdev)
4582{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004583 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004585 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004587
David Quigleyaa9c2662013-05-22 12:50:44 -04004588 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4589
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004590 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4591 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004593 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4594 trace_nfs4_mknod(dir, &dentry->d_name, err);
4595 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 &exception);
4597 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004598
4599 nfs4_label_release_security(label);
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 return err;
4602}
4603
4604static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4605 struct nfs_fsstat *fsstat)
4606{
4607 struct nfs4_statfs_arg args = {
4608 .fh = fhandle,
4609 .bitmask = server->attr_bitmask,
4610 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004611 struct nfs4_statfs_res res = {
4612 .fsstat = fsstat,
4613 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 struct rpc_message msg = {
4615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4616 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004617 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 };
4619
Trond Myklebust0e574af2005-10-27 22:12:38 -04004620 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004621 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622}
4623
4624static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4625{
4626 struct nfs4_exception exception = { };
4627 int err;
4628 do {
4629 err = nfs4_handle_exception(server,
4630 _nfs4_proc_statfs(server, fhandle, fsstat),
4631 &exception);
4632 } while (exception.retry);
4633 return err;
4634}
4635
4636static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4637 struct nfs_fsinfo *fsinfo)
4638{
4639 struct nfs4_fsinfo_arg args = {
4640 .fh = fhandle,
4641 .bitmask = server->attr_bitmask,
4642 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004643 struct nfs4_fsinfo_res res = {
4644 .fsinfo = fsinfo,
4645 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 struct rpc_message msg = {
4647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4648 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004649 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 };
4651
Bryan Schumaker7c513052011-03-24 17:12:24 +00004652 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
4655static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4656{
4657 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004658 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 int err;
4660
4661 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004662 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004663 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004664 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004665 nfs4_set_lease_period(server->nfs_client,
4666 fsinfo->lease_time * HZ,
4667 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004668 break;
4669 }
4670 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 } while (exception.retry);
4672 return err;
4673}
4674
4675static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4676{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004677 int error;
4678
Trond Myklebust0e574af2005-10-27 22:12:38 -04004679 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004680 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004681 if (error == 0) {
4682 /* block layout checks this! */
4683 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004684 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004685 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004686
4687 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688}
4689
4690static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4691 struct nfs_pathconf *pathconf)
4692{
4693 struct nfs4_pathconf_arg args = {
4694 .fh = fhandle,
4695 .bitmask = server->attr_bitmask,
4696 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004697 struct nfs4_pathconf_res res = {
4698 .pathconf = pathconf,
4699 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 struct rpc_message msg = {
4701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4702 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004703 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 };
4705
4706 /* None of the pathconf attributes are mandatory to implement */
4707 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4708 memset(pathconf, 0, sizeof(*pathconf));
4709 return 0;
4710 }
4711
Trond Myklebust0e574af2005-10-27 22:12:38 -04004712 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004713 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
4716static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4717 struct nfs_pathconf *pathconf)
4718{
4719 struct nfs4_exception exception = { };
4720 int err;
4721
4722 do {
4723 err = nfs4_handle_exception(server,
4724 _nfs4_proc_pathconf(server, fhandle, pathconf),
4725 &exception);
4726 } while (exception.retry);
4727 return err;
4728}
4729
Trond Myklebust5521abf2013-03-16 20:54:34 -04004730int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004731 const struct nfs_open_context *ctx,
4732 const struct nfs_lock_context *l_ctx,
4733 fmode_t fmode)
4734{
NeilBrown17393472016-10-13 15:26:47 +11004735 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004736}
4737EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4738
Trond Myklebust5521abf2013-03-16 20:54:34 -04004739static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4740 const struct nfs_open_context *ctx,
4741 const struct nfs_lock_context *l_ctx,
4742 fmode_t fmode)
4743{
4744 nfs4_stateid current_stateid;
4745
Trond Myklebuste1253be2014-03-05 08:44:23 -05004746 /* If the current stateid represents a lost lock, then exit */
4747 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4748 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004749 return nfs4_stateid_match(stateid, &current_stateid);
4750}
4751
4752static bool nfs4_error_stateid_expired(int err)
4753{
4754 switch (err) {
4755 case -NFS4ERR_DELEG_REVOKED:
4756 case -NFS4ERR_ADMIN_REVOKED:
4757 case -NFS4ERR_BAD_STATEID:
4758 case -NFS4ERR_STALE_STATEID:
4759 case -NFS4ERR_OLD_STATEID:
4760 case -NFS4ERR_OPENMODE:
4761 case -NFS4ERR_EXPIRED:
4762 return true;
4763 }
4764 return false;
4765}
4766
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004767static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004769 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004771 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004772 if (task->tk_status < 0) {
4773 struct nfs4_exception exception = {
4774 .inode = hdr->inode,
4775 .state = hdr->args.context->state,
4776 .stateid = &hdr->args.stateid,
4777 };
4778 task->tk_status = nfs4_async_handle_exception(task,
4779 server, task->tk_status, &exception);
4780 if (exception.retry) {
4781 rpc_restart_call_prepare(task);
4782 return -EAGAIN;
4783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004785
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004787 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
Trond Myklebust5521abf2013-03-16 20:54:34 -04004791static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004792 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004793{
4794
4795 if (!nfs4_error_stateid_expired(task->tk_status) ||
4796 nfs4_stateid_is_current(&args->stateid,
4797 args->context,
4798 args->lock_context,
4799 FMODE_READ))
4800 return false;
4801 rpc_restart_call_prepare(task);
4802 return true;
4803}
4804
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004806{
4807
4808 dprintk("--> %s\n", __func__);
4809
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004810 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004811 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004812 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004813 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004814 if (task->tk_status > 0)
4815 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4817 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004818}
4819
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004820static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4821 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004823 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004824 if (!hdr->pgio_done_cb)
4825 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004826 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004827 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004830static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4831 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004832{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004833 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004834 &hdr->args.seq_args,
4835 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004836 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004837 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004838 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4839 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004840 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004841 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004842 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004843 return -EIO;
4844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845}
4846
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004847static int nfs4_write_done_cb(struct rpc_task *task,
4848 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004850 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004851
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004852 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004853 if (task->tk_status < 0) {
4854 struct nfs4_exception exception = {
4855 .inode = hdr->inode,
4856 .state = hdr->args.context->state,
4857 .stateid = &hdr->args.stateid,
4858 };
4859 task->tk_status = nfs4_async_handle_exception(task,
4860 NFS_SERVER(inode), task->tk_status,
4861 &exception);
4862 if (exception.retry) {
4863 rpc_restart_call_prepare(task);
4864 return -EAGAIN;
4865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004867 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004868 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004869 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004870 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
4873
Trond Myklebust5521abf2013-03-16 20:54:34 -04004874static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004875 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004876{
4877
4878 if (!nfs4_error_stateid_expired(task->tk_status) ||
4879 nfs4_stateid_is_current(&args->stateid,
4880 args->context,
4881 args->lock_context,
4882 FMODE_WRITE))
4883 return false;
4884 rpc_restart_call_prepare(task);
4885 return true;
4886}
4887
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004888static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004889{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004890 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004891 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004892 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004893 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4895 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004896}
4897
Trond Myklebust5a37f852012-04-28 14:55:16 -04004898static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004899bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004900{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004901 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004902 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004903 return false;
4904 /* Otherwise, request attributes if and only if we don't hold
4905 * a delegation
4906 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004907 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004908}
Fred Isamana69aef12011-03-03 15:13:47 +00004909
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004910static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4911 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004913 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004914
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004915 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4916 hdr->args.bitmask = NULL;
4917 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004918 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004919 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004920
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004921 if (!hdr->pgio_done_cb)
4922 hdr->pgio_done_cb = nfs4_write_done_cb;
4923 hdr->res.server = server;
4924 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004926 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004927 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928}
4929
Fred Isaman0b7c0152012-04-20 14:47:39 -04004930static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4931{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004932 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004933 &data->args.seq_args,
4934 &data->res.seq_res,
4935 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Fred Isaman0b7c0152012-04-20 14:47:39 -04004938static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004941
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004942 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004943 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4944 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004945 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004946 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
Fred Isaman0b7c0152012-04-20 14:47:39 -04004951static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004952{
4953 if (!nfs4_sequence_done(task, &data->res.seq_res))
4954 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004955 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004956}
4957
Fred Isaman0b7c0152012-04-20 14:47:39 -04004958static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004960 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004961
Fred Isaman0b7c0152012-04-20 14:47:39 -04004962 if (data->commit_done_cb == NULL)
4963 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004964 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004965 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004966 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
4968
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004969struct nfs4_renewdata {
4970 struct nfs_client *client;
4971 unsigned long timestamp;
4972};
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974/*
4975 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4976 * standalone procedure for queueing an asynchronous RENEW.
4977 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004978static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004979{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004980 struct nfs4_renewdata *data = calldata;
4981 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004982
Elena Reshetova212bf412017-10-20 12:53:38 +03004983 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004984 nfs4_schedule_state_renewal(clp);
4985 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004986 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004987}
4988
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004989static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004991 struct nfs4_renewdata *data = calldata;
4992 struct nfs_client *clp = data->client;
4993 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004995 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004996 switch (task->tk_status) {
4997 case 0:
4998 break;
4999 case -NFS4ERR_LEASE_MOVED:
5000 nfs4_schedule_lease_moved_recovery(clp);
5001 break;
5002 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005003 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005004 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5005 return;
5006 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005007 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005008 return;
5009 }
5010 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005012 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005015static const struct rpc_call_ops nfs4_renew_ops = {
5016 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005017 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005018};
5019
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005020static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
5022 struct rpc_message msg = {
5023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5024 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005025 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005027 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005029 if (renew_flags == 0)
5030 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005031 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005032 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005033 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005034 if (data == NULL) {
5035 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005036 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005037 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005038 data->client = clp;
5039 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005040 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005041 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042}
5043
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005044static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
5046 struct rpc_message msg = {
5047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5048 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005049 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 };
5051 unsigned long now = jiffies;
5052 int status;
5053
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005054 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 if (status < 0)
5056 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005057 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 return 0;
5059}
5060
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005061static inline int nfs4_server_supports_acls(struct nfs_server *server)
5062{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005063 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005064}
5065
Trond Myklebust21f498c2012-08-24 10:59:25 -04005066/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5067 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005068 * the stack.
5069 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005070#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005071
Neil Hormane9e3d722011-03-04 19:26:03 -05005072static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005073 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005074{
5075 struct page *newpage, **spages;
5076 int rc = 0;
5077 size_t len;
5078 spages = pages;
5079
5080 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005081 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005082 newpage = alloc_page(GFP_KERNEL);
5083
5084 if (newpage == NULL)
5085 goto unwind;
5086 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005087 buf += len;
5088 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005089 *pages++ = newpage;
5090 rc++;
5091 } while (buflen != 0);
5092
5093 return rc;
5094
5095unwind:
5096 for(; rc > 0; rc--)
5097 __free_page(spages[rc-1]);
5098 return -ENOMEM;
5099}
5100
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005101struct nfs4_cached_acl {
5102 int cached;
5103 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005104 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005105};
5106
5107static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005108{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005109 struct nfs_inode *nfsi = NFS_I(inode);
5110
5111 spin_lock(&inode->i_lock);
5112 kfree(nfsi->nfs4_acl);
5113 nfsi->nfs4_acl = acl;
5114 spin_unlock(&inode->i_lock);
5115}
5116
5117static void nfs4_zap_acl_attr(struct inode *inode)
5118{
5119 nfs4_set_cached_acl(inode, NULL);
5120}
5121
5122static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5123{
5124 struct nfs_inode *nfsi = NFS_I(inode);
5125 struct nfs4_cached_acl *acl;
5126 int ret = -ENOENT;
5127
5128 spin_lock(&inode->i_lock);
5129 acl = nfsi->nfs4_acl;
5130 if (acl == NULL)
5131 goto out;
5132 if (buf == NULL) /* user is just asking for length */
5133 goto out_len;
5134 if (acl->cached == 0)
5135 goto out;
5136 ret = -ERANGE; /* see getxattr(2) man page */
5137 if (acl->len > buflen)
5138 goto out;
5139 memcpy(buf, acl->data, acl->len);
5140out_len:
5141 ret = acl->len;
5142out:
5143 spin_unlock(&inode->i_lock);
5144 return ret;
5145}
5146
Sachin Prabhu5794d212012-04-17 14:36:40 +01005147static 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 +00005148{
5149 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005150 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005151
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005152 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005153 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005154 if (acl == NULL)
5155 goto out;
5156 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005157 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005158 } else {
5159 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5160 if (acl == NULL)
5161 goto out;
5162 acl->cached = 0;
5163 }
5164 acl->len = acl_len;
5165out:
5166 nfs4_set_cached_acl(inode, acl);
5167}
5168
Andy Adamsonbf118a32011-12-07 11:55:27 -05005169/*
5170 * The getxattr API returns the required buffer length when called with a
5171 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5172 * the required buf. On a NULL buf, we send a page of data to the server
5173 * guessing that the ACL request can be serviced by a page. If so, we cache
5174 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5175 * the cache. If not so, we throw away the page, and cache the required
5176 * length. The next getxattr call will then produce another round trip to
5177 * the server, this time with the input buf of the required size.
5178 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005179static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005180{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005181 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005182 struct nfs_getaclargs args = {
5183 .fh = NFS_FH(inode),
5184 .acl_pages = pages,
5185 .acl_len = buflen,
5186 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005187 struct nfs_getaclres res = {
5188 .acl_len = buflen,
5189 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005190 struct rpc_message msg = {
5191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5192 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005193 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005194 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005195 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005196 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005197
Trond Myklebust21f498c2012-08-24 10:59:25 -04005198 if (npages > ARRAY_SIZE(pages))
5199 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005200
Andy Adamsonbf118a32011-12-07 11:55:27 -05005201 for (i = 0; i < npages; i++) {
5202 pages[i] = alloc_page(GFP_KERNEL);
5203 if (!pages[i])
5204 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005205 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005206
5207 /* for decoding across pages */
5208 res.acl_scratch = alloc_page(GFP_KERNEL);
5209 if (!res.acl_scratch)
5210 goto out_free;
5211
Andy Adamsonbf118a32011-12-07 11:55:27 -05005212 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005213
Peng Taode040be2012-01-10 22:42:47 +08005214 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005215 __func__, buf, buflen, npages, args.acl_len);
5216 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5217 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005218 if (ret)
5219 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005220
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005221 /* Handle the case where the passed-in buffer is too short */
5222 if (res.acl_flags & NFS4_ACL_TRUNC) {
5223 /* Did the user only issue a request for the acl length? */
5224 if (buf == NULL)
5225 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005226 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005227 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005228 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005229 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005230 if (buf) {
5231 if (res.acl_len > buflen) {
5232 ret = -ERANGE;
5233 goto out_free;
5234 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005235 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005236 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005237out_ok:
5238 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005239out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005240 for (i = 0; i < npages; i++)
5241 if (pages[i])
5242 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005243 if (res.acl_scratch)
5244 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005245 return ret;
5246}
5247
Trond Myklebust16b42892006-08-24 12:27:15 -04005248static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5249{
5250 struct nfs4_exception exception = { };
5251 ssize_t ret;
5252 do {
5253 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005254 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005255 if (ret >= 0)
5256 break;
5257 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5258 } while (exception.retry);
5259 return ret;
5260}
5261
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005262static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5263{
5264 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005265 int ret;
5266
5267 if (!nfs4_server_supports_acls(server))
5268 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005269 ret = nfs_revalidate_inode(server, inode);
5270 if (ret < 0)
5271 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005272 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5273 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005274 ret = nfs4_read_cached_acl(inode, buf, buflen);
5275 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005276 /* -ENOENT is returned if there is no ACL or if there is an ACL
5277 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005278 return ret;
5279 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005280}
5281
Trond Myklebust16b42892006-08-24 12:27:15 -04005282static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005283{
5284 struct nfs_server *server = NFS_SERVER(inode);
5285 struct page *pages[NFS4ACL_MAXPAGES];
5286 struct nfs_setaclargs arg = {
5287 .fh = NFS_FH(inode),
5288 .acl_pages = pages,
5289 .acl_len = buflen,
5290 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005291 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005292 struct rpc_message msg = {
5293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5294 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005295 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005296 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005297 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005298 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005299
5300 if (!nfs4_server_supports_acls(server))
5301 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005302 if (npages > ARRAY_SIZE(pages))
5303 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005304 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005305 if (i < 0)
5306 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005307 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005308 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005309
5310 /*
5311 * Free each page after tx, so the only ref left is
5312 * held by the network stack
5313 */
5314 for (; i > 0; i--)
5315 put_page(pages[i-1]);
5316
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005317 /*
5318 * Acl update can result in inode attribute update.
5319 * so mark the attribute cache invalid.
5320 */
5321 spin_lock(&inode->i_lock);
5322 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5323 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005324 nfs_access_zap_cache(inode);
5325 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005326 return ret;
5327}
5328
Trond Myklebust16b42892006-08-24 12:27:15 -04005329static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5330{
5331 struct nfs4_exception exception = { };
5332 int err;
5333 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005334 err = __nfs4_proc_set_acl(inode, buf, buflen);
5335 trace_nfs4_set_acl(inode, err);
5336 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005337 &exception);
5338 } while (exception.retry);
5339 return err;
5340}
5341
David Quigleyaa9c2662013-05-22 12:50:44 -04005342#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5343static int _nfs4_get_security_label(struct inode *inode, void *buf,
5344 size_t buflen)
5345{
5346 struct nfs_server *server = NFS_SERVER(inode);
5347 struct nfs_fattr fattr;
5348 struct nfs4_label label = {0, 0, buflen, buf};
5349
5350 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005351 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005352 .fh = NFS_FH(inode),
5353 .bitmask = bitmask,
5354 };
5355 struct nfs4_getattr_res res = {
5356 .fattr = &fattr,
5357 .label = &label,
5358 .server = server,
5359 };
5360 struct rpc_message msg = {
5361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005362 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005363 .rpc_resp = &res,
5364 };
5365 int ret;
5366
5367 nfs_fattr_init(&fattr);
5368
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005369 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005370 if (ret)
5371 return ret;
5372 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5373 return -ENOENT;
5374 if (buflen < label.len)
5375 return -ERANGE;
5376 return 0;
5377}
5378
5379static int nfs4_get_security_label(struct inode *inode, void *buf,
5380 size_t buflen)
5381{
5382 struct nfs4_exception exception = { };
5383 int err;
5384
5385 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5386 return -EOPNOTSUPP;
5387
5388 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005389 err = _nfs4_get_security_label(inode, buf, buflen);
5390 trace_nfs4_get_security_label(inode, err);
5391 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005392 &exception);
5393 } while (exception.retry);
5394 return err;
5395}
5396
5397static int _nfs4_do_set_security_label(struct inode *inode,
5398 struct nfs4_label *ilabel,
5399 struct nfs_fattr *fattr,
5400 struct nfs4_label *olabel)
5401{
5402
5403 struct iattr sattr = {0};
5404 struct nfs_server *server = NFS_SERVER(inode);
5405 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005406 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005407 .fh = NFS_FH(inode),
5408 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005409 .server = server,
5410 .bitmask = bitmask,
5411 .label = ilabel,
5412 };
5413 struct nfs_setattrres res = {
5414 .fattr = fattr,
5415 .label = olabel,
5416 .server = server,
5417 };
5418 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5420 .rpc_argp = &arg,
5421 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005422 };
5423 int status;
5424
Jeff Layton12207f62013-11-01 10:49:32 -04005425 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005426
Jeff Layton12207f62013-11-01 10:49:32 -04005427 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005428 if (status)
5429 dprintk("%s failed: %d\n", __func__, status);
5430
5431 return status;
5432}
5433
5434static int nfs4_do_set_security_label(struct inode *inode,
5435 struct nfs4_label *ilabel,
5436 struct nfs_fattr *fattr,
5437 struct nfs4_label *olabel)
5438{
5439 struct nfs4_exception exception = { };
5440 int err;
5441
5442 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005443 err = _nfs4_do_set_security_label(inode, ilabel,
5444 fattr, olabel);
5445 trace_nfs4_set_security_label(inode, err);
5446 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005447 &exception);
5448 } while (exception.retry);
5449 return err;
5450}
5451
5452static int
Al Viro59301222016-05-27 10:19:30 -04005453nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005454{
5455 struct nfs4_label ilabel, *olabel = NULL;
5456 struct nfs_fattr fattr;
5457 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005458 int status;
5459
5460 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5461 return -EOPNOTSUPP;
5462
5463 nfs_fattr_init(&fattr);
5464
5465 ilabel.pi = 0;
5466 ilabel.lfs = 0;
5467 ilabel.label = (char *)buf;
5468 ilabel.len = buflen;
5469
5470 cred = rpc_lookup_cred();
5471 if (IS_ERR(cred))
5472 return PTR_ERR(cred);
5473
5474 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5475 if (IS_ERR(olabel)) {
5476 status = -PTR_ERR(olabel);
5477 goto out;
5478 }
5479
5480 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5481 if (status == 0)
5482 nfs_setsecurity(inode, &fattr, olabel);
5483
5484 nfs4_label_free(olabel);
5485out:
5486 put_rpccred(cred);
5487 return status;
5488}
5489#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5490
5491
Chuck Leverf0920752012-05-21 22:45:41 -04005492static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5493 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005494{
5495 __be32 verf[2];
5496
Chuck Lever2c820d92012-05-21 22:45:33 -04005497 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5498 /* An impossible timestamp guarantees this value
5499 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005500 verf[0] = cpu_to_be32(U32_MAX);
5501 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005502 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005503 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005504 u64 ns = ktime_to_ns(nn->boot_time);
5505
5506 verf[0] = cpu_to_be32(ns >> 32);
5507 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005508 }
Chuck Levercd937102012-03-02 17:14:31 -05005509 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5510}
5511
Jeff Laytona3192682015-06-09 19:43:59 -04005512static int
5513nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005514{
Jeff Laytona3192682015-06-09 19:43:59 -04005515 size_t len;
5516 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005517
Trond Myklebustceb3a162015-01-03 15:16:04 -05005518 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005519 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005520
Jeff Laytona3192682015-06-09 19:43:59 -04005521 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005522 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005523 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5524 1 +
5525 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5526 1;
5527 rcu_read_unlock();
5528
5529 if (len > NFS4_OPAQUE_LIMIT + 1)
5530 return -EINVAL;
5531
5532 /*
5533 * Since this string is allocated at mount time, and held until the
5534 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5535 * about a memory-reclaim deadlock.
5536 */
5537 str = kmalloc(len, GFP_KERNEL);
5538 if (!str)
5539 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005540
Chuck Levere984a552012-09-14 17:24:21 -04005541 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005542 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005543 clp->cl_ipaddr,
5544 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5545 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005546 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005547
Jeff Laytona3192682015-06-09 19:43:59 -04005548 clp->cl_owner_id = str;
5549 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005550}
5551
Jeff Layton873e3852015-06-09 19:44:00 -04005552static int
5553nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005554{
Jeff Layton873e3852015-06-09 19:44:00 -04005555 size_t len;
5556 char *str;
5557
5558 len = 10 + 10 + 1 + 10 + 1 +
5559 strlen(nfs4_client_id_uniquifier) + 1 +
5560 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5561
5562 if (len > NFS4_OPAQUE_LIMIT + 1)
5563 return -EINVAL;
5564
5565 /*
5566 * Since this string is allocated at mount time, and held until the
5567 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5568 * about a memory-reclaim deadlock.
5569 */
5570 str = kmalloc(len, GFP_KERNEL);
5571 if (!str)
5572 return -ENOMEM;
5573
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005574 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005575 clp->rpc_ops->version, clp->cl_minorversion,
5576 nfs4_client_id_uniquifier,
5577 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005578 clp->cl_owner_id = str;
5579 return 0;
5580}
5581
5582static int
5583nfs4_init_uniform_client_string(struct nfs_client *clp)
5584{
Jeff Layton873e3852015-06-09 19:44:00 -04005585 size_t len;
5586 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005587
5588 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005589 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005590
5591 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005592 return nfs4_init_uniquifier_client_string(clp);
5593
5594 len = 10 + 10 + 1 + 10 + 1 +
5595 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5596
5597 if (len > NFS4_OPAQUE_LIMIT + 1)
5598 return -EINVAL;
5599
5600 /*
5601 * Since this string is allocated at mount time, and held until the
5602 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5603 * about a memory-reclaim deadlock.
5604 */
5605 str = kmalloc(len, GFP_KERNEL);
5606 if (!str)
5607 return -ENOMEM;
5608
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005609 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005610 clp->rpc_ops->version, clp->cl_minorversion,
5611 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005612 clp->cl_owner_id = str;
5613 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005614}
5615
Chuck Lever706cb8d2014-03-12 12:51:47 -04005616/*
5617 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5618 * services. Advertise one based on the address family of the
5619 * clientaddr.
5620 */
5621static unsigned int
5622nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5623{
5624 if (strchr(clp->cl_ipaddr, ':') != NULL)
5625 return scnprintf(buf, len, "tcp6");
5626 else
5627 return scnprintf(buf, len, "tcp");
5628}
5629
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005630static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5631{
5632 struct nfs4_setclientid *sc = calldata;
5633
5634 if (task->tk_status == 0)
5635 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5636}
5637
5638static const struct rpc_call_ops nfs4_setclientid_ops = {
5639 .rpc_call_done = nfs4_setclientid_done,
5640};
5641
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005642/**
5643 * nfs4_proc_setclientid - Negotiate client ID
5644 * @clp: state data structure
5645 * @program: RPC program for NFSv4 callback service
5646 * @port: IP port number for NFS4 callback service
5647 * @cred: RPC credential to use for this call
5648 * @res: where to place the result
5649 *
5650 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5651 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005652int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5653 unsigned short port, struct rpc_cred *cred,
5654 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655{
5656 nfs4_verifier sc_verifier;
5657 struct nfs4_setclientid setclientid = {
5658 .sc_verifier = &sc_verifier,
5659 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005660 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 };
5662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5664 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005665 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005666 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005668 struct rpc_task *task;
5669 struct rpc_task_setup task_setup_data = {
5670 .rpc_client = clp->cl_rpcclient,
5671 .rpc_message = &msg,
5672 .callback_ops = &nfs4_setclientid_ops,
5673 .callback_data = &setclientid,
5674 .flags = RPC_TASK_TIMEOUT,
5675 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005676 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
Chuck Leverde734832012-07-11 16:30:50 -04005678 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005679 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005680
5681 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5682 status = nfs4_init_uniform_client_string(clp);
5683 else
Jeff Laytona3192682015-06-09 19:43:59 -04005684 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005685
5686 if (status)
5687 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005688
Chuck Leverde734832012-07-11 16:30:50 -04005689 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005690 setclientid.sc_netid_len =
5691 nfs4_init_callback_netid(clp,
5692 setclientid.sc_netid,
5693 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005694 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005695 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 clp->cl_ipaddr, port >> 8, port & 255);
5697
Jeff Layton3a6bb732015-06-09 19:43:57 -04005698 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005699 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005700 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005701 task = rpc_run_task(&task_setup_data);
5702 if (IS_ERR(task)) {
5703 status = PTR_ERR(task);
5704 goto out;
5705 }
5706 status = task->tk_status;
5707 if (setclientid.sc_cred) {
5708 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5709 put_rpccred(setclientid.sc_cred);
5710 }
5711 rpc_put_task(task);
5712out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005713 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005714 dprintk("NFS reply setclientid: %d\n", status);
5715 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005718/**
5719 * nfs4_proc_setclientid_confirm - Confirm client ID
5720 * @clp: state data structure
5721 * @res: result of a previous SETCLIENTID
5722 * @cred: RPC credential to use for this call
5723 *
5724 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5725 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005726int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005727 struct nfs4_setclientid_res *arg,
5728 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 struct rpc_message msg = {
5731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005732 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005733 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 int status;
5736
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005737 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5738 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5739 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005740 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005741 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005742 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 return status;
5744}
5745
Trond Myklebustfe650402006-01-03 09:55:18 +01005746struct nfs4_delegreturndata {
5747 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005748 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005749 struct nfs_fh fh;
5750 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005751 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005752 struct {
5753 struct nfs4_layoutreturn_args arg;
5754 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005755 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005756 u32 roc_barrier;
5757 bool roc;
5758 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005759 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005760 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005761 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005762};
5763
Trond Myklebustfe650402006-01-03 09:55:18 +01005764static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5765{
5766 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005767 struct nfs4_exception exception = {
5768 .inode = data->inode,
5769 .stateid = &data->stateid,
5770 };
Andy Adamson938e1012009-04-01 09:22:28 -04005771
Trond Myklebust14516c32010-07-31 14:29:06 -04005772 if (!nfs4_sequence_done(task, &data->res.seq_res))
5773 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005774
Trond Myklebustca8acf82013-08-13 10:36:56 -04005775 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005776
5777 /* Handle Layoutreturn errors */
5778 if (data->args.lr_args && task->tk_status != 0) {
5779 switch(data->res.lr_ret) {
5780 default:
5781 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5782 break;
5783 case 0:
5784 data->args.lr_args = NULL;
5785 data->res.lr_res = NULL;
5786 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005787 case -NFS4ERR_OLD_STATEID:
5788 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005789 data->inode))
5790 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005791 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005792 case -NFS4ERR_ADMIN_REVOKED:
5793 case -NFS4ERR_DELEG_REVOKED:
5794 case -NFS4ERR_EXPIRED:
5795 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005796 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5797 case -NFS4ERR_WRONG_CRED:
5798 data->args.lr_args = NULL;
5799 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005800 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005801 }
5802 }
5803
Ricardo Labiaga79708862009-12-07 09:23:21 -05005804 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005805 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005806 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005807 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005808 case -NFS4ERR_ADMIN_REVOKED:
5809 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005810 case -NFS4ERR_EXPIRED:
5811 nfs4_free_revoked_stateid(data->res.server,
5812 data->args.stateid,
5813 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005814 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005815 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005816 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005817 task->tk_status = 0;
5818 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005819 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005820 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5821 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005822 task->tk_status = 0;
5823 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005824 case -NFS4ERR_ACCESS:
5825 if (data->args.bitmask) {
5826 data->args.bitmask = NULL;
5827 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005828 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005829 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005830 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005831 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005832 task->tk_status = nfs4_async_handle_exception(task,
5833 data->res.server, task->tk_status,
5834 &exception);
5835 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005836 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005837 }
5838 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005839 return;
5840lr_restart:
5841 data->res.lr_ret = 0;
5842out_restart:
5843 task->tk_status = 0;
5844 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005845}
5846
5847static void nfs4_delegreturn_release(void *calldata)
5848{
Peng Tao039b7562014-07-03 13:05:02 +08005849 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005850 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005851
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005852 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005853 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005854 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5855 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005856 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005857 nfs_iput_and_deactive(inode);
5858 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005859 kfree(calldata);
5860}
5861
Andy Adamson938e1012009-04-01 09:22:28 -04005862static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5863{
5864 struct nfs4_delegreturndata *d_data;
5865
5866 d_data = (struct nfs4_delegreturndata *)data;
5867
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005868 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005869 return;
5870
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005871 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005872 &d_data->args.seq_args,
5873 &d_data->res.seq_res,
5874 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005875}
Andy Adamson938e1012009-04-01 09:22:28 -04005876
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005877static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005878 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005879 .rpc_call_done = nfs4_delegreturn_done,
5880 .rpc_release = nfs4_delegreturn_release,
5881};
5882
Trond Myklebuste6f81072008-01-24 18:14:34 -05005883static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005884{
5885 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005886 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005887 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005888 struct rpc_message msg = {
5889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5890 .rpc_cred = cred,
5891 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005892 struct rpc_task_setup task_setup_data = {
5893 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005894 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005895 .callback_ops = &nfs4_delegreturn_ops,
5896 .flags = RPC_TASK_ASYNC,
5897 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005898 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005899
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005900 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005901 if (data == NULL)
5902 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005903 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005904
5905 nfs4_state_protect(server->nfs_client,
5906 NFS_SP4_MACH_CRED_CLEANUP,
5907 &task_setup_data.rpc_client, &msg);
5908
Trond Myklebustfe650402006-01-03 09:55:18 +01005909 data->args.fhandle = &data->fh;
5910 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005911 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005912 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005913 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005914 data->res.fattr = &data->fattr;
5915 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005916 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005917 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005918 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005919 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005920 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005921 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005922 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005923 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005924 if (data->lr.roc) {
5925 data->args.lr_args = &data->lr.arg;
5926 data->res.lr_res = &data->lr.res;
5927 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005928 } else if (data->lr.roc) {
5929 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5930 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005931 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005932
Trond Myklebustc970aa82007-07-14 15:39:59 -04005933 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005934 msg.rpc_argp = &data->args;
5935 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005936 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005937 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005938 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005939 if (!issync)
5940 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005941 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005942 if (status != 0)
5943 goto out;
5944 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005945out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005946 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005947 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948}
5949
Trond Myklebuste6f81072008-01-24 18:14:34 -05005950int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951{
5952 struct nfs_server *server = NFS_SERVER(inode);
5953 struct nfs4_exception exception = { };
5954 int err;
5955 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005956 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005957 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 switch (err) {
5959 case -NFS4ERR_STALE_STATEID:
5960 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 case 0:
5962 return 0;
5963 }
5964 err = nfs4_handle_exception(server, err, &exception);
5965 } while (exception.retry);
5966 return err;
5967}
5968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5970{
5971 struct inode *inode = state->inode;
5972 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005973 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005974 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005976 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005978 struct nfs_lockt_res res = {
5979 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 };
5981 struct rpc_message msg = {
5982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005983 .rpc_argp = &arg,
5984 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 .rpc_cred = state->owner->so_cred,
5986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 struct nfs4_lock_state *lsp;
5988 int status;
5989
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005990 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005991 status = nfs4_set_lock_state(state, request);
5992 if (status != 0)
5993 goto out;
5994 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005995 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005996 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005997 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005998 switch (status) {
5999 case 0:
6000 request->fl_type = F_UNLCK;
6001 break;
6002 case -NFS4ERR_DENIED:
6003 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006005 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006006 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006007out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 return status;
6009}
6010
6011static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6012{
6013 struct nfs4_exception exception = { };
6014 int err;
6015
6016 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006017 err = _nfs4_proc_getlk(state, cmd, request);
6018 trace_nfs4_get_lock(request, state, cmd, err);
6019 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 &exception);
6021 } while (exception.retry);
6022 return err;
6023}
6024
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006025struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006026 struct nfs_locku_args arg;
6027 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006028 struct nfs4_lock_state *lsp;
6029 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006030 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006031 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006032 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006033 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006034};
6035
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006036static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6037 struct nfs_open_context *ctx,
6038 struct nfs4_lock_state *lsp,
6039 struct nfs_seqid *seqid)
6040{
6041 struct nfs4_unlockdata *p;
6042 struct inode *inode = lsp->ls_state->inode;
6043
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006044 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006045 if (p == NULL)
6046 return NULL;
6047 p->arg.fh = NFS_FH(inode);
6048 p->arg.fl = &p->fl;
6049 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006050 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006051 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006052 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006053 /* Ensure we don't close file until we're done freeing locks! */
6054 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006055 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006056 memcpy(&p->fl, fl, sizeof(p->fl));
6057 p->server = NFS_SERVER(inode);
6058 return p;
6059}
6060
Trond Myklebust06f814a2006-01-03 09:55:07 +01006061static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006062{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006063 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006064 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006065 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006066 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006067 put_nfs_open_context(calldata->ctx);
6068 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006069}
6070
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006071static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006072{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006073 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006074
Trond Myklebust14516c32010-07-31 14:29:06 -04006075 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6076 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006077 switch (task->tk_status) {
6078 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006079 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006080 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006081 if (nfs4_update_lock_stateid(calldata->lsp,
6082 &calldata->res.stateid))
6083 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006084 case -NFS4ERR_ADMIN_REVOKED:
6085 case -NFS4ERR_EXPIRED:
6086 nfs4_free_revoked_stateid(calldata->server,
6087 &calldata->arg.stateid,
6088 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006089 case -NFS4ERR_BAD_STATEID:
6090 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006091 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006092 if (!nfs4_stateid_match(&calldata->arg.stateid,
6093 &calldata->lsp->ls_stateid))
6094 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006095 break;
6096 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006097 if (nfs4_async_handle_error(task, calldata->server,
6098 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006099 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006100 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006101 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006102}
6103
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006104static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006105{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006106 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006108 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6109 nfs_async_iocounter_wait(task, calldata->l_ctx))
6110 return;
6111
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006112 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006113 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006114 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006115 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006116 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006117 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006118 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006119 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006120 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006121 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006122 &calldata->res.seq_res,
6123 task) != 0)
6124 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006125 return;
6126out_no_action:
6127 task->tk_action = NULL;
6128out_wait:
6129 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130}
6131
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006132static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006133 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006134 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006135 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006136};
6137
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6139 struct nfs_open_context *ctx,
6140 struct nfs4_lock_state *lsp,
6141 struct nfs_seqid *seqid)
6142{
6143 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006144 struct rpc_message msg = {
6145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6146 .rpc_cred = ctx->cred,
6147 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006148 struct rpc_task_setup task_setup_data = {
6149 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006150 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006151 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006152 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006153 .flags = RPC_TASK_ASYNC,
6154 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006155
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006156 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6157 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6158
Frank Filz137d6ac2007-07-09 15:32:29 -07006159 /* Ensure this is an unlock - when canceling a lock, the
6160 * canceled lock is passed in, and it won't be an unlock.
6161 */
6162 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006163 if (fl->fl_flags & FL_CLOSE)
6164 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006165
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006166 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6167 if (data == NULL) {
6168 nfs_free_seqid(seqid);
6169 return ERR_PTR(-ENOMEM);
6170 }
6171
Chuck Levera9c92d62013-08-09 12:48:18 -04006172 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006173 msg.rpc_argp = &data->arg;
6174 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006175 task_setup_data.callback_data = data;
6176 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006177}
6178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6180{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006181 struct inode *inode = state->inode;
6182 struct nfs4_state_owner *sp = state->owner;
6183 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006184 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006185 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006186 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006187 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006188 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006189 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
Trond Myklebust9b073572006-06-29 16:38:34 -04006191 status = nfs4_set_lock_state(state, request);
6192 /* Unlock _before_ we do the RPC call */
6193 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006194 /* Exclude nfs_delegation_claim_locks() */
6195 mutex_lock(&sp->so_delegreturn_mutex);
6196 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006197 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006198 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006199 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006200 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006201 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006202 }
6203 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006204 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006205 if (status != 0)
6206 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006207 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006208 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006209 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6210 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006211 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6212 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006213 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006214 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006215 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006216 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 status = PTR_ERR(task);
6218 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006219 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006220 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006221 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006222out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006223 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006224 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006225 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226}
6227
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006228struct nfs4_lockdata {
6229 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006230 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231 struct nfs4_lock_state *lsp;
6232 struct nfs_open_context *ctx;
6233 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006234 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006235 int rpc_status;
6236 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006237 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006238};
6239
6240static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006241 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6242 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006243{
6244 struct nfs4_lockdata *p;
6245 struct inode *inode = lsp->ls_state->inode;
6246 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006247 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006248
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006249 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006250 if (p == NULL)
6251 return NULL;
6252
6253 p->arg.fh = NFS_FH(inode);
6254 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006255 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006256 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006257 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006258 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6259 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006260 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006261 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006262 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006263 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006264 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006265 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006266 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006267 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006268 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006269 p->ctx = get_nfs_open_context(ctx);
6270 memcpy(&p->fl, fl, sizeof(p->fl));
6271 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006272out_free_seqid:
6273 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274out_free:
6275 kfree(p);
6276 return NULL;
6277}
6278
6279static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6280{
6281 struct nfs4_lockdata *data = calldata;
6282 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
Harvey Harrison3110ff82008-05-02 13:42:44 -07006284 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006285 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006286 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006287 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006288 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006289 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006290 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006291 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006292 nfs4_stateid_copy(&data->arg.open_stateid,
6293 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006294 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006295 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006296 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006297 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006298 nfs4_stateid_copy(&data->arg.lock_stateid,
6299 &data->lsp->ls_stateid);
6300 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006301 if (!nfs4_valid_open_stateid(state)) {
6302 data->rpc_status = -EBADF;
6303 task->tk_action = NULL;
6304 goto out_release_open_seqid;
6305 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006306 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006307 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006308 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006309 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006310 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006311 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006312out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006313 nfs_release_seqid(data->arg.open_seqid);
6314out_release_lock_seqid:
6315 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006316out_wait:
6317 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006318 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006319}
6320
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006321static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6322{
6323 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006324 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006325
Harvey Harrison3110ff82008-05-02 13:42:44 -07006326 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006327
Trond Myklebust14516c32010-07-31 14:29:06 -04006328 if (!nfs4_sequence_done(task, &data->res.seq_res))
6329 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006330
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006331 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006332 switch (task->tk_status) {
6333 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006334 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006335 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006336 if (data->arg.new_lock) {
6337 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006338 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006339 rpc_restart_call_prepare(task);
6340 break;
6341 }
6342 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006343 if (data->arg.new_lock_owner != 0) {
6344 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6345 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6346 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6347 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6348 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006349 break;
6350 case -NFS4ERR_BAD_STATEID:
6351 case -NFS4ERR_OLD_STATEID:
6352 case -NFS4ERR_STALE_STATEID:
6353 case -NFS4ERR_EXPIRED:
6354 if (data->arg.new_lock_owner != 0) {
6355 if (!nfs4_stateid_match(&data->arg.open_stateid,
6356 &lsp->ls_state->open_stateid))
6357 rpc_restart_call_prepare(task);
6358 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6359 &lsp->ls_stateid))
6360 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006361 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006362 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006363}
6364
6365static void nfs4_lock_release(void *calldata)
6366{
6367 struct nfs4_lockdata *data = calldata;
6368
Harvey Harrison3110ff82008-05-02 13:42:44 -07006369 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006370 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006371 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006372 struct rpc_task *task;
6373 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6374 data->arg.lock_seqid);
6375 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006376 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006377 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006378 } else
6379 nfs_free_seqid(data->arg.lock_seqid);
6380 nfs4_put_lock_state(data->lsp);
6381 put_nfs_open_context(data->ctx);
6382 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006383 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006384}
6385
6386static const struct rpc_call_ops nfs4_lock_ops = {
6387 .rpc_call_prepare = nfs4_lock_prepare,
6388 .rpc_call_done = nfs4_lock_done,
6389 .rpc_release = nfs4_lock_release,
6390};
6391
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006392static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6393{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006394 switch (error) {
6395 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006396 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006397 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006398 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006399 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006400 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006401 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006402 break;
6403 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006404 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006405 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006406 };
6407}
6408
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006409static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006410{
6411 struct nfs4_lockdata *data;
6412 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006413 struct rpc_message msg = {
6414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6415 .rpc_cred = state->owner->so_cred,
6416 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006417 struct rpc_task_setup task_setup_data = {
6418 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006419 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006420 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006421 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006422 .flags = RPC_TASK_ASYNC,
6423 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006424 int ret;
6425
Harvey Harrison3110ff82008-05-02 13:42:44 -07006426 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006427 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006428 fl->fl_u.nfs4_fl.owner,
6429 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006430 if (data == NULL)
6431 return -ENOMEM;
6432 if (IS_SETLKW(cmd))
6433 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006434 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006435 msg.rpc_argp = &data->arg;
6436 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006437 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006438 if (recovery_type > NFS_LOCK_NEW) {
6439 if (recovery_type == NFS_LOCK_RECLAIM)
6440 data->arg.reclaim = NFS_LOCK_RECLAIM;
6441 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006442 } else
6443 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006444 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006445 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006446 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006447 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006448 if (ret == 0) {
6449 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006450 if (ret)
6451 nfs4_handle_setlk_error(data->server, data->lsp,
6452 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006453 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006454 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006455 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006456 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006457 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459}
6460
6461static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6462{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006463 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006464 struct nfs4_exception exception = {
6465 .inode = state->inode,
6466 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006467 int err;
6468
6469 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006470 /* Cache the lock if possible... */
6471 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6472 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006473 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006474 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006475 break;
6476 nfs4_handle_exception(server, err, &exception);
6477 } while (exception.retry);
6478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479}
6480
6481static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6482{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006483 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006484 struct nfs4_exception exception = {
6485 .inode = state->inode,
6486 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006487 int err;
6488
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006489 err = nfs4_set_lock_state(state, request);
6490 if (err != 0)
6491 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006492 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006493 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6494 return 0;
6495 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006496 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006497 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6498 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006499 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006500 switch (err) {
6501 default:
6502 goto out;
6503 case -NFS4ERR_GRACE:
6504 case -NFS4ERR_DELAY:
6505 nfs4_handle_exception(server, err, &exception);
6506 err = 0;
6507 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006508 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006509out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006510 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511}
6512
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006513#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006514static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6515{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006516 struct nfs4_lock_state *lsp;
6517 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006518
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006519 status = nfs4_set_lock_state(state, request);
6520 if (status != 0)
6521 return status;
6522 lsp = request->fl_u.nfs4_fl.owner;
6523 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6524 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6525 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006526 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006527}
6528#endif
6529
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6531{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006532 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006533 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006534 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006535 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
Trond Myklebust01c3b862006-06-29 16:38:39 -04006537 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006538 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006539 if (status < 0)
6540 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006541 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006542 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006543 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006544 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006545 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006546 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006547 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006548 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006549 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006550 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006551 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006552 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006553 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006554 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006555out:
6556 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 return status;
6558}
6559
6560static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6561{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006562 struct nfs4_exception exception = {
6563 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006564 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006565 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 int err;
6567
6568 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006569 err = _nfs4_proc_setlk(state, cmd, request);
6570 if (err == -NFS4ERR_DENIED)
6571 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006573 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 } while (exception.retry);
6575 return err;
6576}
6577
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006578#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6579#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6580
6581static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006582nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6583 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006584{
6585 int status = -ERESTARTSYS;
6586 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6587
6588 while(!signalled()) {
6589 status = nfs4_proc_setlk(state, cmd, request);
6590 if ((status != -EAGAIN) || IS_SETLK(cmd))
6591 break;
6592 freezable_schedule_timeout_interruptible(timeout);
6593 timeout *= 2;
6594 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6595 status = -ERESTARTSYS;
6596 }
6597 return status;
6598}
6599
Jeff Laytona1d617d82016-09-17 18:17:39 -04006600#ifdef CONFIG_NFS_V4_1
6601struct nfs4_lock_waiter {
6602 struct task_struct *task;
6603 struct inode *inode;
6604 struct nfs_lowner *owner;
6605 bool notified;
6606};
6607
6608static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006609nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006610{
6611 int ret;
6612 struct cb_notify_lock_args *cbnl = key;
6613 struct nfs4_lock_waiter *waiter = wait->private;
6614 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6615 *wowner = waiter->owner;
6616
6617 /* Only wake if the callback was for the same owner */
6618 if (lowner->clientid != wowner->clientid ||
6619 lowner->id != wowner->id ||
6620 lowner->s_dev != wowner->s_dev)
6621 return 0;
6622
6623 /* Make sure it's for the right inode */
6624 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6625 return 0;
6626
6627 waiter->notified = true;
6628
6629 /* override "private" so we can use default_wake_function */
6630 wait->private = waiter->task;
6631 ret = autoremove_wake_function(wait, mode, flags, key);
6632 wait->private = waiter;
6633 return ret;
6634}
6635
6636static int
6637nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6638{
6639 int status = -ERESTARTSYS;
6640 unsigned long flags;
6641 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6642 struct nfs_server *server = NFS_SERVER(state->inode);
6643 struct nfs_client *clp = server->nfs_client;
6644 wait_queue_head_t *q = &clp->cl_lock_waitq;
6645 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6646 .id = lsp->ls_seqid.owner_id,
6647 .s_dev = server->s_dev };
6648 struct nfs4_lock_waiter waiter = { .task = current,
6649 .inode = state->inode,
6650 .owner = &owner,
6651 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006652 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006653
6654 /* Don't bother with waitqueue if we don't expect a callback */
6655 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6656 return nfs4_retry_setlk_simple(state, cmd, request);
6657
6658 init_wait(&wait);
6659 wait.private = &waiter;
6660 wait.func = nfs4_wake_lock_waiter;
6661 add_wait_queue(q, &wait);
6662
6663 while(!signalled()) {
6664 status = nfs4_proc_setlk(state, cmd, request);
6665 if ((status != -EAGAIN) || IS_SETLK(cmd))
6666 break;
6667
6668 status = -ERESTARTSYS;
6669 spin_lock_irqsave(&q->lock, flags);
6670 if (waiter.notified) {
6671 spin_unlock_irqrestore(&q->lock, flags);
6672 continue;
6673 }
6674 set_current_state(TASK_INTERRUPTIBLE);
6675 spin_unlock_irqrestore(&q->lock, flags);
6676
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006677 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006678 }
6679
6680 finish_wait(q, &wait);
6681 return status;
6682}
6683#else /* !CONFIG_NFS_V4_1 */
6684static inline int
6685nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6686{
6687 return nfs4_retry_setlk_simple(state, cmd, request);
6688}
6689#endif
6690
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691static int
6692nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6693{
6694 struct nfs_open_context *ctx;
6695 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 int status;
6697
6698 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006699 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 state = ctx->state;
6701
Trond Myklebustd9531262009-07-21 19:22:38 -04006702 if (IS_GETLK(cmd)) {
6703 if (state != NULL)
6704 return nfs4_proc_getlk(state, F_GETLK, request);
6705 return 0;
6706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
6708 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6709 return -EINVAL;
6710
Trond Myklebustd9531262009-07-21 19:22:38 -04006711 if (request->fl_type == F_UNLCK) {
6712 if (state != NULL)
6713 return nfs4_proc_unlck(state, cmd, request);
6714 return 0;
6715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Trond Myklebustd9531262009-07-21 19:22:38 -04006717 if (state == NULL)
6718 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006719
6720 if ((request->fl_flags & FL_POSIX) &&
6721 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6722 return -ENOLCK;
6723
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006724 /*
6725 * Don't rely on the VFS having checked the file open mode,
6726 * since it won't do this for flock() locks.
6727 */
6728 switch (request->fl_type) {
6729 case F_RDLCK:
6730 if (!(filp->f_mode & FMODE_READ))
6731 return -EBADF;
6732 break;
6733 case F_WRLCK:
6734 if (!(filp->f_mode & FMODE_WRITE))
6735 return -EBADF;
6736 }
6737
Jeff Layton1ea67db2016-09-17 18:17:37 -04006738 status = nfs4_set_lock_state(state, request);
6739 if (status != 0)
6740 return status;
6741
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006742 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743}
6744
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006745int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006746{
6747 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006748 int err;
6749
6750 err = nfs4_set_lock_state(state, fl);
6751 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006752 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006753 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006754 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006755}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006756
Trond Myklebustcf470c32012-03-07 13:49:12 -05006757struct nfs_release_lockowner_data {
6758 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006759 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006760 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006761 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006762 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006763};
6764
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006765static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6766{
6767 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006768 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006769 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6770 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006771 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006772 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006773}
6774
6775static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6776{
6777 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006778 struct nfs_server *server = data->server;
6779
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006780 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006781
6782 switch (task->tk_status) {
6783 case 0:
6784 renew_lease(server, data->timestamp);
6785 break;
6786 case -NFS4ERR_STALE_CLIENTID:
6787 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006788 nfs4_schedule_lease_recovery(server->nfs_client);
6789 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006790 case -NFS4ERR_LEASE_MOVED:
6791 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006792 if (nfs4_async_handle_error(task, server,
6793 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006794 rpc_restart_call_prepare(task);
6795 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006796}
6797
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006798static void nfs4_release_lockowner_release(void *calldata)
6799{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006800 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006801 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006802 kfree(calldata);
6803}
6804
Trond Myklebust17280172012-03-11 13:11:00 -04006805static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006806 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6807 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006808 .rpc_release = nfs4_release_lockowner_release,
6809};
6810
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006811static void
6812nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006813{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006814 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006815 struct rpc_message msg = {
6816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6817 };
6818
6819 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006820 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006821
Trond Myklebustcf470c32012-03-07 13:49:12 -05006822 data = kmalloc(sizeof(*data), GFP_NOFS);
6823 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006824 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006825 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006826 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006827 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6828 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6829 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006830
Trond Myklebustcf470c32012-03-07 13:49:12 -05006831 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006832 msg.rpc_resp = &data->res;
6833 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006834 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006835}
6836
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006837#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6838
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006839static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006840 struct dentry *unused, struct inode *inode,
6841 const char *key, const void *buf,
6842 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006843{
Al Viro59301222016-05-27 10:19:30 -04006844 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006845}
6846
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006847static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006848 struct dentry *unused, struct inode *inode,
6849 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006850{
Al Virob2968212016-04-10 20:48:24 -04006851 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006852}
6853
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006854static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006855{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006856 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006857}
6858
David Quigleyc9bccef2013-05-22 12:50:45 -04006859#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006860
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006861static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006862 struct dentry *unused, struct inode *inode,
6863 const char *key, const void *buf,
6864 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006865{
6866 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006867 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006868
6869 return -EOPNOTSUPP;
6870}
6871
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006872static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006873 struct dentry *unused, struct inode *inode,
6874 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006875{
6876 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006877 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006878 return -EOPNOTSUPP;
6879}
6880
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006881static ssize_t
6882nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006883{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006884 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006885
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006886 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6887 len = security_inode_listsecurity(inode, list, list_len);
6888 if (list_len && len > list_len)
6889 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006890 }
6891 return len;
6892}
6893
6894static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6895 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006896 .get = nfs4_xattr_get_nfs4_label,
6897 .set = nfs4_xattr_set_nfs4_label,
6898};
David Quigleyc9bccef2013-05-22 12:50:45 -04006899
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006900#else
6901
6902static ssize_t
6903nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6904{
6905 return 0;
6906}
6907
6908#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006909
Andy Adamson533eb462011-06-13 18:25:56 -04006910/*
6911 * nfs_fhget will use either the mounted_on_fileid or the fileid
6912 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006913static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6914{
Andy Adamson533eb462011-06-13 18:25:56 -04006915 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6916 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6917 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006918 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006919 return;
6920
6921 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006922 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006923 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6924 fattr->nlink = 2;
6925}
6926
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006927static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6928 const struct qstr *name,
6929 struct nfs4_fs_locations *fs_locations,
6930 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006931{
6932 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006933 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006934 struct nfs4_fs_locations_arg args = {
6935 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006936 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006937 .page = page,
6938 .bitmask = bitmask,
6939 };
Benny Halevy22958462009-04-01 09:22:02 -04006940 struct nfs4_fs_locations_res res = {
6941 .fs_locations = fs_locations,
6942 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006943 struct rpc_message msg = {
6944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6945 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006946 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006947 };
6948 int status;
6949
Harvey Harrison3110ff82008-05-02 13:42:44 -07006950 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006951
Chuck Leverc05cefc2017-11-05 15:45:22 -05006952 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6953 bitmask[1] = nfs4_fattr_bitmap[1];
6954
Andy Adamson533eb462011-06-13 18:25:56 -04006955 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6956 * is not supported */
6957 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05006958 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006959 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05006960 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006961
Trond Myklebustc228fd32007-01-13 02:28:11 -05006962 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006963 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006964 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006965 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006966 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006967 return status;
6968}
6969
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006970int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6971 const struct qstr *name,
6972 struct nfs4_fs_locations *fs_locations,
6973 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006974{
6975 struct nfs4_exception exception = { };
6976 int err;
6977 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006978 err = _nfs4_proc_fs_locations(client, dir, name,
6979 fs_locations, page);
6980 trace_nfs4_get_fs_locations(dir, name, err);
6981 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006982 &exception);
6983 } while (exception.retry);
6984 return err;
6985}
6986
Chuck Leverb03d7352013-10-17 14:12:50 -04006987/*
6988 * This operation also signals the server that this client is
6989 * performing migration recovery. The server can stop returning
6990 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6991 * appended to this compound to identify the client ID which is
6992 * performing recovery.
6993 */
6994static int _nfs40_proc_get_locations(struct inode *inode,
6995 struct nfs4_fs_locations *locations,
6996 struct page *page, struct rpc_cred *cred)
6997{
6998 struct nfs_server *server = NFS_SERVER(inode);
6999 struct rpc_clnt *clnt = server->client;
7000 u32 bitmask[2] = {
7001 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7002 };
7003 struct nfs4_fs_locations_arg args = {
7004 .clientid = server->nfs_client->cl_clientid,
7005 .fh = NFS_FH(inode),
7006 .page = page,
7007 .bitmask = bitmask,
7008 .migration = 1, /* skip LOOKUP */
7009 .renew = 1, /* append RENEW */
7010 };
7011 struct nfs4_fs_locations_res res = {
7012 .fs_locations = locations,
7013 .migration = 1,
7014 .renew = 1,
7015 };
7016 struct rpc_message msg = {
7017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7018 .rpc_argp = &args,
7019 .rpc_resp = &res,
7020 .rpc_cred = cred,
7021 };
7022 unsigned long now = jiffies;
7023 int status;
7024
7025 nfs_fattr_init(&locations->fattr);
7026 locations->server = server;
7027 locations->nlocations = 0;
7028
7029 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7030 nfs4_set_sequence_privileged(&args.seq_args);
7031 status = nfs4_call_sync_sequence(clnt, server, &msg,
7032 &args.seq_args, &res.seq_res);
7033 if (status)
7034 return status;
7035
7036 renew_lease(server, now);
7037 return 0;
7038}
7039
7040#ifdef CONFIG_NFS_V4_1
7041
7042/*
7043 * This operation also signals the server that this client is
7044 * performing migration recovery. The server can stop asserting
7045 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7046 * performing this operation is identified in the SEQUENCE
7047 * operation in this compound.
7048 *
7049 * When the client supports GETATTR(fs_locations_info), it can
7050 * be plumbed in here.
7051 */
7052static int _nfs41_proc_get_locations(struct inode *inode,
7053 struct nfs4_fs_locations *locations,
7054 struct page *page, struct rpc_cred *cred)
7055{
7056 struct nfs_server *server = NFS_SERVER(inode);
7057 struct rpc_clnt *clnt = server->client;
7058 u32 bitmask[2] = {
7059 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7060 };
7061 struct nfs4_fs_locations_arg args = {
7062 .fh = NFS_FH(inode),
7063 .page = page,
7064 .bitmask = bitmask,
7065 .migration = 1, /* skip LOOKUP */
7066 };
7067 struct nfs4_fs_locations_res res = {
7068 .fs_locations = locations,
7069 .migration = 1,
7070 };
7071 struct rpc_message msg = {
7072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7073 .rpc_argp = &args,
7074 .rpc_resp = &res,
7075 .rpc_cred = cred,
7076 };
7077 int status;
7078
7079 nfs_fattr_init(&locations->fattr);
7080 locations->server = server;
7081 locations->nlocations = 0;
7082
7083 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7084 nfs4_set_sequence_privileged(&args.seq_args);
7085 status = nfs4_call_sync_sequence(clnt, server, &msg,
7086 &args.seq_args, &res.seq_res);
7087 if (status == NFS4_OK &&
7088 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7089 status = -NFS4ERR_LEASE_MOVED;
7090 return status;
7091}
7092
7093#endif /* CONFIG_NFS_V4_1 */
7094
7095/**
7096 * nfs4_proc_get_locations - discover locations for a migrated FSID
7097 * @inode: inode on FSID that is migrating
7098 * @locations: result of query
7099 * @page: buffer
7100 * @cred: credential to use for this operation
7101 *
7102 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7103 * operation failed, or a negative errno if a local error occurred.
7104 *
7105 * On success, "locations" is filled in, but if the server has
7106 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7107 * asserted.
7108 *
7109 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7110 * from this client that require migration recovery.
7111 */
7112int nfs4_proc_get_locations(struct inode *inode,
7113 struct nfs4_fs_locations *locations,
7114 struct page *page, struct rpc_cred *cred)
7115{
7116 struct nfs_server *server = NFS_SERVER(inode);
7117 struct nfs_client *clp = server->nfs_client;
7118 const struct nfs4_mig_recovery_ops *ops =
7119 clp->cl_mvops->mig_recovery_ops;
7120 struct nfs4_exception exception = { };
7121 int status;
7122
7123 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7124 (unsigned long long)server->fsid.major,
7125 (unsigned long long)server->fsid.minor,
7126 clp->cl_hostname);
7127 nfs_display_fhandle(NFS_FH(inode), __func__);
7128
7129 do {
7130 status = ops->get_locations(inode, locations, page, cred);
7131 if (status != -NFS4ERR_DELAY)
7132 break;
7133 nfs4_handle_exception(server, status, &exception);
7134 } while (exception.retry);
7135 return status;
7136}
7137
Chuck Lever44c99932013-10-17 14:13:30 -04007138/*
7139 * This operation also signals the server that this client is
7140 * performing "lease moved" recovery. The server can stop
7141 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7142 * is appended to this compound to identify the client ID which is
7143 * performing recovery.
7144 */
7145static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7146{
7147 struct nfs_server *server = NFS_SERVER(inode);
7148 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7149 struct rpc_clnt *clnt = server->client;
7150 struct nfs4_fsid_present_arg args = {
7151 .fh = NFS_FH(inode),
7152 .clientid = clp->cl_clientid,
7153 .renew = 1, /* append RENEW */
7154 };
7155 struct nfs4_fsid_present_res res = {
7156 .renew = 1,
7157 };
7158 struct rpc_message msg = {
7159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7160 .rpc_argp = &args,
7161 .rpc_resp = &res,
7162 .rpc_cred = cred,
7163 };
7164 unsigned long now = jiffies;
7165 int status;
7166
7167 res.fh = nfs_alloc_fhandle();
7168 if (res.fh == NULL)
7169 return -ENOMEM;
7170
7171 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7172 nfs4_set_sequence_privileged(&args.seq_args);
7173 status = nfs4_call_sync_sequence(clnt, server, &msg,
7174 &args.seq_args, &res.seq_res);
7175 nfs_free_fhandle(res.fh);
7176 if (status)
7177 return status;
7178
7179 do_renew_lease(clp, now);
7180 return 0;
7181}
7182
7183#ifdef CONFIG_NFS_V4_1
7184
7185/*
7186 * This operation also signals the server that this client is
7187 * performing "lease moved" recovery. The server can stop asserting
7188 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7189 * this operation is identified in the SEQUENCE operation in this
7190 * compound.
7191 */
7192static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7193{
7194 struct nfs_server *server = NFS_SERVER(inode);
7195 struct rpc_clnt *clnt = server->client;
7196 struct nfs4_fsid_present_arg args = {
7197 .fh = NFS_FH(inode),
7198 };
7199 struct nfs4_fsid_present_res res = {
7200 };
7201 struct rpc_message msg = {
7202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7203 .rpc_argp = &args,
7204 .rpc_resp = &res,
7205 .rpc_cred = cred,
7206 };
7207 int status;
7208
7209 res.fh = nfs_alloc_fhandle();
7210 if (res.fh == NULL)
7211 return -ENOMEM;
7212
7213 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7214 nfs4_set_sequence_privileged(&args.seq_args);
7215 status = nfs4_call_sync_sequence(clnt, server, &msg,
7216 &args.seq_args, &res.seq_res);
7217 nfs_free_fhandle(res.fh);
7218 if (status == NFS4_OK &&
7219 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7220 status = -NFS4ERR_LEASE_MOVED;
7221 return status;
7222}
7223
7224#endif /* CONFIG_NFS_V4_1 */
7225
7226/**
7227 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7228 * @inode: inode on FSID to check
7229 * @cred: credential to use for this operation
7230 *
7231 * Server indicates whether the FSID is present, moved, or not
7232 * recognized. This operation is necessary to clear a LEASE_MOVED
7233 * condition for this client ID.
7234 *
7235 * Returns NFS4_OK if the FSID is present on this server,
7236 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7237 * NFS4ERR code if some error occurred on the server, or a
7238 * negative errno if a local failure occurred.
7239 */
7240int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7241{
7242 struct nfs_server *server = NFS_SERVER(inode);
7243 struct nfs_client *clp = server->nfs_client;
7244 const struct nfs4_mig_recovery_ops *ops =
7245 clp->cl_mvops->mig_recovery_ops;
7246 struct nfs4_exception exception = { };
7247 int status;
7248
7249 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7250 (unsigned long long)server->fsid.major,
7251 (unsigned long long)server->fsid.minor,
7252 clp->cl_hostname);
7253 nfs_display_fhandle(NFS_FH(inode), __func__);
7254
7255 do {
7256 status = ops->fsid_present(inode, cred);
7257 if (status != -NFS4ERR_DELAY)
7258 break;
7259 nfs4_handle_exception(server, status, &exception);
7260 } while (exception.retry);
7261 return status;
7262}
7263
Andy Adamson5ec16a82013-08-08 10:57:55 -04007264/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007265 * If 'use_integrity' is true and the state managment nfs_client
7266 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7267 * and the machine credential as per RFC3530bis and RFC5661 Security
7268 * Considerations sections. Otherwise, just use the user cred with the
7269 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007270 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007271static 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 +00007272{
7273 int status;
7274 struct nfs4_secinfo_arg args = {
7275 .dir_fh = NFS_FH(dir),
7276 .name = name,
7277 };
7278 struct nfs4_secinfo_res res = {
7279 .flavors = flavors,
7280 };
7281 struct rpc_message msg = {
7282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7283 .rpc_argp = &args,
7284 .rpc_resp = &res,
7285 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007286 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007287 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007288
7289 if (use_integrity) {
7290 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007291 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7292 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007293 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007294
7295 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007296
7297 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7298 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7299
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007300 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7301 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007302 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007303
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007304 if (cred)
7305 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007306
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007307 return status;
7308}
7309
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007310int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7311 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007312{
7313 struct nfs4_exception exception = { };
7314 int err;
7315 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007316 err = -NFS4ERR_WRONGSEC;
7317
7318 /* try to use integrity protection with machine cred */
7319 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7320 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7321
7322 /*
7323 * if unable to use integrity protection, or SECINFO with
7324 * integrity protection returns NFS4ERR_WRONGSEC (which is
7325 * disallowed by spec, but exists in deployed servers) use
7326 * the current filesystem's rpc_client and the user cred.
7327 */
7328 if (err == -NFS4ERR_WRONGSEC)
7329 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7330
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007331 trace_nfs4_secinfo(dir, name, err);
7332 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007333 &exception);
7334 } while (exception.retry);
7335 return err;
7336}
7337
Andy Adamson557134a2009-04-01 09:21:53 -04007338#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007339/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007340 * Check the exchange flags returned by the server for invalid flags, having
7341 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7342 * DS flags set.
7343 */
7344static int nfs4_check_cl_exchange_flags(u32 flags)
7345{
7346 if (flags & ~EXCHGID4_FLAG_MASK_R)
7347 goto out_inval;
7348 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7349 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7350 goto out_inval;
7351 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7352 goto out_inval;
7353 return NFS_OK;
7354out_inval:
7355 return -NFS4ERR_INVAL;
7356}
7357
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007358static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007359nfs41_same_server_scope(struct nfs41_server_scope *a,
7360 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007361{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007362 if (a->server_scope_sz != b->server_scope_sz)
7363 return false;
7364 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007365}
7366
Andy Adamson02a95de2016-02-05 16:08:37 -05007367static void
7368nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7369{
7370}
7371
7372static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7373 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7374};
7375
Andy Adamson357f54d2010-12-14 10:11:57 -05007376/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007377 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007378 *
7379 * The 4.1 client currently uses the same TCP connection for the
7380 * fore and backchannel.
7381 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007382static
7383int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7384 struct rpc_xprt *xprt,
7385 struct nfs_client *clp,
7386 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007387{
7388 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007389 struct nfs41_bind_conn_to_session_args args = {
7390 .client = clp,
7391 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7392 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007393 struct nfs41_bind_conn_to_session_res res;
7394 struct rpc_message msg = {
7395 .rpc_proc =
7396 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007397 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007398 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007399 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007400 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007401 struct rpc_task_setup task_setup_data = {
7402 .rpc_client = clnt,
7403 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007404 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007405 .rpc_message = &msg,
7406 .flags = RPC_TASK_TIMEOUT,
7407 };
7408 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007409
Trond Myklebust71a097c2015-02-18 09:27:18 -08007410 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7411 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7412 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007413
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007414 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7415 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7416 args.dir = NFS4_CDFC4_FORE;
7417
7418 task = rpc_run_task(&task_setup_data);
7419 if (!IS_ERR(task)) {
7420 status = task->tk_status;
7421 rpc_put_task(task);
7422 } else
7423 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007424 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007425 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007426 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007427 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7428 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007429 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007430 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007431 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007432 dprintk("NFS: %s: Unexpected direction from server\n",
7433 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007434 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007435 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007436 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007437 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7438 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007439 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007440 }
7441 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007442
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007443 return status;
7444}
7445
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007446struct rpc_bind_conn_calldata {
7447 struct nfs_client *clp;
7448 struct rpc_cred *cred;
7449};
7450
7451static int
7452nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7453 struct rpc_xprt *xprt,
7454 void *calldata)
7455{
7456 struct rpc_bind_conn_calldata *p = calldata;
7457
7458 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7459}
7460
7461int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7462{
7463 struct rpc_bind_conn_calldata data = {
7464 .clp = clp,
7465 .cred = cred,
7466 };
7467 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7468 nfs4_proc_bind_conn_to_session_callback, &data);
7469}
7470
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007471/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007472 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7473 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007475static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7476 .how = SP4_MACH_CRED,
7477 .enforce.u.words = {
7478 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7479 1 << (OP_EXCHANGE_ID - 32) |
7480 1 << (OP_CREATE_SESSION - 32) |
7481 1 << (OP_DESTROY_SESSION - 32) |
7482 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007483 },
7484 .allow.u.words = {
7485 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007486 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007487 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007488 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007489 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007490 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007491 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007492 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007493 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007494 1 << (OP_FREE_STATEID - 32) |
7495 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007496 }
7497};
7498
7499/*
7500 * Select the state protection mode for client `clp' given the server results
7501 * from exchange_id in `sp'.
7502 *
7503 * Returns 0 on success, negative errno otherwise.
7504 */
7505static int nfs4_sp4_select_mode(struct nfs_client *clp,
7506 struct nfs41_state_protection *sp)
7507{
7508 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7509 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7510 1 << (OP_EXCHANGE_ID - 32) |
7511 1 << (OP_CREATE_SESSION - 32) |
7512 1 << (OP_DESTROY_SESSION - 32) |
7513 1 << (OP_DESTROY_CLIENTID - 32)
7514 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007515 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007516 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007517 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007518
7519 if (sp->how == SP4_MACH_CRED) {
7520 /* Print state protect result */
7521 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7522 for (i = 0; i <= LAST_NFS4_OP; i++) {
7523 if (test_bit(i, sp->enforce.u.longs))
7524 dfprintk(MOUNT, " enforce op %d\n", i);
7525 if (test_bit(i, sp->allow.u.longs))
7526 dfprintk(MOUNT, " allow op %d\n", i);
7527 }
7528
7529 /* make sure nothing is on enforce list that isn't supported */
7530 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7531 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7532 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007533 ret = -EINVAL;
7534 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007535 }
7536 }
7537
7538 /*
7539 * Minimal mode - state operations are allowed to use machine
7540 * credential. Note this already happens by default, so the
7541 * client doesn't have to do anything more than the negotiation.
7542 *
7543 * NOTE: we don't care if EXCHANGE_ID is in the list -
7544 * we're already using the machine cred for exchange_id
7545 * and will never use a different cred.
7546 */
7547 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7548 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7549 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7550 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7551 dfprintk(MOUNT, "sp4_mach_cred:\n");
7552 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007553 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007554 } else {
7555 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007556 ret = -EINVAL;
7557 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007558 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007559
7560 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007561 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7562 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007563 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7564 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007565 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007566 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007567
Andrew Elble99ade3c2015-12-02 09:39:51 -05007568 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7569 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007570 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007571 }
7572
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007573 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7574 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7575 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007576 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007577 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007578
7579 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7580 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7581 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007582 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007583 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007584
7585 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7586 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007587 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007588 }
7589
7590 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7591 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007592 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007593 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007594 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007595out:
7596 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007597 return 0;
7598}
7599
Andy Adamson8d89bd72016-09-09 09:22:18 -04007600struct nfs41_exchange_id_data {
7601 struct nfs41_exchange_id_res res;
7602 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007603};
7604
Andy Adamson8d89bd72016-09-09 09:22:18 -04007605static void nfs4_exchange_id_release(void *data)
7606{
7607 struct nfs41_exchange_id_data *cdata =
7608 (struct nfs41_exchange_id_data *)data;
7609
Olga Kornievskaia63513232017-03-13 10:36:19 -04007610 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007611 kfree(cdata->res.impl_id);
7612 kfree(cdata->res.server_scope);
7613 kfree(cdata->res.server_owner);
7614 kfree(cdata);
7615}
7616
7617static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007618 .rpc_release = nfs4_exchange_id_release,
7619};
7620
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007621/*
7622 * _nfs4_proc_exchange_id()
7623 *
7624 * Wrapper for EXCHANGE_ID operation.
7625 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007626static struct rpc_task *
7627nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007628 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007629{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007630 struct rpc_message msg = {
7631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007632 .rpc_cred = cred,
7633 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007634 struct rpc_task_setup task_setup_data = {
7635 .rpc_client = clp->cl_rpcclient,
7636 .callback_ops = &nfs4_exchange_id_call_ops,
7637 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007638 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007639 };
7640 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007641 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007642
Elena Reshetova212bf412017-10-20 12:53:38 +03007643 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007644 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007645
Trond Myklebust9c760d12017-07-31 18:38:50 -04007646 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007647 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007648 if (!calldata)
7649 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007650
Trond Myklebustfd405592017-08-01 16:02:47 -04007651 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007652
7653 status = nfs4_init_uniform_client_string(clp);
7654 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007655 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007656
Andy Adamson8d89bd72016-09-09 09:22:18 -04007657 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7658 GFP_NOFS);
7659 status = -ENOMEM;
7660 if (unlikely(calldata->res.server_owner == NULL))
7661 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007662
Andy Adamson8d89bd72016-09-09 09:22:18 -04007663 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007664 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007665 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007666 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007667
Andy Adamson8d89bd72016-09-09 09:22:18 -04007668 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7669 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007670 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007671
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007672 switch (sp4_how) {
7673 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007674 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007675 break;
7676
7677 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007678 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007679 break;
7680
7681 default:
7682 /* unsupported! */
7683 WARN_ON_ONCE(1);
7684 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007685 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007686 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007687 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007688 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007689 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007690 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7691 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007692 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007693 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007694 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7695 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007696#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007697 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007698#endif
7699 msg.rpc_argp = &calldata->args;
7700 msg.rpc_resp = &calldata->res;
7701 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007702
Trond Myklebust9c760d12017-07-31 18:38:50 -04007703 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007704
7705out_impl_id:
7706 kfree(calldata->res.impl_id);
7707out_server_scope:
7708 kfree(calldata->res.server_scope);
7709out_server_owner:
7710 kfree(calldata->res.server_owner);
7711out_calldata:
7712 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007713out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007714 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007715 return ERR_PTR(status);
7716}
7717
7718/*
7719 * _nfs4_proc_exchange_id()
7720 *
7721 * Wrapper for EXCHANGE_ID operation.
7722 */
7723static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7724 u32 sp4_how)
7725{
7726 struct rpc_task *task;
7727 struct nfs41_exchange_id_args *argp;
7728 struct nfs41_exchange_id_res *resp;
7729 int status;
7730
7731 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7732 if (IS_ERR(task))
7733 return PTR_ERR(task);
7734
7735 argp = task->tk_msg.rpc_argp;
7736 resp = task->tk_msg.rpc_resp;
7737 status = task->tk_status;
7738 if (status != 0)
7739 goto out;
7740
7741 status = nfs4_check_cl_exchange_flags(resp->flags);
7742 if (status != 0)
7743 goto out;
7744
7745 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7746 if (status != 0)
7747 goto out;
7748
7749 clp->cl_clientid = resp->clientid;
7750 clp->cl_exchange_flags = resp->flags;
7751 clp->cl_seqid = resp->seqid;
7752 /* Client ID is not confirmed */
7753 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7754 clear_bit(NFS4_SESSION_ESTABLISHED,
7755 &clp->cl_session->session_state);
7756
7757 if (clp->cl_serverscope != NULL &&
7758 !nfs41_same_server_scope(clp->cl_serverscope,
7759 resp->server_scope)) {
7760 dprintk("%s: server_scope mismatch detected\n",
7761 __func__);
7762 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7763 }
7764
7765 swap(clp->cl_serverowner, resp->server_owner);
7766 swap(clp->cl_serverscope, resp->server_scope);
7767 swap(clp->cl_implid, resp->impl_id);
7768
7769 /* Save the EXCHANGE_ID verifier session trunk tests */
7770 memcpy(clp->cl_confirm.data, argp->verifier.data,
7771 sizeof(clp->cl_confirm.data));
7772out:
7773 trace_nfs4_exchange_id(clp, status);
7774 rpc_put_task(task);
7775 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007776}
7777
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007778/*
7779 * nfs4_proc_exchange_id()
7780 *
7781 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7782 *
7783 * Since the clientid has expired, all compounds using sessions
7784 * associated with the stale clientid will be returning
7785 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7786 * be in some phase of session reset.
7787 *
7788 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7789 */
7790int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7791{
7792 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7793 int status;
7794
7795 /* try SP4_MACH_CRED if krb5i/p */
7796 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7797 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007798 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007799 if (!status)
7800 return 0;
7801 }
7802
7803 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007804 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007805}
7806
Andy Adamson04fa2c62016-09-09 09:22:29 -04007807/**
7808 * nfs4_test_session_trunk
7809 *
7810 * This is an add_xprt_test() test function called from
7811 * rpc_clnt_setup_test_and_add_xprt.
7812 *
7813 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7814 * and is dereferrenced in nfs4_exchange_id_release
7815 *
7816 * Upon success, add the new transport to the rpc_clnt
7817 *
7818 * @clnt: struct rpc_clnt to get new transport
7819 * @xprt: the rpc_xprt to test
7820 * @data: call data for _nfs4_proc_exchange_id.
7821 */
7822int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7823 void *data)
7824{
7825 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007826 struct rpc_task *task;
7827 int status;
7828
Andy Adamson04fa2c62016-09-09 09:22:29 -04007829 u32 sp4_how;
7830
7831 dprintk("--> %s try %s\n", __func__,
7832 xprt->address_strings[RPC_DISPLAY_ADDR]);
7833
7834 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7835
7836 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007837 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7838 if (IS_ERR(task))
7839 return PTR_ERR(task);
7840
7841 status = task->tk_status;
7842 if (status == 0)
7843 status = nfs4_detect_session_trunking(adata->clp,
7844 task->tk_msg.rpc_resp, xprt);
7845
7846 rpc_put_task(task);
7847 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007848}
7849EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7850
Trond Myklebust66245532012-05-25 17:18:09 -04007851static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7852 struct rpc_cred *cred)
7853{
7854 struct rpc_message msg = {
7855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7856 .rpc_argp = clp,
7857 .rpc_cred = cred,
7858 };
7859 int status;
7860
7861 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007862 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007863 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007864 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007865 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7866 return status;
7867}
7868
7869static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7870 struct rpc_cred *cred)
7871{
7872 unsigned int loop;
7873 int ret;
7874
7875 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7876 ret = _nfs4_proc_destroy_clientid(clp, cred);
7877 switch (ret) {
7878 case -NFS4ERR_DELAY:
7879 case -NFS4ERR_CLIENTID_BUSY:
7880 ssleep(1);
7881 break;
7882 default:
7883 return ret;
7884 }
7885 }
7886 return 0;
7887}
7888
7889int nfs4_destroy_clientid(struct nfs_client *clp)
7890{
7891 struct rpc_cred *cred;
7892 int ret = 0;
7893
7894 if (clp->cl_mvops->minor_version < 1)
7895 goto out;
7896 if (clp->cl_exchange_flags == 0)
7897 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007898 if (clp->cl_preserve_clid)
7899 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007900 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007901 ret = nfs4_proc_destroy_clientid(clp, cred);
7902 if (cred)
7903 put_rpccred(cred);
7904 switch (ret) {
7905 case 0:
7906 case -NFS4ERR_STALE_CLIENTID:
7907 clp->cl_exchange_flags = 0;
7908 }
7909out:
7910 return ret;
7911}
7912
Andy Adamson2050f0c2009-04-01 09:22:30 -04007913struct nfs4_get_lease_time_data {
7914 struct nfs4_get_lease_time_args *args;
7915 struct nfs4_get_lease_time_res *res;
7916 struct nfs_client *clp;
7917};
7918
7919static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7920 void *calldata)
7921{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007922 struct nfs4_get_lease_time_data *data =
7923 (struct nfs4_get_lease_time_data *)calldata;
7924
7925 dprintk("--> %s\n", __func__);
7926 /* just setup sequence, do not trigger session recovery
7927 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007928 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007929 &data->args->la_seq_args,
7930 &data->res->lr_seq_res,
7931 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007932 dprintk("<-- %s\n", __func__);
7933}
7934
7935/*
7936 * Called from nfs4_state_manager thread for session setup, so don't recover
7937 * from sequence operation or clientid errors.
7938 */
7939static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7940{
7941 struct nfs4_get_lease_time_data *data =
7942 (struct nfs4_get_lease_time_data *)calldata;
7943
7944 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007945 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7946 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007947 switch (task->tk_status) {
7948 case -NFS4ERR_DELAY:
7949 case -NFS4ERR_GRACE:
7950 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7951 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7952 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007953 /* fall through */
7954 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007955 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007956 return;
7957 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007958 dprintk("<-- %s\n", __func__);
7959}
7960
Trond Myklebust17280172012-03-11 13:11:00 -04007961static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007962 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7963 .rpc_call_done = nfs4_get_lease_time_done,
7964};
7965
7966int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7967{
7968 struct rpc_task *task;
7969 struct nfs4_get_lease_time_args args;
7970 struct nfs4_get_lease_time_res res = {
7971 .lr_fsinfo = fsinfo,
7972 };
7973 struct nfs4_get_lease_time_data data = {
7974 .args = &args,
7975 .res = &res,
7976 .clp = clp,
7977 };
7978 struct rpc_message msg = {
7979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7980 .rpc_argp = &args,
7981 .rpc_resp = &res,
7982 };
7983 struct rpc_task_setup task_setup = {
7984 .rpc_client = clp->cl_rpcclient,
7985 .rpc_message = &msg,
7986 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007987 .callback_data = &data,
7988 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007989 };
7990 int status;
7991
Chuck Levera9c92d62013-08-09 12:48:18 -04007992 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007993 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007994 task = rpc_run_task(&task_setup);
7995
7996 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007997 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007998
Anna Schumakerf6148712017-04-07 14:15:23 -04007999 status = task->tk_status;
8000 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008001 return status;
8002}
8003
Andy Adamsonfc931582009-04-01 09:22:31 -04008004/*
8005 * Initialize the values to be used by the client in CREATE_SESSION
8006 * If nfs4_init_session set the fore channel request and response sizes,
8007 * use them.
8008 *
8009 * Set the back channel max_resp_sz_cached to zero to force the client to
8010 * always set csa_cachethis to FALSE because the current implementation
8011 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8012 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008013static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8014 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008015{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008016 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008017 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008018
Andy Adamson18aad3d2013-06-26 12:21:49 -04008019 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8020 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8021
Andy Adamsonfc931582009-04-01 09:22:31 -04008022 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008023 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8024 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008025 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008026 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008027
8028 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008029 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008030 __func__,
8031 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008032 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008033
8034 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008035 args->bc_attrs.max_rqst_sz = max_bc_payload;
8036 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008037 args->bc_attrs.max_resp_sz_cached = 0;
8038 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008039 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008040
8041 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8042 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8043 __func__,
8044 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8045 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8046 args->bc_attrs.max_reqs);
8047}
8048
Trond Myklebust79969dd2015-02-18 11:30:18 -08008049static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8050 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008051{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008052 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008053 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008054
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008055 if (rcvd->max_resp_sz > sent->max_resp_sz)
8056 return -EINVAL;
8057 /*
8058 * Our requested max_ops is the minimum we need; we're not
8059 * prepared to break up compounds into smaller pieces than that.
8060 * So, no point even trying to continue if the server won't
8061 * cooperate:
8062 */
8063 if (rcvd->max_ops < sent->max_ops)
8064 return -EINVAL;
8065 if (rcvd->max_reqs == 0)
8066 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008067 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8068 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008069 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008070}
8071
Trond Myklebust79969dd2015-02-18 11:30:18 -08008072static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8073 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008074{
8075 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008076 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008077
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008078 if (!(res->flags & SESSION4_BACK_CHAN))
8079 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008080 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8081 return -EINVAL;
8082 if (rcvd->max_resp_sz < sent->max_resp_sz)
8083 return -EINVAL;
8084 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8085 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008086 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008087 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008088 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008089 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008090out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008091 return 0;
8092}
Andy Adamson8d353012009-04-01 09:22:32 -04008093
Andy Adamson8d353012009-04-01 09:22:32 -04008094static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008095 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008096{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008097 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008098
Trond Myklebust79969dd2015-02-18 11:30:18 -08008099 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008100 if (ret)
8101 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008102 return nfs4_verify_back_channel_attrs(args, res);
8103}
8104
8105static void nfs4_update_session(struct nfs4_session *session,
8106 struct nfs41_create_session_res *res)
8107{
8108 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008109 /* Mark client id and session as being confirmed */
8110 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8111 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008112 session->flags = res->flags;
8113 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008114 if (res->flags & SESSION4_BACK_CHAN)
8115 memcpy(&session->bc_attrs, &res->bc_attrs,
8116 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008117}
8118
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008119static int _nfs4_proc_create_session(struct nfs_client *clp,
8120 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008121{
8122 struct nfs4_session *session = clp->cl_session;
8123 struct nfs41_create_session_args args = {
8124 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008125 .clientid = clp->cl_clientid,
8126 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008127 .cb_program = NFS4_CALLBACK,
8128 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008129 struct nfs41_create_session_res res;
8130
Andy Adamsonfc931582009-04-01 09:22:31 -04008131 struct rpc_message msg = {
8132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8133 .rpc_argp = &args,
8134 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008135 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008136 };
8137 int status;
8138
Chuck Lever6b26cc82016-05-02 14:40:40 -04008139 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008140 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008141
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008142 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008143 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008144
Trond Myklebustb519d402016-09-11 14:50:01 -04008145 switch (status) {
8146 case -NFS4ERR_STALE_CLIENTID:
8147 case -NFS4ERR_DELAY:
8148 case -ETIMEDOUT:
8149 case -EACCES:
8150 case -EAGAIN:
8151 goto out;
8152 };
8153
8154 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008155 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008156 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008157 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008158 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008159 if (status)
8160 goto out;
8161 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008162 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008163out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008164 return status;
8165}
8166
8167/*
8168 * Issues a CREATE_SESSION operation to the server.
8169 * It is the responsibility of the caller to verify the session is
8170 * expired before calling this routine.
8171 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008172int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008173{
8174 int status;
8175 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008176 struct nfs4_session *session = clp->cl_session;
8177
8178 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8179
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008180 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008181 if (status)
8182 goto out;
8183
Andy Adamsonaacd5532011-11-09 13:58:21 -05008184 /* Init or reset the session slot tables */
8185 status = nfs4_setup_session_slot_tables(session);
8186 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008187 if (status)
8188 goto out;
8189
8190 ptr = (unsigned *)&session->sess_id.data[0];
8191 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8192 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008193out:
8194 dprintk("<-- %s\n", __func__);
8195 return status;
8196}
8197
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008198/*
8199 * Issue the over-the-wire RPC DESTROY_SESSION.
8200 * The caller must serialize access to this routine.
8201 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008202int nfs4_proc_destroy_session(struct nfs4_session *session,
8203 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008204{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008205 struct rpc_message msg = {
8206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8207 .rpc_argp = session,
8208 .rpc_cred = cred,
8209 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008210 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008211
8212 dprintk("--> nfs4_proc_destroy_session\n");
8213
8214 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008215 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8216 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008217
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008218 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008219 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008220
8221 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008222 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008223 "Session has been destroyed regardless...\n", status);
8224
8225 dprintk("<-- nfs4_proc_destroy_session\n");
8226 return status;
8227}
8228
Trond Myklebust7b38c362012-05-23 13:23:31 -04008229/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008230 * Renew the cl_session lease.
8231 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008232struct nfs4_sequence_data {
8233 struct nfs_client *clp;
8234 struct nfs4_sequence_args args;
8235 struct nfs4_sequence_res res;
8236};
8237
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008238static void nfs41_sequence_release(void *data)
8239{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008240 struct nfs4_sequence_data *calldata = data;
8241 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008242
Elena Reshetova212bf412017-10-20 12:53:38 +03008243 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008244 nfs4_schedule_state_renewal(clp);
8245 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008246 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008247}
8248
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008249static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8250{
8251 switch(task->tk_status) {
8252 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008253 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8254 return -EAGAIN;
8255 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008256 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008257 }
8258 return 0;
8259}
8260
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008261static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008262{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008263 struct nfs4_sequence_data *calldata = data;
8264 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008265
Trond Myklebust14516c32010-07-31 14:29:06 -04008266 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8267 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008268
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008269 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008270 if (task->tk_status < 0) {
8271 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008272 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008273 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008274
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008275 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8276 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008277 return;
8278 }
8279 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008280 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008281out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008282 dprintk("<-- %s\n", __func__);
8283}
8284
8285static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8286{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008287 struct nfs4_sequence_data *calldata = data;
8288 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008289 struct nfs4_sequence_args *args;
8290 struct nfs4_sequence_res *res;
8291
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008292 args = task->tk_msg.rpc_argp;
8293 res = task->tk_msg.rpc_resp;
8294
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008295 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008296}
8297
8298static const struct rpc_call_ops nfs41_sequence_ops = {
8299 .rpc_call_done = nfs41_sequence_call_done,
8300 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008301 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008302};
8303
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008304static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8305 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008306 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008307 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008308{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008309 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008310 struct rpc_message msg = {
8311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8312 .rpc_cred = cred,
8313 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008314 struct rpc_task_setup task_setup_data = {
8315 .rpc_client = clp->cl_rpcclient,
8316 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008317 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008318 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008319 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008320 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008321
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008322 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008323 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008324 goto out_err;
8325
8326 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008327 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008328 if (calldata == NULL)
8329 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008330 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008331 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008332 if (is_privileged)
8333 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008334 msg.rpc_argp = &calldata->args;
8335 msg.rpc_resp = &calldata->res;
8336 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008337 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008338
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008339 ret = rpc_run_task(&task_setup_data);
8340 if (IS_ERR(ret))
8341 goto out_err;
8342 return ret;
8343out_put_clp:
8344 nfs_put_client(clp);
8345out_err:
8346 nfs41_release_slot(slot);
8347 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008348}
8349
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008350static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008351{
8352 struct rpc_task *task;
8353 int ret = 0;
8354
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008355 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008356 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008357 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008358 if (IS_ERR(task))
8359 ret = PTR_ERR(task);
8360 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008361 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008362 dprintk("<-- %s status=%d\n", __func__, ret);
8363 return ret;
8364}
8365
8366static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8367{
8368 struct rpc_task *task;
8369 int ret;
8370
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008371 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008372 if (IS_ERR(task)) {
8373 ret = PTR_ERR(task);
8374 goto out;
8375 }
8376 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008377 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008378 ret = task->tk_status;
8379 rpc_put_task(task);
8380out:
8381 dprintk("<-- %s status=%d\n", __func__, ret);
8382 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008383}
8384
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008385struct nfs4_reclaim_complete_data {
8386 struct nfs_client *clp;
8387 struct nfs41_reclaim_complete_args arg;
8388 struct nfs41_reclaim_complete_res res;
8389};
8390
8391static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8392{
8393 struct nfs4_reclaim_complete_data *calldata = data;
8394
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008395 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008396 &calldata->arg.seq_args,
8397 &calldata->res.seq_res,
8398 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008399}
8400
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008401static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8402{
8403 switch(task->tk_status) {
8404 case 0:
8405 case -NFS4ERR_COMPLETE_ALREADY:
8406 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8407 break;
8408 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008409 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008410 /* fall through */
8411 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008412 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008413 case -NFS4ERR_BADSESSION:
8414 case -NFS4ERR_DEADSESSION:
8415 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8416 nfs4_schedule_session_recovery(clp->cl_session,
8417 task->tk_status);
8418 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008419 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008420 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008421 }
8422 return 0;
8423}
8424
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008425static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8426{
8427 struct nfs4_reclaim_complete_data *calldata = data;
8428 struct nfs_client *clp = calldata->clp;
8429 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8430
8431 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008432 if (!nfs41_sequence_done(task, res))
8433 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008434
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008435 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008436 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8437 rpc_restart_call_prepare(task);
8438 return;
8439 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008440 dprintk("<-- %s\n", __func__);
8441}
8442
8443static void nfs4_free_reclaim_complete_data(void *data)
8444{
8445 struct nfs4_reclaim_complete_data *calldata = data;
8446
8447 kfree(calldata);
8448}
8449
8450static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8451 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8452 .rpc_call_done = nfs4_reclaim_complete_done,
8453 .rpc_release = nfs4_free_reclaim_complete_data,
8454};
8455
8456/*
8457 * Issue a global reclaim complete.
8458 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008459static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8460 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008461{
8462 struct nfs4_reclaim_complete_data *calldata;
8463 struct rpc_task *task;
8464 struct rpc_message msg = {
8465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008466 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008467 };
8468 struct rpc_task_setup task_setup_data = {
8469 .rpc_client = clp->cl_rpcclient,
8470 .rpc_message = &msg,
8471 .callback_ops = &nfs4_reclaim_complete_call_ops,
8472 .flags = RPC_TASK_ASYNC,
8473 };
8474 int status = -ENOMEM;
8475
8476 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008477 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008478 if (calldata == NULL)
8479 goto out;
8480 calldata->clp = clp;
8481 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008482
Chuck Levera9c92d62013-08-09 12:48:18 -04008483 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008484 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008485 msg.rpc_argp = &calldata->arg;
8486 msg.rpc_resp = &calldata->res;
8487 task_setup_data.callback_data = calldata;
8488 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008489 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008490 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008491 goto out;
8492 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008493 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008494 if (status == 0)
8495 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008496 rpc_put_task(task);
8497out:
8498 dprintk("<-- %s status=%d\n", __func__, status);
8499 return status;
8500}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008501
8502static void
8503nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8504{
8505 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008506 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507
8508 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008509 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008510 &lgp->res.seq_res, task);
8511 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008512}
8513
8514static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8515{
8516 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008517
8518 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008519 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008520 dprintk("<-- %s\n", __func__);
8521}
8522
8523static int
8524nfs4_layoutget_handle_exception(struct rpc_task *task,
8525 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8526{
Trond Myklebustee314c22012-10-01 17:25:48 -07008527 struct inode *inode = lgp->args.inode;
8528 struct nfs_server *server = NFS_SERVER(inode);
8529 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008530 int nfs4err = task->tk_status;
8531 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008532 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008533
Boaz Harroshed7e5422014-01-22 20:34:54 +02008534 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008535
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008536 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008537 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008538 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008539
8540 /*
8541 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8542 * on the file. set tk_status to -ENODATA to tell upper layer to
8543 * retry go inband.
8544 */
8545 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008546 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008547 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008548 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008549 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8550 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8551 */
8552 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008553 status = -EOVERFLOW;
8554 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008555 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008556 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008557 * (or clients) writing to the same RAID stripe except when
8558 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008559 *
8560 * Treat it like we would RECALLCONFLICT -- we retry for a little
8561 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008562 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008563 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008564 if (lgp->args.minlength == 0) {
8565 status = -EOVERFLOW;
8566 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008567 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008568 status = -EBUSY;
8569 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008570 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008571 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008572 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008573 case -NFS4ERR_DELEG_REVOKED:
8574 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008575 case -NFS4ERR_EXPIRED:
8576 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008577 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008578 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008579 lo = NFS_I(inode)->layout;
8580 /* If the open stateid was bad, then recover it. */
8581 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008582 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008583 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008584 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008585 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008586 break;
8587 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008588
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008589 /*
8590 * Mark the bad layout state as invalid, then retry
8591 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008592 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008593 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008594 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008595 pnfs_free_lseg_list(&head);
8596 status = -EAGAIN;
8597 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008598 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008599
Trond Myklebust8ac09252017-01-23 22:44:12 -05008600 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008601 err = nfs4_handle_exception(server, nfs4err, exception);
8602 if (!status) {
8603 if (exception->retry)
8604 status = -EAGAIN;
8605 else
8606 status = err;
8607 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008608out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008609 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008610 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611}
8612
Idan Kedar85541162012-08-02 11:47:10 +03008613static size_t max_response_pages(struct nfs_server *server)
8614{
8615 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8616 return nfs_page_array_len(0, max_resp_sz);
8617}
8618
8619static void nfs4_free_pages(struct page **pages, size_t size)
8620{
8621 int i;
8622
8623 if (!pages)
8624 return;
8625
8626 for (i = 0; i < size; i++) {
8627 if (!pages[i])
8628 break;
8629 __free_page(pages[i]);
8630 }
8631 kfree(pages);
8632}
8633
8634static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8635{
8636 struct page **pages;
8637 int i;
8638
8639 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8640 if (!pages) {
8641 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8642 return NULL;
8643 }
8644
8645 for (i = 0; i < size; i++) {
8646 pages[i] = alloc_page(gfp_flags);
8647 if (!pages[i]) {
8648 dprintk("%s: failed to allocate page\n", __func__);
8649 nfs4_free_pages(pages, size);
8650 return NULL;
8651 }
8652 }
8653
8654 return pages;
8655}
8656
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008657static void nfs4_layoutget_release(void *calldata)
8658{
8659 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008660 struct inode *inode = lgp->args.inode;
8661 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008662 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663
8664 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008665 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008666 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008667 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008668 put_nfs_open_context(lgp->args.ctx);
8669 kfree(calldata);
8670 dprintk("<-- %s\n", __func__);
8671}
8672
8673static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8674 .rpc_call_prepare = nfs4_layoutget_prepare,
8675 .rpc_call_done = nfs4_layoutget_done,
8676 .rpc_release = nfs4_layoutget_release,
8677};
8678
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008679struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008680nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008681{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008682 struct inode *inode = lgp->args.inode;
8683 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008684 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008685 struct rpc_task *task;
8686 struct rpc_message msg = {
8687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8688 .rpc_argp = &lgp->args,
8689 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008690 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008691 };
8692 struct rpc_task_setup task_setup_data = {
8693 .rpc_client = server->client,
8694 .rpc_message = &msg,
8695 .callback_ops = &nfs4_layoutget_call_ops,
8696 .callback_data = lgp,
8697 .flags = RPC_TASK_ASYNC,
8698 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008699 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008700 struct nfs4_exception exception = {
8701 .inode = inode,
8702 .timeout = *timeout,
8703 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008704 int status = 0;
8705
8706 dprintk("--> %s\n", __func__);
8707
Peng Tao4bd5a982014-11-17 11:05:17 +08008708 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8709 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8710
Idan Kedar85541162012-08-02 11:47:10 +03008711 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8712 if (!lgp->args.layout.pages) {
8713 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008714 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008715 }
8716 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8717
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008718 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008719 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008720 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008721
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008722 task = rpc_run_task(&task_setup_data);
8723 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008724 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008725 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008726 if (status == 0) {
8727 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8728 *timeout = exception.timeout;
8729 }
8730
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008731 trace_nfs4_layoutget(lgp->args.ctx,
8732 &lgp->args.range,
8733 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008734 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008735 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008736
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008737 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8738 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008739 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740 rpc_put_task(task);
8741 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008742 if (status)
8743 return ERR_PTR(status);
8744 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008745}
8746
Benny Halevycbe82602011-05-22 19:52:37 +03008747static void
8748nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8749{
8750 struct nfs4_layoutreturn *lrp = calldata;
8751
8752 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008753 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008754 &lrp->args.seq_args,
8755 &lrp->res.seq_res,
8756 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008757}
8758
8759static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8760{
8761 struct nfs4_layoutreturn *lrp = calldata;
8762 struct nfs_server *server;
8763
8764 dprintk("--> %s\n", __func__);
8765
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008766 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008767 return;
8768
8769 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008770 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008771 case -NFS4ERR_OLD_STATEID:
8772 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8773 lrp->args.inode))
8774 goto out_restart;
8775 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008776 default:
8777 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008778 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008779 case 0:
8780 break;
8781 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008782 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008783 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008784 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008785 }
Benny Halevycbe82602011-05-22 19:52:37 +03008786 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008787 return;
8788out_restart:
8789 task->tk_status = 0;
8790 nfs4_sequence_free_slot(&lrp->res.seq_res);
8791 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008792}
8793
8794static void nfs4_layoutreturn_release(void *calldata)
8795{
8796 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008797 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008798
8799 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008800 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008801 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008802 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008803 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8804 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008805 pnfs_put_layout_hdr(lrp->args.layout);
8806 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008807 kfree(calldata);
8808 dprintk("<-- %s\n", __func__);
8809}
8810
8811static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8812 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8813 .rpc_call_done = nfs4_layoutreturn_done,
8814 .rpc_release = nfs4_layoutreturn_release,
8815};
8816
Peng Tao6c166052014-11-17 09:30:40 +08008817int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008818{
8819 struct rpc_task *task;
8820 struct rpc_message msg = {
8821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8822 .rpc_argp = &lrp->args,
8823 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008824 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008825 };
8826 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008827 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008828 .rpc_message = &msg,
8829 .callback_ops = &nfs4_layoutreturn_call_ops,
8830 .callback_data = lrp,
8831 };
Peng Tao6c166052014-11-17 09:30:40 +08008832 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008833
Andrew Elble99ade3c2015-12-02 09:39:51 -05008834 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8835 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8836 &task_setup_data.rpc_client, &msg);
8837
Benny Halevycbe82602011-05-22 19:52:37 +03008838 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008839 if (!sync) {
8840 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8841 if (!lrp->inode) {
8842 nfs4_layoutreturn_release(lrp);
8843 return -EAGAIN;
8844 }
8845 task_setup_data.flags |= RPC_TASK_ASYNC;
8846 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008847 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008848 task = rpc_run_task(&task_setup_data);
8849 if (IS_ERR(task))
8850 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008851 if (sync)
8852 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008853 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008854 dprintk("<-- %s status=%d\n", __func__, status);
8855 rpc_put_task(task);
8856 return status;
8857}
8858
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008859static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008860_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8861 struct pnfs_device *pdev,
8862 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008863{
8864 struct nfs4_getdeviceinfo_args args = {
8865 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008866 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8867 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008868 };
8869 struct nfs4_getdeviceinfo_res res = {
8870 .pdev = pdev,
8871 };
8872 struct rpc_message msg = {
8873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8874 .rpc_argp = &args,
8875 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008876 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008877 };
8878 int status;
8879
8880 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008881 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008882 if (res.notification & ~args.notify_types)
8883 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008884 if (res.notification != args.notify_types)
8885 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008886
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008887 dprintk("<-- %s status=%d\n", __func__, status);
8888
8889 return status;
8890}
8891
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008892int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8893 struct pnfs_device *pdev,
8894 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008895{
8896 struct nfs4_exception exception = { };
8897 int err;
8898
8899 do {
8900 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008901 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008902 &exception);
8903 } while (exception.retry);
8904 return err;
8905}
8906EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8907
Andy Adamson863a3c62011-03-23 13:27:54 +00008908static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8909{
8910 struct nfs4_layoutcommit_data *data = calldata;
8911 struct nfs_server *server = NFS_SERVER(data->args.inode);
8912
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008913 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008914 &data->args.seq_args,
8915 &data->res.seq_res,
8916 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008917}
8918
8919static void
8920nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8921{
8922 struct nfs4_layoutcommit_data *data = calldata;
8923 struct nfs_server *server = NFS_SERVER(data->args.inode);
8924
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008925 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008926 return;
8927
8928 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008929 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8930 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8931 case -NFS4ERR_BADLAYOUT: /* no layout */
8932 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008933 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008934 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008935 break;
8936 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008937 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008938 rpc_restart_call_prepare(task);
8939 return;
8940 }
8941 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008942}
8943
8944static void nfs4_layoutcommit_release(void *calldata)
8945{
8946 struct nfs4_layoutcommit_data *data = calldata;
8947
Andy Adamsondb29c082011-07-30 20:52:38 -04008948 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008949 nfs_post_op_update_inode_force_wcc(data->args.inode,
8950 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008951 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008952 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008953 kfree(data);
8954}
8955
8956static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8957 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8958 .rpc_call_done = nfs4_layoutcommit_done,
8959 .rpc_release = nfs4_layoutcommit_release,
8960};
8961
8962int
Andy Adamsonef311532011-03-12 02:58:10 -05008963nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008964{
8965 struct rpc_message msg = {
8966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8967 .rpc_argp = &data->args,
8968 .rpc_resp = &data->res,
8969 .rpc_cred = data->cred,
8970 };
8971 struct rpc_task_setup task_setup_data = {
8972 .task = &data->task,
8973 .rpc_client = NFS_CLIENT(data->args.inode),
8974 .rpc_message = &msg,
8975 .callback_ops = &nfs4_layoutcommit_ops,
8976 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008977 };
8978 struct rpc_task *task;
8979 int status = 0;
8980
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008981 dprintk("NFS: initiating layoutcommit call. sync %d "
8982 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008983 data->args.lastbytewritten,
8984 data->args.inode->i_ino);
8985
Trond Myklebust472e2592015-02-05 16:50:30 -05008986 if (!sync) {
8987 data->inode = nfs_igrab_and_active(data->args.inode);
8988 if (data->inode == NULL) {
8989 nfs4_layoutcommit_release(data);
8990 return -EAGAIN;
8991 }
8992 task_setup_data.flags = RPC_TASK_ASYNC;
8993 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008994 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008995 task = rpc_run_task(&task_setup_data);
8996 if (IS_ERR(task))
8997 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008998 if (sync)
8999 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009000 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009001 dprintk("%s: status %d\n", __func__, status);
9002 rpc_put_task(task);
9003 return status;
9004}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009005
Andy Adamson97431202013-08-08 10:57:56 -04009006/**
9007 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9008 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9009 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009010static int
9011_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009012 struct nfs_fsinfo *info,
9013 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009014{
9015 struct nfs41_secinfo_no_name_args args = {
9016 .style = SECINFO_STYLE_CURRENT_FH,
9017 };
9018 struct nfs4_secinfo_res res = {
9019 .flavors = flavors,
9020 };
9021 struct rpc_message msg = {
9022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9023 .rpc_argp = &args,
9024 .rpc_resp = &res,
9025 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009026 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009027 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009028 int status;
9029
9030 if (use_integrity) {
9031 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009032 cred = nfs4_get_clid_cred(server->nfs_client);
9033 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009034 }
9035
9036 dprintk("--> %s\n", __func__);
9037 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9038 &res.seq_res, 0);
9039 dprintk("<-- %s status=%d\n", __func__, status);
9040
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009041 if (cred)
9042 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009043
9044 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009045}
9046
9047static int
9048nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9049 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9050{
9051 struct nfs4_exception exception = { };
9052 int err;
9053 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009054 /* first try using integrity protection */
9055 err = -NFS4ERR_WRONGSEC;
9056
9057 /* try to use integrity protection with machine cred */
9058 if (_nfs4_is_integrity_protected(server->nfs_client))
9059 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9060 flavors, true);
9061
9062 /*
9063 * if unable to use integrity protection, or SECINFO with
9064 * integrity protection returns NFS4ERR_WRONGSEC (which is
9065 * disallowed by spec, but exists in deployed servers) use
9066 * the current filesystem's rpc_client and the user cred.
9067 */
9068 if (err == -NFS4ERR_WRONGSEC)
9069 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9070 flavors, false);
9071
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009072 switch (err) {
9073 case 0:
9074 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009075 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009076 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009077 default:
9078 err = nfs4_handle_exception(server, err, &exception);
9079 }
9080 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009081out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009082 return err;
9083}
9084
9085static int
9086nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9087 struct nfs_fsinfo *info)
9088{
9089 int err;
9090 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009091 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009092 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009093 struct nfs4_secinfo4 *secinfo;
9094 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009095
9096 page = alloc_page(GFP_KERNEL);
9097 if (!page) {
9098 err = -ENOMEM;
9099 goto out;
9100 }
9101
9102 flavors = page_address(page);
9103 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9104
9105 /*
9106 * Fall back on "guess and check" method if
9107 * the server doesn't support SECINFO_NO_NAME
9108 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009109 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009110 err = nfs4_find_root_sec(server, fhandle, info);
9111 goto out_freepage;
9112 }
9113 if (err)
9114 goto out_freepage;
9115
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009116 for (i = 0; i < flavors->num_flavors; i++) {
9117 secinfo = &flavors->flavors[i];
9118
9119 switch (secinfo->flavor) {
9120 case RPC_AUTH_NULL:
9121 case RPC_AUTH_UNIX:
9122 case RPC_AUTH_GSS:
9123 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9124 &secinfo->flavor_info);
9125 break;
9126 default:
9127 flavor = RPC_AUTH_MAXFLAVOR;
9128 break;
9129 }
9130
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009131 if (!nfs_auth_info_match(&server->auth_info, flavor))
9132 flavor = RPC_AUTH_MAXFLAVOR;
9133
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009134 if (flavor != RPC_AUTH_MAXFLAVOR) {
9135 err = nfs4_lookup_root_sec(server, fhandle,
9136 info, flavor);
9137 if (!err)
9138 break;
9139 }
9140 }
9141
9142 if (flavor == RPC_AUTH_MAXFLAVOR)
9143 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009144
9145out_freepage:
9146 put_page(page);
9147 if (err == -EACCES)
9148 return -EPERM;
9149out:
9150 return err;
9151}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009152
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009153static int _nfs41_test_stateid(struct nfs_server *server,
9154 nfs4_stateid *stateid,
9155 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009156{
9157 int status;
9158 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009159 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009160 };
9161 struct nfs41_test_stateid_res res;
9162 struct rpc_message msg = {
9163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9164 .rpc_argp = &args,
9165 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009166 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009167 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009168 struct rpc_clnt *rpc_client = server->client;
9169
9170 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9171 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009172
Chuck Lever38527b12012-07-11 16:30:23 -04009173 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009174 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009175 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009176 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009177 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009178 if (status != NFS_OK) {
9179 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009180 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009181 }
9182 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009183 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009184}
9185
Trond Myklebust43912bb2016-09-22 13:38:56 -04009186static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9187 int err, struct nfs4_exception *exception)
9188{
9189 exception->retry = 0;
9190 switch(err) {
9191 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009192 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009193 nfs4_handle_exception(server, err, exception);
9194 break;
9195 case -NFS4ERR_BADSESSION:
9196 case -NFS4ERR_BADSLOT:
9197 case -NFS4ERR_BAD_HIGH_SLOT:
9198 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9199 case -NFS4ERR_DEADSESSION:
9200 nfs4_do_handle_exception(server, err, exception);
9201 }
9202}
9203
Chuck Lever38527b12012-07-11 16:30:23 -04009204/**
9205 * nfs41_test_stateid - perform a TEST_STATEID operation
9206 *
9207 * @server: server / transport on which to perform the operation
9208 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009209 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009210 *
9211 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9212 * Otherwise a negative NFS4ERR value is returned if the operation
9213 * failed or the state ID is not currently valid.
9214 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009215static int nfs41_test_stateid(struct nfs_server *server,
9216 nfs4_stateid *stateid,
9217 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009218{
9219 struct nfs4_exception exception = { };
9220 int err;
9221 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009222 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009223 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009224 } while (exception.retry);
9225 return err;
9226}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009227
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009228struct nfs_free_stateid_data {
9229 struct nfs_server *server;
9230 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009231 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009232};
9233
9234static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9235{
9236 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009237 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009238 &data->args.seq_args,
9239 &data->res.seq_res,
9240 task);
9241}
9242
9243static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9244{
9245 struct nfs_free_stateid_data *data = calldata;
9246
9247 nfs41_sequence_done(task, &data->res.seq_res);
9248
9249 switch (task->tk_status) {
9250 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009251 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009252 rpc_restart_call_prepare(task);
9253 }
9254}
9255
9256static void nfs41_free_stateid_release(void *calldata)
9257{
9258 kfree(calldata);
9259}
9260
Trond Myklebust17f26b12013-08-21 15:48:42 -04009261static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009262 .rpc_call_prepare = nfs41_free_stateid_prepare,
9263 .rpc_call_done = nfs41_free_stateid_done,
9264 .rpc_release = nfs41_free_stateid_release,
9265};
9266
9267static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009268 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009269 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009270 bool privileged)
9271{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009272 struct rpc_message msg = {
9273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009274 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009275 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009276 struct rpc_task_setup task_setup = {
9277 .rpc_client = server->client,
9278 .rpc_message = &msg,
9279 .callback_ops = &nfs41_free_stateid_ops,
9280 .flags = RPC_TASK_ASYNC,
9281 };
9282 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009283
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009284 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9285 &task_setup.rpc_client, &msg);
9286
Chuck Lever38527b12012-07-11 16:30:23 -04009287 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009288 data = kmalloc(sizeof(*data), GFP_NOFS);
9289 if (!data)
9290 return ERR_PTR(-ENOMEM);
9291 data->server = server;
9292 nfs4_stateid_copy(&data->args.stateid, stateid);
9293
9294 task_setup.callback_data = data;
9295
9296 msg.rpc_argp = &data->args;
9297 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009298 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009299 if (privileged)
9300 nfs4_set_sequence_privileged(&data->args.seq_args);
9301
9302 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009303}
9304
Chuck Lever38527b12012-07-11 16:30:23 -04009305/**
9306 * nfs41_free_stateid - perform a FREE_STATEID operation
9307 *
9308 * @server: server / transport on which to perform the operation
9309 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009310 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009311 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009312 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009313 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009314 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009315static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009316 const nfs4_stateid *stateid,
9317 struct rpc_cred *cred,
9318 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009319{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009320 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009321
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009322 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009323 if (IS_ERR(task))
9324 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009325 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009326 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009327}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009328
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009329static void
9330nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009331{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009332 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009333
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009334 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009335 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009336}
9337
Trond Myklebust36281ca2012-03-04 18:13:56 -05009338static bool nfs41_match_stateid(const nfs4_stateid *s1,
9339 const nfs4_stateid *s2)
9340{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009341 if (s1->type != s2->type)
9342 return false;
9343
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009344 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009345 return false;
9346
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009347 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009348 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009349
Anna Schumaker045c5512017-01-11 16:59:48 -05009350 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009351}
9352
Andy Adamson557134a2009-04-01 09:21:53 -04009353#endif /* CONFIG_NFS_V4_1 */
9354
Trond Myklebust36281ca2012-03-04 18:13:56 -05009355static bool nfs4_match_stateid(const nfs4_stateid *s1,
9356 const nfs4_stateid *s2)
9357{
Trond Myklebustf597c532012-03-04 18:13:56 -05009358 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009359}
9360
9361
Trond Myklebust17280172012-03-11 13:11:00 -04009362static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009363 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009364 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .recover_open = nfs4_open_reclaim,
9366 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009367 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009368 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369};
9370
Andy Adamson591d71c2009-04-01 09:22:47 -04009371#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009372static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009373 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9374 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9375 .recover_open = nfs4_open_reclaim,
9376 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009377 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009378 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009379 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009380};
9381#endif /* CONFIG_NFS_V4_1 */
9382
Trond Myklebust17280172012-03-11 13:11:00 -04009383static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009384 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009385 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009386 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009388 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389};
9390
Andy Adamson591d71c2009-04-01 09:22:47 -04009391#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009392static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009393 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9394 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009395 .recover_open = nfs41_open_expired,
9396 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009397 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009398};
9399#endif /* CONFIG_NFS_V4_1 */
9400
Trond Myklebust17280172012-03-11 13:11:00 -04009401static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009402 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009403 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009404 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009405};
9406
9407#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009408static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009409 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009410 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009411 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009412};
9413#endif
9414
Chuck Leverec011fe2013-10-17 14:12:39 -04009415static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009416 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009417 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009418};
9419
9420#if defined(CONFIG_NFS_V4_1)
9421static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009422 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009423 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009424};
9425#endif /* CONFIG_NFS_V4_1 */
9426
Trond Myklebust97dc1352010-06-16 09:52:26 -04009427static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9428 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009429 .init_caps = NFS_CAP_READDIRPLUS
9430 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009431 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009432 .init_client = nfs40_init_client,
9433 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009434 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009435 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009436 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009437 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009438 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009439 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009440 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9441 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9442 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009443 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009444};
9445
9446#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009447static struct nfs_seqid *
9448nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9449{
9450 return NULL;
9451}
9452
Trond Myklebust97dc1352010-06-16 09:52:26 -04009453static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9454 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009455 .init_caps = NFS_CAP_READDIRPLUS
9456 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009457 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009458 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009459 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009460 .init_client = nfs41_init_client,
9461 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009462 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009463 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009464 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009465 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009466 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009467 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009468 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009469 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9470 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9471 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009472 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009473};
9474#endif
9475
Steve Dickson42c2c422013-05-22 12:50:38 -04009476#if defined(CONFIG_NFS_V4_2)
9477static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9478 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009479 .init_caps = NFS_CAP_READDIRPLUS
9480 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009481 | NFS_CAP_POSIX_LOCK
9482 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009483 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009484 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009485 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009486 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009487 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009488 | NFS_CAP_LAYOUTSTATS
9489 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009490 .init_client = nfs41_init_client,
9491 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009492 .match_stateid = nfs41_match_stateid,
9493 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009494 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009495 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009496 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009497 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009498 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009499 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9500 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9501 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009502 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009503};
9504#endif
9505
Trond Myklebust97dc1352010-06-16 09:52:26 -04009506const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9507 [0] = &nfs_v4_0_minor_ops,
9508#if defined(CONFIG_NFS_V4_1)
9509 [1] = &nfs_v4_1_minor_ops,
9510#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009511#if defined(CONFIG_NFS_V4_2)
9512 [2] = &nfs_v4_2_minor_ops,
9513#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009514};
9515
Trond Myklebust13997822016-07-24 17:10:52 -04009516static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009517{
9518 ssize_t error, error2;
9519
9520 error = generic_listxattr(dentry, list, size);
9521 if (error < 0)
9522 return error;
9523 if (list) {
9524 list += error;
9525 size -= error;
9526 }
9527
9528 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9529 if (error2 < 0)
9530 return error2;
9531 return error + error2;
9532}
9533
Trond Myklebust17f26b12013-08-21 15:48:42 -04009534static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009535 .create = nfs_create,
9536 .lookup = nfs_lookup,
9537 .atomic_open = nfs_atomic_open,
9538 .link = nfs_link,
9539 .unlink = nfs_unlink,
9540 .symlink = nfs_symlink,
9541 .mkdir = nfs_mkdir,
9542 .rmdir = nfs_rmdir,
9543 .mknod = nfs_mknod,
9544 .rename = nfs_rename,
9545 .permission = nfs_permission,
9546 .getattr = nfs_getattr,
9547 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009548 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009549};
9550
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009551static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009552 .permission = nfs_permission,
9553 .getattr = nfs_getattr,
9554 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009555 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009556};
9557
David Howells509de812006-08-22 20:06:11 -04009558const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 .version = 4, /* protocol version */
9560 .dentry_ops = &nfs4_dentry_operations,
9561 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009562 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009563 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009564 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009565 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009566 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 .getattr = nfs4_proc_getattr,
9568 .setattr = nfs4_proc_setattr,
9569 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009570 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 .access = nfs4_proc_access,
9572 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 .create = nfs4_proc_create,
9574 .remove = nfs4_proc_remove,
9575 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009576 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009578 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009579 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009580 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009581 .link = nfs4_proc_link,
9582 .symlink = nfs4_proc_symlink,
9583 .mkdir = nfs4_proc_mkdir,
9584 .rmdir = nfs4_proc_remove,
9585 .readdir = nfs4_proc_readdir,
9586 .mknod = nfs4_proc_mknod,
9587 .statfs = nfs4_proc_statfs,
9588 .fsinfo = nfs4_proc_fsinfo,
9589 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009590 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009592 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009594 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009596 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009598 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009599 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009600 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009601 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009602 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009603 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009604 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009605 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009606 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009607 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009608 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009609 .create_server = nfs4_create_server,
9610 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611};
9612
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009613static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009614 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009615 .list = nfs4_xattr_list_nfs4_acl,
9616 .get = nfs4_xattr_get_nfs4_acl,
9617 .set = nfs4_xattr_set_nfs4_acl,
9618};
9619
9620const struct xattr_handler *nfs4_xattr_handlers[] = {
9621 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009622#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9623 &nfs4_xattr_nfs4_label_handler,
9624#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009625 NULL
9626};
9627
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628/*
9629 * Local variables:
9630 * c-basic-offset: 8
9631 * End:
9632 */