blob: b927fda32e7408190e63999b461b1a2f72a45916 [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 Myklebust82571552017-11-07 11:14:49 -05006074 struct nfs4_exception exception = {
6075 .inode = calldata->lsp->ls_state->inode,
6076 .stateid = &calldata->arg.stateid,
6077 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006078
Trond Myklebust14516c32010-07-31 14:29:06 -04006079 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6080 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006081 switch (task->tk_status) {
6082 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006083 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006084 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006085 if (nfs4_update_lock_stateid(calldata->lsp,
6086 &calldata->res.stateid))
6087 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006088 case -NFS4ERR_ADMIN_REVOKED:
6089 case -NFS4ERR_EXPIRED:
6090 nfs4_free_revoked_stateid(calldata->server,
6091 &calldata->arg.stateid,
6092 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006093 case -NFS4ERR_BAD_STATEID:
6094 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006095 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006096 if (!nfs4_stateid_match(&calldata->arg.stateid,
6097 &calldata->lsp->ls_stateid))
6098 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006099 break;
6100 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006101 task->tk_status = nfs4_async_handle_exception(task,
6102 calldata->server, task->tk_status,
6103 &exception);
6104 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006105 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006106 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006107 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006108}
6109
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006110static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006111{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006112 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006114 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6115 nfs_async_iocounter_wait(task, calldata->l_ctx))
6116 return;
6117
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006118 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006119 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006120 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006121 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006122 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006123 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006124 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006125 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006126 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006127 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006128 &calldata->res.seq_res,
6129 task) != 0)
6130 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006131 return;
6132out_no_action:
6133 task->tk_action = NULL;
6134out_wait:
6135 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136}
6137
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006138static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006139 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006140 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006141 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006142};
6143
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006144static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6145 struct nfs_open_context *ctx,
6146 struct nfs4_lock_state *lsp,
6147 struct nfs_seqid *seqid)
6148{
6149 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006150 struct rpc_message msg = {
6151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6152 .rpc_cred = ctx->cred,
6153 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006154 struct rpc_task_setup task_setup_data = {
6155 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006156 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006157 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006158 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006159 .flags = RPC_TASK_ASYNC,
6160 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006162 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6163 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6164
Frank Filz137d6ac2007-07-09 15:32:29 -07006165 /* Ensure this is an unlock - when canceling a lock, the
6166 * canceled lock is passed in, and it won't be an unlock.
6167 */
6168 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006169 if (fl->fl_flags & FL_CLOSE)
6170 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006171
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006172 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6173 if (data == NULL) {
6174 nfs_free_seqid(seqid);
6175 return ERR_PTR(-ENOMEM);
6176 }
6177
Chuck Levera9c92d62013-08-09 12:48:18 -04006178 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006179 msg.rpc_argp = &data->arg;
6180 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006181 task_setup_data.callback_data = data;
6182 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006183}
6184
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6186{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006187 struct inode *inode = state->inode;
6188 struct nfs4_state_owner *sp = state->owner;
6189 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006190 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006191 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006192 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006193 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006194 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006195 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196
Trond Myklebust9b073572006-06-29 16:38:34 -04006197 status = nfs4_set_lock_state(state, request);
6198 /* Unlock _before_ we do the RPC call */
6199 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006200 /* Exclude nfs_delegation_claim_locks() */
6201 mutex_lock(&sp->so_delegreturn_mutex);
6202 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006203 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006204 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006205 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006206 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006207 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006208 }
6209 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006210 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006211 if (status != 0)
6212 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006213 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006214 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006215 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6216 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006217 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6218 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006219 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006220 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006221 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006222 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006223 status = PTR_ERR(task);
6224 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006225 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006226 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006227 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006228out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006229 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006230 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006231 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232}
6233
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006234struct nfs4_lockdata {
6235 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006236 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006237 struct nfs4_lock_state *lsp;
6238 struct nfs_open_context *ctx;
6239 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006240 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 int rpc_status;
6242 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006243 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006244};
6245
6246static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006247 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6248 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006249{
6250 struct nfs4_lockdata *p;
6251 struct inode *inode = lsp->ls_state->inode;
6252 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006253 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006255 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006256 if (p == NULL)
6257 return NULL;
6258
6259 p->arg.fh = NFS_FH(inode);
6260 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006261 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006262 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006263 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006264 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6265 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006266 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006267 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006268 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006269 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006270 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006271 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006272 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006273 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006274 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006275 p->ctx = get_nfs_open_context(ctx);
6276 memcpy(&p->fl, fl, sizeof(p->fl));
6277 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006278out_free_seqid:
6279 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006280out_free:
6281 kfree(p);
6282 return NULL;
6283}
6284
6285static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6286{
6287 struct nfs4_lockdata *data = calldata;
6288 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Harvey Harrison3110ff82008-05-02 13:42:44 -07006290 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006291 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006292 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006294 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006295 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006296 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006297 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006298 nfs4_stateid_copy(&data->arg.open_stateid,
6299 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006300 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006301 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006302 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006303 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006304 nfs4_stateid_copy(&data->arg.lock_stateid,
6305 &data->lsp->ls_stateid);
6306 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006307 if (!nfs4_valid_open_stateid(state)) {
6308 data->rpc_status = -EBADF;
6309 task->tk_action = NULL;
6310 goto out_release_open_seqid;
6311 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006312 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006313 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006314 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006315 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006316 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006317 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006318out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006319 nfs_release_seqid(data->arg.open_seqid);
6320out_release_lock_seqid:
6321 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006322out_wait:
6323 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006324 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006325}
6326
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006327static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6328{
6329 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006330 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006331
Harvey Harrison3110ff82008-05-02 13:42:44 -07006332 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006333
Trond Myklebust14516c32010-07-31 14:29:06 -04006334 if (!nfs4_sequence_done(task, &data->res.seq_res))
6335 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006336
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006337 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006338 switch (task->tk_status) {
6339 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006340 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006341 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006342 if (data->arg.new_lock) {
6343 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006344 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006345 rpc_restart_call_prepare(task);
6346 break;
6347 }
6348 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006349 if (data->arg.new_lock_owner != 0) {
6350 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6351 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6352 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6353 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6354 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006355 break;
6356 case -NFS4ERR_BAD_STATEID:
6357 case -NFS4ERR_OLD_STATEID:
6358 case -NFS4ERR_STALE_STATEID:
6359 case -NFS4ERR_EXPIRED:
6360 if (data->arg.new_lock_owner != 0) {
6361 if (!nfs4_stateid_match(&data->arg.open_stateid,
6362 &lsp->ls_state->open_stateid))
6363 rpc_restart_call_prepare(task);
6364 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6365 &lsp->ls_stateid))
6366 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006367 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006368 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006369}
6370
6371static void nfs4_lock_release(void *calldata)
6372{
6373 struct nfs4_lockdata *data = calldata;
6374
Harvey Harrison3110ff82008-05-02 13:42:44 -07006375 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006376 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006377 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006378 struct rpc_task *task;
6379 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6380 data->arg.lock_seqid);
6381 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006382 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006383 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006384 } else
6385 nfs_free_seqid(data->arg.lock_seqid);
6386 nfs4_put_lock_state(data->lsp);
6387 put_nfs_open_context(data->ctx);
6388 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006389 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006390}
6391
6392static const struct rpc_call_ops nfs4_lock_ops = {
6393 .rpc_call_prepare = nfs4_lock_prepare,
6394 .rpc_call_done = nfs4_lock_done,
6395 .rpc_release = nfs4_lock_release,
6396};
6397
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006398static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6399{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006400 switch (error) {
6401 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006402 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006403 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006404 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006405 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006406 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006407 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006408 break;
6409 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006410 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006411 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006412 };
6413}
6414
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006415static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006416{
6417 struct nfs4_lockdata *data;
6418 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006419 struct rpc_message msg = {
6420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6421 .rpc_cred = state->owner->so_cred,
6422 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006423 struct rpc_task_setup task_setup_data = {
6424 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006425 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006426 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006427 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006428 .flags = RPC_TASK_ASYNC,
6429 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006430 int ret;
6431
Harvey Harrison3110ff82008-05-02 13:42:44 -07006432 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006433 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006434 fl->fl_u.nfs4_fl.owner,
6435 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006436 if (data == NULL)
6437 return -ENOMEM;
6438 if (IS_SETLKW(cmd))
6439 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006440 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006441 msg.rpc_argp = &data->arg;
6442 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006443 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006444 if (recovery_type > NFS_LOCK_NEW) {
6445 if (recovery_type == NFS_LOCK_RECLAIM)
6446 data->arg.reclaim = NFS_LOCK_RECLAIM;
6447 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006448 } else
6449 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006450 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006451 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006452 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006453 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006454 if (ret == 0) {
6455 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006456 if (ret)
6457 nfs4_handle_setlk_error(data->server, data->lsp,
6458 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006459 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006460 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006461 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006462 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006463 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465}
6466
6467static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6468{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006469 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006470 struct nfs4_exception exception = {
6471 .inode = state->inode,
6472 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006473 int err;
6474
6475 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006476 /* Cache the lock if possible... */
6477 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6478 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006479 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006480 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006481 break;
6482 nfs4_handle_exception(server, err, &exception);
6483 } while (exception.retry);
6484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485}
6486
6487static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6488{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006489 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006490 struct nfs4_exception exception = {
6491 .inode = state->inode,
6492 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006493 int err;
6494
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006495 err = nfs4_set_lock_state(state, request);
6496 if (err != 0)
6497 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006498 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006499 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6500 return 0;
6501 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006502 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006503 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6504 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006505 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006506 switch (err) {
6507 default:
6508 goto out;
6509 case -NFS4ERR_GRACE:
6510 case -NFS4ERR_DELAY:
6511 nfs4_handle_exception(server, err, &exception);
6512 err = 0;
6513 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006514 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006515out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517}
6518
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006519#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006520static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6521{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006522 struct nfs4_lock_state *lsp;
6523 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006524
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006525 status = nfs4_set_lock_state(state, request);
6526 if (status != 0)
6527 return status;
6528 lsp = request->fl_u.nfs4_fl.owner;
6529 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6530 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6531 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006532 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006533}
6534#endif
6535
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6537{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006538 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006539 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006540 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006541 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542
Trond Myklebust01c3b862006-06-29 16:38:39 -04006543 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006544 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006545 if (status < 0)
6546 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006547 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006548 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006549 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006550 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006551 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006552 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006553 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006554 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006555 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006556 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006557 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006558 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006559 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006560 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006561out:
6562 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 return status;
6564}
6565
6566static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6567{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006568 struct nfs4_exception exception = {
6569 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006570 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 int err;
6573
6574 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006575 err = _nfs4_proc_setlk(state, cmd, request);
6576 if (err == -NFS4ERR_DENIED)
6577 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006579 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 } while (exception.retry);
6581 return err;
6582}
6583
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006584#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6585#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6586
6587static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006588nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6589 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006590{
6591 int status = -ERESTARTSYS;
6592 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6593
6594 while(!signalled()) {
6595 status = nfs4_proc_setlk(state, cmd, request);
6596 if ((status != -EAGAIN) || IS_SETLK(cmd))
6597 break;
6598 freezable_schedule_timeout_interruptible(timeout);
6599 timeout *= 2;
6600 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6601 status = -ERESTARTSYS;
6602 }
6603 return status;
6604}
6605
Jeff Laytona1d617d82016-09-17 18:17:39 -04006606#ifdef CONFIG_NFS_V4_1
6607struct nfs4_lock_waiter {
6608 struct task_struct *task;
6609 struct inode *inode;
6610 struct nfs_lowner *owner;
6611 bool notified;
6612};
6613
6614static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006615nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006616{
6617 int ret;
6618 struct cb_notify_lock_args *cbnl = key;
6619 struct nfs4_lock_waiter *waiter = wait->private;
6620 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6621 *wowner = waiter->owner;
6622
6623 /* Only wake if the callback was for the same owner */
6624 if (lowner->clientid != wowner->clientid ||
6625 lowner->id != wowner->id ||
6626 lowner->s_dev != wowner->s_dev)
6627 return 0;
6628
6629 /* Make sure it's for the right inode */
6630 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6631 return 0;
6632
6633 waiter->notified = true;
6634
6635 /* override "private" so we can use default_wake_function */
6636 wait->private = waiter->task;
6637 ret = autoremove_wake_function(wait, mode, flags, key);
6638 wait->private = waiter;
6639 return ret;
6640}
6641
6642static int
6643nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6644{
6645 int status = -ERESTARTSYS;
6646 unsigned long flags;
6647 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6648 struct nfs_server *server = NFS_SERVER(state->inode);
6649 struct nfs_client *clp = server->nfs_client;
6650 wait_queue_head_t *q = &clp->cl_lock_waitq;
6651 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6652 .id = lsp->ls_seqid.owner_id,
6653 .s_dev = server->s_dev };
6654 struct nfs4_lock_waiter waiter = { .task = current,
6655 .inode = state->inode,
6656 .owner = &owner,
6657 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006658 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006659
6660 /* Don't bother with waitqueue if we don't expect a callback */
6661 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6662 return nfs4_retry_setlk_simple(state, cmd, request);
6663
6664 init_wait(&wait);
6665 wait.private = &waiter;
6666 wait.func = nfs4_wake_lock_waiter;
6667 add_wait_queue(q, &wait);
6668
6669 while(!signalled()) {
6670 status = nfs4_proc_setlk(state, cmd, request);
6671 if ((status != -EAGAIN) || IS_SETLK(cmd))
6672 break;
6673
6674 status = -ERESTARTSYS;
6675 spin_lock_irqsave(&q->lock, flags);
6676 if (waiter.notified) {
6677 spin_unlock_irqrestore(&q->lock, flags);
6678 continue;
6679 }
6680 set_current_state(TASK_INTERRUPTIBLE);
6681 spin_unlock_irqrestore(&q->lock, flags);
6682
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006683 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006684 }
6685
6686 finish_wait(q, &wait);
6687 return status;
6688}
6689#else /* !CONFIG_NFS_V4_1 */
6690static inline int
6691nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6692{
6693 return nfs4_retry_setlk_simple(state, cmd, request);
6694}
6695#endif
6696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697static int
6698nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6699{
6700 struct nfs_open_context *ctx;
6701 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 int status;
6703
6704 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006705 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 state = ctx->state;
6707
Trond Myklebustd9531262009-07-21 19:22:38 -04006708 if (IS_GETLK(cmd)) {
6709 if (state != NULL)
6710 return nfs4_proc_getlk(state, F_GETLK, request);
6711 return 0;
6712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
6714 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6715 return -EINVAL;
6716
Trond Myklebustd9531262009-07-21 19:22:38 -04006717 if (request->fl_type == F_UNLCK) {
6718 if (state != NULL)
6719 return nfs4_proc_unlck(state, cmd, request);
6720 return 0;
6721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722
Trond Myklebustd9531262009-07-21 19:22:38 -04006723 if (state == NULL)
6724 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006725
6726 if ((request->fl_flags & FL_POSIX) &&
6727 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6728 return -ENOLCK;
6729
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006730 /*
6731 * Don't rely on the VFS having checked the file open mode,
6732 * since it won't do this for flock() locks.
6733 */
6734 switch (request->fl_type) {
6735 case F_RDLCK:
6736 if (!(filp->f_mode & FMODE_READ))
6737 return -EBADF;
6738 break;
6739 case F_WRLCK:
6740 if (!(filp->f_mode & FMODE_WRITE))
6741 return -EBADF;
6742 }
6743
Jeff Layton1ea67db2016-09-17 18:17:37 -04006744 status = nfs4_set_lock_state(state, request);
6745 if (status != 0)
6746 return status;
6747
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006748 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749}
6750
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006751int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006752{
6753 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006754 int err;
6755
6756 err = nfs4_set_lock_state(state, fl);
6757 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006758 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006759 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006760 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006761}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006762
Trond Myklebustcf470c32012-03-07 13:49:12 -05006763struct nfs_release_lockowner_data {
6764 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006765 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006766 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006767 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006768 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006769};
6770
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006771static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6772{
6773 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006774 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006775 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6776 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006777 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006778 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006779}
6780
6781static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6782{
6783 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006784 struct nfs_server *server = data->server;
6785
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006786 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006787
6788 switch (task->tk_status) {
6789 case 0:
6790 renew_lease(server, data->timestamp);
6791 break;
6792 case -NFS4ERR_STALE_CLIENTID:
6793 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006794 nfs4_schedule_lease_recovery(server->nfs_client);
6795 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006796 case -NFS4ERR_LEASE_MOVED:
6797 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006798 if (nfs4_async_handle_error(task, server,
6799 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006800 rpc_restart_call_prepare(task);
6801 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006802}
6803
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006804static void nfs4_release_lockowner_release(void *calldata)
6805{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006806 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006807 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006808 kfree(calldata);
6809}
6810
Trond Myklebust17280172012-03-11 13:11:00 -04006811static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006812 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6813 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006814 .rpc_release = nfs4_release_lockowner_release,
6815};
6816
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006817static void
6818nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006819{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006820 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006821 struct rpc_message msg = {
6822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6823 };
6824
6825 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006826 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006827
Trond Myklebustcf470c32012-03-07 13:49:12 -05006828 data = kmalloc(sizeof(*data), GFP_NOFS);
6829 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006830 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006831 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006832 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006833 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6834 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6835 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006836
Trond Myklebustcf470c32012-03-07 13:49:12 -05006837 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006838 msg.rpc_resp = &data->res;
6839 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006840 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006841}
6842
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006843#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6844
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006845static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006846 struct dentry *unused, struct inode *inode,
6847 const char *key, const void *buf,
6848 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006849{
Al Viro59301222016-05-27 10:19:30 -04006850 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006851}
6852
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006853static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006854 struct dentry *unused, struct inode *inode,
6855 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006856{
Al Virob2968212016-04-10 20:48:24 -04006857 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006858}
6859
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006860static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006861{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006862 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006863}
6864
David Quigleyc9bccef2013-05-22 12:50:45 -04006865#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006866
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006867static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006868 struct dentry *unused, struct inode *inode,
6869 const char *key, const void *buf,
6870 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006871{
6872 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006873 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006874
6875 return -EOPNOTSUPP;
6876}
6877
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006878static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006879 struct dentry *unused, struct inode *inode,
6880 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006881{
6882 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006883 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006884 return -EOPNOTSUPP;
6885}
6886
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006887static ssize_t
6888nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006889{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006890 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006891
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006892 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6893 len = security_inode_listsecurity(inode, list, list_len);
6894 if (list_len && len > list_len)
6895 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006896 }
6897 return len;
6898}
6899
6900static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6901 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006902 .get = nfs4_xattr_get_nfs4_label,
6903 .set = nfs4_xattr_set_nfs4_label,
6904};
David Quigleyc9bccef2013-05-22 12:50:45 -04006905
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006906#else
6907
6908static ssize_t
6909nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6910{
6911 return 0;
6912}
6913
6914#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006915
Andy Adamson533eb462011-06-13 18:25:56 -04006916/*
6917 * nfs_fhget will use either the mounted_on_fileid or the fileid
6918 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006919static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6920{
Andy Adamson533eb462011-06-13 18:25:56 -04006921 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6922 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6923 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006924 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006925 return;
6926
6927 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006928 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006929 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6930 fattr->nlink = 2;
6931}
6932
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006933static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6934 const struct qstr *name,
6935 struct nfs4_fs_locations *fs_locations,
6936 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006937{
6938 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006939 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006940 struct nfs4_fs_locations_arg args = {
6941 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006942 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006943 .page = page,
6944 .bitmask = bitmask,
6945 };
Benny Halevy22958462009-04-01 09:22:02 -04006946 struct nfs4_fs_locations_res res = {
6947 .fs_locations = fs_locations,
6948 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006949 struct rpc_message msg = {
6950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6951 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006952 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006953 };
6954 int status;
6955
Harvey Harrison3110ff82008-05-02 13:42:44 -07006956 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006957
Chuck Leverc05cefc2017-11-05 15:45:22 -05006958 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6959 bitmask[1] = nfs4_fattr_bitmap[1];
6960
Andy Adamson533eb462011-06-13 18:25:56 -04006961 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6962 * is not supported */
6963 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05006964 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006965 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05006966 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006967
Trond Myklebustc228fd32007-01-13 02:28:11 -05006968 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006969 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006970 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006971 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006972 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006973 return status;
6974}
6975
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006976int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6977 const struct qstr *name,
6978 struct nfs4_fs_locations *fs_locations,
6979 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006980{
6981 struct nfs4_exception exception = { };
6982 int err;
6983 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006984 err = _nfs4_proc_fs_locations(client, dir, name,
6985 fs_locations, page);
6986 trace_nfs4_get_fs_locations(dir, name, err);
6987 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006988 &exception);
6989 } while (exception.retry);
6990 return err;
6991}
6992
Chuck Leverb03d7352013-10-17 14:12:50 -04006993/*
6994 * This operation also signals the server that this client is
6995 * performing migration recovery. The server can stop returning
6996 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6997 * appended to this compound to identify the client ID which is
6998 * performing recovery.
6999 */
7000static int _nfs40_proc_get_locations(struct inode *inode,
7001 struct nfs4_fs_locations *locations,
7002 struct page *page, struct rpc_cred *cred)
7003{
7004 struct nfs_server *server = NFS_SERVER(inode);
7005 struct rpc_clnt *clnt = server->client;
7006 u32 bitmask[2] = {
7007 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7008 };
7009 struct nfs4_fs_locations_arg args = {
7010 .clientid = server->nfs_client->cl_clientid,
7011 .fh = NFS_FH(inode),
7012 .page = page,
7013 .bitmask = bitmask,
7014 .migration = 1, /* skip LOOKUP */
7015 .renew = 1, /* append RENEW */
7016 };
7017 struct nfs4_fs_locations_res res = {
7018 .fs_locations = locations,
7019 .migration = 1,
7020 .renew = 1,
7021 };
7022 struct rpc_message msg = {
7023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7024 .rpc_argp = &args,
7025 .rpc_resp = &res,
7026 .rpc_cred = cred,
7027 };
7028 unsigned long now = jiffies;
7029 int status;
7030
7031 nfs_fattr_init(&locations->fattr);
7032 locations->server = server;
7033 locations->nlocations = 0;
7034
7035 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7036 nfs4_set_sequence_privileged(&args.seq_args);
7037 status = nfs4_call_sync_sequence(clnt, server, &msg,
7038 &args.seq_args, &res.seq_res);
7039 if (status)
7040 return status;
7041
7042 renew_lease(server, now);
7043 return 0;
7044}
7045
7046#ifdef CONFIG_NFS_V4_1
7047
7048/*
7049 * This operation also signals the server that this client is
7050 * performing migration recovery. The server can stop asserting
7051 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7052 * performing this operation is identified in the SEQUENCE
7053 * operation in this compound.
7054 *
7055 * When the client supports GETATTR(fs_locations_info), it can
7056 * be plumbed in here.
7057 */
7058static int _nfs41_proc_get_locations(struct inode *inode,
7059 struct nfs4_fs_locations *locations,
7060 struct page *page, struct rpc_cred *cred)
7061{
7062 struct nfs_server *server = NFS_SERVER(inode);
7063 struct rpc_clnt *clnt = server->client;
7064 u32 bitmask[2] = {
7065 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7066 };
7067 struct nfs4_fs_locations_arg args = {
7068 .fh = NFS_FH(inode),
7069 .page = page,
7070 .bitmask = bitmask,
7071 .migration = 1, /* skip LOOKUP */
7072 };
7073 struct nfs4_fs_locations_res res = {
7074 .fs_locations = locations,
7075 .migration = 1,
7076 };
7077 struct rpc_message msg = {
7078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7079 .rpc_argp = &args,
7080 .rpc_resp = &res,
7081 .rpc_cred = cred,
7082 };
7083 int status;
7084
7085 nfs_fattr_init(&locations->fattr);
7086 locations->server = server;
7087 locations->nlocations = 0;
7088
7089 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7090 nfs4_set_sequence_privileged(&args.seq_args);
7091 status = nfs4_call_sync_sequence(clnt, server, &msg,
7092 &args.seq_args, &res.seq_res);
7093 if (status == NFS4_OK &&
7094 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7095 status = -NFS4ERR_LEASE_MOVED;
7096 return status;
7097}
7098
7099#endif /* CONFIG_NFS_V4_1 */
7100
7101/**
7102 * nfs4_proc_get_locations - discover locations for a migrated FSID
7103 * @inode: inode on FSID that is migrating
7104 * @locations: result of query
7105 * @page: buffer
7106 * @cred: credential to use for this operation
7107 *
7108 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7109 * operation failed, or a negative errno if a local error occurred.
7110 *
7111 * On success, "locations" is filled in, but if the server has
7112 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7113 * asserted.
7114 *
7115 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7116 * from this client that require migration recovery.
7117 */
7118int nfs4_proc_get_locations(struct inode *inode,
7119 struct nfs4_fs_locations *locations,
7120 struct page *page, struct rpc_cred *cred)
7121{
7122 struct nfs_server *server = NFS_SERVER(inode);
7123 struct nfs_client *clp = server->nfs_client;
7124 const struct nfs4_mig_recovery_ops *ops =
7125 clp->cl_mvops->mig_recovery_ops;
7126 struct nfs4_exception exception = { };
7127 int status;
7128
7129 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7130 (unsigned long long)server->fsid.major,
7131 (unsigned long long)server->fsid.minor,
7132 clp->cl_hostname);
7133 nfs_display_fhandle(NFS_FH(inode), __func__);
7134
7135 do {
7136 status = ops->get_locations(inode, locations, page, cred);
7137 if (status != -NFS4ERR_DELAY)
7138 break;
7139 nfs4_handle_exception(server, status, &exception);
7140 } while (exception.retry);
7141 return status;
7142}
7143
Chuck Lever44c99932013-10-17 14:13:30 -04007144/*
7145 * This operation also signals the server that this client is
7146 * performing "lease moved" recovery. The server can stop
7147 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7148 * is appended to this compound to identify the client ID which is
7149 * performing recovery.
7150 */
7151static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7152{
7153 struct nfs_server *server = NFS_SERVER(inode);
7154 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7155 struct rpc_clnt *clnt = server->client;
7156 struct nfs4_fsid_present_arg args = {
7157 .fh = NFS_FH(inode),
7158 .clientid = clp->cl_clientid,
7159 .renew = 1, /* append RENEW */
7160 };
7161 struct nfs4_fsid_present_res res = {
7162 .renew = 1,
7163 };
7164 struct rpc_message msg = {
7165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7166 .rpc_argp = &args,
7167 .rpc_resp = &res,
7168 .rpc_cred = cred,
7169 };
7170 unsigned long now = jiffies;
7171 int status;
7172
7173 res.fh = nfs_alloc_fhandle();
7174 if (res.fh == NULL)
7175 return -ENOMEM;
7176
7177 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7178 nfs4_set_sequence_privileged(&args.seq_args);
7179 status = nfs4_call_sync_sequence(clnt, server, &msg,
7180 &args.seq_args, &res.seq_res);
7181 nfs_free_fhandle(res.fh);
7182 if (status)
7183 return status;
7184
7185 do_renew_lease(clp, now);
7186 return 0;
7187}
7188
7189#ifdef CONFIG_NFS_V4_1
7190
7191/*
7192 * This operation also signals the server that this client is
7193 * performing "lease moved" recovery. The server can stop asserting
7194 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7195 * this operation is identified in the SEQUENCE operation in this
7196 * compound.
7197 */
7198static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7199{
7200 struct nfs_server *server = NFS_SERVER(inode);
7201 struct rpc_clnt *clnt = server->client;
7202 struct nfs4_fsid_present_arg args = {
7203 .fh = NFS_FH(inode),
7204 };
7205 struct nfs4_fsid_present_res res = {
7206 };
7207 struct rpc_message msg = {
7208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7209 .rpc_argp = &args,
7210 .rpc_resp = &res,
7211 .rpc_cred = cred,
7212 };
7213 int status;
7214
7215 res.fh = nfs_alloc_fhandle();
7216 if (res.fh == NULL)
7217 return -ENOMEM;
7218
7219 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7220 nfs4_set_sequence_privileged(&args.seq_args);
7221 status = nfs4_call_sync_sequence(clnt, server, &msg,
7222 &args.seq_args, &res.seq_res);
7223 nfs_free_fhandle(res.fh);
7224 if (status == NFS4_OK &&
7225 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7226 status = -NFS4ERR_LEASE_MOVED;
7227 return status;
7228}
7229
7230#endif /* CONFIG_NFS_V4_1 */
7231
7232/**
7233 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7234 * @inode: inode on FSID to check
7235 * @cred: credential to use for this operation
7236 *
7237 * Server indicates whether the FSID is present, moved, or not
7238 * recognized. This operation is necessary to clear a LEASE_MOVED
7239 * condition for this client ID.
7240 *
7241 * Returns NFS4_OK if the FSID is present on this server,
7242 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7243 * NFS4ERR code if some error occurred on the server, or a
7244 * negative errno if a local failure occurred.
7245 */
7246int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7247{
7248 struct nfs_server *server = NFS_SERVER(inode);
7249 struct nfs_client *clp = server->nfs_client;
7250 const struct nfs4_mig_recovery_ops *ops =
7251 clp->cl_mvops->mig_recovery_ops;
7252 struct nfs4_exception exception = { };
7253 int status;
7254
7255 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7256 (unsigned long long)server->fsid.major,
7257 (unsigned long long)server->fsid.minor,
7258 clp->cl_hostname);
7259 nfs_display_fhandle(NFS_FH(inode), __func__);
7260
7261 do {
7262 status = ops->fsid_present(inode, cred);
7263 if (status != -NFS4ERR_DELAY)
7264 break;
7265 nfs4_handle_exception(server, status, &exception);
7266 } while (exception.retry);
7267 return status;
7268}
7269
Andy Adamson5ec16a82013-08-08 10:57:55 -04007270/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007271 * If 'use_integrity' is true and the state managment nfs_client
7272 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7273 * and the machine credential as per RFC3530bis and RFC5661 Security
7274 * Considerations sections. Otherwise, just use the user cred with the
7275 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007276 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007277static 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 +00007278{
7279 int status;
7280 struct nfs4_secinfo_arg args = {
7281 .dir_fh = NFS_FH(dir),
7282 .name = name,
7283 };
7284 struct nfs4_secinfo_res res = {
7285 .flavors = flavors,
7286 };
7287 struct rpc_message msg = {
7288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7289 .rpc_argp = &args,
7290 .rpc_resp = &res,
7291 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007292 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007293 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007294
7295 if (use_integrity) {
7296 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007297 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7298 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007299 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007300
7301 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007302
7303 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7304 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7305
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007306 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7307 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007308 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007309
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007310 if (cred)
7311 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007312
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007313 return status;
7314}
7315
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007316int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7317 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007318{
7319 struct nfs4_exception exception = { };
7320 int err;
7321 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007322 err = -NFS4ERR_WRONGSEC;
7323
7324 /* try to use integrity protection with machine cred */
7325 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7326 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7327
7328 /*
7329 * if unable to use integrity protection, or SECINFO with
7330 * integrity protection returns NFS4ERR_WRONGSEC (which is
7331 * disallowed by spec, but exists in deployed servers) use
7332 * the current filesystem's rpc_client and the user cred.
7333 */
7334 if (err == -NFS4ERR_WRONGSEC)
7335 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7336
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007337 trace_nfs4_secinfo(dir, name, err);
7338 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007339 &exception);
7340 } while (exception.retry);
7341 return err;
7342}
7343
Andy Adamson557134a2009-04-01 09:21:53 -04007344#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007345/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007346 * Check the exchange flags returned by the server for invalid flags, having
7347 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7348 * DS flags set.
7349 */
7350static int nfs4_check_cl_exchange_flags(u32 flags)
7351{
7352 if (flags & ~EXCHGID4_FLAG_MASK_R)
7353 goto out_inval;
7354 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7355 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7356 goto out_inval;
7357 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7358 goto out_inval;
7359 return NFS_OK;
7360out_inval:
7361 return -NFS4ERR_INVAL;
7362}
7363
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007364static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007365nfs41_same_server_scope(struct nfs41_server_scope *a,
7366 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007367{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007368 if (a->server_scope_sz != b->server_scope_sz)
7369 return false;
7370 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007371}
7372
Andy Adamson02a95de2016-02-05 16:08:37 -05007373static void
7374nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7375{
7376}
7377
7378static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7379 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7380};
7381
Andy Adamson357f54d2010-12-14 10:11:57 -05007382/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007383 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007384 *
7385 * The 4.1 client currently uses the same TCP connection for the
7386 * fore and backchannel.
7387 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007388static
7389int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7390 struct rpc_xprt *xprt,
7391 struct nfs_client *clp,
7392 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007393{
7394 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007395 struct nfs41_bind_conn_to_session_args args = {
7396 .client = clp,
7397 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7398 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007399 struct nfs41_bind_conn_to_session_res res;
7400 struct rpc_message msg = {
7401 .rpc_proc =
7402 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007403 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007404 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007405 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007406 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007407 struct rpc_task_setup task_setup_data = {
7408 .rpc_client = clnt,
7409 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007410 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007411 .rpc_message = &msg,
7412 .flags = RPC_TASK_TIMEOUT,
7413 };
7414 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007415
Trond Myklebust71a097c2015-02-18 09:27:18 -08007416 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7417 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7418 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007419
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007420 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7421 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7422 args.dir = NFS4_CDFC4_FORE;
7423
7424 task = rpc_run_task(&task_setup_data);
7425 if (!IS_ERR(task)) {
7426 status = task->tk_status;
7427 rpc_put_task(task);
7428 } else
7429 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007430 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007431 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007432 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007433 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7434 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007435 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007436 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007437 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007438 dprintk("NFS: %s: Unexpected direction from server\n",
7439 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007440 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007441 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007442 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007443 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7444 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007445 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007446 }
7447 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007448
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007449 return status;
7450}
7451
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007452struct rpc_bind_conn_calldata {
7453 struct nfs_client *clp;
7454 struct rpc_cred *cred;
7455};
7456
7457static int
7458nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7459 struct rpc_xprt *xprt,
7460 void *calldata)
7461{
7462 struct rpc_bind_conn_calldata *p = calldata;
7463
7464 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7465}
7466
7467int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7468{
7469 struct rpc_bind_conn_calldata data = {
7470 .clp = clp,
7471 .cred = cred,
7472 };
7473 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7474 nfs4_proc_bind_conn_to_session_callback, &data);
7475}
7476
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007477/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007478 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7479 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007480 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007481static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7482 .how = SP4_MACH_CRED,
7483 .enforce.u.words = {
7484 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7485 1 << (OP_EXCHANGE_ID - 32) |
7486 1 << (OP_CREATE_SESSION - 32) |
7487 1 << (OP_DESTROY_SESSION - 32) |
7488 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007489 },
7490 .allow.u.words = {
7491 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007492 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007493 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007494 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007495 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007496 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007497 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007498 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007499 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007500 1 << (OP_FREE_STATEID - 32) |
7501 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007502 }
7503};
7504
7505/*
7506 * Select the state protection mode for client `clp' given the server results
7507 * from exchange_id in `sp'.
7508 *
7509 * Returns 0 on success, negative errno otherwise.
7510 */
7511static int nfs4_sp4_select_mode(struct nfs_client *clp,
7512 struct nfs41_state_protection *sp)
7513{
7514 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7515 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7516 1 << (OP_EXCHANGE_ID - 32) |
7517 1 << (OP_CREATE_SESSION - 32) |
7518 1 << (OP_DESTROY_SESSION - 32) |
7519 1 << (OP_DESTROY_CLIENTID - 32)
7520 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007521 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007522 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007523 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007524
7525 if (sp->how == SP4_MACH_CRED) {
7526 /* Print state protect result */
7527 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7528 for (i = 0; i <= LAST_NFS4_OP; i++) {
7529 if (test_bit(i, sp->enforce.u.longs))
7530 dfprintk(MOUNT, " enforce op %d\n", i);
7531 if (test_bit(i, sp->allow.u.longs))
7532 dfprintk(MOUNT, " allow op %d\n", i);
7533 }
7534
7535 /* make sure nothing is on enforce list that isn't supported */
7536 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7537 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7538 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007539 ret = -EINVAL;
7540 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007541 }
7542 }
7543
7544 /*
7545 * Minimal mode - state operations are allowed to use machine
7546 * credential. Note this already happens by default, so the
7547 * client doesn't have to do anything more than the negotiation.
7548 *
7549 * NOTE: we don't care if EXCHANGE_ID is in the list -
7550 * we're already using the machine cred for exchange_id
7551 * and will never use a different cred.
7552 */
7553 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7554 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7555 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7556 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7557 dfprintk(MOUNT, "sp4_mach_cred:\n");
7558 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007559 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007560 } else {
7561 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007562 ret = -EINVAL;
7563 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007564 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007565
7566 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007567 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7568 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007569 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7570 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007571 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007572 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007573
Andrew Elble99ade3c2015-12-02 09:39:51 -05007574 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7575 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007576 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007577 }
7578
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007579 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7580 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7581 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007582 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007583 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007584
7585 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7586 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7587 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007588 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007589 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007590
7591 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7592 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007593 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007594 }
7595
7596 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7597 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007598 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007599 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007600 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007601out:
7602 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007603 return 0;
7604}
7605
Andy Adamson8d89bd72016-09-09 09:22:18 -04007606struct nfs41_exchange_id_data {
7607 struct nfs41_exchange_id_res res;
7608 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007609};
7610
Andy Adamson8d89bd72016-09-09 09:22:18 -04007611static void nfs4_exchange_id_release(void *data)
7612{
7613 struct nfs41_exchange_id_data *cdata =
7614 (struct nfs41_exchange_id_data *)data;
7615
Olga Kornievskaia63513232017-03-13 10:36:19 -04007616 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007617 kfree(cdata->res.impl_id);
7618 kfree(cdata->res.server_scope);
7619 kfree(cdata->res.server_owner);
7620 kfree(cdata);
7621}
7622
7623static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007624 .rpc_release = nfs4_exchange_id_release,
7625};
7626
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007627/*
7628 * _nfs4_proc_exchange_id()
7629 *
7630 * Wrapper for EXCHANGE_ID operation.
7631 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007632static struct rpc_task *
7633nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007634 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007635{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007636 struct rpc_message msg = {
7637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007638 .rpc_cred = cred,
7639 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007640 struct rpc_task_setup task_setup_data = {
7641 .rpc_client = clp->cl_rpcclient,
7642 .callback_ops = &nfs4_exchange_id_call_ops,
7643 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007644 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007645 };
7646 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007647 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007648
Elena Reshetova212bf412017-10-20 12:53:38 +03007649 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007650 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007651
Trond Myklebust9c760d12017-07-31 18:38:50 -04007652 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007653 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007654 if (!calldata)
7655 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007656
Trond Myklebustfd405592017-08-01 16:02:47 -04007657 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007658
7659 status = nfs4_init_uniform_client_string(clp);
7660 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007661 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007662
Andy Adamson8d89bd72016-09-09 09:22:18 -04007663 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7664 GFP_NOFS);
7665 status = -ENOMEM;
7666 if (unlikely(calldata->res.server_owner == NULL))
7667 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007668
Andy Adamson8d89bd72016-09-09 09:22:18 -04007669 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007670 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007671 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007672 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007673
Andy Adamson8d89bd72016-09-09 09:22:18 -04007674 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7675 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007676 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007677
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007678 switch (sp4_how) {
7679 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007680 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007681 break;
7682
7683 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007684 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007685 break;
7686
7687 default:
7688 /* unsupported! */
7689 WARN_ON_ONCE(1);
7690 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007691 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007692 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007693 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007694 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007695 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007696 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7697 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007698 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007699 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007700 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7701 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007702#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007703 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007704#endif
7705 msg.rpc_argp = &calldata->args;
7706 msg.rpc_resp = &calldata->res;
7707 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007708
Trond Myklebust9c760d12017-07-31 18:38:50 -04007709 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007710
7711out_impl_id:
7712 kfree(calldata->res.impl_id);
7713out_server_scope:
7714 kfree(calldata->res.server_scope);
7715out_server_owner:
7716 kfree(calldata->res.server_owner);
7717out_calldata:
7718 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007719out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007720 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007721 return ERR_PTR(status);
7722}
7723
7724/*
7725 * _nfs4_proc_exchange_id()
7726 *
7727 * Wrapper for EXCHANGE_ID operation.
7728 */
7729static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7730 u32 sp4_how)
7731{
7732 struct rpc_task *task;
7733 struct nfs41_exchange_id_args *argp;
7734 struct nfs41_exchange_id_res *resp;
7735 int status;
7736
7737 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7738 if (IS_ERR(task))
7739 return PTR_ERR(task);
7740
7741 argp = task->tk_msg.rpc_argp;
7742 resp = task->tk_msg.rpc_resp;
7743 status = task->tk_status;
7744 if (status != 0)
7745 goto out;
7746
7747 status = nfs4_check_cl_exchange_flags(resp->flags);
7748 if (status != 0)
7749 goto out;
7750
7751 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7752 if (status != 0)
7753 goto out;
7754
7755 clp->cl_clientid = resp->clientid;
7756 clp->cl_exchange_flags = resp->flags;
7757 clp->cl_seqid = resp->seqid;
7758 /* Client ID is not confirmed */
7759 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7760 clear_bit(NFS4_SESSION_ESTABLISHED,
7761 &clp->cl_session->session_state);
7762
7763 if (clp->cl_serverscope != NULL &&
7764 !nfs41_same_server_scope(clp->cl_serverscope,
7765 resp->server_scope)) {
7766 dprintk("%s: server_scope mismatch detected\n",
7767 __func__);
7768 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7769 }
7770
7771 swap(clp->cl_serverowner, resp->server_owner);
7772 swap(clp->cl_serverscope, resp->server_scope);
7773 swap(clp->cl_implid, resp->impl_id);
7774
7775 /* Save the EXCHANGE_ID verifier session trunk tests */
7776 memcpy(clp->cl_confirm.data, argp->verifier.data,
7777 sizeof(clp->cl_confirm.data));
7778out:
7779 trace_nfs4_exchange_id(clp, status);
7780 rpc_put_task(task);
7781 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007782}
7783
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007784/*
7785 * nfs4_proc_exchange_id()
7786 *
7787 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7788 *
7789 * Since the clientid has expired, all compounds using sessions
7790 * associated with the stale clientid will be returning
7791 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7792 * be in some phase of session reset.
7793 *
7794 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7795 */
7796int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7797{
7798 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7799 int status;
7800
7801 /* try SP4_MACH_CRED if krb5i/p */
7802 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7803 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007804 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007805 if (!status)
7806 return 0;
7807 }
7808
7809 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007810 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007811}
7812
Andy Adamson04fa2c62016-09-09 09:22:29 -04007813/**
7814 * nfs4_test_session_trunk
7815 *
7816 * This is an add_xprt_test() test function called from
7817 * rpc_clnt_setup_test_and_add_xprt.
7818 *
7819 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7820 * and is dereferrenced in nfs4_exchange_id_release
7821 *
7822 * Upon success, add the new transport to the rpc_clnt
7823 *
7824 * @clnt: struct rpc_clnt to get new transport
7825 * @xprt: the rpc_xprt to test
7826 * @data: call data for _nfs4_proc_exchange_id.
7827 */
7828int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7829 void *data)
7830{
7831 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007832 struct rpc_task *task;
7833 int status;
7834
Andy Adamson04fa2c62016-09-09 09:22:29 -04007835 u32 sp4_how;
7836
7837 dprintk("--> %s try %s\n", __func__,
7838 xprt->address_strings[RPC_DISPLAY_ADDR]);
7839
7840 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7841
7842 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007843 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7844 if (IS_ERR(task))
7845 return PTR_ERR(task);
7846
7847 status = task->tk_status;
7848 if (status == 0)
7849 status = nfs4_detect_session_trunking(adata->clp,
7850 task->tk_msg.rpc_resp, xprt);
7851
7852 rpc_put_task(task);
7853 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007854}
7855EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7856
Trond Myklebust66245532012-05-25 17:18:09 -04007857static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7858 struct rpc_cred *cred)
7859{
7860 struct rpc_message msg = {
7861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7862 .rpc_argp = clp,
7863 .rpc_cred = cred,
7864 };
7865 int status;
7866
7867 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007868 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007869 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007870 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007871 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7872 return status;
7873}
7874
7875static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7876 struct rpc_cred *cred)
7877{
7878 unsigned int loop;
7879 int ret;
7880
7881 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7882 ret = _nfs4_proc_destroy_clientid(clp, cred);
7883 switch (ret) {
7884 case -NFS4ERR_DELAY:
7885 case -NFS4ERR_CLIENTID_BUSY:
7886 ssleep(1);
7887 break;
7888 default:
7889 return ret;
7890 }
7891 }
7892 return 0;
7893}
7894
7895int nfs4_destroy_clientid(struct nfs_client *clp)
7896{
7897 struct rpc_cred *cred;
7898 int ret = 0;
7899
7900 if (clp->cl_mvops->minor_version < 1)
7901 goto out;
7902 if (clp->cl_exchange_flags == 0)
7903 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007904 if (clp->cl_preserve_clid)
7905 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007906 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007907 ret = nfs4_proc_destroy_clientid(clp, cred);
7908 if (cred)
7909 put_rpccred(cred);
7910 switch (ret) {
7911 case 0:
7912 case -NFS4ERR_STALE_CLIENTID:
7913 clp->cl_exchange_flags = 0;
7914 }
7915out:
7916 return ret;
7917}
7918
Andy Adamson2050f0c2009-04-01 09:22:30 -04007919struct nfs4_get_lease_time_data {
7920 struct nfs4_get_lease_time_args *args;
7921 struct nfs4_get_lease_time_res *res;
7922 struct nfs_client *clp;
7923};
7924
7925static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7926 void *calldata)
7927{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007928 struct nfs4_get_lease_time_data *data =
7929 (struct nfs4_get_lease_time_data *)calldata;
7930
7931 dprintk("--> %s\n", __func__);
7932 /* just setup sequence, do not trigger session recovery
7933 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007934 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007935 &data->args->la_seq_args,
7936 &data->res->lr_seq_res,
7937 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007938 dprintk("<-- %s\n", __func__);
7939}
7940
7941/*
7942 * Called from nfs4_state_manager thread for session setup, so don't recover
7943 * from sequence operation or clientid errors.
7944 */
7945static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7946{
7947 struct nfs4_get_lease_time_data *data =
7948 (struct nfs4_get_lease_time_data *)calldata;
7949
7950 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007951 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7952 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007953 switch (task->tk_status) {
7954 case -NFS4ERR_DELAY:
7955 case -NFS4ERR_GRACE:
7956 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7957 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7958 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007959 /* fall through */
7960 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007961 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007962 return;
7963 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007964 dprintk("<-- %s\n", __func__);
7965}
7966
Trond Myklebust17280172012-03-11 13:11:00 -04007967static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007968 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7969 .rpc_call_done = nfs4_get_lease_time_done,
7970};
7971
7972int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7973{
7974 struct rpc_task *task;
7975 struct nfs4_get_lease_time_args args;
7976 struct nfs4_get_lease_time_res res = {
7977 .lr_fsinfo = fsinfo,
7978 };
7979 struct nfs4_get_lease_time_data data = {
7980 .args = &args,
7981 .res = &res,
7982 .clp = clp,
7983 };
7984 struct rpc_message msg = {
7985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7986 .rpc_argp = &args,
7987 .rpc_resp = &res,
7988 };
7989 struct rpc_task_setup task_setup = {
7990 .rpc_client = clp->cl_rpcclient,
7991 .rpc_message = &msg,
7992 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007993 .callback_data = &data,
7994 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007995 };
7996 int status;
7997
Chuck Levera9c92d62013-08-09 12:48:18 -04007998 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007999 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008000 task = rpc_run_task(&task_setup);
8001
8002 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008003 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008004
Anna Schumakerf6148712017-04-07 14:15:23 -04008005 status = task->tk_status;
8006 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008007 return status;
8008}
8009
Andy Adamsonfc931582009-04-01 09:22:31 -04008010/*
8011 * Initialize the values to be used by the client in CREATE_SESSION
8012 * If nfs4_init_session set the fore channel request and response sizes,
8013 * use them.
8014 *
8015 * Set the back channel max_resp_sz_cached to zero to force the client to
8016 * always set csa_cachethis to FALSE because the current implementation
8017 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8018 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008019static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8020 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008021{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008022 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008023 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008024
Andy Adamson18aad3d2013-06-26 12:21:49 -04008025 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8026 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8027
Andy Adamsonfc931582009-04-01 09:22:31 -04008028 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008029 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8030 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008031 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008032 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008033
8034 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008035 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008036 __func__,
8037 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008038 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008039
8040 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008041 args->bc_attrs.max_rqst_sz = max_bc_payload;
8042 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008043 args->bc_attrs.max_resp_sz_cached = 0;
8044 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008045 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008046
8047 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8048 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8049 __func__,
8050 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8051 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8052 args->bc_attrs.max_reqs);
8053}
8054
Trond Myklebust79969dd2015-02-18 11:30:18 -08008055static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8056 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008057{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008058 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008059 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008060
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008061 if (rcvd->max_resp_sz > sent->max_resp_sz)
8062 return -EINVAL;
8063 /*
8064 * Our requested max_ops is the minimum we need; we're not
8065 * prepared to break up compounds into smaller pieces than that.
8066 * So, no point even trying to continue if the server won't
8067 * cooperate:
8068 */
8069 if (rcvd->max_ops < sent->max_ops)
8070 return -EINVAL;
8071 if (rcvd->max_reqs == 0)
8072 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008073 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8074 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008075 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008076}
8077
Trond Myklebust79969dd2015-02-18 11:30:18 -08008078static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8079 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008080{
8081 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008082 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008083
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008084 if (!(res->flags & SESSION4_BACK_CHAN))
8085 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008086 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8087 return -EINVAL;
8088 if (rcvd->max_resp_sz < sent->max_resp_sz)
8089 return -EINVAL;
8090 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8091 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008092 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008093 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008094 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008095 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008096out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008097 return 0;
8098}
Andy Adamson8d353012009-04-01 09:22:32 -04008099
Andy Adamson8d353012009-04-01 09:22:32 -04008100static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008101 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008102{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008103 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008104
Trond Myklebust79969dd2015-02-18 11:30:18 -08008105 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008106 if (ret)
8107 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008108 return nfs4_verify_back_channel_attrs(args, res);
8109}
8110
8111static void nfs4_update_session(struct nfs4_session *session,
8112 struct nfs41_create_session_res *res)
8113{
8114 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008115 /* Mark client id and session as being confirmed */
8116 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8117 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008118 session->flags = res->flags;
8119 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008120 if (res->flags & SESSION4_BACK_CHAN)
8121 memcpy(&session->bc_attrs, &res->bc_attrs,
8122 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008123}
8124
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008125static int _nfs4_proc_create_session(struct nfs_client *clp,
8126 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008127{
8128 struct nfs4_session *session = clp->cl_session;
8129 struct nfs41_create_session_args args = {
8130 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008131 .clientid = clp->cl_clientid,
8132 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008133 .cb_program = NFS4_CALLBACK,
8134 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008135 struct nfs41_create_session_res res;
8136
Andy Adamsonfc931582009-04-01 09:22:31 -04008137 struct rpc_message msg = {
8138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8139 .rpc_argp = &args,
8140 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008141 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008142 };
8143 int status;
8144
Chuck Lever6b26cc82016-05-02 14:40:40 -04008145 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008146 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008147
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008148 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008149 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008150
Trond Myklebustb519d402016-09-11 14:50:01 -04008151 switch (status) {
8152 case -NFS4ERR_STALE_CLIENTID:
8153 case -NFS4ERR_DELAY:
8154 case -ETIMEDOUT:
8155 case -EACCES:
8156 case -EAGAIN:
8157 goto out;
8158 };
8159
8160 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008161 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008162 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008163 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008164 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008165 if (status)
8166 goto out;
8167 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008168 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008169out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008170 return status;
8171}
8172
8173/*
8174 * Issues a CREATE_SESSION operation to the server.
8175 * It is the responsibility of the caller to verify the session is
8176 * expired before calling this routine.
8177 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008178int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008179{
8180 int status;
8181 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008182 struct nfs4_session *session = clp->cl_session;
8183
8184 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8185
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008186 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008187 if (status)
8188 goto out;
8189
Andy Adamsonaacd5532011-11-09 13:58:21 -05008190 /* Init or reset the session slot tables */
8191 status = nfs4_setup_session_slot_tables(session);
8192 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008193 if (status)
8194 goto out;
8195
8196 ptr = (unsigned *)&session->sess_id.data[0];
8197 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8198 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008199out:
8200 dprintk("<-- %s\n", __func__);
8201 return status;
8202}
8203
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008204/*
8205 * Issue the over-the-wire RPC DESTROY_SESSION.
8206 * The caller must serialize access to this routine.
8207 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008208int nfs4_proc_destroy_session(struct nfs4_session *session,
8209 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008210{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008211 struct rpc_message msg = {
8212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8213 .rpc_argp = session,
8214 .rpc_cred = cred,
8215 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008216 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008217
8218 dprintk("--> nfs4_proc_destroy_session\n");
8219
8220 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008221 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8222 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008223
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008224 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008225 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008226
8227 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008228 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008229 "Session has been destroyed regardless...\n", status);
8230
8231 dprintk("<-- nfs4_proc_destroy_session\n");
8232 return status;
8233}
8234
Trond Myklebust7b38c362012-05-23 13:23:31 -04008235/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008236 * Renew the cl_session lease.
8237 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008238struct nfs4_sequence_data {
8239 struct nfs_client *clp;
8240 struct nfs4_sequence_args args;
8241 struct nfs4_sequence_res res;
8242};
8243
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008244static void nfs41_sequence_release(void *data)
8245{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008246 struct nfs4_sequence_data *calldata = data;
8247 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008248
Elena Reshetova212bf412017-10-20 12:53:38 +03008249 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008250 nfs4_schedule_state_renewal(clp);
8251 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008252 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008253}
8254
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008255static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8256{
8257 switch(task->tk_status) {
8258 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008259 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8260 return -EAGAIN;
8261 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008262 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008263 }
8264 return 0;
8265}
8266
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008267static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008268{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008269 struct nfs4_sequence_data *calldata = data;
8270 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008271
Trond Myklebust14516c32010-07-31 14:29:06 -04008272 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8273 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008274
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008275 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008276 if (task->tk_status < 0) {
8277 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008278 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008279 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008280
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008281 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8282 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008283 return;
8284 }
8285 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008286 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008287out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008288 dprintk("<-- %s\n", __func__);
8289}
8290
8291static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8292{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008293 struct nfs4_sequence_data *calldata = data;
8294 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008295 struct nfs4_sequence_args *args;
8296 struct nfs4_sequence_res *res;
8297
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008298 args = task->tk_msg.rpc_argp;
8299 res = task->tk_msg.rpc_resp;
8300
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008301 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008302}
8303
8304static const struct rpc_call_ops nfs41_sequence_ops = {
8305 .rpc_call_done = nfs41_sequence_call_done,
8306 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008307 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008308};
8309
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008310static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8311 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008312 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008313 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008314{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008315 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008316 struct rpc_message msg = {
8317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8318 .rpc_cred = cred,
8319 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008320 struct rpc_task_setup task_setup_data = {
8321 .rpc_client = clp->cl_rpcclient,
8322 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008323 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008324 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008325 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008326 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008327
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008328 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008329 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008330 goto out_err;
8331
8332 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008333 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008334 if (calldata == NULL)
8335 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008336 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008337 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008338 if (is_privileged)
8339 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008340 msg.rpc_argp = &calldata->args;
8341 msg.rpc_resp = &calldata->res;
8342 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008343 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008344
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008345 ret = rpc_run_task(&task_setup_data);
8346 if (IS_ERR(ret))
8347 goto out_err;
8348 return ret;
8349out_put_clp:
8350 nfs_put_client(clp);
8351out_err:
8352 nfs41_release_slot(slot);
8353 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008354}
8355
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008356static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008357{
8358 struct rpc_task *task;
8359 int ret = 0;
8360
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008361 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008362 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008363 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008364 if (IS_ERR(task))
8365 ret = PTR_ERR(task);
8366 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008367 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008368 dprintk("<-- %s status=%d\n", __func__, ret);
8369 return ret;
8370}
8371
8372static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8373{
8374 struct rpc_task *task;
8375 int ret;
8376
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008377 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008378 if (IS_ERR(task)) {
8379 ret = PTR_ERR(task);
8380 goto out;
8381 }
8382 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008383 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008384 ret = task->tk_status;
8385 rpc_put_task(task);
8386out:
8387 dprintk("<-- %s status=%d\n", __func__, ret);
8388 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008389}
8390
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008391struct nfs4_reclaim_complete_data {
8392 struct nfs_client *clp;
8393 struct nfs41_reclaim_complete_args arg;
8394 struct nfs41_reclaim_complete_res res;
8395};
8396
8397static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8398{
8399 struct nfs4_reclaim_complete_data *calldata = data;
8400
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008401 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008402 &calldata->arg.seq_args,
8403 &calldata->res.seq_res,
8404 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008405}
8406
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008407static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8408{
8409 switch(task->tk_status) {
8410 case 0:
8411 case -NFS4ERR_COMPLETE_ALREADY:
8412 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8413 break;
8414 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008415 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008416 /* fall through */
8417 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008418 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008419 case -NFS4ERR_BADSESSION:
8420 case -NFS4ERR_DEADSESSION:
8421 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8422 nfs4_schedule_session_recovery(clp->cl_session,
8423 task->tk_status);
8424 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008425 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008426 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008427 }
8428 return 0;
8429}
8430
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008431static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8432{
8433 struct nfs4_reclaim_complete_data *calldata = data;
8434 struct nfs_client *clp = calldata->clp;
8435 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8436
8437 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008438 if (!nfs41_sequence_done(task, res))
8439 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008440
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008441 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008442 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8443 rpc_restart_call_prepare(task);
8444 return;
8445 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008446 dprintk("<-- %s\n", __func__);
8447}
8448
8449static void nfs4_free_reclaim_complete_data(void *data)
8450{
8451 struct nfs4_reclaim_complete_data *calldata = data;
8452
8453 kfree(calldata);
8454}
8455
8456static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8457 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8458 .rpc_call_done = nfs4_reclaim_complete_done,
8459 .rpc_release = nfs4_free_reclaim_complete_data,
8460};
8461
8462/*
8463 * Issue a global reclaim complete.
8464 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008465static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8466 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008467{
8468 struct nfs4_reclaim_complete_data *calldata;
8469 struct rpc_task *task;
8470 struct rpc_message msg = {
8471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008472 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008473 };
8474 struct rpc_task_setup task_setup_data = {
8475 .rpc_client = clp->cl_rpcclient,
8476 .rpc_message = &msg,
8477 .callback_ops = &nfs4_reclaim_complete_call_ops,
8478 .flags = RPC_TASK_ASYNC,
8479 };
8480 int status = -ENOMEM;
8481
8482 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008483 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008484 if (calldata == NULL)
8485 goto out;
8486 calldata->clp = clp;
8487 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008488
Chuck Levera9c92d62013-08-09 12:48:18 -04008489 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008490 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008491 msg.rpc_argp = &calldata->arg;
8492 msg.rpc_resp = &calldata->res;
8493 task_setup_data.callback_data = calldata;
8494 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008495 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008496 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008497 goto out;
8498 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008499 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008500 if (status == 0)
8501 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008502 rpc_put_task(task);
8503out:
8504 dprintk("<-- %s status=%d\n", __func__, status);
8505 return status;
8506}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507
8508static void
8509nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8510{
8511 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008512 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008513
8514 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008515 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008516 &lgp->res.seq_res, task);
8517 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518}
8519
8520static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8521{
8522 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008523
8524 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008525 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008526 dprintk("<-- %s\n", __func__);
8527}
8528
8529static int
8530nfs4_layoutget_handle_exception(struct rpc_task *task,
8531 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8532{
Trond Myklebustee314c22012-10-01 17:25:48 -07008533 struct inode *inode = lgp->args.inode;
8534 struct nfs_server *server = NFS_SERVER(inode);
8535 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008536 int nfs4err = task->tk_status;
8537 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008538 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539
Boaz Harroshed7e5422014-01-22 20:34:54 +02008540 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008541
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008542 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008543 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008544 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008545
8546 /*
8547 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8548 * on the file. set tk_status to -ENODATA to tell upper layer to
8549 * retry go inband.
8550 */
8551 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008552 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008553 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008554 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008555 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8556 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8557 */
8558 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008559 status = -EOVERFLOW;
8560 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008561 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008562 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008563 * (or clients) writing to the same RAID stripe except when
8564 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008565 *
8566 * Treat it like we would RECALLCONFLICT -- we retry for a little
8567 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008568 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008569 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008570 if (lgp->args.minlength == 0) {
8571 status = -EOVERFLOW;
8572 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008573 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008574 status = -EBUSY;
8575 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008576 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008577 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008578 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008579 case -NFS4ERR_DELEG_REVOKED:
8580 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008581 case -NFS4ERR_EXPIRED:
8582 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008583 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008584 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008585 lo = NFS_I(inode)->layout;
8586 /* If the open stateid was bad, then recover it. */
8587 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008588 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008589 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008590 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008591 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008592 break;
8593 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008594
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008595 /*
8596 * Mark the bad layout state as invalid, then retry
8597 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008598 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008599 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008600 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008601 pnfs_free_lseg_list(&head);
8602 status = -EAGAIN;
8603 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008604 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008605
Trond Myklebust8ac09252017-01-23 22:44:12 -05008606 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008607 err = nfs4_handle_exception(server, nfs4err, exception);
8608 if (!status) {
8609 if (exception->retry)
8610 status = -EAGAIN;
8611 else
8612 status = err;
8613 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008614out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008616 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008617}
8618
Idan Kedar85541162012-08-02 11:47:10 +03008619static size_t max_response_pages(struct nfs_server *server)
8620{
8621 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8622 return nfs_page_array_len(0, max_resp_sz);
8623}
8624
8625static void nfs4_free_pages(struct page **pages, size_t size)
8626{
8627 int i;
8628
8629 if (!pages)
8630 return;
8631
8632 for (i = 0; i < size; i++) {
8633 if (!pages[i])
8634 break;
8635 __free_page(pages[i]);
8636 }
8637 kfree(pages);
8638}
8639
8640static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8641{
8642 struct page **pages;
8643 int i;
8644
8645 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8646 if (!pages) {
8647 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8648 return NULL;
8649 }
8650
8651 for (i = 0; i < size; i++) {
8652 pages[i] = alloc_page(gfp_flags);
8653 if (!pages[i]) {
8654 dprintk("%s: failed to allocate page\n", __func__);
8655 nfs4_free_pages(pages, size);
8656 return NULL;
8657 }
8658 }
8659
8660 return pages;
8661}
8662
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663static void nfs4_layoutget_release(void *calldata)
8664{
8665 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008666 struct inode *inode = lgp->args.inode;
8667 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008668 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008669
8670 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008671 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008672 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008673 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008674 put_nfs_open_context(lgp->args.ctx);
8675 kfree(calldata);
8676 dprintk("<-- %s\n", __func__);
8677}
8678
8679static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8680 .rpc_call_prepare = nfs4_layoutget_prepare,
8681 .rpc_call_done = nfs4_layoutget_done,
8682 .rpc_release = nfs4_layoutget_release,
8683};
8684
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008685struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008686nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008687{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008688 struct inode *inode = lgp->args.inode;
8689 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008690 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008691 struct rpc_task *task;
8692 struct rpc_message msg = {
8693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8694 .rpc_argp = &lgp->args,
8695 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008696 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008697 };
8698 struct rpc_task_setup task_setup_data = {
8699 .rpc_client = server->client,
8700 .rpc_message = &msg,
8701 .callback_ops = &nfs4_layoutget_call_ops,
8702 .callback_data = lgp,
8703 .flags = RPC_TASK_ASYNC,
8704 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008705 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008706 struct nfs4_exception exception = {
8707 .inode = inode,
8708 .timeout = *timeout,
8709 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008710 int status = 0;
8711
8712 dprintk("--> %s\n", __func__);
8713
Peng Tao4bd5a982014-11-17 11:05:17 +08008714 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8715 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8716
Idan Kedar85541162012-08-02 11:47:10 +03008717 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8718 if (!lgp->args.layout.pages) {
8719 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008720 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008721 }
8722 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8723
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008724 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008725 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008726 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008727
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008728 task = rpc_run_task(&task_setup_data);
8729 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008730 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008731 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008732 if (status == 0) {
8733 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8734 *timeout = exception.timeout;
8735 }
8736
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008737 trace_nfs4_layoutget(lgp->args.ctx,
8738 &lgp->args.range,
8739 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008740 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008741 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008742
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008743 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8744 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008745 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008746 rpc_put_task(task);
8747 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008748 if (status)
8749 return ERR_PTR(status);
8750 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008751}
8752
Benny Halevycbe82602011-05-22 19:52:37 +03008753static void
8754nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8755{
8756 struct nfs4_layoutreturn *lrp = calldata;
8757
8758 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008759 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008760 &lrp->args.seq_args,
8761 &lrp->res.seq_res,
8762 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008763}
8764
8765static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8766{
8767 struct nfs4_layoutreturn *lrp = calldata;
8768 struct nfs_server *server;
8769
8770 dprintk("--> %s\n", __func__);
8771
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008772 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008773 return;
8774
8775 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008776 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008777 case -NFS4ERR_OLD_STATEID:
8778 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8779 lrp->args.inode))
8780 goto out_restart;
8781 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008782 default:
8783 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008784 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008785 case 0:
8786 break;
8787 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008788 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008789 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008790 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008791 }
Benny Halevycbe82602011-05-22 19:52:37 +03008792 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008793 return;
8794out_restart:
8795 task->tk_status = 0;
8796 nfs4_sequence_free_slot(&lrp->res.seq_res);
8797 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008798}
8799
8800static void nfs4_layoutreturn_release(void *calldata)
8801{
8802 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008803 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008804
8805 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008806 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008807 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008808 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008809 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8810 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008811 pnfs_put_layout_hdr(lrp->args.layout);
8812 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008813 kfree(calldata);
8814 dprintk("<-- %s\n", __func__);
8815}
8816
8817static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8818 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8819 .rpc_call_done = nfs4_layoutreturn_done,
8820 .rpc_release = nfs4_layoutreturn_release,
8821};
8822
Peng Tao6c166052014-11-17 09:30:40 +08008823int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008824{
8825 struct rpc_task *task;
8826 struct rpc_message msg = {
8827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8828 .rpc_argp = &lrp->args,
8829 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008830 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008831 };
8832 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008833 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008834 .rpc_message = &msg,
8835 .callback_ops = &nfs4_layoutreturn_call_ops,
8836 .callback_data = lrp,
8837 };
Peng Tao6c166052014-11-17 09:30:40 +08008838 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008839
Andrew Elble99ade3c2015-12-02 09:39:51 -05008840 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8841 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8842 &task_setup_data.rpc_client, &msg);
8843
Benny Halevycbe82602011-05-22 19:52:37 +03008844 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008845 if (!sync) {
8846 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8847 if (!lrp->inode) {
8848 nfs4_layoutreturn_release(lrp);
8849 return -EAGAIN;
8850 }
8851 task_setup_data.flags |= RPC_TASK_ASYNC;
8852 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008853 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008854 task = rpc_run_task(&task_setup_data);
8855 if (IS_ERR(task))
8856 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008857 if (sync)
8858 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008859 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008860 dprintk("<-- %s status=%d\n", __func__, status);
8861 rpc_put_task(task);
8862 return status;
8863}
8864
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008865static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008866_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8867 struct pnfs_device *pdev,
8868 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008869{
8870 struct nfs4_getdeviceinfo_args args = {
8871 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008872 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8873 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008874 };
8875 struct nfs4_getdeviceinfo_res res = {
8876 .pdev = pdev,
8877 };
8878 struct rpc_message msg = {
8879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8880 .rpc_argp = &args,
8881 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008882 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008883 };
8884 int status;
8885
8886 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008887 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008888 if (res.notification & ~args.notify_types)
8889 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008890 if (res.notification != args.notify_types)
8891 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008892
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008893 dprintk("<-- %s status=%d\n", __func__, status);
8894
8895 return status;
8896}
8897
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008898int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8899 struct pnfs_device *pdev,
8900 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008901{
8902 struct nfs4_exception exception = { };
8903 int err;
8904
8905 do {
8906 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008907 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008908 &exception);
8909 } while (exception.retry);
8910 return err;
8911}
8912EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8913
Andy Adamson863a3c62011-03-23 13:27:54 +00008914static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8915{
8916 struct nfs4_layoutcommit_data *data = calldata;
8917 struct nfs_server *server = NFS_SERVER(data->args.inode);
8918
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008919 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008920 &data->args.seq_args,
8921 &data->res.seq_res,
8922 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008923}
8924
8925static void
8926nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8927{
8928 struct nfs4_layoutcommit_data *data = calldata;
8929 struct nfs_server *server = NFS_SERVER(data->args.inode);
8930
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008931 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008932 return;
8933
8934 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008935 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8936 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8937 case -NFS4ERR_BADLAYOUT: /* no layout */
8938 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008939 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008940 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008941 break;
8942 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008943 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008944 rpc_restart_call_prepare(task);
8945 return;
8946 }
8947 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008948}
8949
8950static void nfs4_layoutcommit_release(void *calldata)
8951{
8952 struct nfs4_layoutcommit_data *data = calldata;
8953
Andy Adamsondb29c082011-07-30 20:52:38 -04008954 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008955 nfs_post_op_update_inode_force_wcc(data->args.inode,
8956 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008957 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008958 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008959 kfree(data);
8960}
8961
8962static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8963 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8964 .rpc_call_done = nfs4_layoutcommit_done,
8965 .rpc_release = nfs4_layoutcommit_release,
8966};
8967
8968int
Andy Adamsonef311532011-03-12 02:58:10 -05008969nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008970{
8971 struct rpc_message msg = {
8972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8973 .rpc_argp = &data->args,
8974 .rpc_resp = &data->res,
8975 .rpc_cred = data->cred,
8976 };
8977 struct rpc_task_setup task_setup_data = {
8978 .task = &data->task,
8979 .rpc_client = NFS_CLIENT(data->args.inode),
8980 .rpc_message = &msg,
8981 .callback_ops = &nfs4_layoutcommit_ops,
8982 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008983 };
8984 struct rpc_task *task;
8985 int status = 0;
8986
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008987 dprintk("NFS: initiating layoutcommit call. sync %d "
8988 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008989 data->args.lastbytewritten,
8990 data->args.inode->i_ino);
8991
Trond Myklebust472e2592015-02-05 16:50:30 -05008992 if (!sync) {
8993 data->inode = nfs_igrab_and_active(data->args.inode);
8994 if (data->inode == NULL) {
8995 nfs4_layoutcommit_release(data);
8996 return -EAGAIN;
8997 }
8998 task_setup_data.flags = RPC_TASK_ASYNC;
8999 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009000 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009001 task = rpc_run_task(&task_setup_data);
9002 if (IS_ERR(task))
9003 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009004 if (sync)
9005 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009006 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009007 dprintk("%s: status %d\n", __func__, status);
9008 rpc_put_task(task);
9009 return status;
9010}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009011
Andy Adamson97431202013-08-08 10:57:56 -04009012/**
9013 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9014 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9015 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009016static int
9017_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009018 struct nfs_fsinfo *info,
9019 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009020{
9021 struct nfs41_secinfo_no_name_args args = {
9022 .style = SECINFO_STYLE_CURRENT_FH,
9023 };
9024 struct nfs4_secinfo_res res = {
9025 .flavors = flavors,
9026 };
9027 struct rpc_message msg = {
9028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9029 .rpc_argp = &args,
9030 .rpc_resp = &res,
9031 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009032 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009033 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009034 int status;
9035
9036 if (use_integrity) {
9037 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009038 cred = nfs4_get_clid_cred(server->nfs_client);
9039 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009040 }
9041
9042 dprintk("--> %s\n", __func__);
9043 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9044 &res.seq_res, 0);
9045 dprintk("<-- %s status=%d\n", __func__, status);
9046
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009047 if (cred)
9048 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009049
9050 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009051}
9052
9053static int
9054nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9055 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9056{
9057 struct nfs4_exception exception = { };
9058 int err;
9059 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009060 /* first try using integrity protection */
9061 err = -NFS4ERR_WRONGSEC;
9062
9063 /* try to use integrity protection with machine cred */
9064 if (_nfs4_is_integrity_protected(server->nfs_client))
9065 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9066 flavors, true);
9067
9068 /*
9069 * if unable to use integrity protection, or SECINFO with
9070 * integrity protection returns NFS4ERR_WRONGSEC (which is
9071 * disallowed by spec, but exists in deployed servers) use
9072 * the current filesystem's rpc_client and the user cred.
9073 */
9074 if (err == -NFS4ERR_WRONGSEC)
9075 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9076 flavors, false);
9077
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009078 switch (err) {
9079 case 0:
9080 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009081 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009082 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009083 default:
9084 err = nfs4_handle_exception(server, err, &exception);
9085 }
9086 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009087out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009088 return err;
9089}
9090
9091static int
9092nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9093 struct nfs_fsinfo *info)
9094{
9095 int err;
9096 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009097 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009098 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009099 struct nfs4_secinfo4 *secinfo;
9100 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009101
9102 page = alloc_page(GFP_KERNEL);
9103 if (!page) {
9104 err = -ENOMEM;
9105 goto out;
9106 }
9107
9108 flavors = page_address(page);
9109 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9110
9111 /*
9112 * Fall back on "guess and check" method if
9113 * the server doesn't support SECINFO_NO_NAME
9114 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009115 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009116 err = nfs4_find_root_sec(server, fhandle, info);
9117 goto out_freepage;
9118 }
9119 if (err)
9120 goto out_freepage;
9121
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009122 for (i = 0; i < flavors->num_flavors; i++) {
9123 secinfo = &flavors->flavors[i];
9124
9125 switch (secinfo->flavor) {
9126 case RPC_AUTH_NULL:
9127 case RPC_AUTH_UNIX:
9128 case RPC_AUTH_GSS:
9129 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9130 &secinfo->flavor_info);
9131 break;
9132 default:
9133 flavor = RPC_AUTH_MAXFLAVOR;
9134 break;
9135 }
9136
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009137 if (!nfs_auth_info_match(&server->auth_info, flavor))
9138 flavor = RPC_AUTH_MAXFLAVOR;
9139
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009140 if (flavor != RPC_AUTH_MAXFLAVOR) {
9141 err = nfs4_lookup_root_sec(server, fhandle,
9142 info, flavor);
9143 if (!err)
9144 break;
9145 }
9146 }
9147
9148 if (flavor == RPC_AUTH_MAXFLAVOR)
9149 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009150
9151out_freepage:
9152 put_page(page);
9153 if (err == -EACCES)
9154 return -EPERM;
9155out:
9156 return err;
9157}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009158
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009159static int _nfs41_test_stateid(struct nfs_server *server,
9160 nfs4_stateid *stateid,
9161 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009162{
9163 int status;
9164 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009165 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009166 };
9167 struct nfs41_test_stateid_res res;
9168 struct rpc_message msg = {
9169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9170 .rpc_argp = &args,
9171 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009172 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009173 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009174 struct rpc_clnt *rpc_client = server->client;
9175
9176 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9177 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009178
Chuck Lever38527b12012-07-11 16:30:23 -04009179 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009180 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009181 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009182 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009183 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009184 if (status != NFS_OK) {
9185 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009186 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009187 }
9188 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009189 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009190}
9191
Trond Myklebust43912bb2016-09-22 13:38:56 -04009192static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9193 int err, struct nfs4_exception *exception)
9194{
9195 exception->retry = 0;
9196 switch(err) {
9197 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009198 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009199 nfs4_handle_exception(server, err, exception);
9200 break;
9201 case -NFS4ERR_BADSESSION:
9202 case -NFS4ERR_BADSLOT:
9203 case -NFS4ERR_BAD_HIGH_SLOT:
9204 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9205 case -NFS4ERR_DEADSESSION:
9206 nfs4_do_handle_exception(server, err, exception);
9207 }
9208}
9209
Chuck Lever38527b12012-07-11 16:30:23 -04009210/**
9211 * nfs41_test_stateid - perform a TEST_STATEID operation
9212 *
9213 * @server: server / transport on which to perform the operation
9214 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009215 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009216 *
9217 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9218 * Otherwise a negative NFS4ERR value is returned if the operation
9219 * failed or the state ID is not currently valid.
9220 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009221static int nfs41_test_stateid(struct nfs_server *server,
9222 nfs4_stateid *stateid,
9223 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009224{
9225 struct nfs4_exception exception = { };
9226 int err;
9227 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009228 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009229 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009230 } while (exception.retry);
9231 return err;
9232}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009233
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009234struct nfs_free_stateid_data {
9235 struct nfs_server *server;
9236 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009237 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009238};
9239
9240static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9241{
9242 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009243 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009244 &data->args.seq_args,
9245 &data->res.seq_res,
9246 task);
9247}
9248
9249static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9250{
9251 struct nfs_free_stateid_data *data = calldata;
9252
9253 nfs41_sequence_done(task, &data->res.seq_res);
9254
9255 switch (task->tk_status) {
9256 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009257 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009258 rpc_restart_call_prepare(task);
9259 }
9260}
9261
9262static void nfs41_free_stateid_release(void *calldata)
9263{
9264 kfree(calldata);
9265}
9266
Trond Myklebust17f26b12013-08-21 15:48:42 -04009267static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009268 .rpc_call_prepare = nfs41_free_stateid_prepare,
9269 .rpc_call_done = nfs41_free_stateid_done,
9270 .rpc_release = nfs41_free_stateid_release,
9271};
9272
9273static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009274 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009275 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009276 bool privileged)
9277{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009278 struct rpc_message msg = {
9279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009280 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009281 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009282 struct rpc_task_setup task_setup = {
9283 .rpc_client = server->client,
9284 .rpc_message = &msg,
9285 .callback_ops = &nfs41_free_stateid_ops,
9286 .flags = RPC_TASK_ASYNC,
9287 };
9288 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009289
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009290 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9291 &task_setup.rpc_client, &msg);
9292
Chuck Lever38527b12012-07-11 16:30:23 -04009293 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009294 data = kmalloc(sizeof(*data), GFP_NOFS);
9295 if (!data)
9296 return ERR_PTR(-ENOMEM);
9297 data->server = server;
9298 nfs4_stateid_copy(&data->args.stateid, stateid);
9299
9300 task_setup.callback_data = data;
9301
9302 msg.rpc_argp = &data->args;
9303 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009304 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009305 if (privileged)
9306 nfs4_set_sequence_privileged(&data->args.seq_args);
9307
9308 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009309}
9310
Chuck Lever38527b12012-07-11 16:30:23 -04009311/**
9312 * nfs41_free_stateid - perform a FREE_STATEID operation
9313 *
9314 * @server: server / transport on which to perform the operation
9315 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009316 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009317 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009318 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009319 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009320 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009321static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009322 const nfs4_stateid *stateid,
9323 struct rpc_cred *cred,
9324 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009325{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009326 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009327
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009328 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009329 if (IS_ERR(task))
9330 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009331 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009332 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009333}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009334
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009335static void
9336nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009337{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009338 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009339
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009340 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009341 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009342}
9343
Trond Myklebust36281ca2012-03-04 18:13:56 -05009344static bool nfs41_match_stateid(const nfs4_stateid *s1,
9345 const nfs4_stateid *s2)
9346{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009347 if (s1->type != s2->type)
9348 return false;
9349
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009350 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009351 return false;
9352
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009353 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009354 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009355
Anna Schumaker045c5512017-01-11 16:59:48 -05009356 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009357}
9358
Andy Adamson557134a2009-04-01 09:21:53 -04009359#endif /* CONFIG_NFS_V4_1 */
9360
Trond Myklebust36281ca2012-03-04 18:13:56 -05009361static bool nfs4_match_stateid(const nfs4_stateid *s1,
9362 const nfs4_stateid *s2)
9363{
Trond Myklebustf597c532012-03-04 18:13:56 -05009364 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009365}
9366
9367
Trond Myklebust17280172012-03-11 13:11:00 -04009368static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009369 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009370 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371 .recover_open = nfs4_open_reclaim,
9372 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009373 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009374 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375};
9376
Andy Adamson591d71c2009-04-01 09:22:47 -04009377#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009378static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009379 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9380 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9381 .recover_open = nfs4_open_reclaim,
9382 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009383 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009384 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009385 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009386};
9387#endif /* CONFIG_NFS_V4_1 */
9388
Trond Myklebust17280172012-03-11 13:11:00 -04009389static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009390 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009391 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009392 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009394 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395};
9396
Andy Adamson591d71c2009-04-01 09:22:47 -04009397#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009398static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009399 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9400 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009401 .recover_open = nfs41_open_expired,
9402 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009403 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009404};
9405#endif /* CONFIG_NFS_V4_1 */
9406
Trond Myklebust17280172012-03-11 13:11:00 -04009407static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009408 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009409 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009410 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009411};
9412
9413#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009414static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009415 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009416 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009417 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009418};
9419#endif
9420
Chuck Leverec011fe2013-10-17 14:12:39 -04009421static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009422 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009423 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009424};
9425
9426#if defined(CONFIG_NFS_V4_1)
9427static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009428 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009429 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009430};
9431#endif /* CONFIG_NFS_V4_1 */
9432
Trond Myklebust97dc1352010-06-16 09:52:26 -04009433static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9434 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009435 .init_caps = NFS_CAP_READDIRPLUS
9436 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009437 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009438 .init_client = nfs40_init_client,
9439 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009440 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009441 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009442 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009443 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009444 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009445 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009446 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9447 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9448 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009449 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009450};
9451
9452#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009453static struct nfs_seqid *
9454nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9455{
9456 return NULL;
9457}
9458
Trond Myklebust97dc1352010-06-16 09:52:26 -04009459static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9460 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009461 .init_caps = NFS_CAP_READDIRPLUS
9462 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009463 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009464 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009465 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009466 .init_client = nfs41_init_client,
9467 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009468 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009469 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009470 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009471 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009472 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009473 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009474 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009475 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9476 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9477 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009478 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009479};
9480#endif
9481
Steve Dickson42c2c422013-05-22 12:50:38 -04009482#if defined(CONFIG_NFS_V4_2)
9483static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9484 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009485 .init_caps = NFS_CAP_READDIRPLUS
9486 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009487 | NFS_CAP_POSIX_LOCK
9488 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009489 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009490 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009491 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009492 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009493 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009494 | NFS_CAP_LAYOUTSTATS
9495 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009496 .init_client = nfs41_init_client,
9497 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009498 .match_stateid = nfs41_match_stateid,
9499 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009500 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009501 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009502 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009503 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009504 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009505 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9506 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9507 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009508 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009509};
9510#endif
9511
Trond Myklebust97dc1352010-06-16 09:52:26 -04009512const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9513 [0] = &nfs_v4_0_minor_ops,
9514#if defined(CONFIG_NFS_V4_1)
9515 [1] = &nfs_v4_1_minor_ops,
9516#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009517#if defined(CONFIG_NFS_V4_2)
9518 [2] = &nfs_v4_2_minor_ops,
9519#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009520};
9521
Trond Myklebust13997822016-07-24 17:10:52 -04009522static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009523{
9524 ssize_t error, error2;
9525
9526 error = generic_listxattr(dentry, list, size);
9527 if (error < 0)
9528 return error;
9529 if (list) {
9530 list += error;
9531 size -= error;
9532 }
9533
9534 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9535 if (error2 < 0)
9536 return error2;
9537 return error + error2;
9538}
9539
Trond Myklebust17f26b12013-08-21 15:48:42 -04009540static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009541 .create = nfs_create,
9542 .lookup = nfs_lookup,
9543 .atomic_open = nfs_atomic_open,
9544 .link = nfs_link,
9545 .unlink = nfs_unlink,
9546 .symlink = nfs_symlink,
9547 .mkdir = nfs_mkdir,
9548 .rmdir = nfs_rmdir,
9549 .mknod = nfs_mknod,
9550 .rename = nfs_rename,
9551 .permission = nfs_permission,
9552 .getattr = nfs_getattr,
9553 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009554 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009555};
9556
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009557static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009558 .permission = nfs_permission,
9559 .getattr = nfs_getattr,
9560 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009561 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009562};
9563
David Howells509de812006-08-22 20:06:11 -04009564const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 .version = 4, /* protocol version */
9566 .dentry_ops = &nfs4_dentry_operations,
9567 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009568 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009569 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009571 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009572 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 .getattr = nfs4_proc_getattr,
9574 .setattr = nfs4_proc_setattr,
9575 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009576 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577 .access = nfs4_proc_access,
9578 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579 .create = nfs4_proc_create,
9580 .remove = nfs4_proc_remove,
9581 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009582 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009584 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009585 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009586 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 .link = nfs4_proc_link,
9588 .symlink = nfs4_proc_symlink,
9589 .mkdir = nfs4_proc_mkdir,
9590 .rmdir = nfs4_proc_remove,
9591 .readdir = nfs4_proc_readdir,
9592 .mknod = nfs4_proc_mknod,
9593 .statfs = nfs4_proc_statfs,
9594 .fsinfo = nfs4_proc_fsinfo,
9595 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009596 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009598 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009600 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009602 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009603 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009604 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009605 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009606 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009607 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009608 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009609 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009610 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009611 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009612 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009613 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009614 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009615 .create_server = nfs4_create_server,
9616 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009617};
9618
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009619static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009620 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009621 .list = nfs4_xattr_list_nfs4_acl,
9622 .get = nfs4_xattr_get_nfs4_acl,
9623 .set = nfs4_xattr_set_nfs4_acl,
9624};
9625
9626const struct xattr_handler *nfs4_xattr_handlers[] = {
9627 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009628#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9629 &nfs4_xattr_nfs4_label_handler,
9630#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009631 NULL
9632};
9633
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634/*
9635 * Local variables:
9636 * c-basic-offset: 8
9637 * End:
9638 */