blob: 1090016a238fd962d0ce86fb66b98ae447f27013 [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] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400261 FATTR4_WORD0_TYPE
262 | FATTR4_WORD0_CHANGE
263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
267 FATTR4_WORD1_MODE
268 | FATTR4_WORD1_NUMLINKS
269 | FATTR4_WORD1_OWNER
270 | FATTR4_WORD1_OWNER_GROUP
271 | FATTR4_WORD1_RAWDEV
272 | FATTR4_WORD1_SPACE_USED
273 | FATTR4_WORD1_TIME_ACCESS
274 | FATTR4_WORD1_TIME_METADATA
275 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400276 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400277};
278
Al Virobc4785c2006-10-19 23:28:51 -0700279static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct nfs4_readdir_arg *readdir)
281{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400282 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700283 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000286 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
288 return;
289 }
290
291 readdir->cookie = 0;
292 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
293 if (cookie == 2)
294 return;
295
296 /*
297 * NFSv4 servers do not return entries for '.' and '..'
298 * Therefore, we fake these entries here. We let '.'
299 * have cookie 0 and '..' have cookie 1. Note that
300 * when talking to the server, we always send cookie 0
301 * instead of 1 or 2.
302 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800303 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (cookie == 0) {
306 *p++ = xdr_one; /* next */
307 *p++ = xdr_zero; /* cookie, first word */
308 *p++ = xdr_one; /* cookie, second word */
309 *p++ = xdr_one; /* entry len */
310 memcpy(p, ".\0\0\0", 4); /* entry */
311 p++;
312 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400313 *p++ = htonl(attrs); /* bitmap */
314 *p++ = htonl(12); /* attribute buffer length */
315 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000316 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 *p++ = xdr_one; /* next */
320 *p++ = xdr_zero; /* cookie, first word */
321 *p++ = xdr_two; /* cookie, second word */
322 *p++ = xdr_two; /* entry len */
323 memcpy(p, "..\0\0", 4); /* entry */
324 p++;
325 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400326 *p++ = htonl(attrs); /* bitmap */
327 *p++ = htonl(12); /* attribute buffer length */
328 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000329 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 readdir->pgbase = (char *)p - (char *)start;
332 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800333 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Trond Myklebust26d36302016-09-22 13:39:05 -0400336static void nfs4_test_and_free_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
341
342 ops->test_and_free_expired(server, stateid, cred);
343}
344
345static void __nfs4_free_revoked_stateid(struct nfs_server *server,
346 nfs4_stateid *stateid,
347 struct rpc_cred *cred)
348{
349 stateid->type = NFS4_REVOKED_STATEID_TYPE;
350 nfs4_test_and_free_stateid(server, stateid, cred);
351}
352
353static void nfs4_free_revoked_stateid(struct nfs_server *server,
354 const nfs4_stateid *stateid,
355 struct rpc_cred *cred)
356{
357 nfs4_stateid tmp;
358
359 nfs4_stateid_copy(&tmp, stateid);
360 __nfs4_free_revoked_stateid(server, &tmp, cred);
361}
362
NeilBrown8478eaa2014-09-18 16:09:27 +1000363static long nfs4_update_delay(long *timeout)
364{
365 long ret;
366 if (!timeout)
367 return NFS4_POLL_RETRY_MAX;
368 if (*timeout <= 0)
369 *timeout = NFS4_POLL_RETRY_MIN;
370 if (*timeout > NFS4_POLL_RETRY_MAX)
371 *timeout = NFS4_POLL_RETRY_MAX;
372 ret = *timeout;
373 *timeout <<= 1;
374 return ret;
375}
376
Trond Myklebust65de8722008-12-23 15:21:44 -0500377static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
378{
379 int res = 0;
380
381 might_sleep();
382
NeilBrown8478eaa2014-09-18 16:09:27 +1000383 freezable_schedule_timeout_killable_unsafe(
384 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 if (fatal_signal_pending(current))
386 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500387 return res;
388}
389
390/* This is the error handling routine for processes that are allowed
391 * to sleep.
392 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393static int nfs4_do_handle_exception(struct nfs_server *server,
394 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500395{
396 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500397 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400398 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500399 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 int ret = errorcode;
401
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400402 exception->delay = 0;
403 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400405
406 if (stateid == NULL && state != NULL)
407 stateid = &state->stateid;
408
Trond Myklebust65de8722008-12-23 15:21:44 -0500409 switch(errorcode) {
410 case 0:
411 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400412 case -NFS4ERR_DELEG_REVOKED:
413 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400414 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400415 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400416 if (inode != NULL && stateid != NULL) {
417 nfs_inode_find_state_and_recover(inode,
418 stateid);
419 goto wait_on_recovery;
420 }
421 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400422 if (inode) {
423 int err;
424
425 err = nfs_async_inode_return_delegation(inode,
426 stateid);
427 if (err == 0)
428 goto wait_on_recovery;
429 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
430 exception->retry = 1;
431 break;
432 }
433 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 if (state == NULL)
435 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400436 ret = nfs4_schedule_stateid_recovery(server, state);
437 if (ret < 0)
438 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500439 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500440 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500442 nfs4_schedule_lease_recovery(clp);
443 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400444 case -NFS4ERR_MOVED:
445 ret = nfs4_schedule_migration_recovery(server);
446 if (ret < 0)
447 break;
448 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400449 case -NFS4ERR_LEASE_MOVED:
450 nfs4_schedule_lease_moved_recovery(clp);
451 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500452#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400453 case -NFS4ERR_BADSESSION:
454 case -NFS4ERR_BADSLOT:
455 case -NFS4ERR_BAD_HIGH_SLOT:
456 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
457 case -NFS4ERR_DEADSESSION:
458 case -NFS4ERR_SEQ_FALSE_RETRY:
459 case -NFS4ERR_SEQ_MISORDERED:
460 dprintk("%s ERROR: %d Reset session\n", __func__,
461 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400462 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400463 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500464#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500465 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500466 if (exception->timeout > HZ) {
467 /* We have retried a decent amount, time to
468 * fail
469 */
470 ret = -EBUSY;
471 break;
472 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400474 nfs_inc_server_stats(server, NFSIOS_DELAY);
475 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400476 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400477 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400478 exception->delay = 1;
479 return 0;
480
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400481 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500482 case -NFS4ERR_OLD_STATEID:
483 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800484 break;
485 case -NFS4ERR_BADOWNER:
486 /* The following works around a Linux server bug! */
487 case -NFS4ERR_BADNAME:
488 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
489 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
490 exception->retry = 1;
491 printk(KERN_WARNING "NFS: v4 server %s "
492 "does not accept raw "
493 "uid/gids. "
494 "Reenabling the idmapper.\n",
495 server->nfs_client->cl_hostname);
496 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500497 }
498 /* We failed to handle the error */
499 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500500wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400501 exception->recovering = 1;
502 return 0;
503}
504
505/* This is the error handling routine for processes that are allowed
506 * to sleep.
507 */
508int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
509{
510 struct nfs_client *clp = server->nfs_client;
511 int ret;
512
513 ret = nfs4_do_handle_exception(server, errorcode, exception);
514 if (exception->delay) {
515 ret = nfs4_delay(server->client, &exception->timeout);
516 goto out_retry;
517 }
518 if (exception->recovering) {
519 ret = nfs4_wait_clnt_recover(clp);
520 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
521 return -EIO;
522 goto out_retry;
523 }
524 return ret;
525out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500526 if (ret == 0)
527 exception->retry = 1;
528 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500529}
530
Trond Myklebust037fc982015-09-20 15:51:00 -0400531static int
532nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
533 int errorcode, struct nfs4_exception *exception)
534{
535 struct nfs_client *clp = server->nfs_client;
536 int ret;
537
538 ret = nfs4_do_handle_exception(server, errorcode, exception);
539 if (exception->delay) {
540 rpc_delay(task, nfs4_update_delay(&exception->timeout));
541 goto out_retry;
542 }
543 if (exception->recovering) {
544 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
545 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
546 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
547 goto out_retry;
548 }
549 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
550 ret = -EIO;
551 return ret;
552out_retry:
553 if (ret == 0)
554 exception->retry = 1;
555 return ret;
556}
557
558static int
559nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
560 struct nfs4_state *state, long *timeout)
561{
562 struct nfs4_exception exception = {
563 .state = state,
564 };
565
566 if (task->tk_status >= 0)
567 return 0;
568 if (timeout)
569 exception.timeout = *timeout;
570 task->tk_status = nfs4_async_handle_exception(task, server,
571 task->tk_status,
572 &exception);
573 if (exception.delay && timeout)
574 *timeout = exception.timeout;
575 if (exception.retry)
576 return -EAGAIN;
577 return 0;
578}
579
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400580/*
581 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
582 * or 'false' otherwise.
583 */
584static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
585{
586 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500587 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400627static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400628{
629 struct nfs4_slot *slot = res->sr_slot;
630 struct nfs4_slot_table *tbl;
631
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 tbl = slot->table;
633 spin_lock(&tbl->slot_tbl_lock);
634 if (!nfs41_wake_and_assign_slot(tbl, slot))
635 nfs4_free_slot(tbl, slot);
636 spin_unlock(&tbl->slot_tbl_lock);
637
638 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400639}
640
641static int nfs40_sequence_done(struct rpc_task *task,
642 struct nfs4_sequence_res *res)
643{
644 if (res->sr_slot != NULL)
645 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400646 return 1;
647}
648
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400649#if defined(CONFIG_NFS_V4_1)
650
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400651static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400652{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500653 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400654 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500655 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400656
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400657 if (!slot)
658 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500659 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500660 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500661
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400662 /* Bump the slot sequence number */
663 if (slot->seq_done)
664 slot->seq_nr++;
665 slot->seq_done = 0;
666
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500667 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500668 /* Be nice to the server: try to ensure that the last transmitted
669 * value for highest_user_slotid <= target_highest_slotid
670 */
671 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
672 send_new_highest_used_slotid = true;
673
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500674 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500675 send_new_highest_used_slotid = false;
676 goto out_unlock;
677 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500678 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500679
680 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
681 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500682out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500683 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500684 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400685 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400686 if (waitqueue_active(&tbl->slot_waitq))
687 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400688}
689
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400690static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
691{
692 nfs41_release_slot(res->sr_slot);
693 res->sr_slot = NULL;
694}
695
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400696static int nfs41_sequence_process(struct rpc_task *task,
697 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400698{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500699 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500702 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500703 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400704
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 if (slot == NULL)
706 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400707 /* don't increment the sequence number if the task wasn't sent */
708 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400709 goto out;
710
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500711 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500712
Trond Myklebustac20d162012-12-15 15:36:07 -0500713 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400714 if (res->sr_status != -NFS4ERR_DELAY)
715 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500716 interrupted = true;
717 }
718
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400719 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500720 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 switch (res->sr_status) {
722 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400723 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400724 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500725 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500726 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500727 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400728 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
729 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500730 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400731 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500732 case 1:
733 /*
734 * sr_status remains 1 if an RPC level error occurred.
735 * The server may or may not have processed the sequence
736 * operation..
737 * Mark the slot as having hosted an interrupted RPC call.
738 */
739 slot->interrupted = 1;
740 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400741 case -NFS4ERR_DELAY:
742 /* The server detected a resend of the RPC call and
743 * returned NFS4ERR_DELAY as per Section 2.10.6.2
744 * of RFC5661.
745 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500746 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400747 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500748 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500749 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400750 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500751 case -NFS4ERR_BADSLOT:
752 /*
753 * The slot id we used was probably retired. Try again
754 * using a different slot id.
755 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400756 if (slot->seq_nr < slot->table->target_highest_slotid)
757 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500758 goto retry_nowait;
759 case -NFS4ERR_SEQ_MISORDERED:
760 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500761 * Was the last operation on this sequence interrupted?
762 * If so, retry after bumping the sequence number.
763 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400764 if (interrupted)
765 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500766 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500767 * Could this slot have been previously retired?
768 * If so, then the server may be expecting seq_nr = 1!
769 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500770 if (slot->seq_nr != 1) {
771 slot->seq_nr = 1;
772 goto retry_nowait;
773 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400774 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500775 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400776 if (interrupted)
777 goto retry_new_seq;
778 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400779 default:
780 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400781 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400782 }
783out:
784 /* The session may be reset by one of the error handlers. */
785 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500786out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500787 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400788session_recover:
789 nfs4_schedule_session_recovery(session, res->sr_status);
790 goto retry_nowait;
791retry_new_seq:
792 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500793retry_nowait:
794 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400795 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500796 task->tk_status = 0;
797 ret = 0;
798 }
799 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400800out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400801 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 goto out;
803 rpc_delay(task, NFS4_POLL_RETRY_MAX);
804 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400806
807int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
808{
809 if (!nfs41_sequence_process(task, res))
810 return 0;
811 if (res->sr_slot != NULL)
812 nfs41_sequence_free_slot(res);
813 return 1;
814
815}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500816EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400817
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400818static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
819{
820 if (res->sr_slot == NULL)
821 return 1;
822 if (res->sr_slot->table->session != NULL)
823 return nfs41_sequence_process(task, res);
824 return nfs40_sequence_done(task, res);
825}
826
827static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
828{
829 if (res->sr_slot != NULL) {
830 if (res->sr_slot->table->session != NULL)
831 nfs41_sequence_free_slot(res);
832 else
833 nfs40_sequence_free_slot(res);
834 }
835}
836
Peng Tao2c4b1312014-06-11 05:24:16 +0800837int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400838{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500839 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400841 if (!res->sr_slot->table->session)
842 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400843 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844}
Peng Tao2c4b1312014-06-11 05:24:16 +0800845EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400846
Andy Adamsonce5039c2009-04-01 09:22:13 -0400847static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
848{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400849 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400850
Trond Myklebust035168ab2010-06-16 09:52:26 -0400851 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
852
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500853 nfs4_setup_sequence(data->seq_server->nfs_client,
854 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855}
856
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
858{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400859 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400860
Trond Myklebust14516c32010-07-31 14:29:06 -0400861 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400862}
863
Trond Myklebust17280172012-03-11 13:11:00 -0400864static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400866 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400867};
868
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400869static void
870nfs4_sequence_process_interrupted(struct nfs_client *client,
871 struct nfs4_slot *slot, struct rpc_cred *cred)
872{
873 struct rpc_task *task;
874
875 task = _nfs41_proc_sequence(client, cred, slot, true);
876 if (!IS_ERR(task))
877 rpc_put_task_async(task);
878}
879
Chuck Lever3bd23842013-08-09 12:49:19 -0400880#else /* !CONFIG_NFS_V4_1 */
881
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400882static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
883{
884 return nfs40_sequence_done(task, res);
885}
886
887static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
888{
889 if (res->sr_slot != NULL)
890 nfs40_sequence_free_slot(res);
891}
892
Peng Tao2c4b1312014-06-11 05:24:16 +0800893int nfs4_sequence_done(struct rpc_task *task,
894 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400895{
Chuck Lever3bd23842013-08-09 12:49:19 -0400896 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400897}
Peng Tao2c4b1312014-06-11 05:24:16 +0800898EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400899
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400900static void
901nfs4_sequence_process_interrupted(struct nfs_client *client,
902 struct nfs4_slot *slot, struct rpc_cred *cred)
903{
904 WARN_ON_ONCE(1);
905 slot->interrupted = 0;
906}
907
Chuck Lever3bd23842013-08-09 12:49:19 -0400908#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400909
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400910static
911void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 struct nfs4_slot *slot)
914{
915 if (!slot)
916 return;
917 slot->privileged = args->sa_privileged ? 1 : 0;
918 args->sa_slot = slot;
919
920 res->sr_slot = slot;
921 res->sr_timestamp = jiffies;
922 res->sr_status_flags = 0;
923 res->sr_status = 1;
924
925}
926
927int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500928 struct nfs4_sequence_args *args,
929 struct nfs4_sequence_res *res,
930 struct rpc_task *task)
931{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500932 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500933 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500934 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500935
Anna Schumaker9dd91072017-01-10 12:01:46 -0500936 /* slot already allocated? */
937 if (res->sr_slot != NULL)
938 goto out_start;
939
Anna Schumaker76ee0352017-01-10 16:49:31 -0500940 if (session) {
941 tbl = &session->fc_slot_table;
942 task->tk_timeout = 0;
943 }
944
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400945 for (;;) {
946 spin_lock(&tbl->slot_tbl_lock);
947 /* The state manager will wait until the slot table is empty */
948 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
949 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500950
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400951 slot = nfs4_alloc_slot(tbl);
952 if (IS_ERR(slot)) {
953 /* Try again in 1/4 second */
954 if (slot == ERR_PTR(-ENOMEM))
955 task->tk_timeout = HZ >> 2;
956 goto out_sleep;
957 }
958 spin_unlock(&tbl->slot_tbl_lock);
959
960 if (likely(!slot->interrupted))
961 break;
962 nfs4_sequence_process_interrupted(client,
963 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500964 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500965
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400966 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500967
Anna Schumakerad05cc02017-01-11 13:37:06 -0500968 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500969out_start:
970 rpc_call_start(task);
971 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500972
973out_sleep:
974 if (args->sa_privileged)
975 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
976 NULL, RPC_PRIORITY_PRIVILEGED);
977 else
978 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
979 spin_unlock(&tbl->slot_tbl_lock);
980 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500981}
982EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
983
Chuck Lever9915ea72013-08-09 12:48:27 -0400984static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
985{
986 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500987 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400988 data->seq_args, data->seq_res, task);
989}
990
991static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
992{
993 struct nfs4_call_sync_data *data = calldata;
994 nfs4_sequence_done(task, data->seq_res);
995}
996
997static const struct rpc_call_ops nfs40_call_sync_ops = {
998 .rpc_call_prepare = nfs40_call_sync_prepare,
999 .rpc_call_done = nfs40_call_sync_done,
1000};
1001
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001002static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001003 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 struct nfs4_sequence_args *args,
1006 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001007{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 int ret;
1009 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001010 struct nfs_client *clp = server->nfs_client;
1011 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012 .seq_server = server,
1013 .seq_args = args,
1014 .seq_res = res,
1015 };
1016 struct rpc_task_setup task_setup = {
1017 .rpc_client = clnt,
1018 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001019 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001020 .callback_data = &data
1021 };
1022
1023 task = rpc_run_task(&task_setup);
1024 if (IS_ERR(task))
1025 ret = PTR_ERR(task);
1026 else {
1027 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001028 rpc_put_task(task);
1029 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 return ret;
1031}
1032
Bryan Schumaker7c513052011-03-24 17:12:24 +00001033int nfs4_call_sync(struct rpc_clnt *clnt,
1034 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001035 struct rpc_message *msg,
1036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 int cache_reply)
1039{
Chuck Levera9c92d62013-08-09 12:48:18 -04001040 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001041 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001044static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1045 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct nfs_inode *nfsi = NFS_I(dir);
1048
1049 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001051 if (cinfo->atomic && cinfo->before == dir->i_version) {
1052 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1053 nfsi->attrtimeo_timestamp = jiffies;
1054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001056 if (cinfo->before != dir->i_version)
1057 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1058 NFS_INO_INVALID_ACL;
1059 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001060 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001061 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001062 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001063 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock(&dir->i_lock);
1065}
1066
1067struct nfs4_opendata {
1068 struct kref kref;
1069 struct nfs_openargs o_arg;
1070 struct nfs_openres o_res;
1071 struct nfs_open_confirmargs c_arg;
1072 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001073 struct nfs4_string owner_name;
1074 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001075 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001077 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001079 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct nfs4_state_owner *owner;
1081 struct nfs4_state *state;
1082 struct iattr attrs;
1083 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001084 bool rpc_done;
1085 bool file_created;
1086 bool is_recover;
1087 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001088 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001090
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001091struct nfs4_open_createattrs {
1092 struct nfs4_label *label;
1093 struct iattr *sattr;
1094 const __u32 verf[2];
1095};
1096
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001097static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1098 int err, struct nfs4_exception *exception)
1099{
1100 if (err != -EINVAL)
1101 return false;
1102 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1103 return false;
1104 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1105 exception->retry = 1;
1106 return true;
1107}
1108
Trond Myklebust6ae37332015-01-30 14:21:14 -05001109static u32
1110nfs4_map_atomic_open_share(struct nfs_server *server,
1111 fmode_t fmode, int openflags)
1112{
1113 u32 res = 0;
1114
1115 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1116 case FMODE_READ:
1117 res = NFS4_SHARE_ACCESS_READ;
1118 break;
1119 case FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_WRITE;
1121 break;
1122 case FMODE_READ|FMODE_WRITE:
1123 res = NFS4_SHARE_ACCESS_BOTH;
1124 }
1125 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1126 goto out;
1127 /* Want no delegation if we're using O_DIRECT */
1128 if (openflags & O_DIRECT)
1129 res |= NFS4_SHARE_WANT_NO_DELEG;
1130out:
1131 return res;
1132}
1133
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001134static enum open_claim_type4
1135nfs4_map_atomic_open_claim(struct nfs_server *server,
1136 enum open_claim_type4 claim)
1137{
1138 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1139 return claim;
1140 switch (claim) {
1141 default:
1142 return claim;
1143 case NFS4_OPEN_CLAIM_FH:
1144 return NFS4_OPEN_CLAIM_NULL;
1145 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1147 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1148 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1149 }
1150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153{
1154 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001155 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 p->o_res.seqid = p->o_arg.seqid;
1157 p->c_res.seqid = p->c_arg.seqid;
1158 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001159 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001161 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162}
1163
Al Viro82a2c1b2011-06-22 18:30:55 -04001164static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001166 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001167 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001168 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169{
Al Viro82a2c1b2011-06-22 18:30:55 -04001170 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001171 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001173 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001174 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 struct nfs4_opendata *p;
1176
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001177 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 if (p == NULL)
1179 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001180
1181 p->f_label = nfs4_label_alloc(server, gfp_mask);
1182 if (IS_ERR(p->f_label))
1183 goto err_free_p;
1184
Kinglong Meea49c2692015-07-27 15:31:38 +08001185 p->a_label = nfs4_label_alloc(server, gfp_mask);
1186 if (IS_ERR(p->a_label))
1187 goto err_free_f;
1188
Trond Myklebust63f5f792015-01-23 19:19:25 -05001189 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1190 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001191 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001192 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001193 nfs_sb_active(dentry->d_sb);
1194 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 p->dir = parent;
1196 p->owner = sp;
1197 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001198 p->o_arg.open_flags = flags;
1199 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001200 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001201 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001202 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1203 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001204 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1205 * will return permission denied for all bits until close */
1206 if (!(flags & O_EXCL)) {
1207 /* ask server to check for all possible rights as results
1208 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001209 switch (p->o_arg.claim) {
1210 default:
1211 break;
1212 case NFS4_OPEN_CLAIM_NULL:
1213 case NFS4_OPEN_CLAIM_FH:
1214 p->o_arg.access = NFS4_ACCESS_READ |
1215 NFS4_ACCESS_MODIFY |
1216 NFS4_ACCESS_EXTEND |
1217 NFS4_ACCESS_EXECUTE;
1218 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 }
David Howells7539bba2006-08-22 20:06:09 -04001220 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001221 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1222 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001225 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001226 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001228 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001229 case NFS4_OPEN_CLAIM_NULL:
1230 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1231 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1232 p->o_arg.fh = NFS_FH(dir);
1233 break;
1234 case NFS4_OPEN_CLAIM_PREVIOUS:
1235 case NFS4_OPEN_CLAIM_FH:
1236 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1237 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001238 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001239 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001241 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001242 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001243
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001244 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001245 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247 p->c_arg.fh = &p->o_res.fh;
1248 p->c_arg.stateid = &p->o_res.stateid;
1249 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001252 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001253
1254err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001255 nfs4_label_free(p->a_label);
1256err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001257 nfs4_label_free(p->f_label);
1258err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 kfree(p);
1260err:
1261 dput(parent);
1262 return NULL;
1263}
1264
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267 struct nfs4_opendata *p = container_of(kref,
1268 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001269 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270
1271 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001272 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 if (p->state != NULL)
1274 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001276
Kinglong Meea49c2692015-07-27 15:31:38 +08001277 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001278 nfs4_label_free(p->f_label);
1279
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 dput(p->dentry);
1282 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001283 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001284 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285 kfree(p);
1286}
1287
1288static void nfs4_opendata_put(struct nfs4_opendata *p)
1289{
1290 if (p != NULL)
1291 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292}
1293
Trond Myklebust24311f82015-09-20 10:50:17 -04001294static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1295 fmode_t fmode)
1296{
1297 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1298 case FMODE_READ|FMODE_WRITE:
1299 return state->n_rdwr != 0;
1300 case FMODE_WRITE:
1301 return state->n_wronly != 0;
1302 case FMODE_READ:
1303 return state->n_rdonly != 0;
1304 }
1305 WARN_ON_ONCE(1);
1306 return false;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310{
1311 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312
Trond Myklebust536e43d2012-01-17 22:04:26 -05001313 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 goto out;
1315 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001317 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1318 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319 break;
1320 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001321 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1322 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001323 break;
1324 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001325 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1326 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001328out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001329 return ret;
1330}
1331
Trond Myklebust2a606182015-08-19 22:30:00 -05001332static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1333 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001335 if (delegation == NULL)
1336 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001339 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1340 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001341 switch (claim) {
1342 case NFS4_OPEN_CLAIM_NULL:
1343 case NFS4_OPEN_CLAIM_FH:
1344 break;
1345 case NFS4_OPEN_CLAIM_PREVIOUS:
1346 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1347 break;
1348 default:
1349 return 0;
1350 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001351 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 return 1;
1353}
1354
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001356{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001358 case FMODE_WRITE:
1359 state->n_wronly++;
1360 break;
1361 case FMODE_READ:
1362 state->n_rdonly++;
1363 break;
1364 case FMODE_READ|FMODE_WRITE:
1365 state->n_rdwr++;
1366 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001368}
1369
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001370#ifdef CONFIG_NFS_V4_1
1371static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1372{
1373 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1376 return true;
1377 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1378 return true;
1379 return false;
1380}
1381#endif /* CONFIG_NFS_V4_1 */
1382
Trond Myklebustc9399f22017-11-06 15:28:01 -05001383static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1384{
1385 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1386 wake_up_all(&state->waitq);
1387}
1388
1389static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1390 const nfs4_stateid *stateid)
1391{
1392 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1393 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1394
1395 if (stateid_seqid == state_seqid + 1U ||
1396 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1397 nfs_state_log_update_open_stateid(state);
1398 else
1399 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1400}
1401
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001403{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001404 struct nfs_client *clp = state->owner->so_server->nfs_client;
1405 bool need_recover = false;
1406
1407 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1410 need_recover = true;
1411 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1412 need_recover = true;
1413 if (need_recover)
1414 nfs4_state_mark_reclaim_nograce(clp, state);
1415}
1416
Trond Myklebustc9399f22017-11-06 15:28:01 -05001417/*
1418 * Check for whether or not the caller may update the open stateid
1419 * to the value passed in by stateid.
1420 *
1421 * Note: This function relies heavily on the server implementing
1422 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1423 * correctly.
1424 * i.e. The stateid seqids have to be initialised to 1, and
1425 * are then incremented on every state transition.
1426 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001427static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001429{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001430 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1431 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1432 if (stateid->seqid == cpu_to_be32(1))
1433 nfs_state_log_update_open_stateid(state);
1434 else
1435 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001438
1439 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1440 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001442 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001443 return false;
1444}
1445
Trond Myklebustf95549c2015-01-23 18:06:09 -05001446static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1447{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001448 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1449 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001450 if (state->n_wronly)
1451 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 if (state->n_rdonly)
1453 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 if (state->n_rdwr)
1455 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001456 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001457}
1458
Trond Myklebust226056c2014-02-11 10:41:07 -05001459static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1460 nfs4_stateid *stateid, fmode_t fmode)
1461{
1462 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1463 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1464 case FMODE_WRITE:
1465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 break;
1467 case FMODE_READ:
1468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 break;
1470 case 0:
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1472 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1473 clear_bit(NFS_OPEN_STATE, &state->flags);
1474 }
1475 if (stateid == NULL)
1476 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001477 /* Handle OPEN+OPEN_DOWNGRADE races */
1478 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1479 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001481 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001482 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001484 nfs4_stateid_copy(&state->stateid, stateid);
1485 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001486 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001487out:
1488 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001489}
1490
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001491static void nfs_clear_open_stateid(struct nfs4_state *state,
1492 nfs4_stateid *arg_stateid,
1493 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001494{
1495 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001496 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1497 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1498 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001500 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1501 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001502}
1503
Trond Myklebust1393d962016-09-22 13:39:13 -04001504static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001505 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001506{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001507 DEFINE_WAIT(wait);
1508 int status = 0;
1509 for (;;) {
1510
1511 if (!nfs_need_update_open_stateid(state, stateid))
1512 return;
1513 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001514 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001515 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001516 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001517 /* Rely on seqids for serialisation with NFSv4.0 */
1518 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1519 break;
1520
1521 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1522 /*
1523 * Ensure we process the state changes in the same order
1524 * in which the server processed them by delaying the
1525 * update of the stateid until we are in sequence.
1526 */
1527 write_sequnlock(&state->seqlock);
1528 spin_unlock(&state->owner->so_lock);
1529 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001530 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531 if (!signal_pending(current)) {
1532 if (schedule_timeout(5*HZ) == 0)
1533 status = -EAGAIN;
1534 else
1535 status = 0;
1536 } else
1537 status = -EINTR;
1538 finish_wait(&state->waitq, &wait);
1539 rcu_read_lock();
1540 spin_lock(&state->owner->so_lock);
1541 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001542 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543
1544 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1545 nfs4_stateid_copy(freeme, &state->open_stateid);
1546 nfs_test_and_clear_all_open_stateid(state);
1547 }
1548
Trond Myklebuste999e802014-02-10 18:20:47 -05001549 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1550 nfs4_stateid_copy(&state->stateid, stateid);
1551 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001552 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001553 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001554}
1555
Trond Myklebustc9399f22017-11-06 15:28:01 -05001556static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001557 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001558 fmode_t fmode,
1559 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001561 /*
1562 * Protect the call to nfs4_state_set_mode_locked and
1563 * serialise the stateid update
1564 */
1565 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001566 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1567 switch (fmode) {
1568 case FMODE_READ:
1569 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1570 break;
1571 case FMODE_WRITE:
1572 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1573 break;
1574 case FMODE_READ|FMODE_WRITE:
1575 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001576 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001577 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001578 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001579}
1580
1581static void nfs_state_set_delegation(struct nfs4_state *state,
1582 const nfs4_stateid *deleg_stateid,
1583 fmode_t fmode)
1584{
1585 /*
1586 * Protect the call to nfs4_state_set_mode_locked and
1587 * serialise the stateid update
1588 */
1589 write_seqlock(&state->seqlock);
1590 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1591 set_bit(NFS_DELEGATED_STATE, &state->flags);
1592 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Trond Myklebust1393d962016-09-22 13:39:13 -04001595static int update_open_stateid(struct nfs4_state *state,
1596 const nfs4_stateid *open_stateid,
1597 const nfs4_stateid *delegation,
1598 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001599{
Trond Myklebust1393d962016-09-22 13:39:13 -04001600 struct nfs_server *server = NFS_SERVER(state->inode);
1601 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001602 struct nfs_inode *nfsi = NFS_I(state->inode);
1603 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001604 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001605 int ret = 0;
1606
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001607 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001608
1609 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001610 spin_lock(&state->owner->so_lock);
1611 if (open_stateid != NULL) {
1612 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1613 ret = 1;
1614 }
1615
Trond Myklebust34310432008-12-23 15:21:38 -05001616 deleg_cur = rcu_dereference(nfsi->delegation);
1617 if (deleg_cur == NULL)
1618 goto no_delegation;
1619
1620 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001621 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001622 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001624 goto no_delegation_unlock;
1625
1626 if (delegation == NULL)
1627 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001628 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001629 goto no_delegation_unlock;
1630
Trond Myklebustb7391f42008-12-23 15:21:52 -05001631 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001632 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001633 ret = 1;
1634no_delegation_unlock:
1635 spin_unlock(&deleg_cur->lock);
1636no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001637 if (ret)
1638 update_open_stateflags(state, fmode);
1639 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001640 rcu_read_unlock();
1641
Trond Myklebust4f14c192014-02-12 19:15:06 -05001642 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001643 nfs4_schedule_state_manager(clp);
1644 if (freeme.type != 0)
1645 nfs4_test_and_free_stateid(server, &freeme,
1646 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001647
1648 return ret;
1649}
1650
Trond Myklebust39071e62015-01-24 15:07:56 -05001651static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1652 const nfs4_stateid *stateid)
1653{
1654 struct nfs4_state *state = lsp->ls_state;
1655 bool ret = false;
1656
1657 spin_lock(&state->state_lock);
1658 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1659 goto out_noupdate;
1660 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1661 goto out_noupdate;
1662 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1663 ret = true;
1664out_noupdate:
1665 spin_unlock(&state->state_lock);
1666 return ret;
1667}
Trond Myklebust34310432008-12-23 15:21:38 -05001668
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001669static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001670{
1671 struct nfs_delegation *delegation;
1672
1673 rcu_read_lock();
1674 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 rcu_read_unlock();
1677 return;
1678 }
1679 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001680 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681}
1682
Trond Myklebust6ee41262007-07-08 14:11:36 -04001683static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684{
1685 struct nfs4_state *state = opendata->state;
1686 struct nfs_inode *nfsi = NFS_I(state->inode);
1687 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001688 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001690 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691 nfs4_stateid stateid;
1692 int ret = -EAGAIN;
1693
Trond Myklebustaac00a82007-07-05 19:02:21 -04001694 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001700 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001701 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001702 rcu_read_lock();
1703 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001704 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001705 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001706 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001707 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001709 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001711 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001712 if (!opendata->is_recover) {
1713 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1714 if (ret != 0)
1715 goto out;
1716 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001718
1719 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001721 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723out:
1724 return ERR_PTR(ret);
1725out_return_state:
1726 atomic_inc(&state->count);
1727 return state;
1728}
1729
Andy Adamsone23008e2012-10-02 21:07:32 -04001730static void
1731nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1732{
1733 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1734 struct nfs_delegation *delegation;
1735 int delegation_flags = 0;
1736
1737 rcu_read_lock();
1738 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1739 if (delegation)
1740 delegation_flags = delegation->flags;
1741 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001742 switch (data->o_arg.claim) {
1743 default:
1744 break;
1745 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1746 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001747 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1748 "returning a delegation for "
1749 "OPEN(CLAIM_DELEGATE_CUR)\n",
1750 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001751 return;
1752 }
1753 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001754 nfs_inode_set_delegation(state->inode,
1755 data->owner->so_cred,
1756 &data->o_res);
1757 else
1758 nfs_inode_reclaim_delegation(state->inode,
1759 data->owner->so_cred,
1760 &data->o_res);
1761}
1762
1763/*
1764 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1765 * and update the nfs4_state.
1766 */
1767static struct nfs4_state *
1768_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1769{
1770 struct inode *inode = data->state->inode;
1771 struct nfs4_state *state = data->state;
1772 int ret;
1773
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001774 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001775 if (data->rpc_status)
1776 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001777 /* cached opens have already been processed */
1778 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 }
1780
Andy Adamsone23008e2012-10-02 21:07:32 -04001781 ret = nfs_refresh_inode(inode, &data->f_attr);
1782 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001783 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001784
1785 if (data->o_res.delegation_type != 0)
1786 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001787update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 update_open_stateid(state, &data->o_res.stateid, NULL,
1789 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001790 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001791
1792 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001793}
1794
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001795static struct inode *
1796nfs4_opendata_get_inode(struct nfs4_opendata *data)
1797{
1798 struct inode *inode;
1799
1800 switch (data->o_arg.claim) {
1801 case NFS4_OPEN_CLAIM_NULL:
1802 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1803 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1804 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1805 return ERR_PTR(-EAGAIN);
1806 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1807 &data->f_attr, data->f_label);
1808 break;
1809 default:
1810 inode = d_inode(data->dentry);
1811 ihold(inode);
1812 nfs_refresh_inode(inode, &data->f_attr);
1813 }
1814 return inode;
1815}
1816
Andy Adamsone23008e2012-10-02 21:07:32 -04001817static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001818nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1819{
1820 struct nfs4_state *state;
1821 struct inode *inode;
1822
1823 inode = nfs4_opendata_get_inode(data);
1824 if (IS_ERR(inode))
1825 return ERR_CAST(inode);
1826 if (data->state != NULL && data->state->inode == inode) {
1827 state = data->state;
1828 atomic_inc(&state->count);
1829 } else
1830 state = nfs4_get_open_state(inode, data->owner);
1831 iput(inode);
1832 if (state == NULL)
1833 state = ERR_PTR(-ENOMEM);
1834 return state;
1835}
1836
Andy Adamsone23008e2012-10-02 21:07:32 -04001837static struct nfs4_state *
1838_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001840 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001843 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001844 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 goto out;
1846 }
1847
Trond Myklebust75e8c482017-08-08 10:38:07 -04001848 state = nfs4_opendata_find_nfs4_state(data);
1849 if (IS_ERR(state))
1850 goto out;
1851
Andy Adamsone23008e2012-10-02 21:07:32 -04001852 if (data->o_res.delegation_type != 0)
1853 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001854 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001855 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001857 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001858 return state;
1859}
1860
Andy Adamsone23008e2012-10-02 21:07:32 -04001861static struct nfs4_state *
1862nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1863{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001864 struct nfs4_state *ret;
1865
Andy Adamsone23008e2012-10-02 21:07:32 -04001866 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001867 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1868 else
1869 ret = _nfs4_opendata_to_nfs4_state(data);
1870 nfs4_sequence_free_slot(&data->o_res.seq_res);
1871 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001872}
1873
Trond Myklebust864472e2006-01-03 09:55:15 +01001874static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1875{
1876 struct nfs_inode *nfsi = NFS_I(state->inode);
1877 struct nfs_open_context *ctx;
1878
1879 spin_lock(&state->inode->i_lock);
1880 list_for_each_entry(ctx, &nfsi->open_files, list) {
1881 if (ctx->state != state)
1882 continue;
1883 get_nfs_open_context(ctx);
1884 spin_unlock(&state->inode->i_lock);
1885 return ctx;
1886 }
1887 spin_unlock(&state->inode->i_lock);
1888 return ERR_PTR(-ENOENT);
1889}
1890
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001891static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1892 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001893{
1894 struct nfs4_opendata *opendata;
1895
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001896 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001897 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001898 if (opendata == NULL)
1899 return ERR_PTR(-ENOMEM);
1900 opendata->state = state;
1901 atomic_inc(&state->count);
1902 return opendata;
1903}
1904
Trond Myklebust24311f82015-09-20 10:50:17 -04001905static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1906 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001907{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001908 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001909 int ret;
1910
Trond Myklebust24311f82015-09-20 10:50:17 -04001911 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001912 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001913 opendata->o_arg.open_flags = 0;
1914 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001915 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1916 NFS_SB(opendata->dentry->d_sb),
1917 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001918 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1919 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1920 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001921 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 if (ret != 0)
1923 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001924 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001925 if (IS_ERR(newstate))
1926 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001927 if (newstate != opendata->state)
1928 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001929 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001930 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001931}
1932
1933static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1934{
Trond Myklebust864472e2006-01-03 09:55:15 +01001935 int ret;
1936
Trond Myklebust4f14c192014-02-12 19:15:06 -05001937 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1938 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1939 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001941 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001943 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001945 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1946 if (ret != 0)
1947 return ret;
1948 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1949 if (ret != 0)
1950 return ret;
1951 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1952 if (ret != 0)
1953 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001954 /*
1955 * We may have performed cached opens for all three recoveries.
1956 * Check if we need to update the current stateid.
1957 */
1958 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001959 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001960 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001961 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001962 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001963 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001964 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001965 return 0;
1966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * OPEN_RECLAIM:
1970 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001972static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001974 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001975 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001976 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int status;
1978
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001979 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1980 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001981 if (IS_ERR(opendata))
1982 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001983 rcu_read_lock();
1984 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001985 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001986 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001987 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001988 opendata->o_arg.u.delegation_type = delegation_type;
1989 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001990 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return status;
1992}
1993
Trond Myklebust539cd032007-06-05 11:46:42 -04001994static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 struct nfs_server *server = NFS_SERVER(state->inode);
1997 struct nfs4_exception exception = { };
1998 int err;
1999 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002000 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002001 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002002 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2003 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002004 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002005 break;
2006 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } while (exception.retry);
2008 return err;
2009}
2010
Trond Myklebust864472e2006-01-03 09:55:15 +01002011static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012{
2013 struct nfs_open_context *ctx;
2014 int ret;
2015
2016 ctx = nfs4_state_find_open_context(state);
2017 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002018 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002019 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002020 put_nfs_open_context(ctx);
2021 return ret;
2022}
2023
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002024static 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 -07002025{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002026 switch (err) {
2027 default:
2028 printk(KERN_ERR "NFS: %s: unhandled error "
2029 "%d.\n", __func__, err);
2030 case 0:
2031 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002032 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002033 case -ESTALE:
2034 break;
2035 case -NFS4ERR_BADSESSION:
2036 case -NFS4ERR_BADSLOT:
2037 case -NFS4ERR_BAD_HIGH_SLOT:
2038 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2039 case -NFS4ERR_DEADSESSION:
2040 set_bit(NFS_DELEGATED_STATE, &state->flags);
2041 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2042 return -EAGAIN;
2043 case -NFS4ERR_STALE_CLIENTID:
2044 case -NFS4ERR_STALE_STATEID:
2045 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002046 /* Don't recall a delegation if it was lost */
2047 nfs4_schedule_lease_recovery(server->nfs_client);
2048 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002049 case -NFS4ERR_MOVED:
2050 nfs4_schedule_migration_recovery(server);
2051 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002052 case -NFS4ERR_LEASE_MOVED:
2053 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2054 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002055 case -NFS4ERR_DELEG_REVOKED:
2056 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002057 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002059 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002060 nfs_inode_find_state_and_recover(state->inode,
2061 stateid);
2062 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002063 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002064 case -NFS4ERR_DELAY:
2065 case -NFS4ERR_GRACE:
2066 set_bit(NFS_DELEGATED_STATE, &state->flags);
2067 ssleep(1);
2068 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002069 case -ENOMEM:
2070 case -NFS4ERR_DENIED:
2071 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2072 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return err;
2075}
2076
Trond Myklebust24311f82015-09-20 10:50:17 -04002077int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2078 struct nfs4_state *state, const nfs4_stateid *stateid,
2079 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002080{
2081 struct nfs_server *server = NFS_SERVER(state->inode);
2082 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002083 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002084
2085 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2086 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2087 if (IS_ERR(opendata))
2088 return PTR_ERR(opendata);
2089 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002090 write_seqlock(&state->seqlock);
2091 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2092 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002093 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2094 switch (type & (FMODE_READ|FMODE_WRITE)) {
2095 case FMODE_READ|FMODE_WRITE:
2096 case FMODE_WRITE:
2097 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2098 if (err)
2099 break;
2100 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2101 if (err)
2102 break;
2103 case FMODE_READ:
2104 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2105 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002106 nfs4_opendata_put(opendata);
2107 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2108}
2109
Chuck Leverbe05c862013-08-09 12:49:47 -04002110static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2111{
2112 struct nfs4_opendata *data = calldata;
2113
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002114 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2115 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002116}
2117
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002118static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2119{
2120 struct nfs4_opendata *data = calldata;
2121
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002122 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002123
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002124 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002126 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002127 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002129 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002130 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002131}
2132
2133static void nfs4_open_confirm_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002139 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002143 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002144 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002145 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002146 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002148 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002149}
2150
2151static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002152 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002153 .rpc_call_done = nfs4_open_confirm_done,
2154 .rpc_release = nfs4_open_confirm_release,
2155};
2156
2157/*
2158 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2159 */
2160static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002163 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002164 struct rpc_message msg = {
2165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2166 .rpc_argp = &data->c_arg,
2167 .rpc_resp = &data->c_res,
2168 .rpc_cred = data->owner->so_cred,
2169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002170 struct rpc_task_setup task_setup_data = {
2171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 .callback_ops = &nfs4_open_confirm_ops,
2174 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002175 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .flags = RPC_TASK_ASYNC,
2177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 int status;
2179
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002180 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002181 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002182 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002183 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002184 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002185 if (data->is_recover)
2186 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002187 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002188 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002189 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002190 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002191 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002192 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002193 smp_wmb();
2194 } else
2195 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002196 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return status;
2198}
2199
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 struct nfs4_opendata *data = calldata;
2203 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002204 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002205 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002206
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002208 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002209 /*
2210 * Check if we still need to send an OPEN call, or if we can use
2211 * a delegation instead.
2212 */
2213 if (data->state != NULL) {
2214 struct nfs_delegation *delegation;
2215
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002216 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002217 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002218 rcu_read_lock();
2219 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002220 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002221 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002222 rcu_read_unlock();
2223 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002224 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002225 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002226 switch (claim) {
2227 default:
2228 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002229 case NFS4_OPEN_CLAIM_PREVIOUS:
2230 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2231 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002232 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002233 case NFS4_OPEN_CLAIM_FH:
2234 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002235 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002236 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002237 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002238 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002239 &data->o_res.seq_res,
2240 task) != 0)
2241 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002242
2243 /* Set the create mode (note dependency on the session type) */
2244 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2245 if (data->o_arg.open_flags & O_EXCL) {
2246 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2247 if (nfs4_has_persistent_session(clp))
2248 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2249 else if (clp->cl_mvops->minor_version > 0)
2250 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2251 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002252 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002253unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002254 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002255 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002256out_no_action:
2257 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002258out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002259 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002260}
2261
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002262static void nfs4_open_done(struct rpc_task *task, void *calldata)
2263{
2264 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002267
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002268 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002269 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002270
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002271 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002272 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2273 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002274 case S_IFREG:
2275 break;
2276 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002278 break;
2279 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002280 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002281 break;
2282 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002284 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002285 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002286 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002287 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2288 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002289 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002290 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002292
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293static void nfs4_open_release(void *calldata)
2294{
2295 struct nfs4_opendata *data = calldata;
2296 struct nfs4_state *state = NULL;
2297
2298 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002299 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002300 goto out_free;
2301 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002302 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303 goto out_free;
2304 /* In case we need an open_confirm, no cleanup! */
2305 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2306 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002308 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002309 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002311 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002312}
2313
2314static const struct rpc_call_ops nfs4_open_ops = {
2315 .rpc_call_prepare = nfs4_open_prepare,
2316 .rpc_call_done = nfs4_open_done,
2317 .rpc_release = nfs4_open_release,
2318};
2319
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002320static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321{
David Howells2b0143b2015-03-17 22:25:59 +00002322 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002323 struct nfs_server *server = NFS_SERVER(dir);
2324 struct nfs_openargs *o_arg = &data->o_arg;
2325 struct nfs_openres *o_res = &data->o_res;
2326 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2329 .rpc_argp = o_arg,
2330 .rpc_resp = o_res,
2331 .rpc_cred = data->owner->so_cred,
2332 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002333 struct rpc_task_setup task_setup_data = {
2334 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002335 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002336 .callback_ops = &nfs4_open_ops,
2337 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002338 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002339 .flags = RPC_TASK_ASYNC,
2340 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002341 int status;
2342
Chuck Levera9c92d62013-08-09 12:48:18 -04002343 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002344 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002345 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002346 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002347 data->cancelled = false;
2348 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002349 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002350 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002351 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002352 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002353 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002354 if (IS_ERR(task))
2355 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002356 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002358 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002359 smp_wmb();
2360 } else
2361 status = data->rpc_status;
2362 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002363
2364 return status;
2365}
2366
2367static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2368{
David Howells2b0143b2015-03-17 22:25:59 +00002369 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002371 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002372
2373 status = nfs4_run_open_task(data, 1);
2374 if (status != 0 || !data->rpc_done)
2375 return status;
2376
Trond Myklebust6926afd2012-01-07 13:22:46 -05002377 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2378
Anna Schumakerd7e98252017-01-11 16:13:29 -05002379 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002380 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002381
2382 return status;
2383}
2384
Trond Myklebustf3792d62014-07-10 08:54:32 -04002385/*
2386 * Additional permission checks in order to distinguish between an
2387 * open for read, and an open for execute. This works around the
2388 * fact that NFSv4 OPEN treats read and execute permissions as being
2389 * the same.
2390 * Note that in the non-execute case, we want to turn off permission
2391 * checking if we just created a new file (POSIX open() semantics).
2392 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002393static int nfs4_opendata_access(struct rpc_cred *cred,
2394 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002395 struct nfs4_state *state, fmode_t fmode,
2396 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002397{
2398 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002399 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002400
2401 /* access call failed or for some reason the server doesn't
2402 * support any access modes -- defer access call until later */
2403 if (opendata->o_res.access_supported == 0)
2404 return 0;
2405
2406 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002407 /*
2408 * Use openflags to check for exec, because fmode won't
2409 * always have FMODE_EXEC set when file open for exec.
2410 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002411 if (openflags & __FMODE_EXEC) {
2412 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002413 if (S_ISDIR(state->inode->i_mode))
2414 mask = NFS4_ACCESS_LOOKUP;
2415 else
2416 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002417 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002418 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002419
2420 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002421 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2422 nfs_access_add_cache(state->inode, &cache);
2423
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002424 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2425 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002426 return 0;
2427
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002428 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002429}
2430
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002431/*
2432 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2433 */
2434static int _nfs4_proc_open(struct nfs4_opendata *data)
2435{
David Howells2b0143b2015-03-17 22:25:59 +00002436 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002437 struct nfs_server *server = NFS_SERVER(dir);
2438 struct nfs_openargs *o_arg = &data->o_arg;
2439 struct nfs_openres *o_res = &data->o_res;
2440 int status;
2441
2442 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002443 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002444 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002445 if (status != 0) {
2446 if (status == -NFS4ERR_BADNAME &&
2447 !(o_arg->open_flags & O_CREAT))
2448 return -ENOENT;
2449 return status;
2450 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002451
Trond Myklebust6926afd2012-01-07 13:22:46 -05002452 nfs_fattr_map_and_free_names(server, &data->f_attr);
2453
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002454 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002455 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002456 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002457 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002458 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002459 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002460 update_changeattr(dir, &o_res->cinfo,
2461 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002462 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002463 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2464 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002466 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002468 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002470 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2471 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002472 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002473 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477/*
2478 * OPEN_EXPIRED:
2479 * reclaim state on the server after a network partition.
2480 * Assumes caller holds the appropriate lock
2481 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002482static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002484 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002485 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002487 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002488 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002489 if (IS_ERR(opendata))
2490 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002491 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002492 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002493 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002494 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002498static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002499{
Trond Myklebust539cd032007-06-05 11:46:42 -04002500 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002501 struct nfs4_exception exception = { };
2502 int err;
2503
2504 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002505 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002506 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002507 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2508 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002509 switch (err) {
2510 default:
2511 goto out;
2512 case -NFS4ERR_GRACE:
2513 case -NFS4ERR_DELAY:
2514 nfs4_handle_exception(server, err, &exception);
2515 err = 0;
2516 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002517 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002518out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002519 return err;
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Trond Myklebust864472e2006-01-03 09:55:15 +01002527 ctx = nfs4_state_find_open_context(state);
2528 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002529 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002530 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002531 put_nfs_open_context(ctx);
2532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Trond Myklebust41020b62016-09-22 13:38:58 -04002535static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2536 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002537{
Trond Myklebust41020b62016-09-22 13:38:58 -04002538 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002539 write_seqlock(&state->seqlock);
2540 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2541 write_sequnlock(&state->seqlock);
2542 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2543}
2544
2545static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2546{
2547 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002548 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002549}
2550
2551static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2552{
2553 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2554 nfs40_clear_delegation_stateid(state);
2555 return nfs4_open_expired(sp, state);
2556}
2557
Trond Myklebust45870d62016-09-22 13:38:59 -04002558static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2559 nfs4_stateid *stateid,
2560 struct rpc_cred *cred)
2561{
2562 return -NFS4ERR_BAD_STATEID;
2563}
2564
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002565#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002566static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2567 nfs4_stateid *stateid,
2568 struct rpc_cred *cred)
2569{
2570 int status;
2571
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002572 switch (stateid->type) {
2573 default:
2574 break;
2575 case NFS4_INVALID_STATEID_TYPE:
2576 case NFS4_SPECIAL_STATEID_TYPE:
2577 return -NFS4ERR_BAD_STATEID;
2578 case NFS4_REVOKED_STATEID_TYPE:
2579 goto out_free;
2580 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002581
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002582 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002583 switch (status) {
2584 case -NFS4ERR_EXPIRED:
2585 case -NFS4ERR_ADMIN_REVOKED:
2586 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002587 break;
2588 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002589 return status;
2590 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002591out_free:
2592 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002593 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002594 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002595}
2596
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002597static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002598{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002599 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002600 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002601 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002602 struct rpc_cred *cred;
2603 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002604
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002605 /* Get the delegation credential for use by test/free_stateid */
2606 rcu_read_lock();
2607 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002608 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002609 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002610 return;
2611 }
2612
2613 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002614 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2615 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2616 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002617 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002618 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002619 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002621
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002622 cred = get_rpccred(delegation->cred);
2623 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002624 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002626 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002627 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002628
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002629 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002630}
2631
2632/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002633 * nfs41_check_expired_locks - possibly free a lock stateid
2634 *
2635 * @state: NFSv4 state for an inode
2636 *
2637 * Returns NFS_OK if recovery for this stateid is now finished.
2638 * Otherwise a negative NFS4ERR value is returned.
2639 */
2640static int nfs41_check_expired_locks(struct nfs4_state *state)
2641{
2642 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002643 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002644 struct nfs_server *server = NFS_SERVER(state->inode);
2645
2646 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2647 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002648
2649 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002650 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2651 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2652 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2653
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002654 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002655 spin_unlock(&state->state_lock);
2656
2657 nfs4_put_lock_state(prev);
2658 prev = lsp;
2659
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002660 status = nfs41_test_and_free_expired_stateid(server,
2661 &lsp->ls_stateid,
2662 cred);
2663 trace_nfs4_test_lock_stateid(state, lsp, status);
2664 if (status == -NFS4ERR_EXPIRED ||
2665 status == -NFS4ERR_BAD_STATEID) {
2666 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002667 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002668 if (!recover_lost_locks)
2669 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2670 } else if (status != NFS_OK) {
2671 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002672 nfs4_put_lock_state(prev);
2673 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002675 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002676 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002677 }
2678 spin_unlock(&state->state_lock);
2679 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002680out:
2681 return ret;
2682}
2683
2684/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002685 * nfs41_check_open_stateid - possibly free an open stateid
2686 *
2687 * @state: NFSv4 state for an inode
2688 *
2689 * Returns NFS_OK if recovery for this stateid is now finished.
2690 * Otherwise a negative NFS4ERR value is returned.
2691 */
2692static int nfs41_check_open_stateid(struct nfs4_state *state)
2693{
2694 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002695 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002696 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002697 int status;
2698
Trond Myklebustb134fc42016-09-22 13:39:16 -04002699 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002700 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2701 if (nfs4_have_delegation(state->inode, state->state))
2702 return NFS_OK;
2703 return -NFS4ERR_OPENMODE;
2704 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002705 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002706 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002707 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002708 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002709 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002710 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2711 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2712 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002713 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002714 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002715 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002716 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002717 if (nfs_open_stateid_recover_openmode(state))
2718 return -NFS4ERR_OPENMODE;
2719 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002720}
2721
2722static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2723{
Chuck Levereb64cf92012-07-11 16:30:05 -04002724 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002725
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002726 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002727 status = nfs41_check_expired_locks(state);
2728 if (status != NFS_OK)
2729 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002730 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002731 if (status != NFS_OK)
2732 status = nfs4_open_expired(sp, state);
2733 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002734}
2735#endif
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002738 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2739 * fields corresponding to attributes that were used to store the verifier.
2740 * Make sure we clobber those fields in the later setattr call
2741 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002742static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2743 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002744{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002745 const u32 *attrset = opendata->o_res.attrset;
2746
2747 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002748 !(sattr->ia_valid & ATTR_ATIME_SET))
2749 sattr->ia_valid |= ATTR_ATIME;
2750
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002751 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002752 !(sattr->ia_valid & ATTR_MTIME_SET))
2753 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002754
2755 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002756 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002757 (attrset[1] & FATTR4_WORD1_MODE ||
2758 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002759 sattr->ia_valid &= ~ATTR_MODE;
2760
2761 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2762 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002763}
2764
Trond Myklebustc21443c2013-02-07 14:26:21 -05002765static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2766 fmode_t fmode,
2767 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002768 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002769{
2770 struct nfs4_state_owner *sp = opendata->owner;
2771 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002772 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002773 struct nfs4_state *state;
2774 unsigned int seq;
2775 int ret;
2776
2777 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2778
2779 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002780 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002781 goto out;
2782
2783 state = nfs4_opendata_to_nfs4_state(opendata);
2784 ret = PTR_ERR(state);
2785 if (IS_ERR(state))
2786 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002787 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002788 if (server->caps & NFS_CAP_POSIX_LOCK)
2789 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002790 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2791 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002792
Trond Myklebust275bb302013-05-29 13:11:28 -04002793 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002794 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002795 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002796 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002797 alias = d_exact_alias(dentry, state->inode);
2798 if (!alias)
2799 alias = d_splice_alias(igrab(state->inode), dentry);
2800 /* d_splice_alias() can't fail here - it's a non-directory */
2801 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002802 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002803 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002804 }
2805 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002806 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002807 }
2808
Trond Myklebustc21443c2013-02-07 14:26:21 -05002809 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2810 if (ret != 0)
2811 goto out;
2812
David Howells2b0143b2015-03-17 22:25:59 +00002813 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002814 nfs_inode_attach_open_context(ctx);
2815 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2816 nfs4_schedule_stateid_recovery(server, state);
2817 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002818out:
2819 return ret;
2820}
2821
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002822/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002823 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 */
Andy Adamson82be4172012-05-23 05:02:35 -04002825static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002826 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002827 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002828 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002829 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 struct nfs4_state_owner *sp;
2832 struct nfs4_state *state = NULL;
2833 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002834 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002835 struct dentry *dentry = ctx->dentry;
2836 struct rpc_cred *cred = ctx->cred;
2837 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2838 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002839 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002840 struct iattr *sattr = c->sattr;
2841 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002842 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002843 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002847 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2848 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2850 goto out_err;
2851 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002852 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002853 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002854 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002855 if (d_really_is_positive(dentry))
2856 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002857 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002858 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002859 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002860 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2861 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002862 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002863 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
David Quigley14c43f72013-05-22 12:50:43 -04002865 if (label) {
2866 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2867 if (IS_ERR(olabel)) {
2868 status = PTR_ERR(olabel);
2869 goto err_opendata_put;
2870 }
2871 }
2872
Trond Myklebuste911b812014-03-26 13:24:37 -07002873 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2874 if (!opendata->f_attr.mdsthreshold) {
2875 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2876 if (!opendata->f_attr.mdsthreshold)
2877 goto err_free_label;
2878 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002879 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002880 }
David Howells2b0143b2015-03-17 22:25:59 +00002881 if (d_really_is_positive(dentry))
2882 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002883
Trond Myklebust3efb9722013-05-29 13:17:04 -04002884 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002885 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002886 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002887 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002888
NeilBrownefcbc042015-07-30 13:00:56 +10002889 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002890 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002891 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002892 /*
2893 * send create attributes which was not set by open
2894 * with an extra setattr.
2895 */
2896 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2897 nfs_fattr_init(opendata->o_res.f_attr);
2898 status = nfs4_do_setattr(state->inode, cred,
2899 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002900 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002901 if (status == 0) {
2902 nfs_setattr_update_inode(state->inode, sattr,
2903 opendata->o_res.f_attr);
2904 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2905 }
David Quigley1775fd32013-05-22 12:50:42 -04002906 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002907 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002908 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002909 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002910
Trond Myklebuste911b812014-03-26 13:24:37 -07002911 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002912 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002913 opendata->f_attr.mdsthreshold = NULL;
2914 }
Andy Adamson82be4172012-05-23 05:02:35 -04002915
David Quigley14c43f72013-05-22 12:50:43 -04002916 nfs4_label_free(olabel);
2917
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002918 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002921err_free_label:
2922 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002923err_opendata_put:
2924 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002925err_put_state_owner:
2926 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return status;
2929}
2930
2931
Andy Adamson82be4172012-05-23 05:02:35 -04002932static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002933 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002934 int flags,
2935 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002936 struct nfs4_label *label,
2937 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002939 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 struct nfs4_exception exception = { };
2941 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002942 struct nfs4_open_createattrs c = {
2943 .label = label,
2944 .sattr = sattr,
2945 .verf = {
2946 [0] = (__u32)jiffies,
2947 [1] = (__u32)current->pid,
2948 },
2949 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 int status;
2951
2952 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002953 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002954 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002955 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (status == 0)
2957 break;
2958 /* NOTE: BAD_SEQID means the server and client disagree about the
2959 * book-keeping w.r.t. state-changing operations
2960 * (OPEN/CLOSE/LOCK/LOCKU...)
2961 * It is actually a sign of a bug on the client or on the server.
2962 *
2963 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * have unhashed the old state_owner for us, and that we can
2966 * therefore safely retry using a new one. We should still warn
2967 * the user though...
2968 */
2969 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002970 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002971 " returned a bad sequence-id error!\n",
2972 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 exception.retry = 1;
2974 continue;
2975 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002976 /*
2977 * BAD_STATEID on OPEN means that the server cancelled our
2978 * state before it received the OPEN_CONFIRM.
2979 * Recover by retrying the request as per the discussion
2980 * on Page 181 of RFC3530.
2981 */
2982 if (status == -NFS4ERR_BAD_STATEID) {
2983 exception.retry = 1;
2984 continue;
2985 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002986 if (status == -EAGAIN) {
2987 /* We must have found a delegation */
2988 exception.retry = 1;
2989 continue;
2990 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002991 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2992 continue;
2993 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 status, &exception));
2995 } while (exception.retry);
2996 return res;
2997}
2998
Trond Myklebust8487c472016-06-26 08:44:35 -04002999static int _nfs4_do_setattr(struct inode *inode,
3000 struct nfs_setattrargs *arg,
3001 struct nfs_setattrres *res,
3002 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003003 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003004{
3005 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003006 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3008 .rpc_argp = arg,
3009 .rpc_resp = res,
3010 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003011 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003012 struct rpc_cred *delegation_cred = NULL;
3013 unsigned long timestamp = jiffies;
3014 fmode_t fmode;
3015 bool truncate;
3016 int status;
3017
3018 nfs_fattr_init(res->fattr);
3019
3020 /* Servers should only apply open mode checks for file size changes */
3021 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3022 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3023
3024 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3025 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003026 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003027 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003028 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003029 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003030 l_ctx = nfs_get_lock_context(ctx);
3031 if (IS_ERR(l_ctx))
3032 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003033 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3034 &arg->stateid, &delegation_cred);
3035 nfs_put_lock_context(l_ctx);
3036 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003037 return -EBADF;
3038 } else
3039 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3040 if (delegation_cred)
3041 msg.rpc_cred = delegation_cred;
3042
3043 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3044
3045 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003046 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003047 renew_lease(server, timestamp);
3048 trace_nfs4_setattr(inode, &arg->stateid, status);
3049 return status;
3050}
3051
3052static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3053 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003054 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003055 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003057 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003058 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003059 struct nfs_setattrargs arg = {
3060 .fh = NFS_FH(inode),
3061 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 .server = server,
3063 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003064 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003065 };
3066 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003068 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003070 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003071 struct nfs4_exception exception = {
3072 .state = state,
3073 .inode = inode,
3074 .stateid = &arg.stateid,
3075 };
3076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
David Quigleyaa9c2662013-05-22 12:50:44 -04003078 arg.bitmask = nfs4_bitmask(server, ilabel);
3079 if (ilabel)
3080 arg.bitmask = nfs4_bitmask(server, olabel);
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003083 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003084 switch (err) {
3085 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003086 if (!(sattr->ia_valid & ATTR_SIZE)) {
3087 pr_warn_once("NFSv4: server %s is incorrectly "
3088 "applying open mode checks to "
3089 "a SETATTR that is not "
3090 "changing file size.\n",
3091 server->nfs_client->cl_hostname);
3092 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003093 if (state && !(state->state & FMODE_WRITE)) {
3094 err = -EBADF;
3095 if (sattr->ia_valid & ATTR_OPEN)
3096 err = -EACCES;
3097 goto out;
3098 }
3099 }
3100 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return err;
3104}
3105
Peng Tao500d7012015-09-22 11:35:22 +08003106static bool
3107nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3108{
3109 if (inode == NULL || !nfs_have_layout(inode))
3110 return false;
3111
3112 return pnfs_wait_on_layoutreturn(inode, task);
3113}
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115struct nfs4_closedata {
3116 struct inode *inode;
3117 struct nfs4_state *state;
3118 struct nfs_closeargs arg;
3119 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003120 struct {
3121 struct nfs4_layoutreturn_args arg;
3122 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003123 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003124 u32 roc_barrier;
3125 bool roc;
3126 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003127 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003128 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129};
3130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003132{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003133 struct nfs4_closedata *calldata = data;
3134 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003135 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003136
Trond Myklebustcf805162016-11-15 14:56:07 -05003137 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003138 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3139 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003140 nfs4_put_open_state(calldata->state);
3141 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003142 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003143 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003144 kfree(calldata);
3145}
3146
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003152 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Chuck Levera3ca5652012-03-01 17:00:40 -05003154 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003155 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3156 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003157 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003158
3159 /* Handle Layoutreturn errors */
3160 if (calldata->arg.lr_args && task->tk_status != 0) {
3161 switch (calldata->res.lr_ret) {
3162 default:
3163 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3164 break;
3165 case 0:
3166 calldata->arg.lr_args = NULL;
3167 calldata->res.lr_res = NULL;
3168 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003169 case -NFS4ERR_OLD_STATEID:
3170 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003171 calldata->inode))
3172 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003173 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003174 case -NFS4ERR_ADMIN_REVOKED:
3175 case -NFS4ERR_DELEG_REVOKED:
3176 case -NFS4ERR_EXPIRED:
3177 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003178 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3179 case -NFS4ERR_WRONG_CRED:
3180 calldata->arg.lr_args = NULL;
3181 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003182 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003183 }
3184 }
3185
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003186 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 * the state_owner. we keep this around to process errors
3188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 switch (task->tk_status) {
3190 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003191 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003192 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003193 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003194 case -NFS4ERR_ACCESS:
3195 if (calldata->arg.bitmask != NULL) {
3196 calldata->arg.bitmask = NULL;
3197 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003198 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003199
3200 }
3201 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003202 case -NFS4ERR_OLD_STATEID:
3203 /* Did we race with OPEN? */
3204 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003205 state))
3206 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003207 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003208 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003210 case -NFS4ERR_EXPIRED:
3211 nfs4_free_revoked_stateid(server,
3212 &calldata->arg.stateid,
3213 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003214 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003215 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 default:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003218 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3219 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003221 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3222 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003223out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003224 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003225 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003226 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003227 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003228 return;
3229lr_restart:
3230 calldata->res.lr_ret = 0;
3231out_restart:
3232 task->tk_status = 0;
3233 rpc_restart_call_prepare(task);
3234 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003237static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003239 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003240 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003241 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003242 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003243 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003244
Chuck Levera3ca5652012-03-01 17:00:40 -05003245 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003247 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003248
Trond Myklebust88069f72009-12-08 08:33:16 -05003249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003250 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003251 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3252 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3253 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003254 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003255 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003256 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003257 if (state->n_rdonly == 0)
3258 call_close |= is_rdonly;
3259 else if (is_rdonly)
3260 calldata->arg.fmode |= FMODE_READ;
3261 if (state->n_wronly == 0)
3262 call_close |= is_wronly;
3263 else if (is_wronly)
3264 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003265 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3266 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003267 } else if (is_rdwr)
3268 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3269
Trond Myklebust5cc78612016-11-14 11:19:56 -05003270 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003271 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003272 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003273 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003274
3275 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003276 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003277 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003278 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003279
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003280 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003281 nfs_release_seqid(calldata->arg.seqid);
3282 goto out_wait;
3283 }
3284
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003285 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003286 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003287
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003288 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003289 /* Close-to-open cache consistency revalidation */
3290 if (!nfs4_have_delegation(inode, FMODE_READ))
3291 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3292 else
3293 calldata->arg.bitmask = NULL;
3294 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003295
Trond Myklebust6ae37332015-01-30 14:21:14 -05003296 calldata->arg.share_access =
3297 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3298 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003299
Trond Myklebustd8d84982016-12-19 12:14:44 -05003300 if (calldata->res.fattr == NULL)
3301 calldata->arg.bitmask = NULL;
3302 else if (calldata->arg.bitmask == NULL)
3303 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003304 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003305 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003306 &calldata->arg.seq_args,
3307 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003308 task) != 0)
3309 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003310 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003311 return;
3312out_no_action:
3313 task->tk_action = NULL;
3314out_wait:
3315 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003318static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003319 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003320 .rpc_call_done = nfs4_close_done,
3321 .rpc_release = nfs4_free_closedata,
3322};
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324/*
3325 * It is possible for data to be read/written from a mem-mapped file
3326 * after the sys_close call (which hits the vfs layer as a flush).
3327 * This means that we can't safely call nfsv4 close on a file until
3328 * the inode is cleared. This in turn means that we are not good
3329 * NFSv4 citizens - we do not indicate to the server to update the file's
3330 * share state even when we are done with one of the three share
3331 * stateid's in the inode.
3332 *
3333 * NOTE: Caller must be holding the sp->so_owner semaphore!
3334 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003335int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003337 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003338 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003340 struct nfs4_state_owner *sp = state->owner;
3341 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003342 struct rpc_message msg = {
3343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3344 .rpc_cred = state->owner->so_cred,
3345 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003346 struct rpc_task_setup task_setup_data = {
3347 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003348 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003349 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003350 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003351 .flags = RPC_TASK_ASYNC,
3352 };
Trond Myklebust95121352005-10-18 14:20:12 -07003353 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003355 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3356 &task_setup_data.rpc_client, &msg);
3357
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003358 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003360 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003361 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003362 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003364 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003365 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3366 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003368 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3369 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003370 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003371 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003372 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003373 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003374 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003375 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003376 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003377 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003378 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003379 calldata->lr.roc = pnfs_roc(state->inode,
3380 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3381 if (calldata->lr.roc) {
3382 calldata->arg.lr_args = &calldata->lr.arg;
3383 calldata->res.lr_res = &calldata->lr.res;
3384 }
Al Viro643168c2011-06-22 18:20:23 -04003385 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003386
Trond Myklebust1174dd12010-12-21 10:52:24 -05003387 msg.rpc_argp = &calldata->arg;
3388 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003389 task_setup_data.callback_data = calldata;
3390 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003391 if (IS_ERR(task))
3392 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003393 status = 0;
3394 if (wait)
3395 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003396 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003397 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003398out_free_calldata:
3399 kfree(calldata);
3400out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003401 nfs4_put_open_state(state);
3402 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
Trond Myklebust2b484292010-09-17 10:56:51 -04003406static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003407nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3408 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003411 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3412
3413 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Trond Myklebust565277f2007-10-15 18:17:53 -04003415 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003416 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003417
3418 nfs4_label_release_security(label);
3419
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003420 if (IS_ERR(state))
3421 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003422 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Trond Myklebust1185a552009-12-03 15:54:02 -05003425static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003426{
3427 if (ctx->state == NULL)
3428 return;
3429 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003430 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003431 else
Al Viro643168c2011-06-22 18:20:23 -04003432 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Trond Myklebustb944dba2013-11-04 15:20:20 -05003435#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3436#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003437#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3440{
Kinglong Mee8c612822015-08-26 21:12:58 +08003441 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003442 struct nfs4_server_caps_arg args = {
3443 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003444 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 struct nfs4_server_caps_res res = {};
3447 struct rpc_message msg = {
3448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003449 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 .rpc_resp = &res,
3451 };
3452 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003453 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Kinglong Mee8c612822015-08-26 21:12:58 +08003455 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3456 FATTR4_WORD0_FH_EXPIRE_TYPE |
3457 FATTR4_WORD0_LINK_SUPPORT |
3458 FATTR4_WORD0_SYMLINK_SUPPORT |
3459 FATTR4_WORD0_ACLSUPPORT;
3460 if (minorversion)
3461 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3462
Bryan Schumaker7c513052011-03-24 17:12:24 +00003463 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003465 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003466 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003467 case 0:
3468 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3469 res.attr_bitmask[2] = 0;
3470 break;
3471 case 1:
3472 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3473 break;
3474 case 2:
3475 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003478 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3479 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3480 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3481 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003482 NFS_CAP_CTIME|NFS_CAP_MTIME|
3483 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003484 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3485 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 server->caps |= NFS_CAP_ACLS;
3487 if (res.has_links != 0)
3488 server->caps |= NFS_CAP_HARDLINKS;
3489 if (res.has_symlinks != 0)
3490 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003491 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3492 server->caps |= NFS_CAP_FILEID;
3493 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3494 server->caps |= NFS_CAP_MODE;
3495 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3496 server->caps |= NFS_CAP_NLINK;
3497 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3498 server->caps |= NFS_CAP_OWNER;
3499 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3500 server->caps |= NFS_CAP_OWNER_GROUP;
3501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3502 server->caps |= NFS_CAP_ATIME;
3503 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3504 server->caps |= NFS_CAP_CTIME;
3505 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3506 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003507#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3508 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3509 server->caps |= NFS_CAP_SECURITY_LABEL;
3510#endif
3511 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3512 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003513 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003514
Trond Myklebusta65318b2009-03-11 14:10:28 -04003515 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3516 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3517 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003518 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003519
3520 /* Avoid a regression due to buggy server */
3521 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3522 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003523 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3524 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003527 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return status;
3531}
3532
Trond Myklebust55a97592006-06-09 09:34:19 -04003533int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 struct nfs4_exception exception = { };
3536 int err;
3537 do {
3538 err = nfs4_handle_exception(server,
3539 _nfs4_server_capabilities(server, fhandle),
3540 &exception);
3541 } while (exception.retry);
3542 return err;
3543}
3544
3545static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3546 struct nfs_fsinfo *info)
3547{
David Quigleyaa9c2662013-05-22 12:50:44 -04003548 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003550 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 };
3552 struct nfs4_lookup_res res = {
3553 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003554 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 .fh = fhandle,
3556 };
3557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3559 .rpc_argp = &args,
3560 .rpc_resp = &res,
3561 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003562
David Quigleyaa9c2662013-05-22 12:50:44 -04003563 bitmask[0] = nfs4_fattr_bitmap[0];
3564 bitmask[1] = nfs4_fattr_bitmap[1];
3565 /*
3566 * Process the label in the upcoming getfattr
3567 */
3568 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3569
Trond Myklebust0e574af2005-10-27 22:12:38 -04003570 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003571 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
3574static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3575 struct nfs_fsinfo *info)
3576{
3577 struct nfs4_exception exception = { };
3578 int err;
3579 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003580 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003581 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003582 switch (err) {
3583 case 0:
3584 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003585 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003586 default:
3587 err = nfs4_handle_exception(server, err, &exception);
3588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003590out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 return err;
3592}
3593
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003594static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3595 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3596{
Trond Myklebustc2190662013-08-26 19:23:04 -04003597 struct rpc_auth_create_args auth_args = {
3598 .pseudoflavor = flavor,
3599 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003600 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003601
Trond Myklebustc2190662013-08-26 19:23:04 -04003602 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003603 if (IS_ERR(auth))
3604 return -EACCES;
3605 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003606}
3607
Chuck Lever9a744ba2013-03-16 15:56:02 -04003608/*
3609 * Retry pseudoroot lookup with various security flavors. We do this when:
3610 *
3611 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3612 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3613 *
3614 * Returns zero on success, or a negative NFS4ERR value, or a
3615 * negative errno value.
3616 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003617static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003618 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003620 /* Per 3530bis 15.33.5 */
3621 static const rpc_authflavor_t flav_array[] = {
3622 RPC_AUTH_GSS_KRB5P,
3623 RPC_AUTH_GSS_KRB5I,
3624 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003625 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003626 RPC_AUTH_NULL,
3627 };
3628 int status = -EPERM;
3629 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003631 if (server->auth_info.flavor_len > 0) {
3632 /* try each flavor specified by user */
3633 for (i = 0; i < server->auth_info.flavor_len; i++) {
3634 status = nfs4_lookup_root_sec(server, fhandle, info,
3635 server->auth_info.flavors[i]);
3636 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3637 continue;
3638 break;
3639 }
3640 } else {
3641 /* no flavors specified by user, try default list */
3642 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3643 status = nfs4_lookup_root_sec(server, fhandle, info,
3644 flav_array[i]);
3645 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3646 continue;
3647 break;
3648 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003649 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003650
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003651 /*
3652 * -EACCESS could mean that the user doesn't have correct permissions
3653 * to access the mount. It could also mean that we tried to mount
3654 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3655 * existing mount programs don't handle -EACCES very well so it should
3656 * be mapped to -EPERM instead.
3657 */
3658 if (status == -EACCES)
3659 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003660 return status;
3661}
3662
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003663/**
3664 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3665 * @server: initialized nfs_server handle
3666 * @fhandle: we fill in the pseudo-fs root file handle
3667 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003668 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003669 *
3670 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003671 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003672int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003673 struct nfs_fsinfo *info,
3674 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003675{
Andre Przywarac7757072015-04-23 17:17:40 +01003676 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003677
Andre Przywarac7757072015-04-23 17:17:40 +01003678 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003679 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003680
3681 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003682 status = server->nfs_client->cl_mvops->find_root_sec(server,
3683 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (status == 0)
3686 status = nfs4_server_capabilities(server, fhandle);
3687 if (status == 0)
3688 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003689
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003690 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Bryan Schumakerbae36242012-05-10 15:07:31 -04003693static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3694 struct nfs_fsinfo *info)
3695{
3696 int error;
3697 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003698 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003699
3700 error = nfs4_server_capabilities(server, mntfh);
3701 if (error < 0) {
3702 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3703 return error;
3704 }
3705
David Quigley14c43f72013-05-22 12:50:43 -04003706 label = nfs4_label_alloc(server, GFP_KERNEL);
3707 if (IS_ERR(label))
3708 return PTR_ERR(label);
3709
David Quigley1775fd32013-05-22 12:50:42 -04003710 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003711 if (error < 0) {
3712 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003713 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003714 }
3715
3716 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3717 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3718 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3719
David Quigley14c43f72013-05-22 12:50:43 -04003720err_free_label:
3721 nfs4_label_free(label);
3722
Bryan Schumakerbae36242012-05-10 15:07:31 -04003723 return error;
3724}
3725
Manoj Naik6b97fd32006-06-09 09:34:29 -04003726/*
3727 * Get locations and (maybe) other attributes of a referral.
3728 * Note that we'll actually follow the referral later when
3729 * we detect fsid mismatch in inode revalidation
3730 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003731static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3732 const struct qstr *name, struct nfs_fattr *fattr,
3733 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003734{
3735 int status = -ENOMEM;
3736 struct page *page = NULL;
3737 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003738
3739 page = alloc_page(GFP_KERNEL);
3740 if (page == NULL)
3741 goto out;
3742 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3743 if (locations == NULL)
3744 goto out;
3745
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003746 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003747 if (status != 0)
3748 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003749
3750 /*
3751 * If the fsid didn't change, this is a migration event, not a
3752 * referral. Cause us to drop into the exception handler, which
3753 * will kick off migration recovery.
3754 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003755 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003756 dprintk("%s: server did not return a different fsid for"
3757 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003758 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003759 goto out;
3760 }
Andy Adamson533eb462011-06-13 18:25:56 -04003761 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3762 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003763
Andy Adamson533eb462011-06-13 18:25:56 -04003764 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003765 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003766 memset(fhandle, 0, sizeof(struct nfs_fh));
3767out:
3768 if (page)
3769 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003770 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003771 return status;
3772}
3773
David Quigley1775fd32013-05-22 12:50:42 -04003774static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3775 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 struct nfs4_getattr_arg args = {
3778 .fh = fhandle,
3779 .bitmask = server->attr_bitmask,
3780 };
3781 struct nfs4_getattr_res res = {
3782 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003783 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 .server = server,
3785 };
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3788 .rpc_argp = &args,
3789 .rpc_resp = &res,
3790 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003791
3792 args.bitmask = nfs4_bitmask(server, label);
3793
Trond Myklebust0e574af2005-10-27 22:12:38 -04003794 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003795 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
David Quigley1775fd32013-05-22 12:50:42 -04003798static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3799 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 struct nfs4_exception exception = { };
3802 int err;
3803 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003804 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3805 trace_nfs4_getattr(server, fhandle, fattr, err);
3806 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 &exception);
3808 } while (exception.retry);
3809 return err;
3810}
3811
3812/*
3813 * The file is not closed if it is opened due to the a request to change
3814 * the size of the file. The open call will not be needed once the
3815 * VFS layer lookup-intents are implemented.
3816 *
3817 * Close is called when the inode is destroyed.
3818 * If we haven't opened the file for O_WRONLY, we
3819 * need to in the size_change case to obtain a stateid.
3820 *
3821 * Got race?
3822 * Because OPEN is always done by name in nfsv4, it is
3823 * possible that we opened a different file by the same
3824 * name. We can recognize this race condition, but we
3825 * can't do anything about it besides returning an error.
3826 *
3827 * This will be fixed with VFS changes (lookup-intent).
3828 */
3829static int
3830nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3831 struct iattr *sattr)
3832{
David Howells2b0143b2015-03-17 22:25:59 +00003833 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003834 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003835 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003836 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 int status;
3838
Peng Tao88ac8152014-09-12 11:04:10 +08003839 if (pnfs_ld_layoutret_on_setattr(inode) &&
3840 sattr->ia_valid & ATTR_SIZE &&
3841 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003842 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003843
Trond Myklebust0e574af2005-10-27 22:12:38 -04003844 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Andy Adamson26699402012-05-30 16:12:24 -04003846 /* Deal with open(O_TRUNC) */
3847 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003848 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003849
3850 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003851 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003852 return 0;
3853
Trond Myklebustd5308382005-11-04 15:33:38 -05003854 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003855 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003856
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003857 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003858 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003859 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003860 }
3861
David Quigley14c43f72013-05-22 12:50:43 -04003862 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3863 if (IS_ERR(label))
3864 return PTR_ERR(label);
3865
NeilBrown29b59f92016-10-13 15:26:47 +11003866 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003867 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003868 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003869 nfs_setsecurity(inode, fattr, label);
3870 }
David Quigley14c43f72013-05-22 12:50:43 -04003871 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return status;
3873}
3874
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003875static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3876 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003877 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003878{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003879 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003880 int status;
3881 struct nfs4_lookup_arg args = {
3882 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003883 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003884 .name = name,
3885 };
3886 struct nfs4_lookup_res res = {
3887 .server = server,
3888 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003889 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003890 .fh = fhandle,
3891 };
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3894 .rpc_argp = &args,
3895 .rpc_resp = &res,
3896 };
3897
David Quigleyaa9c2662013-05-22 12:50:44 -04003898 args.bitmask = nfs4_bitmask(server, label);
3899
David Howells2b3de442006-08-22 20:06:09 -04003900 nfs_fattr_init(fattr);
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003903 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 dprintk("NFS reply lookup: %d\n", status);
3905 return status;
3906}
3907
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003908static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003909{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003910 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003911 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003912 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3913 fattr->nlink = 2;
3914}
3915
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003916static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003917 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003918 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003919{
3920 struct nfs4_exception exception = { };
3921 struct rpc_clnt *client = *clnt;
3922 int err;
3923 do {
David Quigley1775fd32013-05-22 12:50:42 -04003924 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003925 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003926 switch (err) {
3927 case -NFS4ERR_BADNAME:
3928 err = -ENOENT;
3929 goto out;
3930 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003931 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003932 if (err == -NFS4ERR_MOVED)
3933 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003934 goto out;
3935 case -NFS4ERR_WRONGSEC:
3936 err = -EPERM;
3937 if (client != *clnt)
3938 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003939 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003940 if (IS_ERR(client))
3941 return PTR_ERR(client);
3942
3943 exception.retry = 1;
3944 break;
3945 default:
3946 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3947 }
3948 } while (exception.retry);
3949
3950out:
3951 if (err == 0)
3952 *clnt = client;
3953 else if (client != *clnt)
3954 rpc_shutdown_client(client);
3955
3956 return err;
3957}
3958
Al Virobeffb8f2016-07-20 16:34:42 -04003959static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003960 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3961 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003963 int status;
3964 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003965
David Quigley1775fd32013-05-22 12:50:42 -04003966 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003967 if (client != NFS_CLIENT(dir)) {
3968 rpc_shutdown_client(client);
3969 nfs_fixup_secinfo_attributes(fattr);
3970 }
3971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003974struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003975nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003976 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3977{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003978 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003979 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003980
David Quigley1775fd32013-05-22 12:50:42 -04003981 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003982 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003983 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003984 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003985}
3986
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003987static int _nfs4_proc_lookupp(struct inode *inode,
3988 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3989 struct nfs4_label *label)
3990{
3991 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3992 struct nfs_server *server = NFS_SERVER(inode);
3993 int status;
3994 struct nfs4_lookupp_arg args = {
3995 .bitmask = server->attr_bitmask,
3996 .fh = NFS_FH(inode),
3997 };
3998 struct nfs4_lookupp_res res = {
3999 .server = server,
4000 .fattr = fattr,
4001 .label = label,
4002 .fh = fhandle,
4003 };
4004 struct rpc_message msg = {
4005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4006 .rpc_argp = &args,
4007 .rpc_resp = &res,
4008 };
4009
4010 args.bitmask = nfs4_bitmask(server, label);
4011
4012 nfs_fattr_init(fattr);
4013
4014 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4015 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4016 &res.seq_res, 0);
4017 dprintk("NFS reply lookupp: %d\n", status);
4018 return status;
4019}
4020
4021static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4022 struct nfs_fattr *fattr, struct nfs4_label *label)
4023{
4024 struct nfs4_exception exception = { };
4025 int err;
4026 do {
4027 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4028 trace_nfs4_lookupp(inode, err);
4029 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4030 &exception);
4031 } while (exception.retry);
4032 return err;
4033}
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4036{
Trond Myklebust76b32992007-08-10 17:45:11 -04004037 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct nfs4_accessargs args = {
4039 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004040 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004041 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004043 struct nfs4_accessres res = {
4044 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 struct rpc_message msg = {
4047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4048 .rpc_argp = &args,
4049 .rpc_resp = &res,
4050 .rpc_cred = entry->cred,
4051 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004052 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Trond Myklebustc407d412010-04-16 16:22:48 -04004054 res.fattr = nfs_alloc_fattr();
4055 if (res.fattr == NULL)
4056 return -ENOMEM;
4057
Bryan Schumaker7c513052011-03-24 17:12:24 +00004058 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004060 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004061 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004063 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return status;
4065}
4066
4067static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4068{
4069 struct nfs4_exception exception = { };
4070 int err;
4071 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004072 err = _nfs4_proc_access(inode, entry);
4073 trace_nfs4_access(inode, err);
4074 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 &exception);
4076 } while (exception.retry);
4077 return err;
4078}
4079
4080/*
4081 * TODO: For the time being, we don't try to get any attributes
4082 * along with any of the zero-copy operations READ, READDIR,
4083 * READLINK, WRITE.
4084 *
4085 * In the case of the first three, we want to put the GETATTR
4086 * after the read-type operation -- this is because it is hard
4087 * to predict the length of a GETATTR response in v4, and thus
4088 * align the READ data correctly. This means that the GETATTR
4089 * may end up partially falling into the page cache, and we should
4090 * shift it into the 'tail' of the xdr_buf before processing.
4091 * To do this efficiently, we need to know the total length
4092 * of data received, which doesn't seem to be available outside
4093 * of the RPC layer.
4094 *
4095 * In the case of WRITE, we also want to put the GETATTR after
4096 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004097 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 *
4099 * Both of these changes to the XDR layer would in fact be quite
4100 * minor, but I decided to leave them for a subsequent patch.
4101 */
4102static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4103 unsigned int pgbase, unsigned int pglen)
4104{
4105 struct nfs4_readlink args = {
4106 .fh = NFS_FH(inode),
4107 .pgbase = pgbase,
4108 .pglen = pglen,
4109 .pages = &page,
4110 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004111 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 struct rpc_message msg = {
4113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4114 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004115 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 };
4117
Bryan Schumaker7c513052011-03-24 17:12:24 +00004118 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 -07004119}
4120
4121static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4122 unsigned int pgbase, unsigned int pglen)
4123{
4124 struct nfs4_exception exception = { };
4125 int err;
4126 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004127 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4128 trace_nfs4_readlink(inode, err);
4129 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 &exception);
4131 } while (exception.retry);
4132 return err;
4133}
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004136 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138static int
4139nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004140 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004142 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004143 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004144 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 int status = 0;
4147
NeilBrown532d4de2016-10-13 15:26:47 +11004148 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004149 if (IS_ERR(ctx))
4150 return PTR_ERR(ctx);
4151
David Quigleyaa9c2662013-05-22 12:50:44 -04004152 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4153
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004154 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4155 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004156 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 if (IS_ERR(state)) {
4158 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004162 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004163 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 return status;
4165}
4166
Al Virobeffb8f2016-07-20 16:34:42 -04004167static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Trond Myklebust16e42952005-10-27 22:12:44 -04004169 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004170 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004172 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004174 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004175 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4179 .rpc_argp = &args,
4180 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004182 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004183 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004184
Bryan Schumaker7c513052011-03-24 17:12:24 +00004185 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004186 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004187 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return status;
4189}
4190
Al Virobeffb8f2016-07-20 16:34:42 -04004191static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 struct nfs4_exception exception = { };
4194 int err;
4195 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004196 err = _nfs4_proc_remove(dir, name);
4197 trace_nfs4_remove(dir, name, err);
4198 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 &exception);
4200 } while (exception.retry);
4201 return err;
4202}
4203
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004204static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004206 struct nfs_server *server = NFS_SERVER(dir);
4207 struct nfs_removeargs *args = msg->rpc_argp;
4208 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004210 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004212 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004213
4214 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004217static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4218{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004219 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004220 &data->args.seq_args,
4221 &data->res.seq_res,
4222 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004225static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004227 struct nfs_unlinkdata *data = task->tk_calldata;
4228 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004229
Trond Myklebust14516c32010-07-31 14:29:06 -04004230 if (!nfs4_sequence_done(task, &res->seq_res))
4231 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004232 if (nfs4_async_handle_error(task, res->server, NULL,
4233 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004234 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004235 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004236 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004237 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Jeff Laytond3d41522010-09-17 17:31:57 -04004240static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4241{
4242 struct nfs_server *server = NFS_SERVER(dir);
4243 struct nfs_renameargs *arg = msg->rpc_argp;
4244 struct nfs_renameres *res = msg->rpc_resp;
4245
4246 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004247 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004248 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004249}
4250
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004251static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4252{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004253 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004254 &data->args.seq_args,
4255 &data->res.seq_res,
4256 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004257}
4258
4259static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4260 struct inode *new_dir)
4261{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004262 struct nfs_renamedata *data = task->tk_calldata;
4263 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004264
4265 if (!nfs4_sequence_done(task, &res->seq_res))
4266 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004267 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004268 return 0;
4269
Trond Myklebustc733c492017-01-11 12:32:26 -05004270 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004271 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004272 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004273 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004274 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004275 return 1;
4276}
4277
Al Virobeffb8f2016-07-20 16:34:42 -04004278static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004280 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 struct nfs4_link_arg arg = {
4282 .fh = NFS_FH(inode),
4283 .dir_fh = NFS_FH(dir),
4284 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004285 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004287 struct nfs4_link_res res = {
4288 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004289 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004290 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 struct rpc_message msg = {
4292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4293 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004294 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004296 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Trond Myklebust136f2622010-04-16 16:22:49 -04004298 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004299 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004300 goto out;
4301
David Quigley14c43f72013-05-22 12:50:43 -04004302 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4303 if (IS_ERR(res.label)) {
4304 status = PTR_ERR(res.label);
4305 goto out;
4306 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004307 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004308
Bryan Schumaker7c513052011-03-24 17:12:24 +00004309 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004310 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004311 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004312 status = nfs_post_op_update_inode(inode, res.fattr);
4313 if (!status)
4314 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004315 }
David Quigley14c43f72013-05-22 12:50:43 -04004316
4317
4318 nfs4_label_free(res.label);
4319
Trond Myklebust136f2622010-04-16 16:22:49 -04004320out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004321 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return status;
4323}
4324
Al Virobeffb8f2016-07-20 16:34:42 -04004325static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct nfs4_exception exception = { };
4328 int err;
4329 do {
4330 err = nfs4_handle_exception(NFS_SERVER(inode),
4331 _nfs4_proc_link(inode, dir, name),
4332 &exception);
4333 } while (exception.retry);
4334 return err;
4335}
4336
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004337struct nfs4_createdata {
4338 struct rpc_message msg;
4339 struct nfs4_create_arg arg;
4340 struct nfs4_create_res res;
4341 struct nfs_fh fh;
4342 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004343 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344};
4345
4346static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004347 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348{
4349 struct nfs4_createdata *data;
4350
4351 data = kzalloc(sizeof(*data), GFP_KERNEL);
4352 if (data != NULL) {
4353 struct nfs_server *server = NFS_SERVER(dir);
4354
David Quigley14c43f72013-05-22 12:50:43 -04004355 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4356 if (IS_ERR(data->label))
4357 goto out_free;
4358
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004359 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4360 data->msg.rpc_argp = &data->arg;
4361 data->msg.rpc_resp = &data->res;
4362 data->arg.dir_fh = NFS_FH(dir);
4363 data->arg.server = server;
4364 data->arg.name = name;
4365 data->arg.attrs = sattr;
4366 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004367 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004368 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004369 data->res.server = server;
4370 data->res.fh = &data->fh;
4371 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004372 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004373 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004374 }
4375 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004376out_free:
4377 kfree(data);
4378 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004379}
4380
4381static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4382{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004383 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004384 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004385 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004386 update_changeattr(dir, &data->res.dir_cinfo,
4387 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004388 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004389 }
4390 return status;
4391}
4392
4393static void nfs4_free_createdata(struct nfs4_createdata *data)
4394{
David Quigley14c43f72013-05-22 12:50:43 -04004395 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 kfree(data);
4397}
4398
Chuck Lever4f390c12006-08-22 20:06:22 -04004399static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004400 struct page *page, unsigned int len, struct iattr *sattr,
4401 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004403 struct nfs4_createdata *data;
4404 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Chuck Lever94a6d752006-08-22 20:06:23 -04004406 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004408
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004409 status = -ENOMEM;
4410 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4411 if (data == NULL)
4412 goto out;
4413
4414 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4415 data->arg.u.symlink.pages = &page;
4416 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004417 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 status = nfs4_do_create(dir, dentry, data);
4420
4421 nfs4_free_createdata(data);
4422out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return status;
4424}
4425
Chuck Lever4f390c12006-08-22 20:06:22 -04004426static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004427 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004430 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004432
4433 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004436 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4437 trace_nfs4_symlink(dir, &dentry->d_name, err);
4438 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 &exception);
4440 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004441
4442 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 return err;
4444}
4445
4446static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004447 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 struct nfs4_createdata *data;
4450 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004452 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4453 if (data == NULL)
4454 goto out;
4455
David Quigley1775fd32013-05-22 12:50:42 -04004456 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 status = nfs4_do_create(dir, dentry, data);
4458
4459 nfs4_free_createdata(data);
4460out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return status;
4462}
4463
4464static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4465 struct iattr *sattr)
4466{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004467 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004469 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004471
David Quigleyaa9c2662013-05-22 12:50:44 -04004472 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4473
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004474 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4475 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004477 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4478 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4479 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 &exception);
4481 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004482 nfs4_label_release_security(label);
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return err;
4485}
4486
4487static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004488 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
David Howells2b0143b2015-03-17 22:25:59 +00004490 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 struct nfs4_readdir_arg args = {
4492 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004493 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 .pgbase = 0,
4495 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004496 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004497 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 };
4499 struct nfs4_readdir_res res;
4500 struct rpc_message msg = {
4501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4502 .rpc_argp = &args,
4503 .rpc_resp = &res,
4504 .rpc_cred = cred,
4505 };
4506 int status;
4507
Al Viro6de14722013-09-16 10:53:17 -04004508 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4509 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004510 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004511 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004513 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 -05004514 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004515 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004516 status += args.pgbase;
4517 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004518
4519 nfs_invalidate_atime(dir);
4520
Harvey Harrison3110ff82008-05-02 13:42:44 -07004521 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return status;
4523}
4524
4525static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004526 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
4528 struct nfs4_exception exception = { };
4529 int err;
4530 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004531 err = _nfs4_proc_readdir(dentry, cred, cookie,
4532 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004533 trace_nfs4_readdir(d_inode(dentry), err);
4534 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 &exception);
4536 } while (exception.retry);
4537 return err;
4538}
4539
4540static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004541 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004543 struct nfs4_createdata *data;
4544 int mode = sattr->ia_mode;
4545 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004547 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4548 if (data == NULL)
4549 goto out;
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004554 data->arg.ftype = NF4BLK;
4555 data->arg.u.device.specdata1 = MAJOR(rdev);
4556 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
4558 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004559 data->arg.ftype = NF4CHR;
4560 data->arg.u.device.specdata1 = MAJOR(rdev);
4561 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004562 } else if (!S_ISSOCK(mode)) {
4563 status = -EINVAL;
4564 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 }
David Quigley1775fd32013-05-22 12:50:42 -04004566
David Quigleyaa9c2662013-05-22 12:50:44 -04004567 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004568 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004569out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004570 nfs4_free_createdata(data);
4571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return status;
4573}
4574
4575static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4576 struct iattr *sattr, dev_t rdev)
4577{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004578 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004580 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004582
David Quigleyaa9c2662013-05-22 12:50:44 -04004583 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4584
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004585 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4586 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004588 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4589 trace_nfs4_mknod(dir, &dentry->d_name, err);
4590 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 &exception);
4592 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004593
4594 nfs4_label_release_security(label);
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 return err;
4597}
4598
4599static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4600 struct nfs_fsstat *fsstat)
4601{
4602 struct nfs4_statfs_arg args = {
4603 .fh = fhandle,
4604 .bitmask = server->attr_bitmask,
4605 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004606 struct nfs4_statfs_res res = {
4607 .fsstat = fsstat,
4608 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 struct rpc_message msg = {
4610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4611 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004612 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 };
4614
Trond Myklebust0e574af2005-10-27 22:12:38 -04004615 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004616 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
4619static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4620{
4621 struct nfs4_exception exception = { };
4622 int err;
4623 do {
4624 err = nfs4_handle_exception(server,
4625 _nfs4_proc_statfs(server, fhandle, fsstat),
4626 &exception);
4627 } while (exception.retry);
4628 return err;
4629}
4630
4631static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4632 struct nfs_fsinfo *fsinfo)
4633{
4634 struct nfs4_fsinfo_arg args = {
4635 .fh = fhandle,
4636 .bitmask = server->attr_bitmask,
4637 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004638 struct nfs4_fsinfo_res res = {
4639 .fsinfo = fsinfo,
4640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 struct rpc_message msg = {
4642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4643 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004644 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 };
4646
Bryan Schumaker7c513052011-03-24 17:12:24 +00004647 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648}
4649
4650static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4651{
4652 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004653 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 int err;
4655
4656 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004657 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004658 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004659 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004660 nfs4_set_lease_period(server->nfs_client,
4661 fsinfo->lease_time * HZ,
4662 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004663 break;
4664 }
4665 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 } while (exception.retry);
4667 return err;
4668}
4669
4670static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4671{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004672 int error;
4673
Trond Myklebust0e574af2005-10-27 22:12:38 -04004674 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004675 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004676 if (error == 0) {
4677 /* block layout checks this! */
4678 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004679 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004680 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004681
4682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
4685static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4686 struct nfs_pathconf *pathconf)
4687{
4688 struct nfs4_pathconf_arg args = {
4689 .fh = fhandle,
4690 .bitmask = server->attr_bitmask,
4691 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004692 struct nfs4_pathconf_res res = {
4693 .pathconf = pathconf,
4694 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 struct rpc_message msg = {
4696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4697 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004698 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 };
4700
4701 /* None of the pathconf attributes are mandatory to implement */
4702 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4703 memset(pathconf, 0, sizeof(*pathconf));
4704 return 0;
4705 }
4706
Trond Myklebust0e574af2005-10-27 22:12:38 -04004707 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004708 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
4711static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4712 struct nfs_pathconf *pathconf)
4713{
4714 struct nfs4_exception exception = { };
4715 int err;
4716
4717 do {
4718 err = nfs4_handle_exception(server,
4719 _nfs4_proc_pathconf(server, fhandle, pathconf),
4720 &exception);
4721 } while (exception.retry);
4722 return err;
4723}
4724
Trond Myklebust5521abf2013-03-16 20:54:34 -04004725int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004726 const struct nfs_open_context *ctx,
4727 const struct nfs_lock_context *l_ctx,
4728 fmode_t fmode)
4729{
NeilBrown17393472016-10-13 15:26:47 +11004730 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004731}
4732EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4733
Trond Myklebust5521abf2013-03-16 20:54:34 -04004734static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4735 const struct nfs_open_context *ctx,
4736 const struct nfs_lock_context *l_ctx,
4737 fmode_t fmode)
4738{
4739 nfs4_stateid current_stateid;
4740
Trond Myklebuste1253be2014-03-05 08:44:23 -05004741 /* If the current stateid represents a lost lock, then exit */
4742 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4743 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004744 return nfs4_stateid_match(stateid, &current_stateid);
4745}
4746
4747static bool nfs4_error_stateid_expired(int err)
4748{
4749 switch (err) {
4750 case -NFS4ERR_DELEG_REVOKED:
4751 case -NFS4ERR_ADMIN_REVOKED:
4752 case -NFS4ERR_BAD_STATEID:
4753 case -NFS4ERR_STALE_STATEID:
4754 case -NFS4ERR_OLD_STATEID:
4755 case -NFS4ERR_OPENMODE:
4756 case -NFS4ERR_EXPIRED:
4757 return true;
4758 }
4759 return false;
4760}
4761
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004766 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004767 if (task->tk_status < 0) {
4768 struct nfs4_exception exception = {
4769 .inode = hdr->inode,
4770 .state = hdr->args.context->state,
4771 .stateid = &hdr->args.stateid,
4772 };
4773 task->tk_status = nfs4_async_handle_exception(task,
4774 server, task->tk_status, &exception);
4775 if (exception.retry) {
4776 rpc_restart_call_prepare(task);
4777 return -EAGAIN;
4778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004787 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004788{
4789
4790 if (!nfs4_error_stateid_expired(task->tk_status) ||
4791 nfs4_stateid_is_current(&args->stateid,
4792 args->context,
4793 args->lock_context,
4794 FMODE_READ))
4795 return false;
4796 rpc_restart_call_prepare(task);
4797 return true;
4798}
4799
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004800static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004801{
4802
4803 dprintk("--> %s\n", __func__);
4804
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004806 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004807 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004808 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004809 if (task->tk_status > 0)
4810 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004811 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4812 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004813}
4814
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004815static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4816 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004818 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004819 if (!hdr->pgio_done_cb)
4820 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004821 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004822 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004825static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4826 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004827{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004828 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004829 &hdr->args.seq_args,
4830 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004831 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004832 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004833 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4834 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004835 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004836 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004837 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004838 return -EIO;
4839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004842static int nfs4_write_done_cb(struct rpc_task *task,
4843 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004845 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004846
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004847 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004848 if (task->tk_status < 0) {
4849 struct nfs4_exception exception = {
4850 .inode = hdr->inode,
4851 .state = hdr->args.context->state,
4852 .stateid = &hdr->args.stateid,
4853 };
4854 task->tk_status = nfs4_async_handle_exception(task,
4855 NFS_SERVER(inode), task->tk_status,
4856 &exception);
4857 if (exception.retry) {
4858 rpc_restart_call_prepare(task);
4859 return -EAGAIN;
4860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004862 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004863 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004864 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004865 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867}
4868
Trond Myklebust5521abf2013-03-16 20:54:34 -04004869static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004870 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004871{
4872
4873 if (!nfs4_error_stateid_expired(task->tk_status) ||
4874 nfs4_stateid_is_current(&args->stateid,
4875 args->context,
4876 args->lock_context,
4877 FMODE_WRITE))
4878 return false;
4879 rpc_restart_call_prepare(task);
4880 return true;
4881}
4882
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004884{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004885 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004886 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004888 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004889 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4890 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004891}
4892
Trond Myklebust5a37f852012-04-28 14:55:16 -04004893static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004895{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004896 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004897 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004898 return false;
4899 /* Otherwise, request attributes if and only if we don't hold
4900 * a delegation
4901 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004902 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004903}
Fred Isamana69aef12011-03-03 15:13:47 +00004904
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4906 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004909
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004910 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4911 hdr->args.bitmask = NULL;
4912 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004913 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004914 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004915
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916 if (!hdr->pgio_done_cb)
4917 hdr->pgio_done_cb = nfs4_write_done_cb;
4918 hdr->res.server = server;
4919 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004921 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004922 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
4924
Fred Isaman0b7c0152012-04-20 14:47:39 -04004925static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4926{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004927 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004928 &data->args.seq_args,
4929 &data->res.seq_res,
4930 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
Fred Isaman0b7c0152012-04-20 14:47:39 -04004933static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004936
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004937 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004938 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4939 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004940 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004941 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
Fred Isaman0b7c0152012-04-20 14:47:39 -04004946static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004947{
4948 if (!nfs4_sequence_done(task, &data->res.seq_res))
4949 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004950 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004951}
4952
Fred Isaman0b7c0152012-04-20 14:47:39 -04004953static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004955 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004956
Fred Isaman0b7c0152012-04-20 14:47:39 -04004957 if (data->commit_done_cb == NULL)
4958 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004959 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004960 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004961 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962}
4963
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004964struct nfs4_renewdata {
4965 struct nfs_client *client;
4966 unsigned long timestamp;
4967};
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969/*
4970 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4971 * standalone procedure for queueing an asynchronous RENEW.
4972 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004973static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004974{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004975 struct nfs4_renewdata *data = calldata;
4976 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004977
Elena Reshetova212bf412017-10-20 12:53:38 +03004978 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004979 nfs4_schedule_state_renewal(clp);
4980 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004981 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004982}
4983
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004984static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004986 struct nfs4_renewdata *data = calldata;
4987 struct nfs_client *clp = data->client;
4988 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004990 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004991 switch (task->tk_status) {
4992 case 0:
4993 break;
4994 case -NFS4ERR_LEASE_MOVED:
4995 nfs4_schedule_lease_moved_recovery(clp);
4996 break;
4997 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004998 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004999 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5000 return;
5001 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005002 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005003 return;
5004 }
5005 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005007 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
5009
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005010static const struct rpc_call_ops nfs4_renew_ops = {
5011 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005012 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005013};
5014
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005015static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
5017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5019 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005020 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005022 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005024 if (renew_flags == 0)
5025 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005026 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005027 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005028 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005029 if (data == NULL) {
5030 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005031 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005032 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005033 data->client = clp;
5034 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005035 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005036 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005039static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
5041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5043 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005044 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 };
5046 unsigned long now = jiffies;
5047 int status;
5048
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005049 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 if (status < 0)
5051 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005052 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return 0;
5054}
5055
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056static inline int nfs4_server_supports_acls(struct nfs_server *server)
5057{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005058 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059}
5060
Trond Myklebust21f498c2012-08-24 10:59:25 -04005061/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5062 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005063 * the stack.
5064 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005065#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005066
Neil Hormane9e3d722011-03-04 19:26:03 -05005067static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005068 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005069{
5070 struct page *newpage, **spages;
5071 int rc = 0;
5072 size_t len;
5073 spages = pages;
5074
5075 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005076 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005077 newpage = alloc_page(GFP_KERNEL);
5078
5079 if (newpage == NULL)
5080 goto unwind;
5081 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005082 buf += len;
5083 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005084 *pages++ = newpage;
5085 rc++;
5086 } while (buflen != 0);
5087
5088 return rc;
5089
5090unwind:
5091 for(; rc > 0; rc--)
5092 __free_page(spages[rc-1]);
5093 return -ENOMEM;
5094}
5095
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005096struct nfs4_cached_acl {
5097 int cached;
5098 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005099 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005100};
5101
5102static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005103{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005104 struct nfs_inode *nfsi = NFS_I(inode);
5105
5106 spin_lock(&inode->i_lock);
5107 kfree(nfsi->nfs4_acl);
5108 nfsi->nfs4_acl = acl;
5109 spin_unlock(&inode->i_lock);
5110}
5111
5112static void nfs4_zap_acl_attr(struct inode *inode)
5113{
5114 nfs4_set_cached_acl(inode, NULL);
5115}
5116
5117static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5118{
5119 struct nfs_inode *nfsi = NFS_I(inode);
5120 struct nfs4_cached_acl *acl;
5121 int ret = -ENOENT;
5122
5123 spin_lock(&inode->i_lock);
5124 acl = nfsi->nfs4_acl;
5125 if (acl == NULL)
5126 goto out;
5127 if (buf == NULL) /* user is just asking for length */
5128 goto out_len;
5129 if (acl->cached == 0)
5130 goto out;
5131 ret = -ERANGE; /* see getxattr(2) man page */
5132 if (acl->len > buflen)
5133 goto out;
5134 memcpy(buf, acl->data, acl->len);
5135out_len:
5136 ret = acl->len;
5137out:
5138 spin_unlock(&inode->i_lock);
5139 return ret;
5140}
5141
Sachin Prabhu5794d212012-04-17 14:36:40 +01005142static 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 +00005143{
5144 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005145 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005146
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005147 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005148 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005149 if (acl == NULL)
5150 goto out;
5151 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005152 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005153 } else {
5154 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5155 if (acl == NULL)
5156 goto out;
5157 acl->cached = 0;
5158 }
5159 acl->len = acl_len;
5160out:
5161 nfs4_set_cached_acl(inode, acl);
5162}
5163
Andy Adamsonbf118a32011-12-07 11:55:27 -05005164/*
5165 * The getxattr API returns the required buffer length when called with a
5166 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5167 * the required buf. On a NULL buf, we send a page of data to the server
5168 * guessing that the ACL request can be serviced by a page. If so, we cache
5169 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5170 * the cache. If not so, we throw away the page, and cache the required
5171 * length. The next getxattr call will then produce another round trip to
5172 * the server, this time with the input buf of the required size.
5173 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005174static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005175{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005176 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005177 struct nfs_getaclargs args = {
5178 .fh = NFS_FH(inode),
5179 .acl_pages = pages,
5180 .acl_len = buflen,
5181 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005182 struct nfs_getaclres res = {
5183 .acl_len = buflen,
5184 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005185 struct rpc_message msg = {
5186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5187 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005188 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005189 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005190 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005191 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005192
Trond Myklebust21f498c2012-08-24 10:59:25 -04005193 if (npages > ARRAY_SIZE(pages))
5194 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005195
Andy Adamsonbf118a32011-12-07 11:55:27 -05005196 for (i = 0; i < npages; i++) {
5197 pages[i] = alloc_page(GFP_KERNEL);
5198 if (!pages[i])
5199 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005200 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005201
5202 /* for decoding across pages */
5203 res.acl_scratch = alloc_page(GFP_KERNEL);
5204 if (!res.acl_scratch)
5205 goto out_free;
5206
Andy Adamsonbf118a32011-12-07 11:55:27 -05005207 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005208
Peng Taode040be2012-01-10 22:42:47 +08005209 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005210 __func__, buf, buflen, npages, args.acl_len);
5211 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5212 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005213 if (ret)
5214 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005215
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005216 /* Handle the case where the passed-in buffer is too short */
5217 if (res.acl_flags & NFS4_ACL_TRUNC) {
5218 /* Did the user only issue a request for the acl length? */
5219 if (buf == NULL)
5220 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005222 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005223 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005224 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005225 if (buf) {
5226 if (res.acl_len > buflen) {
5227 ret = -ERANGE;
5228 goto out_free;
5229 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005230 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005231 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005232out_ok:
5233 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005234out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005235 for (i = 0; i < npages; i++)
5236 if (pages[i])
5237 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005238 if (res.acl_scratch)
5239 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005240 return ret;
5241}
5242
Trond Myklebust16b42892006-08-24 12:27:15 -04005243static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5244{
5245 struct nfs4_exception exception = { };
5246 ssize_t ret;
5247 do {
5248 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005249 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005250 if (ret >= 0)
5251 break;
5252 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5253 } while (exception.retry);
5254 return ret;
5255}
5256
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005257static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5258{
5259 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005260 int ret;
5261
5262 if (!nfs4_server_supports_acls(server))
5263 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005264 ret = nfs_revalidate_inode(server, inode);
5265 if (ret < 0)
5266 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005267 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5268 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005269 ret = nfs4_read_cached_acl(inode, buf, buflen);
5270 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005271 /* -ENOENT is returned if there is no ACL or if there is an ACL
5272 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005273 return ret;
5274 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005275}
5276
Trond Myklebust16b42892006-08-24 12:27:15 -04005277static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005278{
5279 struct nfs_server *server = NFS_SERVER(inode);
5280 struct page *pages[NFS4ACL_MAXPAGES];
5281 struct nfs_setaclargs arg = {
5282 .fh = NFS_FH(inode),
5283 .acl_pages = pages,
5284 .acl_len = buflen,
5285 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005286 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5289 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005290 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005291 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005292 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005293 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005294
5295 if (!nfs4_server_supports_acls(server))
5296 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005297 if (npages > ARRAY_SIZE(pages))
5298 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005299 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005300 if (i < 0)
5301 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005302 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005303 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005304
5305 /*
5306 * Free each page after tx, so the only ref left is
5307 * held by the network stack
5308 */
5309 for (; i > 0; i--)
5310 put_page(pages[i-1]);
5311
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005312 /*
5313 * Acl update can result in inode attribute update.
5314 * so mark the attribute cache invalid.
5315 */
5316 spin_lock(&inode->i_lock);
5317 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5318 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005319 nfs_access_zap_cache(inode);
5320 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005321 return ret;
5322}
5323
Trond Myklebust16b42892006-08-24 12:27:15 -04005324static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5325{
5326 struct nfs4_exception exception = { };
5327 int err;
5328 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005329 err = __nfs4_proc_set_acl(inode, buf, buflen);
5330 trace_nfs4_set_acl(inode, err);
5331 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005332 &exception);
5333 } while (exception.retry);
5334 return err;
5335}
5336
David Quigleyaa9c2662013-05-22 12:50:44 -04005337#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5338static int _nfs4_get_security_label(struct inode *inode, void *buf,
5339 size_t buflen)
5340{
5341 struct nfs_server *server = NFS_SERVER(inode);
5342 struct nfs_fattr fattr;
5343 struct nfs4_label label = {0, 0, buflen, buf};
5344
5345 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005346 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005347 .fh = NFS_FH(inode),
5348 .bitmask = bitmask,
5349 };
5350 struct nfs4_getattr_res res = {
5351 .fattr = &fattr,
5352 .label = &label,
5353 .server = server,
5354 };
5355 struct rpc_message msg = {
5356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005357 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005358 .rpc_resp = &res,
5359 };
5360 int ret;
5361
5362 nfs_fattr_init(&fattr);
5363
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005364 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005365 if (ret)
5366 return ret;
5367 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5368 return -ENOENT;
5369 if (buflen < label.len)
5370 return -ERANGE;
5371 return 0;
5372}
5373
5374static int nfs4_get_security_label(struct inode *inode, void *buf,
5375 size_t buflen)
5376{
5377 struct nfs4_exception exception = { };
5378 int err;
5379
5380 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5381 return -EOPNOTSUPP;
5382
5383 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005384 err = _nfs4_get_security_label(inode, buf, buflen);
5385 trace_nfs4_get_security_label(inode, err);
5386 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005387 &exception);
5388 } while (exception.retry);
5389 return err;
5390}
5391
5392static int _nfs4_do_set_security_label(struct inode *inode,
5393 struct nfs4_label *ilabel,
5394 struct nfs_fattr *fattr,
5395 struct nfs4_label *olabel)
5396{
5397
5398 struct iattr sattr = {0};
5399 struct nfs_server *server = NFS_SERVER(inode);
5400 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005401 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005402 .fh = NFS_FH(inode),
5403 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005404 .server = server,
5405 .bitmask = bitmask,
5406 .label = ilabel,
5407 };
5408 struct nfs_setattrres res = {
5409 .fattr = fattr,
5410 .label = olabel,
5411 .server = server,
5412 };
5413 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5415 .rpc_argp = &arg,
5416 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005417 };
5418 int status;
5419
Jeff Layton12207f62013-11-01 10:49:32 -04005420 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005421
Jeff Layton12207f62013-11-01 10:49:32 -04005422 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005423 if (status)
5424 dprintk("%s failed: %d\n", __func__, status);
5425
5426 return status;
5427}
5428
5429static int nfs4_do_set_security_label(struct inode *inode,
5430 struct nfs4_label *ilabel,
5431 struct nfs_fattr *fattr,
5432 struct nfs4_label *olabel)
5433{
5434 struct nfs4_exception exception = { };
5435 int err;
5436
5437 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005438 err = _nfs4_do_set_security_label(inode, ilabel,
5439 fattr, olabel);
5440 trace_nfs4_set_security_label(inode, err);
5441 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005442 &exception);
5443 } while (exception.retry);
5444 return err;
5445}
5446
5447static int
Al Viro59301222016-05-27 10:19:30 -04005448nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005449{
5450 struct nfs4_label ilabel, *olabel = NULL;
5451 struct nfs_fattr fattr;
5452 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005453 int status;
5454
5455 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5456 return -EOPNOTSUPP;
5457
5458 nfs_fattr_init(&fattr);
5459
5460 ilabel.pi = 0;
5461 ilabel.lfs = 0;
5462 ilabel.label = (char *)buf;
5463 ilabel.len = buflen;
5464
5465 cred = rpc_lookup_cred();
5466 if (IS_ERR(cred))
5467 return PTR_ERR(cred);
5468
5469 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5470 if (IS_ERR(olabel)) {
5471 status = -PTR_ERR(olabel);
5472 goto out;
5473 }
5474
5475 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5476 if (status == 0)
5477 nfs_setsecurity(inode, &fattr, olabel);
5478
5479 nfs4_label_free(olabel);
5480out:
5481 put_rpccred(cred);
5482 return status;
5483}
5484#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5485
5486
Chuck Leverf0920752012-05-21 22:45:41 -04005487static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5488 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005489{
5490 __be32 verf[2];
5491
Chuck Lever2c820d92012-05-21 22:45:33 -04005492 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5493 /* An impossible timestamp guarantees this value
5494 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005495 verf[0] = cpu_to_be32(U32_MAX);
5496 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005497 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005498 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005499 u64 ns = ktime_to_ns(nn->boot_time);
5500
5501 verf[0] = cpu_to_be32(ns >> 32);
5502 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005503 }
Chuck Levercd937102012-03-02 17:14:31 -05005504 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5505}
5506
Jeff Laytona3192682015-06-09 19:43:59 -04005507static int
5508nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005509{
Jeff Laytona3192682015-06-09 19:43:59 -04005510 size_t len;
5511 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005512
Trond Myklebustceb3a162015-01-03 15:16:04 -05005513 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005514 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005515
Jeff Laytona3192682015-06-09 19:43:59 -04005516 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005517 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005518 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5519 1 +
5520 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5521 1;
5522 rcu_read_unlock();
5523
5524 if (len > NFS4_OPAQUE_LIMIT + 1)
5525 return -EINVAL;
5526
5527 /*
5528 * Since this string is allocated at mount time, and held until the
5529 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5530 * about a memory-reclaim deadlock.
5531 */
5532 str = kmalloc(len, GFP_KERNEL);
5533 if (!str)
5534 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005535
Chuck Levere984a552012-09-14 17:24:21 -04005536 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005537 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005538 clp->cl_ipaddr,
5539 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5540 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005541 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005542
Jeff Laytona3192682015-06-09 19:43:59 -04005543 clp->cl_owner_id = str;
5544 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005545}
5546
Jeff Layton873e3852015-06-09 19:44:00 -04005547static int
5548nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005549{
Jeff Layton873e3852015-06-09 19:44:00 -04005550 size_t len;
5551 char *str;
5552
5553 len = 10 + 10 + 1 + 10 + 1 +
5554 strlen(nfs4_client_id_uniquifier) + 1 +
5555 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5556
5557 if (len > NFS4_OPAQUE_LIMIT + 1)
5558 return -EINVAL;
5559
5560 /*
5561 * Since this string is allocated at mount time, and held until the
5562 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5563 * about a memory-reclaim deadlock.
5564 */
5565 str = kmalloc(len, GFP_KERNEL);
5566 if (!str)
5567 return -ENOMEM;
5568
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005569 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005570 clp->rpc_ops->version, clp->cl_minorversion,
5571 nfs4_client_id_uniquifier,
5572 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005573 clp->cl_owner_id = str;
5574 return 0;
5575}
5576
5577static int
5578nfs4_init_uniform_client_string(struct nfs_client *clp)
5579{
Jeff Layton873e3852015-06-09 19:44:00 -04005580 size_t len;
5581 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005582
5583 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005584 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005585
5586 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005587 return nfs4_init_uniquifier_client_string(clp);
5588
5589 len = 10 + 10 + 1 + 10 + 1 +
5590 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5591
5592 if (len > NFS4_OPAQUE_LIMIT + 1)
5593 return -EINVAL;
5594
5595 /*
5596 * Since this string is allocated at mount time, and held until the
5597 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5598 * about a memory-reclaim deadlock.
5599 */
5600 str = kmalloc(len, GFP_KERNEL);
5601 if (!str)
5602 return -ENOMEM;
5603
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005604 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005605 clp->rpc_ops->version, clp->cl_minorversion,
5606 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005607 clp->cl_owner_id = str;
5608 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005609}
5610
Chuck Lever706cb8d2014-03-12 12:51:47 -04005611/*
5612 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5613 * services. Advertise one based on the address family of the
5614 * clientaddr.
5615 */
5616static unsigned int
5617nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5618{
5619 if (strchr(clp->cl_ipaddr, ':') != NULL)
5620 return scnprintf(buf, len, "tcp6");
5621 else
5622 return scnprintf(buf, len, "tcp");
5623}
5624
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005625static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5626{
5627 struct nfs4_setclientid *sc = calldata;
5628
5629 if (task->tk_status == 0)
5630 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5631}
5632
5633static const struct rpc_call_ops nfs4_setclientid_ops = {
5634 .rpc_call_done = nfs4_setclientid_done,
5635};
5636
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005637/**
5638 * nfs4_proc_setclientid - Negotiate client ID
5639 * @clp: state data structure
5640 * @program: RPC program for NFSv4 callback service
5641 * @port: IP port number for NFS4 callback service
5642 * @cred: RPC credential to use for this call
5643 * @res: where to place the result
5644 *
5645 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5646 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005647int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5648 unsigned short port, struct rpc_cred *cred,
5649 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
5651 nfs4_verifier sc_verifier;
5652 struct nfs4_setclientid setclientid = {
5653 .sc_verifier = &sc_verifier,
5654 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005655 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 };
5657 struct rpc_message msg = {
5658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5659 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005660 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005661 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005663 struct rpc_task *task;
5664 struct rpc_task_setup task_setup_data = {
5665 .rpc_client = clp->cl_rpcclient,
5666 .rpc_message = &msg,
5667 .callback_ops = &nfs4_setclientid_ops,
5668 .callback_data = &setclientid,
5669 .flags = RPC_TASK_TIMEOUT,
5670 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005671 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Chuck Leverde734832012-07-11 16:30:50 -04005673 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005674 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005675
5676 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5677 status = nfs4_init_uniform_client_string(clp);
5678 else
Jeff Laytona3192682015-06-09 19:43:59 -04005679 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005680
5681 if (status)
5682 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005683
Chuck Leverde734832012-07-11 16:30:50 -04005684 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005685 setclientid.sc_netid_len =
5686 nfs4_init_callback_netid(clp,
5687 setclientid.sc_netid,
5688 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005689 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005690 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 clp->cl_ipaddr, port >> 8, port & 255);
5692
Jeff Layton3a6bb732015-06-09 19:43:57 -04005693 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005694 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005695 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005696 task = rpc_run_task(&task_setup_data);
5697 if (IS_ERR(task)) {
5698 status = PTR_ERR(task);
5699 goto out;
5700 }
5701 status = task->tk_status;
5702 if (setclientid.sc_cred) {
5703 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5704 put_rpccred(setclientid.sc_cred);
5705 }
5706 rpc_put_task(task);
5707out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005708 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005709 dprintk("NFS reply setclientid: %d\n", status);
5710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711}
5712
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005713/**
5714 * nfs4_proc_setclientid_confirm - Confirm client ID
5715 * @clp: state data structure
5716 * @res: result of a previous SETCLIENTID
5717 * @cred: RPC credential to use for this call
5718 *
5719 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5720 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005721int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005722 struct nfs4_setclientid_res *arg,
5723 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005727 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005728 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 int status;
5731
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005732 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5733 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5734 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005735 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005736 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005737 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 return status;
5739}
5740
Trond Myklebustfe650402006-01-03 09:55:18 +01005741struct nfs4_delegreturndata {
5742 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005743 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005744 struct nfs_fh fh;
5745 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005746 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005747 struct {
5748 struct nfs4_layoutreturn_args arg;
5749 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005750 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005751 u32 roc_barrier;
5752 bool roc;
5753 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005754 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005755 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005756 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005757};
5758
Trond Myklebustfe650402006-01-03 09:55:18 +01005759static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5760{
5761 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005762
Trond Myklebust14516c32010-07-31 14:29:06 -04005763 if (!nfs4_sequence_done(task, &data->res.seq_res))
5764 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005765
Trond Myklebustca8acf82013-08-13 10:36:56 -04005766 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005767
5768 /* Handle Layoutreturn errors */
5769 if (data->args.lr_args && task->tk_status != 0) {
5770 switch(data->res.lr_ret) {
5771 default:
5772 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5773 break;
5774 case 0:
5775 data->args.lr_args = NULL;
5776 data->res.lr_res = NULL;
5777 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005778 case -NFS4ERR_OLD_STATEID:
5779 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005780 data->inode))
5781 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005782 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005783 case -NFS4ERR_ADMIN_REVOKED:
5784 case -NFS4ERR_DELEG_REVOKED:
5785 case -NFS4ERR_EXPIRED:
5786 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005787 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5788 case -NFS4ERR_WRONG_CRED:
5789 data->args.lr_args = NULL;
5790 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005791 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005792 }
5793 }
5794
Ricardo Labiaga79708862009-12-07 09:23:21 -05005795 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005796 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005797 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005798 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005799 case -NFS4ERR_ADMIN_REVOKED:
5800 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005801 case -NFS4ERR_EXPIRED:
5802 nfs4_free_revoked_stateid(data->res.server,
5803 data->args.stateid,
5804 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005805 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005806 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005807 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005808 task->tk_status = 0;
5809 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005810 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005811 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5812 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005813 task->tk_status = 0;
5814 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005815 case -NFS4ERR_ACCESS:
5816 if (data->args.bitmask) {
5817 data->args.bitmask = NULL;
5818 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005819 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005820 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005821 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005822 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005823 if (nfs4_async_handle_error(task, data->res.server,
5824 NULL, NULL) == -EAGAIN) {
Trond Myklebust140087fd2017-11-06 15:28:10 -05005825 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005826 }
5827 }
5828 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005829 return;
5830lr_restart:
5831 data->res.lr_ret = 0;
5832out_restart:
5833 task->tk_status = 0;
5834 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005835}
5836
5837static void nfs4_delegreturn_release(void *calldata)
5838{
Peng Tao039b7562014-07-03 13:05:02 +08005839 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005840 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005841
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005842 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005843 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005844 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5845 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005846 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005847 nfs_iput_and_deactive(inode);
5848 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005849 kfree(calldata);
5850}
5851
Andy Adamson938e1012009-04-01 09:22:28 -04005852static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5853{
5854 struct nfs4_delegreturndata *d_data;
5855
5856 d_data = (struct nfs4_delegreturndata *)data;
5857
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005858 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005859 return;
5860
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005861 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005862 &d_data->args.seq_args,
5863 &d_data->res.seq_res,
5864 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005865}
Andy Adamson938e1012009-04-01 09:22:28 -04005866
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005867static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005868 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005869 .rpc_call_done = nfs4_delegreturn_done,
5870 .rpc_release = nfs4_delegreturn_release,
5871};
5872
Trond Myklebuste6f81072008-01-24 18:14:34 -05005873static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005874{
5875 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005876 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005877 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005878 struct rpc_message msg = {
5879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5880 .rpc_cred = cred,
5881 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005882 struct rpc_task_setup task_setup_data = {
5883 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005884 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005885 .callback_ops = &nfs4_delegreturn_ops,
5886 .flags = RPC_TASK_ASYNC,
5887 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005888 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005889
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005890 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005891 if (data == NULL)
5892 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005893 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005894
5895 nfs4_state_protect(server->nfs_client,
5896 NFS_SP4_MACH_CRED_CLEANUP,
5897 &task_setup_data.rpc_client, &msg);
5898
Trond Myklebustfe650402006-01-03 09:55:18 +01005899 data->args.fhandle = &data->fh;
5900 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005901 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005902 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005903 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005904 data->res.fattr = &data->fattr;
5905 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005906 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005907 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005908 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005909 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005910 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005911 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005912 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005913 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005914 if (data->lr.roc) {
5915 data->args.lr_args = &data->lr.arg;
5916 data->res.lr_res = &data->lr.res;
5917 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005918 } else if (data->lr.roc) {
5919 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5920 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005921 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005922
Trond Myklebustc970aa82007-07-14 15:39:59 -04005923 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005924 msg.rpc_argp = &data->args;
5925 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005926 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005927 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005928 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005929 if (!issync)
5930 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005931 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005932 if (status != 0)
5933 goto out;
5934 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005935out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005936 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005937 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938}
5939
Trond Myklebuste6f81072008-01-24 18:14:34 -05005940int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941{
5942 struct nfs_server *server = NFS_SERVER(inode);
5943 struct nfs4_exception exception = { };
5944 int err;
5945 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005946 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005947 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 switch (err) {
5949 case -NFS4ERR_STALE_STATEID:
5950 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 case 0:
5952 return 0;
5953 }
5954 err = nfs4_handle_exception(server, err, &exception);
5955 } while (exception.retry);
5956 return err;
5957}
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5960{
5961 struct inode *inode = state->inode;
5962 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005963 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005964 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005966 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005968 struct nfs_lockt_res res = {
5969 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 };
5971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005973 .rpc_argp = &arg,
5974 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 .rpc_cred = state->owner->so_cred,
5976 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 struct nfs4_lock_state *lsp;
5978 int status;
5979
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005980 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005981 status = nfs4_set_lock_state(state, request);
5982 if (status != 0)
5983 goto out;
5984 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005985 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005986 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005987 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005988 switch (status) {
5989 case 0:
5990 request->fl_type = F_UNLCK;
5991 break;
5992 case -NFS4ERR_DENIED:
5993 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005995 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005996 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005997out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 return status;
5999}
6000
6001static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6002{
6003 struct nfs4_exception exception = { };
6004 int err;
6005
6006 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006007 err = _nfs4_proc_getlk(state, cmd, request);
6008 trace_nfs4_get_lock(request, state, cmd, err);
6009 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 &exception);
6011 } while (exception.retry);
6012 return err;
6013}
6014
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006015struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006016 struct nfs_locku_args arg;
6017 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006018 struct nfs4_lock_state *lsp;
6019 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006020 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006021 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006022 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006023 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006024};
6025
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006026static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6027 struct nfs_open_context *ctx,
6028 struct nfs4_lock_state *lsp,
6029 struct nfs_seqid *seqid)
6030{
6031 struct nfs4_unlockdata *p;
6032 struct inode *inode = lsp->ls_state->inode;
6033
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006034 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006035 if (p == NULL)
6036 return NULL;
6037 p->arg.fh = NFS_FH(inode);
6038 p->arg.fl = &p->fl;
6039 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006040 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006041 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006042 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006043 /* Ensure we don't close file until we're done freeing locks! */
6044 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006045 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006046 memcpy(&p->fl, fl, sizeof(p->fl));
6047 p->server = NFS_SERVER(inode);
6048 return p;
6049}
6050
Trond Myklebust06f814a2006-01-03 09:55:07 +01006051static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006052{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006053 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006054 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006055 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006056 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006057 put_nfs_open_context(calldata->ctx);
6058 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006059}
6060
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006061static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006062{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006063 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006064
Trond Myklebust14516c32010-07-31 14:29:06 -04006065 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6066 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006067 switch (task->tk_status) {
6068 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006069 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006070 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006071 if (nfs4_update_lock_stateid(calldata->lsp,
6072 &calldata->res.stateid))
6073 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006074 case -NFS4ERR_ADMIN_REVOKED:
6075 case -NFS4ERR_EXPIRED:
6076 nfs4_free_revoked_stateid(calldata->server,
6077 &calldata->arg.stateid,
6078 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006079 case -NFS4ERR_BAD_STATEID:
6080 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006081 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006082 if (!nfs4_stateid_match(&calldata->arg.stateid,
6083 &calldata->lsp->ls_stateid))
6084 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006085 break;
6086 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006087 if (nfs4_async_handle_error(task, calldata->server,
6088 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006089 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006090 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006091 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006092}
6093
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006094static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006095{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006096 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006098 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6099 nfs_async_iocounter_wait(task, calldata->l_ctx))
6100 return;
6101
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006102 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006103 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006104 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006105 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006106 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006107 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006108 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006109 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006110 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006111 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006112 &calldata->res.seq_res,
6113 task) != 0)
6114 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006115 return;
6116out_no_action:
6117 task->tk_action = NULL;
6118out_wait:
6119 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120}
6121
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006122static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006123 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006124 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006125 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006126};
6127
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006128static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6129 struct nfs_open_context *ctx,
6130 struct nfs4_lock_state *lsp,
6131 struct nfs_seqid *seqid)
6132{
6133 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006134 struct rpc_message msg = {
6135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6136 .rpc_cred = ctx->cred,
6137 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006138 struct rpc_task_setup task_setup_data = {
6139 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006140 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006141 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006142 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006143 .flags = RPC_TASK_ASYNC,
6144 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006146 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6147 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6148
Frank Filz137d6ac2007-07-09 15:32:29 -07006149 /* Ensure this is an unlock - when canceling a lock, the
6150 * canceled lock is passed in, and it won't be an unlock.
6151 */
6152 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006153 if (fl->fl_flags & FL_CLOSE)
6154 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006155
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006156 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6157 if (data == NULL) {
6158 nfs_free_seqid(seqid);
6159 return ERR_PTR(-ENOMEM);
6160 }
6161
Chuck Levera9c92d62013-08-09 12:48:18 -04006162 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006163 msg.rpc_argp = &data->arg;
6164 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006165 task_setup_data.callback_data = data;
6166 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006167}
6168
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6170{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006171 struct inode *inode = state->inode;
6172 struct nfs4_state_owner *sp = state->owner;
6173 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006174 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006175 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006176 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006177 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006178 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006179 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
Trond Myklebust9b073572006-06-29 16:38:34 -04006181 status = nfs4_set_lock_state(state, request);
6182 /* Unlock _before_ we do the RPC call */
6183 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006184 /* Exclude nfs_delegation_claim_locks() */
6185 mutex_lock(&sp->so_delegreturn_mutex);
6186 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006187 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006188 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006189 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006190 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006191 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006192 }
6193 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006194 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006195 if (status != 0)
6196 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006197 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006198 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006199 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6200 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006201 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6202 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006203 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006204 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006205 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006206 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207 status = PTR_ERR(task);
6208 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006209 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006210 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006211 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006212out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006213 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006214 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006215 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216}
6217
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006218struct nfs4_lockdata {
6219 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006220 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006221 struct nfs4_lock_state *lsp;
6222 struct nfs_open_context *ctx;
6223 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006224 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006225 int rpc_status;
6226 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006227 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006228};
6229
6230static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006231 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6232 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006233{
6234 struct nfs4_lockdata *p;
6235 struct inode *inode = lsp->ls_state->inode;
6236 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006237 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006238
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006239 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006240 if (p == NULL)
6241 return NULL;
6242
6243 p->arg.fh = NFS_FH(inode);
6244 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006245 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006246 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006247 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006248 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6249 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006250 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006251 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006252 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006253 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006254 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006255 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006256 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006257 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006258 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006259 p->ctx = get_nfs_open_context(ctx);
6260 memcpy(&p->fl, fl, sizeof(p->fl));
6261 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006262out_free_seqid:
6263 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006264out_free:
6265 kfree(p);
6266 return NULL;
6267}
6268
6269static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6270{
6271 struct nfs4_lockdata *data = calldata;
6272 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Harvey Harrison3110ff82008-05-02 13:42:44 -07006274 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006275 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006276 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006277 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006278 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006279 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006280 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006281 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006282 nfs4_stateid_copy(&data->arg.open_stateid,
6283 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006284 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006285 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006286 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006287 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006288 nfs4_stateid_copy(&data->arg.lock_stateid,
6289 &data->lsp->ls_stateid);
6290 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006291 if (!nfs4_valid_open_stateid(state)) {
6292 data->rpc_status = -EBADF;
6293 task->tk_action = NULL;
6294 goto out_release_open_seqid;
6295 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006296 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006297 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006298 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006299 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006300 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006301 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006302out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006303 nfs_release_seqid(data->arg.open_seqid);
6304out_release_lock_seqid:
6305 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006306out_wait:
6307 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006308 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006309}
6310
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006311static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6312{
6313 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006314 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315
Harvey Harrison3110ff82008-05-02 13:42:44 -07006316 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006317
Trond Myklebust14516c32010-07-31 14:29:06 -04006318 if (!nfs4_sequence_done(task, &data->res.seq_res))
6319 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006320
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006321 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006322 switch (task->tk_status) {
6323 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006324 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006325 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006326 if (data->arg.new_lock) {
6327 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006328 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006329 rpc_restart_call_prepare(task);
6330 break;
6331 }
6332 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006333 if (data->arg.new_lock_owner != 0) {
6334 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6335 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6336 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6337 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6338 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006339 break;
6340 case -NFS4ERR_BAD_STATEID:
6341 case -NFS4ERR_OLD_STATEID:
6342 case -NFS4ERR_STALE_STATEID:
6343 case -NFS4ERR_EXPIRED:
6344 if (data->arg.new_lock_owner != 0) {
6345 if (!nfs4_stateid_match(&data->arg.open_stateid,
6346 &lsp->ls_state->open_stateid))
6347 rpc_restart_call_prepare(task);
6348 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6349 &lsp->ls_stateid))
6350 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006352 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006353}
6354
6355static void nfs4_lock_release(void *calldata)
6356{
6357 struct nfs4_lockdata *data = calldata;
6358
Harvey Harrison3110ff82008-05-02 13:42:44 -07006359 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006360 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006361 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006362 struct rpc_task *task;
6363 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6364 data->arg.lock_seqid);
6365 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006366 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006367 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006368 } else
6369 nfs_free_seqid(data->arg.lock_seqid);
6370 nfs4_put_lock_state(data->lsp);
6371 put_nfs_open_context(data->ctx);
6372 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006373 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006374}
6375
6376static const struct rpc_call_ops nfs4_lock_ops = {
6377 .rpc_call_prepare = nfs4_lock_prepare,
6378 .rpc_call_done = nfs4_lock_done,
6379 .rpc_release = nfs4_lock_release,
6380};
6381
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006382static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6383{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006384 switch (error) {
6385 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006386 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006387 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006388 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006389 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006390 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006391 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006392 break;
6393 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006394 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006395 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006396 };
6397}
6398
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006399static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006400{
6401 struct nfs4_lockdata *data;
6402 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006403 struct rpc_message msg = {
6404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6405 .rpc_cred = state->owner->so_cred,
6406 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006407 struct rpc_task_setup task_setup_data = {
6408 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006409 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006410 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006411 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006412 .flags = RPC_TASK_ASYNC,
6413 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006414 int ret;
6415
Harvey Harrison3110ff82008-05-02 13:42:44 -07006416 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006417 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006418 fl->fl_u.nfs4_fl.owner,
6419 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006420 if (data == NULL)
6421 return -ENOMEM;
6422 if (IS_SETLKW(cmd))
6423 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006424 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006425 msg.rpc_argp = &data->arg;
6426 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006427 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006428 if (recovery_type > NFS_LOCK_NEW) {
6429 if (recovery_type == NFS_LOCK_RECLAIM)
6430 data->arg.reclaim = NFS_LOCK_RECLAIM;
6431 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006432 } else
6433 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006434 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006435 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006436 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006437 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438 if (ret == 0) {
6439 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006440 if (ret)
6441 nfs4_handle_setlk_error(data->server, data->lsp,
6442 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006443 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006444 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006445 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006446 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006447 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449}
6450
6451static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6452{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006453 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006454 struct nfs4_exception exception = {
6455 .inode = state->inode,
6456 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006457 int err;
6458
6459 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006460 /* Cache the lock if possible... */
6461 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6462 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006463 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006464 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006465 break;
6466 nfs4_handle_exception(server, err, &exception);
6467 } while (exception.retry);
6468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469}
6470
6471static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6472{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006473 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006474 struct nfs4_exception exception = {
6475 .inode = state->inode,
6476 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006477 int err;
6478
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006479 err = nfs4_set_lock_state(state, request);
6480 if (err != 0)
6481 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006482 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006483 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6484 return 0;
6485 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006486 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006487 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6488 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006489 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006490 switch (err) {
6491 default:
6492 goto out;
6493 case -NFS4ERR_GRACE:
6494 case -NFS4ERR_DELAY:
6495 nfs4_handle_exception(server, err, &exception);
6496 err = 0;
6497 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006498 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006499out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501}
6502
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006503#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006504static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6505{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006506 struct nfs4_lock_state *lsp;
6507 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006508
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006509 status = nfs4_set_lock_state(state, request);
6510 if (status != 0)
6511 return status;
6512 lsp = request->fl_u.nfs4_fl.owner;
6513 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6514 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6515 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006516 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006517}
6518#endif
6519
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6521{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006522 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006523 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006524 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006525 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
Trond Myklebust01c3b862006-06-29 16:38:39 -04006527 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006528 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006529 if (status < 0)
6530 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006531 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006532 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006533 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006534 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006535 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006536 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006537 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006538 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006539 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006540 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006541 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006542 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006543 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006544 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006545out:
6546 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 return status;
6548}
6549
6550static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6551{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006552 struct nfs4_exception exception = {
6553 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006554 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006555 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 int err;
6557
6558 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006559 err = _nfs4_proc_setlk(state, cmd, request);
6560 if (err == -NFS4ERR_DENIED)
6561 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006563 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 } while (exception.retry);
6565 return err;
6566}
6567
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006568#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6569#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6570
6571static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006572nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6573 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006574{
6575 int status = -ERESTARTSYS;
6576 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6577
6578 while(!signalled()) {
6579 status = nfs4_proc_setlk(state, cmd, request);
6580 if ((status != -EAGAIN) || IS_SETLK(cmd))
6581 break;
6582 freezable_schedule_timeout_interruptible(timeout);
6583 timeout *= 2;
6584 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6585 status = -ERESTARTSYS;
6586 }
6587 return status;
6588}
6589
Jeff Laytona1d617d82016-09-17 18:17:39 -04006590#ifdef CONFIG_NFS_V4_1
6591struct nfs4_lock_waiter {
6592 struct task_struct *task;
6593 struct inode *inode;
6594 struct nfs_lowner *owner;
6595 bool notified;
6596};
6597
6598static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006599nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006600{
6601 int ret;
6602 struct cb_notify_lock_args *cbnl = key;
6603 struct nfs4_lock_waiter *waiter = wait->private;
6604 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6605 *wowner = waiter->owner;
6606
6607 /* Only wake if the callback was for the same owner */
6608 if (lowner->clientid != wowner->clientid ||
6609 lowner->id != wowner->id ||
6610 lowner->s_dev != wowner->s_dev)
6611 return 0;
6612
6613 /* Make sure it's for the right inode */
6614 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6615 return 0;
6616
6617 waiter->notified = true;
6618
6619 /* override "private" so we can use default_wake_function */
6620 wait->private = waiter->task;
6621 ret = autoremove_wake_function(wait, mode, flags, key);
6622 wait->private = waiter;
6623 return ret;
6624}
6625
6626static int
6627nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6628{
6629 int status = -ERESTARTSYS;
6630 unsigned long flags;
6631 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6632 struct nfs_server *server = NFS_SERVER(state->inode);
6633 struct nfs_client *clp = server->nfs_client;
6634 wait_queue_head_t *q = &clp->cl_lock_waitq;
6635 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6636 .id = lsp->ls_seqid.owner_id,
6637 .s_dev = server->s_dev };
6638 struct nfs4_lock_waiter waiter = { .task = current,
6639 .inode = state->inode,
6640 .owner = &owner,
6641 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006642 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006643
6644 /* Don't bother with waitqueue if we don't expect a callback */
6645 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6646 return nfs4_retry_setlk_simple(state, cmd, request);
6647
6648 init_wait(&wait);
6649 wait.private = &waiter;
6650 wait.func = nfs4_wake_lock_waiter;
6651 add_wait_queue(q, &wait);
6652
6653 while(!signalled()) {
6654 status = nfs4_proc_setlk(state, cmd, request);
6655 if ((status != -EAGAIN) || IS_SETLK(cmd))
6656 break;
6657
6658 status = -ERESTARTSYS;
6659 spin_lock_irqsave(&q->lock, flags);
6660 if (waiter.notified) {
6661 spin_unlock_irqrestore(&q->lock, flags);
6662 continue;
6663 }
6664 set_current_state(TASK_INTERRUPTIBLE);
6665 spin_unlock_irqrestore(&q->lock, flags);
6666
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006667 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006668 }
6669
6670 finish_wait(q, &wait);
6671 return status;
6672}
6673#else /* !CONFIG_NFS_V4_1 */
6674static inline int
6675nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6676{
6677 return nfs4_retry_setlk_simple(state, cmd, request);
6678}
6679#endif
6680
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681static int
6682nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6683{
6684 struct nfs_open_context *ctx;
6685 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 int status;
6687
6688 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006689 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 state = ctx->state;
6691
Trond Myklebustd9531262009-07-21 19:22:38 -04006692 if (IS_GETLK(cmd)) {
6693 if (state != NULL)
6694 return nfs4_proc_getlk(state, F_GETLK, request);
6695 return 0;
6696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697
6698 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6699 return -EINVAL;
6700
Trond Myklebustd9531262009-07-21 19:22:38 -04006701 if (request->fl_type == F_UNLCK) {
6702 if (state != NULL)
6703 return nfs4_proc_unlck(state, cmd, request);
6704 return 0;
6705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706
Trond Myklebustd9531262009-07-21 19:22:38 -04006707 if (state == NULL)
6708 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006709
6710 if ((request->fl_flags & FL_POSIX) &&
6711 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6712 return -ENOLCK;
6713
Jeff Layton1ea67db2016-09-17 18:17:37 -04006714 status = nfs4_set_lock_state(state, request);
6715 if (status != 0)
6716 return status;
6717
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006718 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719}
6720
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006721int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006722{
6723 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006724 int err;
6725
6726 err = nfs4_set_lock_state(state, fl);
6727 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006728 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006729 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006730 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006731}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006732
Trond Myklebustcf470c32012-03-07 13:49:12 -05006733struct nfs_release_lockowner_data {
6734 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006735 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006736 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006737 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006738 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006739};
6740
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006741static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6742{
6743 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006744 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006745 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6746 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006747 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006748 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006749}
6750
6751static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6752{
6753 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006754 struct nfs_server *server = data->server;
6755
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006756 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006757
6758 switch (task->tk_status) {
6759 case 0:
6760 renew_lease(server, data->timestamp);
6761 break;
6762 case -NFS4ERR_STALE_CLIENTID:
6763 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006764 nfs4_schedule_lease_recovery(server->nfs_client);
6765 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006766 case -NFS4ERR_LEASE_MOVED:
6767 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006768 if (nfs4_async_handle_error(task, server,
6769 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006770 rpc_restart_call_prepare(task);
6771 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006772}
6773
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006774static void nfs4_release_lockowner_release(void *calldata)
6775{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006776 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006777 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006778 kfree(calldata);
6779}
6780
Trond Myklebust17280172012-03-11 13:11:00 -04006781static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006782 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6783 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006784 .rpc_release = nfs4_release_lockowner_release,
6785};
6786
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006787static void
6788nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006789{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006790 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006791 struct rpc_message msg = {
6792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6793 };
6794
6795 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006796 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006797
Trond Myklebustcf470c32012-03-07 13:49:12 -05006798 data = kmalloc(sizeof(*data), GFP_NOFS);
6799 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006800 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006801 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006802 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006803 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6804 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6805 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006806
Trond Myklebustcf470c32012-03-07 13:49:12 -05006807 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006808 msg.rpc_resp = &data->res;
6809 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006810 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006811}
6812
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006813#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6814
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006815static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006816 struct dentry *unused, struct inode *inode,
6817 const char *key, const void *buf,
6818 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006819{
Al Viro59301222016-05-27 10:19:30 -04006820 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006821}
6822
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006823static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006824 struct dentry *unused, struct inode *inode,
6825 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006826{
Al Virob2968212016-04-10 20:48:24 -04006827 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006828}
6829
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006830static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006831{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006832 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006833}
6834
David Quigleyc9bccef2013-05-22 12:50:45 -04006835#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006836
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006837static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006838 struct dentry *unused, struct inode *inode,
6839 const char *key, const void *buf,
6840 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006841{
6842 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006843 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006844
6845 return -EOPNOTSUPP;
6846}
6847
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006848static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006849 struct dentry *unused, struct inode *inode,
6850 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006851{
6852 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006853 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006854 return -EOPNOTSUPP;
6855}
6856
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006857static ssize_t
6858nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006859{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006860 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006861
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006862 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6863 len = security_inode_listsecurity(inode, list, list_len);
6864 if (list_len && len > list_len)
6865 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006866 }
6867 return len;
6868}
6869
6870static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6871 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006872 .get = nfs4_xattr_get_nfs4_label,
6873 .set = nfs4_xattr_set_nfs4_label,
6874};
David Quigleyc9bccef2013-05-22 12:50:45 -04006875
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006876#else
6877
6878static ssize_t
6879nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6880{
6881 return 0;
6882}
6883
6884#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006885
Andy Adamson533eb462011-06-13 18:25:56 -04006886/*
6887 * nfs_fhget will use either the mounted_on_fileid or the fileid
6888 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006889static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6890{
Andy Adamson533eb462011-06-13 18:25:56 -04006891 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6892 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6893 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006894 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006895 return;
6896
6897 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006898 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006899 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6900 fattr->nlink = 2;
6901}
6902
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006903static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6904 const struct qstr *name,
6905 struct nfs4_fs_locations *fs_locations,
6906 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006907{
6908 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006909 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006910 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006911 };
6912 struct nfs4_fs_locations_arg args = {
6913 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006914 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006915 .page = page,
6916 .bitmask = bitmask,
6917 };
Benny Halevy22958462009-04-01 09:22:02 -04006918 struct nfs4_fs_locations_res res = {
6919 .fs_locations = fs_locations,
6920 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006921 struct rpc_message msg = {
6922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6923 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006924 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006925 };
6926 int status;
6927
Harvey Harrison3110ff82008-05-02 13:42:44 -07006928 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006929
6930 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6931 * is not supported */
6932 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6933 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6934 else
6935 bitmask[0] |= FATTR4_WORD0_FILEID;
6936
Trond Myklebustc228fd32007-01-13 02:28:11 -05006937 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006938 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006939 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006940 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006941 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006942 return status;
6943}
6944
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006945int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6946 const struct qstr *name,
6947 struct nfs4_fs_locations *fs_locations,
6948 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006949{
6950 struct nfs4_exception exception = { };
6951 int err;
6952 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006953 err = _nfs4_proc_fs_locations(client, dir, name,
6954 fs_locations, page);
6955 trace_nfs4_get_fs_locations(dir, name, err);
6956 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006957 &exception);
6958 } while (exception.retry);
6959 return err;
6960}
6961
Chuck Leverb03d7352013-10-17 14:12:50 -04006962/*
6963 * This operation also signals the server that this client is
6964 * performing migration recovery. The server can stop returning
6965 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6966 * appended to this compound to identify the client ID which is
6967 * performing recovery.
6968 */
6969static int _nfs40_proc_get_locations(struct inode *inode,
6970 struct nfs4_fs_locations *locations,
6971 struct page *page, struct rpc_cred *cred)
6972{
6973 struct nfs_server *server = NFS_SERVER(inode);
6974 struct rpc_clnt *clnt = server->client;
6975 u32 bitmask[2] = {
6976 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6977 };
6978 struct nfs4_fs_locations_arg args = {
6979 .clientid = server->nfs_client->cl_clientid,
6980 .fh = NFS_FH(inode),
6981 .page = page,
6982 .bitmask = bitmask,
6983 .migration = 1, /* skip LOOKUP */
6984 .renew = 1, /* append RENEW */
6985 };
6986 struct nfs4_fs_locations_res res = {
6987 .fs_locations = locations,
6988 .migration = 1,
6989 .renew = 1,
6990 };
6991 struct rpc_message msg = {
6992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6993 .rpc_argp = &args,
6994 .rpc_resp = &res,
6995 .rpc_cred = cred,
6996 };
6997 unsigned long now = jiffies;
6998 int status;
6999
7000 nfs_fattr_init(&locations->fattr);
7001 locations->server = server;
7002 locations->nlocations = 0;
7003
7004 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7005 nfs4_set_sequence_privileged(&args.seq_args);
7006 status = nfs4_call_sync_sequence(clnt, server, &msg,
7007 &args.seq_args, &res.seq_res);
7008 if (status)
7009 return status;
7010
7011 renew_lease(server, now);
7012 return 0;
7013}
7014
7015#ifdef CONFIG_NFS_V4_1
7016
7017/*
7018 * This operation also signals the server that this client is
7019 * performing migration recovery. The server can stop asserting
7020 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7021 * performing this operation is identified in the SEQUENCE
7022 * operation in this compound.
7023 *
7024 * When the client supports GETATTR(fs_locations_info), it can
7025 * be plumbed in here.
7026 */
7027static int _nfs41_proc_get_locations(struct inode *inode,
7028 struct nfs4_fs_locations *locations,
7029 struct page *page, struct rpc_cred *cred)
7030{
7031 struct nfs_server *server = NFS_SERVER(inode);
7032 struct rpc_clnt *clnt = server->client;
7033 u32 bitmask[2] = {
7034 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7035 };
7036 struct nfs4_fs_locations_arg args = {
7037 .fh = NFS_FH(inode),
7038 .page = page,
7039 .bitmask = bitmask,
7040 .migration = 1, /* skip LOOKUP */
7041 };
7042 struct nfs4_fs_locations_res res = {
7043 .fs_locations = locations,
7044 .migration = 1,
7045 };
7046 struct rpc_message msg = {
7047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7048 .rpc_argp = &args,
7049 .rpc_resp = &res,
7050 .rpc_cred = cred,
7051 };
7052 int status;
7053
7054 nfs_fattr_init(&locations->fattr);
7055 locations->server = server;
7056 locations->nlocations = 0;
7057
7058 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7059 nfs4_set_sequence_privileged(&args.seq_args);
7060 status = nfs4_call_sync_sequence(clnt, server, &msg,
7061 &args.seq_args, &res.seq_res);
7062 if (status == NFS4_OK &&
7063 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7064 status = -NFS4ERR_LEASE_MOVED;
7065 return status;
7066}
7067
7068#endif /* CONFIG_NFS_V4_1 */
7069
7070/**
7071 * nfs4_proc_get_locations - discover locations for a migrated FSID
7072 * @inode: inode on FSID that is migrating
7073 * @locations: result of query
7074 * @page: buffer
7075 * @cred: credential to use for this operation
7076 *
7077 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7078 * operation failed, or a negative errno if a local error occurred.
7079 *
7080 * On success, "locations" is filled in, but if the server has
7081 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7082 * asserted.
7083 *
7084 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7085 * from this client that require migration recovery.
7086 */
7087int nfs4_proc_get_locations(struct inode *inode,
7088 struct nfs4_fs_locations *locations,
7089 struct page *page, struct rpc_cred *cred)
7090{
7091 struct nfs_server *server = NFS_SERVER(inode);
7092 struct nfs_client *clp = server->nfs_client;
7093 const struct nfs4_mig_recovery_ops *ops =
7094 clp->cl_mvops->mig_recovery_ops;
7095 struct nfs4_exception exception = { };
7096 int status;
7097
7098 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7099 (unsigned long long)server->fsid.major,
7100 (unsigned long long)server->fsid.minor,
7101 clp->cl_hostname);
7102 nfs_display_fhandle(NFS_FH(inode), __func__);
7103
7104 do {
7105 status = ops->get_locations(inode, locations, page, cred);
7106 if (status != -NFS4ERR_DELAY)
7107 break;
7108 nfs4_handle_exception(server, status, &exception);
7109 } while (exception.retry);
7110 return status;
7111}
7112
Chuck Lever44c99932013-10-17 14:13:30 -04007113/*
7114 * This operation also signals the server that this client is
7115 * performing "lease moved" recovery. The server can stop
7116 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7117 * is appended to this compound to identify the client ID which is
7118 * performing recovery.
7119 */
7120static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7121{
7122 struct nfs_server *server = NFS_SERVER(inode);
7123 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7124 struct rpc_clnt *clnt = server->client;
7125 struct nfs4_fsid_present_arg args = {
7126 .fh = NFS_FH(inode),
7127 .clientid = clp->cl_clientid,
7128 .renew = 1, /* append RENEW */
7129 };
7130 struct nfs4_fsid_present_res res = {
7131 .renew = 1,
7132 };
7133 struct rpc_message msg = {
7134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7135 .rpc_argp = &args,
7136 .rpc_resp = &res,
7137 .rpc_cred = cred,
7138 };
7139 unsigned long now = jiffies;
7140 int status;
7141
7142 res.fh = nfs_alloc_fhandle();
7143 if (res.fh == NULL)
7144 return -ENOMEM;
7145
7146 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7147 nfs4_set_sequence_privileged(&args.seq_args);
7148 status = nfs4_call_sync_sequence(clnt, server, &msg,
7149 &args.seq_args, &res.seq_res);
7150 nfs_free_fhandle(res.fh);
7151 if (status)
7152 return status;
7153
7154 do_renew_lease(clp, now);
7155 return 0;
7156}
7157
7158#ifdef CONFIG_NFS_V4_1
7159
7160/*
7161 * This operation also signals the server that this client is
7162 * performing "lease moved" recovery. The server can stop asserting
7163 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7164 * this operation is identified in the SEQUENCE operation in this
7165 * compound.
7166 */
7167static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7168{
7169 struct nfs_server *server = NFS_SERVER(inode);
7170 struct rpc_clnt *clnt = server->client;
7171 struct nfs4_fsid_present_arg args = {
7172 .fh = NFS_FH(inode),
7173 };
7174 struct nfs4_fsid_present_res res = {
7175 };
7176 struct rpc_message msg = {
7177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7178 .rpc_argp = &args,
7179 .rpc_resp = &res,
7180 .rpc_cred = cred,
7181 };
7182 int status;
7183
7184 res.fh = nfs_alloc_fhandle();
7185 if (res.fh == NULL)
7186 return -ENOMEM;
7187
7188 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7189 nfs4_set_sequence_privileged(&args.seq_args);
7190 status = nfs4_call_sync_sequence(clnt, server, &msg,
7191 &args.seq_args, &res.seq_res);
7192 nfs_free_fhandle(res.fh);
7193 if (status == NFS4_OK &&
7194 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7195 status = -NFS4ERR_LEASE_MOVED;
7196 return status;
7197}
7198
7199#endif /* CONFIG_NFS_V4_1 */
7200
7201/**
7202 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7203 * @inode: inode on FSID to check
7204 * @cred: credential to use for this operation
7205 *
7206 * Server indicates whether the FSID is present, moved, or not
7207 * recognized. This operation is necessary to clear a LEASE_MOVED
7208 * condition for this client ID.
7209 *
7210 * Returns NFS4_OK if the FSID is present on this server,
7211 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7212 * NFS4ERR code if some error occurred on the server, or a
7213 * negative errno if a local failure occurred.
7214 */
7215int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7216{
7217 struct nfs_server *server = NFS_SERVER(inode);
7218 struct nfs_client *clp = server->nfs_client;
7219 const struct nfs4_mig_recovery_ops *ops =
7220 clp->cl_mvops->mig_recovery_ops;
7221 struct nfs4_exception exception = { };
7222 int status;
7223
7224 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7225 (unsigned long long)server->fsid.major,
7226 (unsigned long long)server->fsid.minor,
7227 clp->cl_hostname);
7228 nfs_display_fhandle(NFS_FH(inode), __func__);
7229
7230 do {
7231 status = ops->fsid_present(inode, cred);
7232 if (status != -NFS4ERR_DELAY)
7233 break;
7234 nfs4_handle_exception(server, status, &exception);
7235 } while (exception.retry);
7236 return status;
7237}
7238
Andy Adamson5ec16a82013-08-08 10:57:55 -04007239/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007240 * If 'use_integrity' is true and the state managment nfs_client
7241 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7242 * and the machine credential as per RFC3530bis and RFC5661 Security
7243 * Considerations sections. Otherwise, just use the user cred with the
7244 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007245 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007246static 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 +00007247{
7248 int status;
7249 struct nfs4_secinfo_arg args = {
7250 .dir_fh = NFS_FH(dir),
7251 .name = name,
7252 };
7253 struct nfs4_secinfo_res res = {
7254 .flavors = flavors,
7255 };
7256 struct rpc_message msg = {
7257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7258 .rpc_argp = &args,
7259 .rpc_resp = &res,
7260 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007261 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007262 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007263
7264 if (use_integrity) {
7265 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007266 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7267 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007268 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007269
7270 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007271
7272 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7273 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7274
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007275 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7276 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007277 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007278
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007279 if (cred)
7280 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007281
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007282 return status;
7283}
7284
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007285int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7286 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007287{
7288 struct nfs4_exception exception = { };
7289 int err;
7290 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007291 err = -NFS4ERR_WRONGSEC;
7292
7293 /* try to use integrity protection with machine cred */
7294 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7295 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7296
7297 /*
7298 * if unable to use integrity protection, or SECINFO with
7299 * integrity protection returns NFS4ERR_WRONGSEC (which is
7300 * disallowed by spec, but exists in deployed servers) use
7301 * the current filesystem's rpc_client and the user cred.
7302 */
7303 if (err == -NFS4ERR_WRONGSEC)
7304 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7305
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007306 trace_nfs4_secinfo(dir, name, err);
7307 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007308 &exception);
7309 } while (exception.retry);
7310 return err;
7311}
7312
Andy Adamson557134a2009-04-01 09:21:53 -04007313#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007314/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007315 * Check the exchange flags returned by the server for invalid flags, having
7316 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7317 * DS flags set.
7318 */
7319static int nfs4_check_cl_exchange_flags(u32 flags)
7320{
7321 if (flags & ~EXCHGID4_FLAG_MASK_R)
7322 goto out_inval;
7323 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7324 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7325 goto out_inval;
7326 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7327 goto out_inval;
7328 return NFS_OK;
7329out_inval:
7330 return -NFS4ERR_INVAL;
7331}
7332
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007333static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007334nfs41_same_server_scope(struct nfs41_server_scope *a,
7335 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007336{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007337 if (a->server_scope_sz != b->server_scope_sz)
7338 return false;
7339 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007340}
7341
Andy Adamson02a95de2016-02-05 16:08:37 -05007342static void
7343nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7344{
7345}
7346
7347static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7348 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7349};
7350
Andy Adamson357f54d2010-12-14 10:11:57 -05007351/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007352 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007353 *
7354 * The 4.1 client currently uses the same TCP connection for the
7355 * fore and backchannel.
7356 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007357static
7358int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7359 struct rpc_xprt *xprt,
7360 struct nfs_client *clp,
7361 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007362{
7363 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007364 struct nfs41_bind_conn_to_session_args args = {
7365 .client = clp,
7366 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7367 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007368 struct nfs41_bind_conn_to_session_res res;
7369 struct rpc_message msg = {
7370 .rpc_proc =
7371 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007372 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007373 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007374 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007375 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007376 struct rpc_task_setup task_setup_data = {
7377 .rpc_client = clnt,
7378 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007379 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007380 .rpc_message = &msg,
7381 .flags = RPC_TASK_TIMEOUT,
7382 };
7383 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007384
Trond Myklebust71a097c2015-02-18 09:27:18 -08007385 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7386 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7387 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007388
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007389 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7390 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7391 args.dir = NFS4_CDFC4_FORE;
7392
7393 task = rpc_run_task(&task_setup_data);
7394 if (!IS_ERR(task)) {
7395 status = task->tk_status;
7396 rpc_put_task(task);
7397 } else
7398 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007399 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007400 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007401 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007402 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7403 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007404 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007405 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007406 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007407 dprintk("NFS: %s: Unexpected direction from server\n",
7408 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007409 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007410 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007411 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007412 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7413 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007414 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007415 }
7416 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007417
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007418 return status;
7419}
7420
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007421struct rpc_bind_conn_calldata {
7422 struct nfs_client *clp;
7423 struct rpc_cred *cred;
7424};
7425
7426static int
7427nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7428 struct rpc_xprt *xprt,
7429 void *calldata)
7430{
7431 struct rpc_bind_conn_calldata *p = calldata;
7432
7433 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7434}
7435
7436int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7437{
7438 struct rpc_bind_conn_calldata data = {
7439 .clp = clp,
7440 .cred = cred,
7441 };
7442 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7443 nfs4_proc_bind_conn_to_session_callback, &data);
7444}
7445
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007446/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007447 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7448 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007449 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007450static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7451 .how = SP4_MACH_CRED,
7452 .enforce.u.words = {
7453 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7454 1 << (OP_EXCHANGE_ID - 32) |
7455 1 << (OP_CREATE_SESSION - 32) |
7456 1 << (OP_DESTROY_SESSION - 32) |
7457 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007458 },
7459 .allow.u.words = {
7460 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007461 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007462 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007463 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007464 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007465 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007466 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007467 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007468 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007469 1 << (OP_FREE_STATEID - 32) |
7470 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007471 }
7472};
7473
7474/*
7475 * Select the state protection mode for client `clp' given the server results
7476 * from exchange_id in `sp'.
7477 *
7478 * Returns 0 on success, negative errno otherwise.
7479 */
7480static int nfs4_sp4_select_mode(struct nfs_client *clp,
7481 struct nfs41_state_protection *sp)
7482{
7483 static const u32 supported_enforce[NFS4_OP_MAP_NUM_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)
7489 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007490 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007491 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007492 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007493
7494 if (sp->how == SP4_MACH_CRED) {
7495 /* Print state protect result */
7496 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7497 for (i = 0; i <= LAST_NFS4_OP; i++) {
7498 if (test_bit(i, sp->enforce.u.longs))
7499 dfprintk(MOUNT, " enforce op %d\n", i);
7500 if (test_bit(i, sp->allow.u.longs))
7501 dfprintk(MOUNT, " allow op %d\n", i);
7502 }
7503
7504 /* make sure nothing is on enforce list that isn't supported */
7505 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7506 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7507 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007508 ret = -EINVAL;
7509 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007510 }
7511 }
7512
7513 /*
7514 * Minimal mode - state operations are allowed to use machine
7515 * credential. Note this already happens by default, so the
7516 * client doesn't have to do anything more than the negotiation.
7517 *
7518 * NOTE: we don't care if EXCHANGE_ID is in the list -
7519 * we're already using the machine cred for exchange_id
7520 * and will never use a different cred.
7521 */
7522 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7523 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7524 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7525 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7526 dfprintk(MOUNT, "sp4_mach_cred:\n");
7527 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007528 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007529 } else {
7530 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007531 ret = -EINVAL;
7532 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007533 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007534
7535 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007536 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7537 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007538 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7539 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007540 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007541 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007542
Andrew Elble99ade3c2015-12-02 09:39:51 -05007543 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7544 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007545 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007546 }
7547
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007548 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7549 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7550 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007551 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007552 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007553
7554 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7555 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7556 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007557 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007558 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007559
7560 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7561 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007562 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007563 }
7564
7565 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7566 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007567 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007568 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007569 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007570out:
7571 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007572 return 0;
7573}
7574
Andy Adamson8d89bd72016-09-09 09:22:18 -04007575struct nfs41_exchange_id_data {
7576 struct nfs41_exchange_id_res res;
7577 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007578};
7579
Andy Adamson8d89bd72016-09-09 09:22:18 -04007580static void nfs4_exchange_id_release(void *data)
7581{
7582 struct nfs41_exchange_id_data *cdata =
7583 (struct nfs41_exchange_id_data *)data;
7584
Olga Kornievskaia63513232017-03-13 10:36:19 -04007585 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007586 kfree(cdata->res.impl_id);
7587 kfree(cdata->res.server_scope);
7588 kfree(cdata->res.server_owner);
7589 kfree(cdata);
7590}
7591
7592static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007593 .rpc_release = nfs4_exchange_id_release,
7594};
7595
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007596/*
7597 * _nfs4_proc_exchange_id()
7598 *
7599 * Wrapper for EXCHANGE_ID operation.
7600 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007601static struct rpc_task *
7602nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007603 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007604{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007605 struct rpc_message msg = {
7606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007607 .rpc_cred = cred,
7608 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007609 struct rpc_task_setup task_setup_data = {
7610 .rpc_client = clp->cl_rpcclient,
7611 .callback_ops = &nfs4_exchange_id_call_ops,
7612 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007613 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007614 };
7615 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007616 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007617
Elena Reshetova212bf412017-10-20 12:53:38 +03007618 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007619 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007620
Trond Myklebust9c760d12017-07-31 18:38:50 -04007621 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007622 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007623 if (!calldata)
7624 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007625
Trond Myklebustfd405592017-08-01 16:02:47 -04007626 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007627
7628 status = nfs4_init_uniform_client_string(clp);
7629 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007631
Andy Adamson8d89bd72016-09-09 09:22:18 -04007632 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7633 GFP_NOFS);
7634 status = -ENOMEM;
7635 if (unlikely(calldata->res.server_owner == NULL))
7636 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007637
Andy Adamson8d89bd72016-09-09 09:22:18 -04007638 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007639 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007640 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007641 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007642
Andy Adamson8d89bd72016-09-09 09:22:18 -04007643 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7644 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007645 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007646
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007647 switch (sp4_how) {
7648 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007649 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007650 break;
7651
7652 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007653 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007654 break;
7655
7656 default:
7657 /* unsupported! */
7658 WARN_ON_ONCE(1);
7659 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007660 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007661 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007662 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007663 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007664 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007665 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7666 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007667 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007668 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007669 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7670 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007671#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007672 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007673#endif
7674 msg.rpc_argp = &calldata->args;
7675 msg.rpc_resp = &calldata->res;
7676 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007677
Trond Myklebust9c760d12017-07-31 18:38:50 -04007678 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007679
7680out_impl_id:
7681 kfree(calldata->res.impl_id);
7682out_server_scope:
7683 kfree(calldata->res.server_scope);
7684out_server_owner:
7685 kfree(calldata->res.server_owner);
7686out_calldata:
7687 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007688out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007689 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007690 return ERR_PTR(status);
7691}
7692
7693/*
7694 * _nfs4_proc_exchange_id()
7695 *
7696 * Wrapper for EXCHANGE_ID operation.
7697 */
7698static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7699 u32 sp4_how)
7700{
7701 struct rpc_task *task;
7702 struct nfs41_exchange_id_args *argp;
7703 struct nfs41_exchange_id_res *resp;
7704 int status;
7705
7706 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7707 if (IS_ERR(task))
7708 return PTR_ERR(task);
7709
7710 argp = task->tk_msg.rpc_argp;
7711 resp = task->tk_msg.rpc_resp;
7712 status = task->tk_status;
7713 if (status != 0)
7714 goto out;
7715
7716 status = nfs4_check_cl_exchange_flags(resp->flags);
7717 if (status != 0)
7718 goto out;
7719
7720 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7721 if (status != 0)
7722 goto out;
7723
7724 clp->cl_clientid = resp->clientid;
7725 clp->cl_exchange_flags = resp->flags;
7726 clp->cl_seqid = resp->seqid;
7727 /* Client ID is not confirmed */
7728 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7729 clear_bit(NFS4_SESSION_ESTABLISHED,
7730 &clp->cl_session->session_state);
7731
7732 if (clp->cl_serverscope != NULL &&
7733 !nfs41_same_server_scope(clp->cl_serverscope,
7734 resp->server_scope)) {
7735 dprintk("%s: server_scope mismatch detected\n",
7736 __func__);
7737 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7738 }
7739
7740 swap(clp->cl_serverowner, resp->server_owner);
7741 swap(clp->cl_serverscope, resp->server_scope);
7742 swap(clp->cl_implid, resp->impl_id);
7743
7744 /* Save the EXCHANGE_ID verifier session trunk tests */
7745 memcpy(clp->cl_confirm.data, argp->verifier.data,
7746 sizeof(clp->cl_confirm.data));
7747out:
7748 trace_nfs4_exchange_id(clp, status);
7749 rpc_put_task(task);
7750 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007751}
7752
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007753/*
7754 * nfs4_proc_exchange_id()
7755 *
7756 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7757 *
7758 * Since the clientid has expired, all compounds using sessions
7759 * associated with the stale clientid will be returning
7760 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7761 * be in some phase of session reset.
7762 *
7763 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7764 */
7765int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7766{
7767 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7768 int status;
7769
7770 /* try SP4_MACH_CRED if krb5i/p */
7771 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7772 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007773 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007774 if (!status)
7775 return 0;
7776 }
7777
7778 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007779 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007780}
7781
Andy Adamson04fa2c62016-09-09 09:22:29 -04007782/**
7783 * nfs4_test_session_trunk
7784 *
7785 * This is an add_xprt_test() test function called from
7786 * rpc_clnt_setup_test_and_add_xprt.
7787 *
7788 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7789 * and is dereferrenced in nfs4_exchange_id_release
7790 *
7791 * Upon success, add the new transport to the rpc_clnt
7792 *
7793 * @clnt: struct rpc_clnt to get new transport
7794 * @xprt: the rpc_xprt to test
7795 * @data: call data for _nfs4_proc_exchange_id.
7796 */
7797int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7798 void *data)
7799{
7800 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007801 struct rpc_task *task;
7802 int status;
7803
Andy Adamson04fa2c62016-09-09 09:22:29 -04007804 u32 sp4_how;
7805
7806 dprintk("--> %s try %s\n", __func__,
7807 xprt->address_strings[RPC_DISPLAY_ADDR]);
7808
7809 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7810
7811 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007812 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7813 if (IS_ERR(task))
7814 return PTR_ERR(task);
7815
7816 status = task->tk_status;
7817 if (status == 0)
7818 status = nfs4_detect_session_trunking(adata->clp,
7819 task->tk_msg.rpc_resp, xprt);
7820
7821 rpc_put_task(task);
7822 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007823}
7824EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7825
Trond Myklebust66245532012-05-25 17:18:09 -04007826static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7827 struct rpc_cred *cred)
7828{
7829 struct rpc_message msg = {
7830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7831 .rpc_argp = clp,
7832 .rpc_cred = cred,
7833 };
7834 int status;
7835
7836 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007837 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007838 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007839 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007840 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7841 return status;
7842}
7843
7844static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7845 struct rpc_cred *cred)
7846{
7847 unsigned int loop;
7848 int ret;
7849
7850 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7851 ret = _nfs4_proc_destroy_clientid(clp, cred);
7852 switch (ret) {
7853 case -NFS4ERR_DELAY:
7854 case -NFS4ERR_CLIENTID_BUSY:
7855 ssleep(1);
7856 break;
7857 default:
7858 return ret;
7859 }
7860 }
7861 return 0;
7862}
7863
7864int nfs4_destroy_clientid(struct nfs_client *clp)
7865{
7866 struct rpc_cred *cred;
7867 int ret = 0;
7868
7869 if (clp->cl_mvops->minor_version < 1)
7870 goto out;
7871 if (clp->cl_exchange_flags == 0)
7872 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007873 if (clp->cl_preserve_clid)
7874 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007875 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007876 ret = nfs4_proc_destroy_clientid(clp, cred);
7877 if (cred)
7878 put_rpccred(cred);
7879 switch (ret) {
7880 case 0:
7881 case -NFS4ERR_STALE_CLIENTID:
7882 clp->cl_exchange_flags = 0;
7883 }
7884out:
7885 return ret;
7886}
7887
Andy Adamson2050f0c2009-04-01 09:22:30 -04007888struct nfs4_get_lease_time_data {
7889 struct nfs4_get_lease_time_args *args;
7890 struct nfs4_get_lease_time_res *res;
7891 struct nfs_client *clp;
7892};
7893
7894static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7895 void *calldata)
7896{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007897 struct nfs4_get_lease_time_data *data =
7898 (struct nfs4_get_lease_time_data *)calldata;
7899
7900 dprintk("--> %s\n", __func__);
7901 /* just setup sequence, do not trigger session recovery
7902 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007903 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007904 &data->args->la_seq_args,
7905 &data->res->lr_seq_res,
7906 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007907 dprintk("<-- %s\n", __func__);
7908}
7909
7910/*
7911 * Called from nfs4_state_manager thread for session setup, so don't recover
7912 * from sequence operation or clientid errors.
7913 */
7914static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7915{
7916 struct nfs4_get_lease_time_data *data =
7917 (struct nfs4_get_lease_time_data *)calldata;
7918
7919 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007920 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7921 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007922 switch (task->tk_status) {
7923 case -NFS4ERR_DELAY:
7924 case -NFS4ERR_GRACE:
7925 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7926 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7927 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007928 /* fall through */
7929 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007930 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007931 return;
7932 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007933 dprintk("<-- %s\n", __func__);
7934}
7935
Trond Myklebust17280172012-03-11 13:11:00 -04007936static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007937 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7938 .rpc_call_done = nfs4_get_lease_time_done,
7939};
7940
7941int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7942{
7943 struct rpc_task *task;
7944 struct nfs4_get_lease_time_args args;
7945 struct nfs4_get_lease_time_res res = {
7946 .lr_fsinfo = fsinfo,
7947 };
7948 struct nfs4_get_lease_time_data data = {
7949 .args = &args,
7950 .res = &res,
7951 .clp = clp,
7952 };
7953 struct rpc_message msg = {
7954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7955 .rpc_argp = &args,
7956 .rpc_resp = &res,
7957 };
7958 struct rpc_task_setup task_setup = {
7959 .rpc_client = clp->cl_rpcclient,
7960 .rpc_message = &msg,
7961 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007962 .callback_data = &data,
7963 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007964 };
7965 int status;
7966
Chuck Levera9c92d62013-08-09 12:48:18 -04007967 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007968 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007969 task = rpc_run_task(&task_setup);
7970
7971 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007972 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007973
Anna Schumakerf6148712017-04-07 14:15:23 -04007974 status = task->tk_status;
7975 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007976 return status;
7977}
7978
Andy Adamsonfc931582009-04-01 09:22:31 -04007979/*
7980 * Initialize the values to be used by the client in CREATE_SESSION
7981 * If nfs4_init_session set the fore channel request and response sizes,
7982 * use them.
7983 *
7984 * Set the back channel max_resp_sz_cached to zero to force the client to
7985 * always set csa_cachethis to FALSE because the current implementation
7986 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7987 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007988static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7989 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007990{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007991 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007992 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007993
Andy Adamson18aad3d2013-06-26 12:21:49 -04007994 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7995 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7996
Andy Adamsonfc931582009-04-01 09:22:31 -04007997 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007998 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7999 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008000 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008001 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008002
8003 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008004 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008005 __func__,
8006 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008007 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008008
8009 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008010 args->bc_attrs.max_rqst_sz = max_bc_payload;
8011 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008012 args->bc_attrs.max_resp_sz_cached = 0;
8013 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008014 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008015
8016 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8017 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8018 __func__,
8019 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8020 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8021 args->bc_attrs.max_reqs);
8022}
8023
Trond Myklebust79969dd2015-02-18 11:30:18 -08008024static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8025 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008026{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008027 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008028 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008029
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008030 if (rcvd->max_resp_sz > sent->max_resp_sz)
8031 return -EINVAL;
8032 /*
8033 * Our requested max_ops is the minimum we need; we're not
8034 * prepared to break up compounds into smaller pieces than that.
8035 * So, no point even trying to continue if the server won't
8036 * cooperate:
8037 */
8038 if (rcvd->max_ops < sent->max_ops)
8039 return -EINVAL;
8040 if (rcvd->max_reqs == 0)
8041 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008042 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8043 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008044 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008045}
8046
Trond Myklebust79969dd2015-02-18 11:30:18 -08008047static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8048 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008049{
8050 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008051 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008052
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008053 if (!(res->flags & SESSION4_BACK_CHAN))
8054 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008055 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8056 return -EINVAL;
8057 if (rcvd->max_resp_sz < sent->max_resp_sz)
8058 return -EINVAL;
8059 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8060 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008061 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008062 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008063 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008064 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008065out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008066 return 0;
8067}
Andy Adamson8d353012009-04-01 09:22:32 -04008068
Andy Adamson8d353012009-04-01 09:22:32 -04008069static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008070 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008071{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008072 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008073
Trond Myklebust79969dd2015-02-18 11:30:18 -08008074 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008075 if (ret)
8076 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008077 return nfs4_verify_back_channel_attrs(args, res);
8078}
8079
8080static void nfs4_update_session(struct nfs4_session *session,
8081 struct nfs41_create_session_res *res)
8082{
8083 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008084 /* Mark client id and session as being confirmed */
8085 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8086 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008087 session->flags = res->flags;
8088 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008089 if (res->flags & SESSION4_BACK_CHAN)
8090 memcpy(&session->bc_attrs, &res->bc_attrs,
8091 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008092}
8093
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008094static int _nfs4_proc_create_session(struct nfs_client *clp,
8095 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008096{
8097 struct nfs4_session *session = clp->cl_session;
8098 struct nfs41_create_session_args args = {
8099 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008100 .clientid = clp->cl_clientid,
8101 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008102 .cb_program = NFS4_CALLBACK,
8103 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008104 struct nfs41_create_session_res res;
8105
Andy Adamsonfc931582009-04-01 09:22:31 -04008106 struct rpc_message msg = {
8107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8108 .rpc_argp = &args,
8109 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008110 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008111 };
8112 int status;
8113
Chuck Lever6b26cc82016-05-02 14:40:40 -04008114 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008115 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008116
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008117 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008118 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008119
Trond Myklebustb519d402016-09-11 14:50:01 -04008120 switch (status) {
8121 case -NFS4ERR_STALE_CLIENTID:
8122 case -NFS4ERR_DELAY:
8123 case -ETIMEDOUT:
8124 case -EACCES:
8125 case -EAGAIN:
8126 goto out;
8127 };
8128
8129 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008130 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008131 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008132 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008133 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008134 if (status)
8135 goto out;
8136 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008137 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008138out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008139 return status;
8140}
8141
8142/*
8143 * Issues a CREATE_SESSION operation to the server.
8144 * It is the responsibility of the caller to verify the session is
8145 * expired before calling this routine.
8146 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008147int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008148{
8149 int status;
8150 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008151 struct nfs4_session *session = clp->cl_session;
8152
8153 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8154
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008155 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008156 if (status)
8157 goto out;
8158
Andy Adamsonaacd5532011-11-09 13:58:21 -05008159 /* Init or reset the session slot tables */
8160 status = nfs4_setup_session_slot_tables(session);
8161 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008162 if (status)
8163 goto out;
8164
8165 ptr = (unsigned *)&session->sess_id.data[0];
8166 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8167 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008168out:
8169 dprintk("<-- %s\n", __func__);
8170 return status;
8171}
8172
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008173/*
8174 * Issue the over-the-wire RPC DESTROY_SESSION.
8175 * The caller must serialize access to this routine.
8176 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008177int nfs4_proc_destroy_session(struct nfs4_session *session,
8178 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008179{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008180 struct rpc_message msg = {
8181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8182 .rpc_argp = session,
8183 .rpc_cred = cred,
8184 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008185 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008186
8187 dprintk("--> nfs4_proc_destroy_session\n");
8188
8189 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008190 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8191 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008192
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008193 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008194 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008195
8196 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008197 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008198 "Session has been destroyed regardless...\n", status);
8199
8200 dprintk("<-- nfs4_proc_destroy_session\n");
8201 return status;
8202}
8203
Trond Myklebust7b38c362012-05-23 13:23:31 -04008204/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008205 * Renew the cl_session lease.
8206 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008207struct nfs4_sequence_data {
8208 struct nfs_client *clp;
8209 struct nfs4_sequence_args args;
8210 struct nfs4_sequence_res res;
8211};
8212
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008213static void nfs41_sequence_release(void *data)
8214{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008215 struct nfs4_sequence_data *calldata = data;
8216 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008217
Elena Reshetova212bf412017-10-20 12:53:38 +03008218 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008219 nfs4_schedule_state_renewal(clp);
8220 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008221 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008222}
8223
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008224static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8225{
8226 switch(task->tk_status) {
8227 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008228 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8229 return -EAGAIN;
8230 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008231 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008232 }
8233 return 0;
8234}
8235
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008236static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008237{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008238 struct nfs4_sequence_data *calldata = data;
8239 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008240
Trond Myklebust14516c32010-07-31 14:29:06 -04008241 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8242 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008243
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008244 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008245 if (task->tk_status < 0) {
8246 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008247 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008248 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008249
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008250 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8251 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008252 return;
8253 }
8254 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008255 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008256out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008257 dprintk("<-- %s\n", __func__);
8258}
8259
8260static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8261{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008262 struct nfs4_sequence_data *calldata = data;
8263 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008264 struct nfs4_sequence_args *args;
8265 struct nfs4_sequence_res *res;
8266
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008267 args = task->tk_msg.rpc_argp;
8268 res = task->tk_msg.rpc_resp;
8269
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008270 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008271}
8272
8273static const struct rpc_call_ops nfs41_sequence_ops = {
8274 .rpc_call_done = nfs41_sequence_call_done,
8275 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008276 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008277};
8278
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008279static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8280 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008281 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008282 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008283{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008284 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008285 struct rpc_message msg = {
8286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8287 .rpc_cred = cred,
8288 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008289 struct rpc_task_setup task_setup_data = {
8290 .rpc_client = clp->cl_rpcclient,
8291 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008292 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008293 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008294 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008295 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008296
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008297 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008298 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008299 goto out_err;
8300
8301 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008302 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008303 if (calldata == NULL)
8304 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008305 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008306 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008307 if (is_privileged)
8308 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008309 msg.rpc_argp = &calldata->args;
8310 msg.rpc_resp = &calldata->res;
8311 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008312 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008313
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008314 ret = rpc_run_task(&task_setup_data);
8315 if (IS_ERR(ret))
8316 goto out_err;
8317 return ret;
8318out_put_clp:
8319 nfs_put_client(clp);
8320out_err:
8321 nfs41_release_slot(slot);
8322 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008323}
8324
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008325static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008326{
8327 struct rpc_task *task;
8328 int ret = 0;
8329
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008330 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008331 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008332 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008333 if (IS_ERR(task))
8334 ret = PTR_ERR(task);
8335 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008336 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008337 dprintk("<-- %s status=%d\n", __func__, ret);
8338 return ret;
8339}
8340
8341static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8342{
8343 struct rpc_task *task;
8344 int ret;
8345
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008346 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008347 if (IS_ERR(task)) {
8348 ret = PTR_ERR(task);
8349 goto out;
8350 }
8351 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008352 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008353 ret = task->tk_status;
8354 rpc_put_task(task);
8355out:
8356 dprintk("<-- %s status=%d\n", __func__, ret);
8357 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008358}
8359
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008360struct nfs4_reclaim_complete_data {
8361 struct nfs_client *clp;
8362 struct nfs41_reclaim_complete_args arg;
8363 struct nfs41_reclaim_complete_res res;
8364};
8365
8366static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8367{
8368 struct nfs4_reclaim_complete_data *calldata = data;
8369
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008370 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008371 &calldata->arg.seq_args,
8372 &calldata->res.seq_res,
8373 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008374}
8375
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008376static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8377{
8378 switch(task->tk_status) {
8379 case 0:
8380 case -NFS4ERR_COMPLETE_ALREADY:
8381 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8382 break;
8383 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008384 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008385 /* fall through */
8386 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008387 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008388 case -NFS4ERR_BADSESSION:
8389 case -NFS4ERR_DEADSESSION:
8390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8391 nfs4_schedule_session_recovery(clp->cl_session,
8392 task->tk_status);
8393 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008394 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008395 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008396 }
8397 return 0;
8398}
8399
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008400static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8401{
8402 struct nfs4_reclaim_complete_data *calldata = data;
8403 struct nfs_client *clp = calldata->clp;
8404 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8405
8406 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008407 if (!nfs41_sequence_done(task, res))
8408 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008409
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008410 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008411 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8412 rpc_restart_call_prepare(task);
8413 return;
8414 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008415 dprintk("<-- %s\n", __func__);
8416}
8417
8418static void nfs4_free_reclaim_complete_data(void *data)
8419{
8420 struct nfs4_reclaim_complete_data *calldata = data;
8421
8422 kfree(calldata);
8423}
8424
8425static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8426 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8427 .rpc_call_done = nfs4_reclaim_complete_done,
8428 .rpc_release = nfs4_free_reclaim_complete_data,
8429};
8430
8431/*
8432 * Issue a global reclaim complete.
8433 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008434static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8435 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008436{
8437 struct nfs4_reclaim_complete_data *calldata;
8438 struct rpc_task *task;
8439 struct rpc_message msg = {
8440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008441 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008442 };
8443 struct rpc_task_setup task_setup_data = {
8444 .rpc_client = clp->cl_rpcclient,
8445 .rpc_message = &msg,
8446 .callback_ops = &nfs4_reclaim_complete_call_ops,
8447 .flags = RPC_TASK_ASYNC,
8448 };
8449 int status = -ENOMEM;
8450
8451 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008452 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008453 if (calldata == NULL)
8454 goto out;
8455 calldata->clp = clp;
8456 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008457
Chuck Levera9c92d62013-08-09 12:48:18 -04008458 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008459 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008460 msg.rpc_argp = &calldata->arg;
8461 msg.rpc_resp = &calldata->res;
8462 task_setup_data.callback_data = calldata;
8463 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008464 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008465 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008466 goto out;
8467 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008468 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008469 if (status == 0)
8470 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008471 rpc_put_task(task);
8472out:
8473 dprintk("<-- %s status=%d\n", __func__, status);
8474 return status;
8475}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008476
8477static void
8478nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8479{
8480 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008481 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008482
8483 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008484 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008485 &lgp->res.seq_res, task);
8486 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008487}
8488
8489static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8490{
8491 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008492
8493 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008494 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008495 dprintk("<-- %s\n", __func__);
8496}
8497
8498static int
8499nfs4_layoutget_handle_exception(struct rpc_task *task,
8500 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8501{
Trond Myklebustee314c22012-10-01 17:25:48 -07008502 struct inode *inode = lgp->args.inode;
8503 struct nfs_server *server = NFS_SERVER(inode);
8504 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008505 int nfs4err = task->tk_status;
8506 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008507 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008508
Boaz Harroshed7e5422014-01-22 20:34:54 +02008509 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008510
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008511 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008512 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008513 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008514
8515 /*
8516 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8517 * on the file. set tk_status to -ENODATA to tell upper layer to
8518 * retry go inband.
8519 */
8520 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008521 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008522 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008523 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008524 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8525 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8526 */
8527 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008528 status = -EOVERFLOW;
8529 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008530 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008531 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008532 * (or clients) writing to the same RAID stripe except when
8533 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008534 *
8535 * Treat it like we would RECALLCONFLICT -- we retry for a little
8536 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008537 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008538 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008539 if (lgp->args.minlength == 0) {
8540 status = -EOVERFLOW;
8541 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008542 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008543 status = -EBUSY;
8544 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008545 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008546 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008547 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008548 case -NFS4ERR_DELEG_REVOKED:
8549 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008550 case -NFS4ERR_EXPIRED:
8551 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008552 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008553 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008554 lo = NFS_I(inode)->layout;
8555 /* If the open stateid was bad, then recover it. */
8556 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008557 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008558 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008559 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008560 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008561 break;
8562 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008563
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008564 /*
8565 * Mark the bad layout state as invalid, then retry
8566 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008567 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008568 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008569 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008570 pnfs_free_lseg_list(&head);
8571 status = -EAGAIN;
8572 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008573 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008574
Trond Myklebust8ac09252017-01-23 22:44:12 -05008575 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008576 err = nfs4_handle_exception(server, nfs4err, exception);
8577 if (!status) {
8578 if (exception->retry)
8579 status = -EAGAIN;
8580 else
8581 status = err;
8582 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008583out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008584 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008585 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008586}
8587
Idan Kedar85541162012-08-02 11:47:10 +03008588static size_t max_response_pages(struct nfs_server *server)
8589{
8590 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8591 return nfs_page_array_len(0, max_resp_sz);
8592}
8593
8594static void nfs4_free_pages(struct page **pages, size_t size)
8595{
8596 int i;
8597
8598 if (!pages)
8599 return;
8600
8601 for (i = 0; i < size; i++) {
8602 if (!pages[i])
8603 break;
8604 __free_page(pages[i]);
8605 }
8606 kfree(pages);
8607}
8608
8609static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8610{
8611 struct page **pages;
8612 int i;
8613
8614 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8615 if (!pages) {
8616 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8617 return NULL;
8618 }
8619
8620 for (i = 0; i < size; i++) {
8621 pages[i] = alloc_page(gfp_flags);
8622 if (!pages[i]) {
8623 dprintk("%s: failed to allocate page\n", __func__);
8624 nfs4_free_pages(pages, size);
8625 return NULL;
8626 }
8627 }
8628
8629 return pages;
8630}
8631
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008632static void nfs4_layoutget_release(void *calldata)
8633{
8634 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008635 struct inode *inode = lgp->args.inode;
8636 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008637 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008638
8639 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008640 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008641 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008642 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008643 put_nfs_open_context(lgp->args.ctx);
8644 kfree(calldata);
8645 dprintk("<-- %s\n", __func__);
8646}
8647
8648static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8649 .rpc_call_prepare = nfs4_layoutget_prepare,
8650 .rpc_call_done = nfs4_layoutget_done,
8651 .rpc_release = nfs4_layoutget_release,
8652};
8653
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008654struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008655nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008656{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008657 struct inode *inode = lgp->args.inode;
8658 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008659 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008660 struct rpc_task *task;
8661 struct rpc_message msg = {
8662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8663 .rpc_argp = &lgp->args,
8664 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008665 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008666 };
8667 struct rpc_task_setup task_setup_data = {
8668 .rpc_client = server->client,
8669 .rpc_message = &msg,
8670 .callback_ops = &nfs4_layoutget_call_ops,
8671 .callback_data = lgp,
8672 .flags = RPC_TASK_ASYNC,
8673 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008674 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008675 struct nfs4_exception exception = {
8676 .inode = inode,
8677 .timeout = *timeout,
8678 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008679 int status = 0;
8680
8681 dprintk("--> %s\n", __func__);
8682
Peng Tao4bd5a982014-11-17 11:05:17 +08008683 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8684 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8685
Idan Kedar85541162012-08-02 11:47:10 +03008686 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8687 if (!lgp->args.layout.pages) {
8688 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008689 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008690 }
8691 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8692
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008693 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008694 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008695 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008696
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008697 task = rpc_run_task(&task_setup_data);
8698 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008699 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008700 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008701 if (status == 0) {
8702 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8703 *timeout = exception.timeout;
8704 }
8705
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008706 trace_nfs4_layoutget(lgp->args.ctx,
8707 &lgp->args.range,
8708 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008709 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008710 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008711
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008712 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8713 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008714 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008715 rpc_put_task(task);
8716 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008717 if (status)
8718 return ERR_PTR(status);
8719 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008720}
8721
Benny Halevycbe82602011-05-22 19:52:37 +03008722static void
8723nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8724{
8725 struct nfs4_layoutreturn *lrp = calldata;
8726
8727 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008728 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008729 &lrp->args.seq_args,
8730 &lrp->res.seq_res,
8731 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008732}
8733
8734static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8735{
8736 struct nfs4_layoutreturn *lrp = calldata;
8737 struct nfs_server *server;
8738
8739 dprintk("--> %s\n", __func__);
8740
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008741 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008742 return;
8743
8744 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008745 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008746 case -NFS4ERR_OLD_STATEID:
8747 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8748 lrp->args.inode))
8749 goto out_restart;
8750 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008751 default:
8752 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008753 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008754 case 0:
8755 break;
8756 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008757 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008758 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008759 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008760 }
Benny Halevycbe82602011-05-22 19:52:37 +03008761 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008762 return;
8763out_restart:
8764 task->tk_status = 0;
8765 nfs4_sequence_free_slot(&lrp->res.seq_res);
8766 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008767}
8768
8769static void nfs4_layoutreturn_release(void *calldata)
8770{
8771 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008772 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008773
8774 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008775 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008776 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008777 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008778 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8779 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008780 pnfs_put_layout_hdr(lrp->args.layout);
8781 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008782 kfree(calldata);
8783 dprintk("<-- %s\n", __func__);
8784}
8785
8786static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8787 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8788 .rpc_call_done = nfs4_layoutreturn_done,
8789 .rpc_release = nfs4_layoutreturn_release,
8790};
8791
Peng Tao6c166052014-11-17 09:30:40 +08008792int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008793{
8794 struct rpc_task *task;
8795 struct rpc_message msg = {
8796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8797 .rpc_argp = &lrp->args,
8798 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008799 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008800 };
8801 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008802 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008803 .rpc_message = &msg,
8804 .callback_ops = &nfs4_layoutreturn_call_ops,
8805 .callback_data = lrp,
8806 };
Peng Tao6c166052014-11-17 09:30:40 +08008807 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008808
Andrew Elble99ade3c2015-12-02 09:39:51 -05008809 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8810 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8811 &task_setup_data.rpc_client, &msg);
8812
Benny Halevycbe82602011-05-22 19:52:37 +03008813 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008814 if (!sync) {
8815 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8816 if (!lrp->inode) {
8817 nfs4_layoutreturn_release(lrp);
8818 return -EAGAIN;
8819 }
8820 task_setup_data.flags |= RPC_TASK_ASYNC;
8821 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008822 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008823 task = rpc_run_task(&task_setup_data);
8824 if (IS_ERR(task))
8825 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008826 if (sync)
8827 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008828 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008829 dprintk("<-- %s status=%d\n", __func__, status);
8830 rpc_put_task(task);
8831 return status;
8832}
8833
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008834static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008835_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8836 struct pnfs_device *pdev,
8837 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008838{
8839 struct nfs4_getdeviceinfo_args args = {
8840 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008841 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8842 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008843 };
8844 struct nfs4_getdeviceinfo_res res = {
8845 .pdev = pdev,
8846 };
8847 struct rpc_message msg = {
8848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8849 .rpc_argp = &args,
8850 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008851 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008852 };
8853 int status;
8854
8855 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008857 if (res.notification & ~args.notify_types)
8858 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008859 if (res.notification != args.notify_types)
8860 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008861
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008862 dprintk("<-- %s status=%d\n", __func__, status);
8863
8864 return status;
8865}
8866
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008867int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8868 struct pnfs_device *pdev,
8869 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008870{
8871 struct nfs4_exception exception = { };
8872 int err;
8873
8874 do {
8875 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008876 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008877 &exception);
8878 } while (exception.retry);
8879 return err;
8880}
8881EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8882
Andy Adamson863a3c62011-03-23 13:27:54 +00008883static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8884{
8885 struct nfs4_layoutcommit_data *data = calldata;
8886 struct nfs_server *server = NFS_SERVER(data->args.inode);
8887
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008888 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008889 &data->args.seq_args,
8890 &data->res.seq_res,
8891 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008892}
8893
8894static void
8895nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8896{
8897 struct nfs4_layoutcommit_data *data = calldata;
8898 struct nfs_server *server = NFS_SERVER(data->args.inode);
8899
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008900 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008901 return;
8902
8903 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008904 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8905 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8906 case -NFS4ERR_BADLAYOUT: /* no layout */
8907 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008908 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008909 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008910 break;
8911 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008912 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008913 rpc_restart_call_prepare(task);
8914 return;
8915 }
8916 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008917}
8918
8919static void nfs4_layoutcommit_release(void *calldata)
8920{
8921 struct nfs4_layoutcommit_data *data = calldata;
8922
Andy Adamsondb29c082011-07-30 20:52:38 -04008923 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008924 nfs_post_op_update_inode_force_wcc(data->args.inode,
8925 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008926 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008927 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008928 kfree(data);
8929}
8930
8931static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8932 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8933 .rpc_call_done = nfs4_layoutcommit_done,
8934 .rpc_release = nfs4_layoutcommit_release,
8935};
8936
8937int
Andy Adamsonef311532011-03-12 02:58:10 -05008938nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008939{
8940 struct rpc_message msg = {
8941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8942 .rpc_argp = &data->args,
8943 .rpc_resp = &data->res,
8944 .rpc_cred = data->cred,
8945 };
8946 struct rpc_task_setup task_setup_data = {
8947 .task = &data->task,
8948 .rpc_client = NFS_CLIENT(data->args.inode),
8949 .rpc_message = &msg,
8950 .callback_ops = &nfs4_layoutcommit_ops,
8951 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008952 };
8953 struct rpc_task *task;
8954 int status = 0;
8955
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008956 dprintk("NFS: initiating layoutcommit call. sync %d "
8957 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008958 data->args.lastbytewritten,
8959 data->args.inode->i_ino);
8960
Trond Myklebust472e2592015-02-05 16:50:30 -05008961 if (!sync) {
8962 data->inode = nfs_igrab_and_active(data->args.inode);
8963 if (data->inode == NULL) {
8964 nfs4_layoutcommit_release(data);
8965 return -EAGAIN;
8966 }
8967 task_setup_data.flags = RPC_TASK_ASYNC;
8968 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008969 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008970 task = rpc_run_task(&task_setup_data);
8971 if (IS_ERR(task))
8972 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008973 if (sync)
8974 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008975 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008976 dprintk("%s: status %d\n", __func__, status);
8977 rpc_put_task(task);
8978 return status;
8979}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008980
Andy Adamson97431202013-08-08 10:57:56 -04008981/**
8982 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8983 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8984 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008985static int
8986_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008987 struct nfs_fsinfo *info,
8988 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008989{
8990 struct nfs41_secinfo_no_name_args args = {
8991 .style = SECINFO_STYLE_CURRENT_FH,
8992 };
8993 struct nfs4_secinfo_res res = {
8994 .flavors = flavors,
8995 };
8996 struct rpc_message msg = {
8997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8998 .rpc_argp = &args,
8999 .rpc_resp = &res,
9000 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009001 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009002 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009003 int status;
9004
9005 if (use_integrity) {
9006 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009007 cred = nfs4_get_clid_cred(server->nfs_client);
9008 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009009 }
9010
9011 dprintk("--> %s\n", __func__);
9012 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9013 &res.seq_res, 0);
9014 dprintk("<-- %s status=%d\n", __func__, status);
9015
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009016 if (cred)
9017 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009018
9019 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009020}
9021
9022static int
9023nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9024 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9025{
9026 struct nfs4_exception exception = { };
9027 int err;
9028 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009029 /* first try using integrity protection */
9030 err = -NFS4ERR_WRONGSEC;
9031
9032 /* try to use integrity protection with machine cred */
9033 if (_nfs4_is_integrity_protected(server->nfs_client))
9034 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9035 flavors, true);
9036
9037 /*
9038 * if unable to use integrity protection, or SECINFO with
9039 * integrity protection returns NFS4ERR_WRONGSEC (which is
9040 * disallowed by spec, but exists in deployed servers) use
9041 * the current filesystem's rpc_client and the user cred.
9042 */
9043 if (err == -NFS4ERR_WRONGSEC)
9044 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9045 flavors, false);
9046
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009047 switch (err) {
9048 case 0:
9049 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009050 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009051 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009052 default:
9053 err = nfs4_handle_exception(server, err, &exception);
9054 }
9055 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009056out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009057 return err;
9058}
9059
9060static int
9061nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9062 struct nfs_fsinfo *info)
9063{
9064 int err;
9065 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009066 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009067 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009068 struct nfs4_secinfo4 *secinfo;
9069 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009070
9071 page = alloc_page(GFP_KERNEL);
9072 if (!page) {
9073 err = -ENOMEM;
9074 goto out;
9075 }
9076
9077 flavors = page_address(page);
9078 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9079
9080 /*
9081 * Fall back on "guess and check" method if
9082 * the server doesn't support SECINFO_NO_NAME
9083 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009084 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009085 err = nfs4_find_root_sec(server, fhandle, info);
9086 goto out_freepage;
9087 }
9088 if (err)
9089 goto out_freepage;
9090
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009091 for (i = 0; i < flavors->num_flavors; i++) {
9092 secinfo = &flavors->flavors[i];
9093
9094 switch (secinfo->flavor) {
9095 case RPC_AUTH_NULL:
9096 case RPC_AUTH_UNIX:
9097 case RPC_AUTH_GSS:
9098 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9099 &secinfo->flavor_info);
9100 break;
9101 default:
9102 flavor = RPC_AUTH_MAXFLAVOR;
9103 break;
9104 }
9105
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009106 if (!nfs_auth_info_match(&server->auth_info, flavor))
9107 flavor = RPC_AUTH_MAXFLAVOR;
9108
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009109 if (flavor != RPC_AUTH_MAXFLAVOR) {
9110 err = nfs4_lookup_root_sec(server, fhandle,
9111 info, flavor);
9112 if (!err)
9113 break;
9114 }
9115 }
9116
9117 if (flavor == RPC_AUTH_MAXFLAVOR)
9118 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009119
9120out_freepage:
9121 put_page(page);
9122 if (err == -EACCES)
9123 return -EPERM;
9124out:
9125 return err;
9126}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009127
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009128static int _nfs41_test_stateid(struct nfs_server *server,
9129 nfs4_stateid *stateid,
9130 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009131{
9132 int status;
9133 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009134 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009135 };
9136 struct nfs41_test_stateid_res res;
9137 struct rpc_message msg = {
9138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9139 .rpc_argp = &args,
9140 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009141 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009142 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009143 struct rpc_clnt *rpc_client = server->client;
9144
9145 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9146 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009147
Chuck Lever38527b12012-07-11 16:30:23 -04009148 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009149 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009150 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009151 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009152 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009153 if (status != NFS_OK) {
9154 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009155 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009156 }
9157 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009158 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009159}
9160
Trond Myklebust43912bb2016-09-22 13:38:56 -04009161static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9162 int err, struct nfs4_exception *exception)
9163{
9164 exception->retry = 0;
9165 switch(err) {
9166 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009167 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009168 nfs4_handle_exception(server, err, exception);
9169 break;
9170 case -NFS4ERR_BADSESSION:
9171 case -NFS4ERR_BADSLOT:
9172 case -NFS4ERR_BAD_HIGH_SLOT:
9173 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9174 case -NFS4ERR_DEADSESSION:
9175 nfs4_do_handle_exception(server, err, exception);
9176 }
9177}
9178
Chuck Lever38527b12012-07-11 16:30:23 -04009179/**
9180 * nfs41_test_stateid - perform a TEST_STATEID operation
9181 *
9182 * @server: server / transport on which to perform the operation
9183 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009184 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009185 *
9186 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9187 * Otherwise a negative NFS4ERR value is returned if the operation
9188 * failed or the state ID is not currently valid.
9189 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009190static int nfs41_test_stateid(struct nfs_server *server,
9191 nfs4_stateid *stateid,
9192 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009193{
9194 struct nfs4_exception exception = { };
9195 int err;
9196 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009197 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009198 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009199 } while (exception.retry);
9200 return err;
9201}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009202
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009203struct nfs_free_stateid_data {
9204 struct nfs_server *server;
9205 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009206 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009207};
9208
9209static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9210{
9211 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009212 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009213 &data->args.seq_args,
9214 &data->res.seq_res,
9215 task);
9216}
9217
9218static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9219{
9220 struct nfs_free_stateid_data *data = calldata;
9221
9222 nfs41_sequence_done(task, &data->res.seq_res);
9223
9224 switch (task->tk_status) {
9225 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009226 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009227 rpc_restart_call_prepare(task);
9228 }
9229}
9230
9231static void nfs41_free_stateid_release(void *calldata)
9232{
9233 kfree(calldata);
9234}
9235
Trond Myklebust17f26b12013-08-21 15:48:42 -04009236static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009237 .rpc_call_prepare = nfs41_free_stateid_prepare,
9238 .rpc_call_done = nfs41_free_stateid_done,
9239 .rpc_release = nfs41_free_stateid_release,
9240};
9241
9242static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009243 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009244 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009245 bool privileged)
9246{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009247 struct rpc_message msg = {
9248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009249 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009250 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009251 struct rpc_task_setup task_setup = {
9252 .rpc_client = server->client,
9253 .rpc_message = &msg,
9254 .callback_ops = &nfs41_free_stateid_ops,
9255 .flags = RPC_TASK_ASYNC,
9256 };
9257 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009258
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009259 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9260 &task_setup.rpc_client, &msg);
9261
Chuck Lever38527b12012-07-11 16:30:23 -04009262 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009263 data = kmalloc(sizeof(*data), GFP_NOFS);
9264 if (!data)
9265 return ERR_PTR(-ENOMEM);
9266 data->server = server;
9267 nfs4_stateid_copy(&data->args.stateid, stateid);
9268
9269 task_setup.callback_data = data;
9270
9271 msg.rpc_argp = &data->args;
9272 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009273 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009274 if (privileged)
9275 nfs4_set_sequence_privileged(&data->args.seq_args);
9276
9277 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009278}
9279
Chuck Lever38527b12012-07-11 16:30:23 -04009280/**
9281 * nfs41_free_stateid - perform a FREE_STATEID operation
9282 *
9283 * @server: server / transport on which to perform the operation
9284 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009285 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009286 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009287 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009288 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009289 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009290static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009291 const nfs4_stateid *stateid,
9292 struct rpc_cred *cred,
9293 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009294{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009295 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009296
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009297 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009298 if (IS_ERR(task))
9299 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009300 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009301 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009302}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009303
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009304static void
9305nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009306{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009307 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009308
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009309 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009310 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009311}
9312
Trond Myklebust36281ca2012-03-04 18:13:56 -05009313static bool nfs41_match_stateid(const nfs4_stateid *s1,
9314 const nfs4_stateid *s2)
9315{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009316 if (s1->type != s2->type)
9317 return false;
9318
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009319 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009320 return false;
9321
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009322 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009323 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009324
Anna Schumaker045c5512017-01-11 16:59:48 -05009325 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009326}
9327
Andy Adamson557134a2009-04-01 09:21:53 -04009328#endif /* CONFIG_NFS_V4_1 */
9329
Trond Myklebust36281ca2012-03-04 18:13:56 -05009330static bool nfs4_match_stateid(const nfs4_stateid *s1,
9331 const nfs4_stateid *s2)
9332{
Trond Myklebustf597c532012-03-04 18:13:56 -05009333 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009334}
9335
9336
Trond Myklebust17280172012-03-11 13:11:00 -04009337static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009338 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009339 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 .recover_open = nfs4_open_reclaim,
9341 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009342 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009343 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344};
9345
Andy Adamson591d71c2009-04-01 09:22:47 -04009346#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009347static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009348 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9349 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9350 .recover_open = nfs4_open_reclaim,
9351 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009352 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009353 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009354 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009355};
9356#endif /* CONFIG_NFS_V4_1 */
9357
Trond Myklebust17280172012-03-11 13:11:00 -04009358static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009359 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009360 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009361 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009363 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364};
9365
Andy Adamson591d71c2009-04-01 09:22:47 -04009366#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009367static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009368 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9369 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009370 .recover_open = nfs41_open_expired,
9371 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009372 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009373};
9374#endif /* CONFIG_NFS_V4_1 */
9375
Trond Myklebust17280172012-03-11 13:11:00 -04009376static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009377 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009378 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009379 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009380};
9381
9382#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009383static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009384 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009385 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009386 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009387};
9388#endif
9389
Chuck Leverec011fe2013-10-17 14:12:39 -04009390static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009391 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009392 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009393};
9394
9395#if defined(CONFIG_NFS_V4_1)
9396static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009397 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009398 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009399};
9400#endif /* CONFIG_NFS_V4_1 */
9401
Trond Myklebust97dc1352010-06-16 09:52:26 -04009402static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9403 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009404 .init_caps = NFS_CAP_READDIRPLUS
9405 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009406 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009407 .init_client = nfs40_init_client,
9408 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009409 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009410 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009411 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009412 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009413 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009414 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009415 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9416 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9417 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009418 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009419};
9420
9421#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009422static struct nfs_seqid *
9423nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9424{
9425 return NULL;
9426}
9427
Trond Myklebust97dc1352010-06-16 09:52:26 -04009428static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9429 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009430 .init_caps = NFS_CAP_READDIRPLUS
9431 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009432 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009433 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009434 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009435 .init_client = nfs41_init_client,
9436 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009437 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009438 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009439 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009440 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009441 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009442 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009443 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009444 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9445 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9446 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009447 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009448};
9449#endif
9450
Steve Dickson42c2c422013-05-22 12:50:38 -04009451#if defined(CONFIG_NFS_V4_2)
9452static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9453 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009454 .init_caps = NFS_CAP_READDIRPLUS
9455 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009456 | NFS_CAP_POSIX_LOCK
9457 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009458 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009459 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009460 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009461 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009462 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009463 | NFS_CAP_LAYOUTSTATS
9464 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009465 .init_client = nfs41_init_client,
9466 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009467 .match_stateid = nfs41_match_stateid,
9468 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009469 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009470 .call_sync_ops = &nfs41_call_sync_ops,
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,
Steve Dickson42c2c422013-05-22 12:50:38 -04009474 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9475 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9476 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009477 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009478};
9479#endif
9480
Trond Myklebust97dc1352010-06-16 09:52:26 -04009481const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9482 [0] = &nfs_v4_0_minor_ops,
9483#if defined(CONFIG_NFS_V4_1)
9484 [1] = &nfs_v4_1_minor_ops,
9485#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009486#if defined(CONFIG_NFS_V4_2)
9487 [2] = &nfs_v4_2_minor_ops,
9488#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009489};
9490
Trond Myklebust13997822016-07-24 17:10:52 -04009491static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009492{
9493 ssize_t error, error2;
9494
9495 error = generic_listxattr(dentry, list, size);
9496 if (error < 0)
9497 return error;
9498 if (list) {
9499 list += error;
9500 size -= error;
9501 }
9502
9503 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9504 if (error2 < 0)
9505 return error2;
9506 return error + error2;
9507}
9508
Trond Myklebust17f26b12013-08-21 15:48:42 -04009509static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009510 .create = nfs_create,
9511 .lookup = nfs_lookup,
9512 .atomic_open = nfs_atomic_open,
9513 .link = nfs_link,
9514 .unlink = nfs_unlink,
9515 .symlink = nfs_symlink,
9516 .mkdir = nfs_mkdir,
9517 .rmdir = nfs_rmdir,
9518 .mknod = nfs_mknod,
9519 .rename = nfs_rename,
9520 .permission = nfs_permission,
9521 .getattr = nfs_getattr,
9522 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009523 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009524};
9525
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009526static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009527 .permission = nfs_permission,
9528 .getattr = nfs_getattr,
9529 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009530 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009531};
9532
David Howells509de812006-08-22 20:06:11 -04009533const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534 .version = 4, /* protocol version */
9535 .dentry_ops = &nfs4_dentry_operations,
9536 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009537 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009538 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009540 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009541 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 .getattr = nfs4_proc_getattr,
9543 .setattr = nfs4_proc_setattr,
9544 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009545 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 .access = nfs4_proc_access,
9547 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 .create = nfs4_proc_create,
9549 .remove = nfs4_proc_remove,
9550 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009551 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009553 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009554 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009555 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556 .link = nfs4_proc_link,
9557 .symlink = nfs4_proc_symlink,
9558 .mkdir = nfs4_proc_mkdir,
9559 .rmdir = nfs4_proc_remove,
9560 .readdir = nfs4_proc_readdir,
9561 .mknod = nfs4_proc_mknod,
9562 .statfs = nfs4_proc_statfs,
9563 .fsinfo = nfs4_proc_fsinfo,
9564 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009565 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009567 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009568 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009569 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009571 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009572 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009573 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009574 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009576 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009577 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009578 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009579 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009580 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009581 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009582 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009583 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009584 .create_server = nfs4_create_server,
9585 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586};
9587
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009588static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009589 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009590 .list = nfs4_xattr_list_nfs4_acl,
9591 .get = nfs4_xattr_get_nfs4_acl,
9592 .set = nfs4_xattr_set_nfs4_acl,
9593};
9594
9595const struct xattr_handler *nfs4_xattr_handlers[] = {
9596 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009597#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9598 &nfs4_xattr_nfs4_label_handler,
9599#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009600 NULL
9601};
9602
Linus Torvalds1da177e2005-04-16 15:20:36 -07009603/*
9604 * Local variables:
9605 * c-basic-offset: 8
9606 * End:
9607 */