blob: cec4bcba65e803dab4e70e4da8f4a523ee4b998f [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 Myklebust49f9a0f2013-03-15 16:44:28 -04001091static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1092 int err, struct nfs4_exception *exception)
1093{
1094 if (err != -EINVAL)
1095 return false;
1096 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1097 return false;
1098 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1099 exception->retry = 1;
1100 return true;
1101}
1102
Trond Myklebust6ae37332015-01-30 14:21:14 -05001103static u32
1104nfs4_map_atomic_open_share(struct nfs_server *server,
1105 fmode_t fmode, int openflags)
1106{
1107 u32 res = 0;
1108
1109 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1110 case FMODE_READ:
1111 res = NFS4_SHARE_ACCESS_READ;
1112 break;
1113 case FMODE_WRITE:
1114 res = NFS4_SHARE_ACCESS_WRITE;
1115 break;
1116 case FMODE_READ|FMODE_WRITE:
1117 res = NFS4_SHARE_ACCESS_BOTH;
1118 }
1119 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1120 goto out;
1121 /* Want no delegation if we're using O_DIRECT */
1122 if (openflags & O_DIRECT)
1123 res |= NFS4_SHARE_WANT_NO_DELEG;
1124out:
1125 return res;
1126}
1127
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001128static enum open_claim_type4
1129nfs4_map_atomic_open_claim(struct nfs_server *server,
1130 enum open_claim_type4 claim)
1131{
1132 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1133 return claim;
1134 switch (claim) {
1135 default:
1136 return claim;
1137 case NFS4_OPEN_CLAIM_FH:
1138 return NFS4_OPEN_CLAIM_NULL;
1139 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1140 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1141 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1142 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1143 }
1144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147{
1148 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001149 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150 p->o_res.seqid = p->o_arg.seqid;
1151 p->c_res.seqid = p->c_arg.seqid;
1152 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001153 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001154 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001155 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156}
1157
Al Viro82a2c1b2011-06-22 18:30:55 -04001158static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001160 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001161 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001162 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001163 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001164{
Al Viro82a2c1b2011-06-22 18:30:55 -04001165 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001166 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001167 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001168 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 struct nfs4_opendata *p;
1170
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001171 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 if (p == NULL)
1173 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001174
1175 p->f_label = nfs4_label_alloc(server, gfp_mask);
1176 if (IS_ERR(p->f_label))
1177 goto err_free_p;
1178
Kinglong Meea49c2692015-07-27 15:31:38 +08001179 p->a_label = nfs4_label_alloc(server, gfp_mask);
1180 if (IS_ERR(p->a_label))
1181 goto err_free_f;
1182
Trond Myklebust63f5f792015-01-23 19:19:25 -05001183 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1184 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001185 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001186 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001187 nfs_sb_active(dentry->d_sb);
1188 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->dir = parent;
1190 p->owner = sp;
1191 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001192 p->o_arg.open_flags = flags;
1193 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001194 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001195 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001196 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1197 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001198 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1199 * will return permission denied for all bits until close */
1200 if (!(flags & O_EXCL)) {
1201 /* ask server to check for all possible rights as results
1202 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001203 switch (p->o_arg.claim) {
1204 default:
1205 break;
1206 case NFS4_OPEN_CLAIM_NULL:
1207 case NFS4_OPEN_CLAIM_FH:
1208 p->o_arg.access = NFS4_ACCESS_READ |
1209 NFS4_ACCESS_MODIFY |
1210 NFS4_ACCESS_EXTEND |
1211 NFS4_ACCESS_EXECUTE;
1212 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001213 }
David Howells7539bba2006-08-22 20:06:09 -04001214 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001215 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1216 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001217 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001218 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001219 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001220 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001221 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001222 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001223 case NFS4_OPEN_CLAIM_NULL:
1224 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1225 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1226 p->o_arg.fh = NFS_FH(dir);
1227 break;
1228 case NFS4_OPEN_CLAIM_PREVIOUS:
1229 case NFS4_OPEN_CLAIM_FH:
1230 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1231 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001232 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001233 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001234 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001235 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001236
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 p->o_arg.u.attrs = &p->attrs;
1238 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001239
1240 verf[0] = jiffies;
1241 verf[1] = current->pid;
1242 memcpy(p->o_arg.u.verifier.data, verf,
1243 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001244 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001245 p->c_arg.fh = &p->o_res.fh;
1246 p->c_arg.stateid = &p->o_res.stateid;
1247 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001249 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001250 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001251
1252err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001253 nfs4_label_free(p->a_label);
1254err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001255 nfs4_label_free(p->f_label);
1256err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 kfree(p);
1258err:
1259 dput(parent);
1260 return NULL;
1261}
1262
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265 struct nfs4_opendata *p = container_of(kref,
1266 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001267 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001268
1269 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001270 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001271 if (p->state != NULL)
1272 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001274
Kinglong Meea49c2692015-07-27 15:31:38 +08001275 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001276 nfs4_label_free(p->f_label);
1277
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001279 dput(p->dentry);
1280 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001281 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001282 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283 kfree(p);
1284}
1285
1286static void nfs4_opendata_put(struct nfs4_opendata *p)
1287{
1288 if (p != NULL)
1289 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290}
1291
Trond Myklebust24311f82015-09-20 10:50:17 -04001292static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1293 fmode_t fmode)
1294{
1295 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1296 case FMODE_READ|FMODE_WRITE:
1297 return state->n_rdwr != 0;
1298 case FMODE_WRITE:
1299 return state->n_wronly != 0;
1300 case FMODE_READ:
1301 return state->n_rdonly != 0;
1302 }
1303 WARN_ON_ONCE(1);
1304 return false;
1305}
1306
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001307static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308{
1309 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310
Trond Myklebust536e43d2012-01-17 22:04:26 -05001311 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312 goto out;
1313 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001314 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001315 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1316 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001317 break;
1318 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001319 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1320 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001321 break;
1322 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001323 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1324 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 return ret;
1328}
1329
Trond Myklebust2a606182015-08-19 22:30:00 -05001330static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1331 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001333 if (delegation == NULL)
1334 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001336 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001337 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1338 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001339 switch (claim) {
1340 case NFS4_OPEN_CLAIM_NULL:
1341 case NFS4_OPEN_CLAIM_FH:
1342 break;
1343 case NFS4_OPEN_CLAIM_PREVIOUS:
1344 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1345 break;
1346 default:
1347 return 0;
1348 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001349 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350 return 1;
1351}
1352
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001354{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001356 case FMODE_WRITE:
1357 state->n_wronly++;
1358 break;
1359 case FMODE_READ:
1360 state->n_rdonly++;
1361 break;
1362 case FMODE_READ|FMODE_WRITE:
1363 state->n_rdwr++;
1364 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001366}
1367
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001368#ifdef CONFIG_NFS_V4_1
1369static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1370{
1371 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1372 return true;
1373 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1376 return true;
1377 return false;
1378}
1379#endif /* CONFIG_NFS_V4_1 */
1380
Trond Myklebustc9399f22017-11-06 15:28:01 -05001381static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1382{
1383 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1384 wake_up_all(&state->waitq);
1385}
1386
1387static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1388 const nfs4_stateid *stateid)
1389{
1390 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1391 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1392
1393 if (stateid_seqid == state_seqid + 1U ||
1394 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1395 nfs_state_log_update_open_stateid(state);
1396 else
1397 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1398}
1399
Trond Myklebust4f14c192014-02-12 19:15:06 -05001400static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001401{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402 struct nfs_client *clp = state->owner->so_server->nfs_client;
1403 bool need_recover = false;
1404
1405 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1406 need_recover = true;
1407 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1410 need_recover = true;
1411 if (need_recover)
1412 nfs4_state_mark_reclaim_nograce(clp, state);
1413}
1414
Trond Myklebustc9399f22017-11-06 15:28:01 -05001415/*
1416 * Check for whether or not the caller may update the open stateid
1417 * to the value passed in by stateid.
1418 *
1419 * Note: This function relies heavily on the server implementing
1420 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1421 * correctly.
1422 * i.e. The stateid seqids have to be initialised to 1, and
1423 * are then incremented on every state transition.
1424 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001425static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001426 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001427{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1429 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1430 if (stateid->seqid == cpu_to_be32(1))
1431 nfs_state_log_update_open_stateid(state);
1432 else
1433 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001434 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001436
1437 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1438 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001439 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001440 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return false;
1442}
1443
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1445{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001446 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1447 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001448 if (state->n_wronly)
1449 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1450 if (state->n_rdonly)
1451 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1452 if (state->n_rdwr)
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001454 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001455}
1456
Trond Myklebust226056c2014-02-11 10:41:07 -05001457static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1458 nfs4_stateid *stateid, fmode_t fmode)
1459{
1460 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1461 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1462 case FMODE_WRITE:
1463 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1464 break;
1465 case FMODE_READ:
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1467 break;
1468 case 0:
1469 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1470 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1471 clear_bit(NFS_OPEN_STATE, &state->flags);
1472 }
1473 if (stateid == NULL)
1474 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001475 /* Handle OPEN+OPEN_DOWNGRADE races */
1476 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1477 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001478 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001479 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001481 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001482 nfs4_stateid_copy(&state->stateid, stateid);
1483 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001484out:
1485 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486}
1487
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001488static void nfs_clear_open_stateid(struct nfs4_state *state,
1489 nfs4_stateid *arg_stateid,
1490 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001491{
1492 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001493 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1494 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1495 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001496 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001497 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1498 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499}
1500
Trond Myklebust1393d962016-09-22 13:39:13 -04001501static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001502 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001503{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001504 DEFINE_WAIT(wait);
1505 int status = 0;
1506 for (;;) {
1507
1508 if (!nfs_need_update_open_stateid(state, stateid))
1509 return;
1510 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001511 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001512 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001513 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001514 /* Rely on seqids for serialisation with NFSv4.0 */
1515 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1516 break;
1517
1518 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1519 /*
1520 * Ensure we process the state changes in the same order
1521 * in which the server processed them by delaying the
1522 * update of the stateid until we are in sequence.
1523 */
1524 write_sequnlock(&state->seqlock);
1525 spin_unlock(&state->owner->so_lock);
1526 rcu_read_unlock();
1527 if (!signal_pending(current)) {
1528 if (schedule_timeout(5*HZ) == 0)
1529 status = -EAGAIN;
1530 else
1531 status = 0;
1532 } else
1533 status = -EINTR;
1534 finish_wait(&state->waitq, &wait);
1535 rcu_read_lock();
1536 spin_lock(&state->owner->so_lock);
1537 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001538 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001539
1540 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1541 nfs4_stateid_copy(freeme, &state->open_stateid);
1542 nfs_test_and_clear_all_open_stateid(state);
1543 }
1544
Trond Myklebuste999e802014-02-10 18:20:47 -05001545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1546 nfs4_stateid_copy(&state->stateid, stateid);
1547 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001548 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001549}
1550
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001552 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001553 fmode_t fmode,
1554 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001556 /*
1557 * Protect the call to nfs4_state_set_mode_locked and
1558 * serialise the stateid update
1559 */
1560 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001561 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1562 switch (fmode) {
1563 case FMODE_READ:
1564 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1565 break;
1566 case FMODE_WRITE:
1567 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1568 break;
1569 case FMODE_READ|FMODE_WRITE:
1570 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001571 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001572 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001573 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001574}
1575
1576static void nfs_state_set_delegation(struct nfs4_state *state,
1577 const nfs4_stateid *deleg_stateid,
1578 fmode_t fmode)
1579{
1580 /*
1581 * Protect the call to nfs4_state_set_mode_locked and
1582 * serialise the stateid update
1583 */
1584 write_seqlock(&state->seqlock);
1585 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1586 set_bit(NFS_DELEGATED_STATE, &state->flags);
1587 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Trond Myklebust1393d962016-09-22 13:39:13 -04001590static int update_open_stateid(struct nfs4_state *state,
1591 const nfs4_stateid *open_stateid,
1592 const nfs4_stateid *delegation,
1593 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001594{
Trond Myklebust1393d962016-09-22 13:39:13 -04001595 struct nfs_server *server = NFS_SERVER(state->inode);
1596 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001597 struct nfs_inode *nfsi = NFS_I(state->inode);
1598 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001599 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001600 int ret = 0;
1601
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001602 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001603
1604 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001605 spin_lock(&state->owner->so_lock);
1606 if (open_stateid != NULL) {
1607 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1608 ret = 1;
1609 }
1610
Trond Myklebust34310432008-12-23 15:21:38 -05001611 deleg_cur = rcu_dereference(nfsi->delegation);
1612 if (deleg_cur == NULL)
1613 goto no_delegation;
1614
1615 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001616 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001617 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001618 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001619 goto no_delegation_unlock;
1620
1621 if (delegation == NULL)
1622 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001623 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001624 goto no_delegation_unlock;
1625
Trond Myklebustb7391f42008-12-23 15:21:52 -05001626 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001627 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001628 ret = 1;
1629no_delegation_unlock:
1630 spin_unlock(&deleg_cur->lock);
1631no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001632 if (ret)
1633 update_open_stateflags(state, fmode);
1634 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001635 rcu_read_unlock();
1636
Trond Myklebust4f14c192014-02-12 19:15:06 -05001637 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001638 nfs4_schedule_state_manager(clp);
1639 if (freeme.type != 0)
1640 nfs4_test_and_free_stateid(server, &freeme,
1641 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001642
1643 return ret;
1644}
1645
Trond Myklebust39071e62015-01-24 15:07:56 -05001646static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1647 const nfs4_stateid *stateid)
1648{
1649 struct nfs4_state *state = lsp->ls_state;
1650 bool ret = false;
1651
1652 spin_lock(&state->state_lock);
1653 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1654 goto out_noupdate;
1655 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1656 goto out_noupdate;
1657 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1658 ret = true;
1659out_noupdate:
1660 spin_unlock(&state->state_lock);
1661 return ret;
1662}
Trond Myklebust34310432008-12-23 15:21:38 -05001663
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001664static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001665{
1666 struct nfs_delegation *delegation;
1667
1668 rcu_read_lock();
1669 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001670 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671 rcu_read_unlock();
1672 return;
1673 }
1674 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001675 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676}
1677
Trond Myklebust6ee41262007-07-08 14:11:36 -04001678static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679{
1680 struct nfs4_state *state = opendata->state;
1681 struct nfs_inode *nfsi = NFS_I(state->inode);
1682 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001683 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001684 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001685 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001686 nfs4_stateid stateid;
1687 int ret = -EAGAIN;
1688
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001690 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001692 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001693 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001694 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001695 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001696 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001697 rcu_read_lock();
1698 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001699 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001700 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001701 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001702 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001703 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001704 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001705 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001706 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001707 if (!opendata->is_recover) {
1708 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1709 if (ret != 0)
1710 goto out;
1711 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001712 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001713
1714 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001715 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001716 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001718out:
1719 return ERR_PTR(ret);
1720out_return_state:
1721 atomic_inc(&state->count);
1722 return state;
1723}
1724
Andy Adamsone23008e2012-10-02 21:07:32 -04001725static void
1726nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1727{
1728 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1729 struct nfs_delegation *delegation;
1730 int delegation_flags = 0;
1731
1732 rcu_read_lock();
1733 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1734 if (delegation)
1735 delegation_flags = delegation->flags;
1736 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001737 switch (data->o_arg.claim) {
1738 default:
1739 break;
1740 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1741 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001742 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1743 "returning a delegation for "
1744 "OPEN(CLAIM_DELEGATE_CUR)\n",
1745 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001746 return;
1747 }
1748 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001749 nfs_inode_set_delegation(state->inode,
1750 data->owner->so_cred,
1751 &data->o_res);
1752 else
1753 nfs_inode_reclaim_delegation(state->inode,
1754 data->owner->so_cred,
1755 &data->o_res);
1756}
1757
1758/*
1759 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1760 * and update the nfs4_state.
1761 */
1762static struct nfs4_state *
1763_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1764{
1765 struct inode *inode = data->state->inode;
1766 struct nfs4_state *state = data->state;
1767 int ret;
1768
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001769 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001770 if (data->rpc_status)
1771 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001772 /* cached opens have already been processed */
1773 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001774 }
1775
Andy Adamsone23008e2012-10-02 21:07:32 -04001776 ret = nfs_refresh_inode(inode, &data->f_attr);
1777 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001778 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001779
1780 if (data->o_res.delegation_type != 0)
1781 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001782update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001783 update_open_stateid(state, &data->o_res.stateid, NULL,
1784 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001785 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001786
1787 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001788}
1789
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001790static struct inode *
1791nfs4_opendata_get_inode(struct nfs4_opendata *data)
1792{
1793 struct inode *inode;
1794
1795 switch (data->o_arg.claim) {
1796 case NFS4_OPEN_CLAIM_NULL:
1797 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1798 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1799 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1800 return ERR_PTR(-EAGAIN);
1801 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1802 &data->f_attr, data->f_label);
1803 break;
1804 default:
1805 inode = d_inode(data->dentry);
1806 ihold(inode);
1807 nfs_refresh_inode(inode, &data->f_attr);
1808 }
1809 return inode;
1810}
1811
Andy Adamsone23008e2012-10-02 21:07:32 -04001812static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001813nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1814{
1815 struct nfs4_state *state;
1816 struct inode *inode;
1817
1818 inode = nfs4_opendata_get_inode(data);
1819 if (IS_ERR(inode))
1820 return ERR_CAST(inode);
1821 if (data->state != NULL && data->state->inode == inode) {
1822 state = data->state;
1823 atomic_inc(&state->count);
1824 } else
1825 state = nfs4_get_open_state(inode, data->owner);
1826 iput(inode);
1827 if (state == NULL)
1828 state = ERR_PTR(-ENOMEM);
1829 return state;
1830}
1831
Andy Adamsone23008e2012-10-02 21:07:32 -04001832static struct nfs4_state *
1833_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001834{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001835 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001836
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001838 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001839 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 goto out;
1841 }
1842
Trond Myklebust75e8c482017-08-08 10:38:07 -04001843 state = nfs4_opendata_find_nfs4_state(data);
1844 if (IS_ERR(state))
1845 goto out;
1846
Andy Adamsone23008e2012-10-02 21:07:32 -04001847 if (data->o_res.delegation_type != 0)
1848 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001849 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001850 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001851out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001852 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001853 return state;
1854}
1855
Andy Adamsone23008e2012-10-02 21:07:32 -04001856static struct nfs4_state *
1857nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1858{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001859 struct nfs4_state *ret;
1860
Andy Adamsone23008e2012-10-02 21:07:32 -04001861 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001862 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1863 else
1864 ret = _nfs4_opendata_to_nfs4_state(data);
1865 nfs4_sequence_free_slot(&data->o_res.seq_res);
1866 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001867}
1868
Trond Myklebust864472e2006-01-03 09:55:15 +01001869static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1870{
1871 struct nfs_inode *nfsi = NFS_I(state->inode);
1872 struct nfs_open_context *ctx;
1873
1874 spin_lock(&state->inode->i_lock);
1875 list_for_each_entry(ctx, &nfsi->open_files, list) {
1876 if (ctx->state != state)
1877 continue;
1878 get_nfs_open_context(ctx);
1879 spin_unlock(&state->inode->i_lock);
1880 return ctx;
1881 }
1882 spin_unlock(&state->inode->i_lock);
1883 return ERR_PTR(-ENOENT);
1884}
1885
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001886static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1887 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001888{
1889 struct nfs4_opendata *opendata;
1890
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001891 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001892 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001893 if (opendata == NULL)
1894 return ERR_PTR(-ENOMEM);
1895 opendata->state = state;
1896 atomic_inc(&state->count);
1897 return opendata;
1898}
1899
Trond Myklebust24311f82015-09-20 10:50:17 -04001900static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1901 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001902{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001903 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001904 int ret;
1905
Trond Myklebust24311f82015-09-20 10:50:17 -04001906 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001907 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001908 opendata->o_arg.open_flags = 0;
1909 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001910 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1911 NFS_SB(opendata->dentry->d_sb),
1912 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001913 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1914 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1915 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001916 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001917 if (ret != 0)
1918 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001919 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001920 if (IS_ERR(newstate))
1921 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001922 if (newstate != opendata->state)
1923 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001924 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001925 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001926}
1927
1928static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1929{
Trond Myklebust864472e2006-01-03 09:55:15 +01001930 int ret;
1931
Trond Myklebust4f14c192014-02-12 19:15:06 -05001932 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1933 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1934 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1935 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001936 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001937 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001938 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001939 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001940 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1941 if (ret != 0)
1942 return ret;
1943 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1944 if (ret != 0)
1945 return ret;
1946 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1947 if (ret != 0)
1948 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001949 /*
1950 * We may have performed cached opens for all three recoveries.
1951 * Check if we need to update the current stateid.
1952 */
1953 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001954 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001955 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001957 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001958 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001959 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001960 return 0;
1961}
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963/*
1964 * OPEN_RECLAIM:
1965 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001967static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001969 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001970 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001971 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 int status;
1973
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001974 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1975 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001976 if (IS_ERR(opendata))
1977 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001978 rcu_read_lock();
1979 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001980 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001981 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001982 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001983 opendata->o_arg.u.delegation_type = delegation_type;
1984 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001985 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return status;
1987}
1988
Trond Myklebust539cd032007-06-05 11:46:42 -04001989static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct nfs_server *server = NFS_SERVER(state->inode);
1992 struct nfs4_exception exception = { };
1993 int err;
1994 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001995 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001996 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001997 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1998 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001999 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002000 break;
2001 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 } while (exception.retry);
2003 return err;
2004}
2005
Trond Myklebust864472e2006-01-03 09:55:15 +01002006static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2007{
2008 struct nfs_open_context *ctx;
2009 int ret;
2010
2011 ctx = nfs4_state_find_open_context(state);
2012 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002013 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002014 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002015 put_nfs_open_context(ctx);
2016 return ret;
2017}
2018
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002019static 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 -07002020{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002021 switch (err) {
2022 default:
2023 printk(KERN_ERR "NFS: %s: unhandled error "
2024 "%d.\n", __func__, err);
2025 case 0:
2026 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002027 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002028 case -ESTALE:
2029 break;
2030 case -NFS4ERR_BADSESSION:
2031 case -NFS4ERR_BADSLOT:
2032 case -NFS4ERR_BAD_HIGH_SLOT:
2033 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2034 case -NFS4ERR_DEADSESSION:
2035 set_bit(NFS_DELEGATED_STATE, &state->flags);
2036 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2037 return -EAGAIN;
2038 case -NFS4ERR_STALE_CLIENTID:
2039 case -NFS4ERR_STALE_STATEID:
2040 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002041 /* Don't recall a delegation if it was lost */
2042 nfs4_schedule_lease_recovery(server->nfs_client);
2043 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002044 case -NFS4ERR_MOVED:
2045 nfs4_schedule_migration_recovery(server);
2046 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002047 case -NFS4ERR_LEASE_MOVED:
2048 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2049 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002050 case -NFS4ERR_DELEG_REVOKED:
2051 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002052 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002053 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002054 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002055 nfs_inode_find_state_and_recover(state->inode,
2056 stateid);
2057 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002058 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002059 case -NFS4ERR_DELAY:
2060 case -NFS4ERR_GRACE:
2061 set_bit(NFS_DELEGATED_STATE, &state->flags);
2062 ssleep(1);
2063 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002064 case -ENOMEM:
2065 case -NFS4ERR_DENIED:
2066 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2067 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return err;
2070}
2071
Trond Myklebust24311f82015-09-20 10:50:17 -04002072int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2073 struct nfs4_state *state, const nfs4_stateid *stateid,
2074 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002075{
2076 struct nfs_server *server = NFS_SERVER(state->inode);
2077 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002078 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002079
2080 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2081 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2082 if (IS_ERR(opendata))
2083 return PTR_ERR(opendata);
2084 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002085 write_seqlock(&state->seqlock);
2086 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2087 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002088 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2089 switch (type & (FMODE_READ|FMODE_WRITE)) {
2090 case FMODE_READ|FMODE_WRITE:
2091 case FMODE_WRITE:
2092 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2093 if (err)
2094 break;
2095 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2096 if (err)
2097 break;
2098 case FMODE_READ:
2099 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2100 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002101 nfs4_opendata_put(opendata);
2102 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2103}
2104
Chuck Leverbe05c862013-08-09 12:49:47 -04002105static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2106{
2107 struct nfs4_opendata *data = calldata;
2108
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002109 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2110 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002111}
2112
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002113static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2114{
2115 struct nfs4_opendata *data = calldata;
2116
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002117 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002118
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002119 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002120 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002121 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002122 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002124 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002126}
2127
2128static void nfs4_open_confirm_release(void *calldata)
2129{
2130 struct nfs4_opendata *data = calldata;
2131 struct nfs4_state *state = NULL;
2132
2133 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002134 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002135 goto out_free;
2136 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002137 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002138 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002139 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002140 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002141 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002142out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002143 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002144}
2145
2146static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002147 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002148 .rpc_call_done = nfs4_open_confirm_done,
2149 .rpc_release = nfs4_open_confirm_release,
2150};
2151
2152/*
2153 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2154 */
2155static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2156{
David Howells2b0143b2015-03-17 22:25:59 +00002157 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002158 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002159 struct rpc_message msg = {
2160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2161 .rpc_argp = &data->c_arg,
2162 .rpc_resp = &data->c_res,
2163 .rpc_cred = data->owner->so_cred,
2164 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002165 struct rpc_task_setup task_setup_data = {
2166 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002168 .callback_ops = &nfs4_open_confirm_ops,
2169 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002170 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002171 .flags = RPC_TASK_ASYNC,
2172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 int status;
2174
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002175 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002176 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002177 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002178 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002179 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002180 if (data->is_recover)
2181 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002182 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002183 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002184 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002185 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002186 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002187 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002188 smp_wmb();
2189 } else
2190 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002191 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return status;
2193}
2194
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002197 struct nfs4_opendata *data = calldata;
2198 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002199 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002200 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002201
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002203 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002204 /*
2205 * Check if we still need to send an OPEN call, or if we can use
2206 * a delegation instead.
2207 */
2208 if (data->state != NULL) {
2209 struct nfs_delegation *delegation;
2210
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002211 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002212 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002213 rcu_read_lock();
2214 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002215 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002216 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002217 rcu_read_unlock();
2218 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002219 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002220 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002221 switch (claim) {
2222 default:
2223 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002224 case NFS4_OPEN_CLAIM_PREVIOUS:
2225 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2226 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002227 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002228 case NFS4_OPEN_CLAIM_FH:
2229 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002230 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002231 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002232 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002233 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002234 &data->o_res.seq_res,
2235 task) != 0)
2236 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002237
2238 /* Set the create mode (note dependency on the session type) */
2239 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2240 if (data->o_arg.open_flags & O_EXCL) {
2241 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2242 if (nfs4_has_persistent_session(clp))
2243 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2244 else if (clp->cl_mvops->minor_version > 0)
2245 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2246 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002247 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002248unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002249 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002250 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002251out_no_action:
2252 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002253out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002254 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002255}
2256
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002257static void nfs4_open_done(struct rpc_task *task, void *calldata)
2258{
2259 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002261 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002262
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002263 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002264 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002265
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002267 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2268 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002269 case S_IFREG:
2270 break;
2271 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002272 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002273 break;
2274 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002276 break;
2277 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002279 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002280 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002281 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002282 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2283 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002284 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002285 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002286}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002287
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002288static void nfs4_open_release(void *calldata)
2289{
2290 struct nfs4_opendata *data = calldata;
2291 struct nfs4_state *state = NULL;
2292
2293 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002294 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295 goto out_free;
2296 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002297 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298 goto out_free;
2299 /* In case we need an open_confirm, no cleanup! */
2300 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2301 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002302 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002303 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002304 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002306 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307}
2308
2309static const struct rpc_call_ops nfs4_open_ops = {
2310 .rpc_call_prepare = nfs4_open_prepare,
2311 .rpc_call_done = nfs4_open_done,
2312 .rpc_release = nfs4_open_release,
2313};
2314
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002315static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002316{
David Howells2b0143b2015-03-17 22:25:59 +00002317 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318 struct nfs_server *server = NFS_SERVER(dir);
2319 struct nfs_openargs *o_arg = &data->o_arg;
2320 struct nfs_openres *o_res = &data->o_res;
2321 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002322 struct rpc_message msg = {
2323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2324 .rpc_argp = o_arg,
2325 .rpc_resp = o_res,
2326 .rpc_cred = data->owner->so_cred,
2327 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002328 struct rpc_task_setup task_setup_data = {
2329 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002330 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 .callback_ops = &nfs4_open_ops,
2332 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002333 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 .flags = RPC_TASK_ASYNC,
2335 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002336 int status;
2337
Chuck Levera9c92d62013-08-09 12:48:18 -04002338 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002339 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002340 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002341 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002342 data->cancelled = false;
2343 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002344 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002345 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002346 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002347 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002348 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002349 if (IS_ERR(task))
2350 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002351 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002352 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002353 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002354 smp_wmb();
2355 } else
2356 status = data->rpc_status;
2357 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002358
2359 return status;
2360}
2361
2362static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2363{
David Howells2b0143b2015-03-17 22:25:59 +00002364 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002365 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002366 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002367
2368 status = nfs4_run_open_task(data, 1);
2369 if (status != 0 || !data->rpc_done)
2370 return status;
2371
Trond Myklebust6926afd2012-01-07 13:22:46 -05002372 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2373
Anna Schumakerd7e98252017-01-11 16:13:29 -05002374 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002375 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376
2377 return status;
2378}
2379
Trond Myklebustf3792d62014-07-10 08:54:32 -04002380/*
2381 * Additional permission checks in order to distinguish between an
2382 * open for read, and an open for execute. This works around the
2383 * fact that NFSv4 OPEN treats read and execute permissions as being
2384 * the same.
2385 * Note that in the non-execute case, we want to turn off permission
2386 * checking if we just created a new file (POSIX open() semantics).
2387 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002388static int nfs4_opendata_access(struct rpc_cred *cred,
2389 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002390 struct nfs4_state *state, fmode_t fmode,
2391 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002392{
2393 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002394 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002395
2396 /* access call failed or for some reason the server doesn't
2397 * support any access modes -- defer access call until later */
2398 if (opendata->o_res.access_supported == 0)
2399 return 0;
2400
2401 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002402 /*
2403 * Use openflags to check for exec, because fmode won't
2404 * always have FMODE_EXEC set when file open for exec.
2405 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002406 if (openflags & __FMODE_EXEC) {
2407 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002408 if (S_ISDIR(state->inode->i_mode))
2409 mask = NFS4_ACCESS_LOOKUP;
2410 else
2411 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002412 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002413 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002414
2415 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002416 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2417 nfs_access_add_cache(state->inode, &cache);
2418
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002419 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2420 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002421 return 0;
2422
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002423 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424}
2425
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002426/*
2427 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2428 */
2429static int _nfs4_proc_open(struct nfs4_opendata *data)
2430{
David Howells2b0143b2015-03-17 22:25:59 +00002431 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002432 struct nfs_server *server = NFS_SERVER(dir);
2433 struct nfs_openargs *o_arg = &data->o_arg;
2434 struct nfs_openres *o_res = &data->o_res;
2435 int status;
2436
2437 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002438 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002439 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002440 if (status != 0) {
2441 if (status == -NFS4ERR_BADNAME &&
2442 !(o_arg->open_flags & O_CREAT))
2443 return -ENOENT;
2444 return status;
2445 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002446
Trond Myklebust6926afd2012-01-07 13:22:46 -05002447 nfs_fattr_map_and_free_names(server, &data->f_attr);
2448
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002449 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002450 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002451 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002452 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002453 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002454 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002455 update_changeattr(dir, &o_res->cinfo,
2456 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002457 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002458 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2459 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002461 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002463 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002465 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2466 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002467 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002468 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/*
2473 * OPEN_EXPIRED:
2474 * reclaim state on the server after a network partition.
2475 * Assumes caller holds the appropriate lock
2476 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002477static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002479 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002480 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002482 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002483 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002484 if (IS_ERR(opendata))
2485 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002486 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002487 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002488 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002489 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002493static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002494{
Trond Myklebust539cd032007-06-05 11:46:42 -04002495 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002496 struct nfs4_exception exception = { };
2497 int err;
2498
2499 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002500 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002501 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002502 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2503 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002504 switch (err) {
2505 default:
2506 goto out;
2507 case -NFS4ERR_GRACE:
2508 case -NFS4ERR_DELAY:
2509 nfs4_handle_exception(server, err, &exception);
2510 err = 0;
2511 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002512 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002513out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002514 return err;
2515}
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002520 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Trond Myklebust864472e2006-01-03 09:55:15 +01002522 ctx = nfs4_state_find_open_context(state);
2523 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002524 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002525 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002526 put_nfs_open_context(ctx);
2527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
Trond Myklebust41020b62016-09-22 13:38:58 -04002530static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2531 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002532{
Trond Myklebust41020b62016-09-22 13:38:58 -04002533 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002534 write_seqlock(&state->seqlock);
2535 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2536 write_sequnlock(&state->seqlock);
2537 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2538}
2539
2540static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2541{
2542 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002543 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002544}
2545
2546static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2547{
2548 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2549 nfs40_clear_delegation_stateid(state);
2550 return nfs4_open_expired(sp, state);
2551}
2552
Trond Myklebust45870d62016-09-22 13:38:59 -04002553static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2554 nfs4_stateid *stateid,
2555 struct rpc_cred *cred)
2556{
2557 return -NFS4ERR_BAD_STATEID;
2558}
2559
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002560#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002561static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2562 nfs4_stateid *stateid,
2563 struct rpc_cred *cred)
2564{
2565 int status;
2566
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002567 switch (stateid->type) {
2568 default:
2569 break;
2570 case NFS4_INVALID_STATEID_TYPE:
2571 case NFS4_SPECIAL_STATEID_TYPE:
2572 return -NFS4ERR_BAD_STATEID;
2573 case NFS4_REVOKED_STATEID_TYPE:
2574 goto out_free;
2575 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002576
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002577 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002578 switch (status) {
2579 case -NFS4ERR_EXPIRED:
2580 case -NFS4ERR_ADMIN_REVOKED:
2581 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002582 break;
2583 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002584 return status;
2585 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002586out_free:
2587 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002588 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002589 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002590}
2591
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002592static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002593{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002594 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002595 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002596 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002597 struct rpc_cred *cred;
2598 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002599
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002600 /* Get the delegation credential for use by test/free_stateid */
2601 rcu_read_lock();
2602 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002603 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002604 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002605 return;
2606 }
2607
2608 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002609 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2610 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2611 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002612 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002613 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002614 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002615 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002616
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002617 cred = get_rpccred(delegation->cred);
2618 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002619 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002621 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002622 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002623
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002624 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625}
2626
2627/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002628 * nfs41_check_expired_locks - possibly free a lock stateid
2629 *
2630 * @state: NFSv4 state for an inode
2631 *
2632 * Returns NFS_OK if recovery for this stateid is now finished.
2633 * Otherwise a negative NFS4ERR value is returned.
2634 */
2635static int nfs41_check_expired_locks(struct nfs4_state *state)
2636{
2637 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002638 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002639 struct nfs_server *server = NFS_SERVER(state->inode);
2640
2641 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2642 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002643
2644 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002645 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2646 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2647 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2648
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002649 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002650 spin_unlock(&state->state_lock);
2651
2652 nfs4_put_lock_state(prev);
2653 prev = lsp;
2654
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002655 status = nfs41_test_and_free_expired_stateid(server,
2656 &lsp->ls_stateid,
2657 cred);
2658 trace_nfs4_test_lock_stateid(state, lsp, status);
2659 if (status == -NFS4ERR_EXPIRED ||
2660 status == -NFS4ERR_BAD_STATEID) {
2661 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002662 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002663 if (!recover_lost_locks)
2664 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2665 } else if (status != NFS_OK) {
2666 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002667 nfs4_put_lock_state(prev);
2668 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002669 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002670 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002671 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002672 }
2673 spin_unlock(&state->state_lock);
2674 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002675out:
2676 return ret;
2677}
2678
2679/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002680 * nfs41_check_open_stateid - possibly free an open stateid
2681 *
2682 * @state: NFSv4 state for an inode
2683 *
2684 * Returns NFS_OK if recovery for this stateid is now finished.
2685 * Otherwise a negative NFS4ERR value is returned.
2686 */
2687static int nfs41_check_open_stateid(struct nfs4_state *state)
2688{
2689 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002690 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002691 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002692 int status;
2693
Trond Myklebustb134fc42016-09-22 13:39:16 -04002694 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002695 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2696 if (nfs4_have_delegation(state->inode, state->state))
2697 return NFS_OK;
2698 return -NFS4ERR_OPENMODE;
2699 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002700 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002701 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002702 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002703 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002704 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002705 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2706 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2707 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002708 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002709 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002710 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002711 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002712 if (nfs_open_stateid_recover_openmode(state))
2713 return -NFS4ERR_OPENMODE;
2714 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002715}
2716
2717static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2718{
Chuck Levereb64cf92012-07-11 16:30:05 -04002719 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002720
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002721 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002722 status = nfs41_check_expired_locks(state);
2723 if (status != NFS_OK)
2724 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002725 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002726 if (status != NFS_OK)
2727 status = nfs4_open_expired(sp, state);
2728 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002729}
2730#endif
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002733 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2734 * fields corresponding to attributes that were used to store the verifier.
2735 * Make sure we clobber those fields in the later setattr call
2736 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002737static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2738 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002739{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002740 const u32 *attrset = opendata->o_res.attrset;
2741
2742 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002743 !(sattr->ia_valid & ATTR_ATIME_SET))
2744 sattr->ia_valid |= ATTR_ATIME;
2745
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002746 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002747 !(sattr->ia_valid & ATTR_MTIME_SET))
2748 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002749
2750 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002751 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002752 (attrset[1] & FATTR4_WORD1_MODE ||
2753 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002754 sattr->ia_valid &= ~ATTR_MODE;
2755
2756 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2757 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002758}
2759
Trond Myklebustc21443c2013-02-07 14:26:21 -05002760static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2761 fmode_t fmode,
2762 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002763 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002764{
2765 struct nfs4_state_owner *sp = opendata->owner;
2766 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002767 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002768 struct nfs4_state *state;
2769 unsigned int seq;
2770 int ret;
2771
2772 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2773
2774 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002775 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002776 goto out;
2777
2778 state = nfs4_opendata_to_nfs4_state(opendata);
2779 ret = PTR_ERR(state);
2780 if (IS_ERR(state))
2781 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002782 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002783 if (server->caps & NFS_CAP_POSIX_LOCK)
2784 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002785 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2786 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002787
Trond Myklebust275bb302013-05-29 13:11:28 -04002788 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002789 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002790 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002791 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002792 alias = d_exact_alias(dentry, state->inode);
2793 if (!alias)
2794 alias = d_splice_alias(igrab(state->inode), dentry);
2795 /* d_splice_alias() can't fail here - it's a non-directory */
2796 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002797 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002798 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002799 }
2800 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002801 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002802 }
2803
Trond Myklebustc21443c2013-02-07 14:26:21 -05002804 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2805 if (ret != 0)
2806 goto out;
2807
David Howells2b0143b2015-03-17 22:25:59 +00002808 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002809 nfs_inode_attach_open_context(ctx);
2810 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2811 nfs4_schedule_stateid_recovery(server, state);
2812 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002813out:
2814 return ret;
2815}
2816
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002817/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002818 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 */
Andy Adamson82be4172012-05-23 05:02:35 -04002820static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002821 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002822 int flags,
2823 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002824 struct nfs4_label *label,
2825 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct nfs4_state_owner *sp;
2828 struct nfs4_state *state = NULL;
2829 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002830 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002831 struct dentry *dentry = ctx->dentry;
2832 struct rpc_cred *cred = ctx->cred;
2833 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2834 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002835 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002836 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002837 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002841 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2842 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2844 goto out_err;
2845 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002846 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002847 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002848 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002849 if (d_really_is_positive(dentry))
2850 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002851 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002852 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002853 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002854 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002855 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002856 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002857 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
David Quigley14c43f72013-05-22 12:50:43 -04002859 if (label) {
2860 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2861 if (IS_ERR(olabel)) {
2862 status = PTR_ERR(olabel);
2863 goto err_opendata_put;
2864 }
2865 }
2866
Trond Myklebuste911b812014-03-26 13:24:37 -07002867 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2868 if (!opendata->f_attr.mdsthreshold) {
2869 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2870 if (!opendata->f_attr.mdsthreshold)
2871 goto err_free_label;
2872 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002873 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002874 }
David Howells2b0143b2015-03-17 22:25:59 +00002875 if (d_really_is_positive(dentry))
2876 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002877
Trond Myklebust3efb9722013-05-29 13:17:04 -04002878 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002879 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002880 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002881 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002882
NeilBrownefcbc042015-07-30 13:00:56 +10002883 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002884 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002885 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002886 /*
2887 * send create attributes which was not set by open
2888 * with an extra setattr.
2889 */
2890 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2891 nfs_fattr_init(opendata->o_res.f_attr);
2892 status = nfs4_do_setattr(state->inode, cred,
2893 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002894 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002895 if (status == 0) {
2896 nfs_setattr_update_inode(state->inode, sattr,
2897 opendata->o_res.f_attr);
2898 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2899 }
David Quigley1775fd32013-05-22 12:50:42 -04002900 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002901 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002902 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002903 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002904
Trond Myklebuste911b812014-03-26 13:24:37 -07002905 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002906 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002907 opendata->f_attr.mdsthreshold = NULL;
2908 }
Andy Adamson82be4172012-05-23 05:02:35 -04002909
David Quigley14c43f72013-05-22 12:50:43 -04002910 nfs4_label_free(olabel);
2911
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002912 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002915err_free_label:
2916 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002917err_opendata_put:
2918 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002919err_put_state_owner:
2920 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return status;
2923}
2924
2925
Andy Adamson82be4172012-05-23 05:02:35 -04002926static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002927 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002928 int flags,
2929 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002930 struct nfs4_label *label,
2931 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002933 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct nfs4_exception exception = { };
2935 struct nfs4_state *res;
2936 int status;
2937
2938 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002939 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002940 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002941 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (status == 0)
2943 break;
2944 /* NOTE: BAD_SEQID means the server and client disagree about the
2945 * book-keeping w.r.t. state-changing operations
2946 * (OPEN/CLOSE/LOCK/LOCKU...)
2947 * It is actually a sign of a bug on the client or on the server.
2948 *
2949 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002950 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 * have unhashed the old state_owner for us, and that we can
2952 * therefore safely retry using a new one. We should still warn
2953 * the user though...
2954 */
2955 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002956 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002957 " returned a bad sequence-id error!\n",
2958 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 exception.retry = 1;
2960 continue;
2961 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002962 /*
2963 * BAD_STATEID on OPEN means that the server cancelled our
2964 * state before it received the OPEN_CONFIRM.
2965 * Recover by retrying the request as per the discussion
2966 * on Page 181 of RFC3530.
2967 */
2968 if (status == -NFS4ERR_BAD_STATEID) {
2969 exception.retry = 1;
2970 continue;
2971 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002972 if (status == -EAGAIN) {
2973 /* We must have found a delegation */
2974 exception.retry = 1;
2975 continue;
2976 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002977 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2978 continue;
2979 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 status, &exception));
2981 } while (exception.retry);
2982 return res;
2983}
2984
Trond Myklebust8487c472016-06-26 08:44:35 -04002985static int _nfs4_do_setattr(struct inode *inode,
2986 struct nfs_setattrargs *arg,
2987 struct nfs_setattrres *res,
2988 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002989 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002990{
2991 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002992 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2994 .rpc_argp = arg,
2995 .rpc_resp = res,
2996 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002997 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002998 struct rpc_cred *delegation_cred = NULL;
2999 unsigned long timestamp = jiffies;
3000 fmode_t fmode;
3001 bool truncate;
3002 int status;
3003
3004 nfs_fattr_init(res->fattr);
3005
3006 /* Servers should only apply open mode checks for file size changes */
3007 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3008 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3009
3010 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3011 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003012 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003013 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003014 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003015 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003016 l_ctx = nfs_get_lock_context(ctx);
3017 if (IS_ERR(l_ctx))
3018 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003019 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3020 &arg->stateid, &delegation_cred);
3021 nfs_put_lock_context(l_ctx);
3022 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003023 return -EBADF;
3024 } else
3025 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3026 if (delegation_cred)
3027 msg.rpc_cred = delegation_cred;
3028
3029 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3030
3031 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003032 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003033 renew_lease(server, timestamp);
3034 trace_nfs4_setattr(inode, &arg->stateid, status);
3035 return status;
3036}
3037
3038static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3039 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003040 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003041 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003043 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003044 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003045 struct nfs_setattrargs arg = {
3046 .fh = NFS_FH(inode),
3047 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 .server = server,
3049 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003050 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003051 };
3052 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003054 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003056 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003057 struct nfs4_exception exception = {
3058 .state = state,
3059 .inode = inode,
3060 .stateid = &arg.stateid,
3061 };
3062 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
David Quigleyaa9c2662013-05-22 12:50:44 -04003064 arg.bitmask = nfs4_bitmask(server, ilabel);
3065 if (ilabel)
3066 arg.bitmask = nfs4_bitmask(server, olabel);
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003069 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003070 switch (err) {
3071 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003072 if (!(sattr->ia_valid & ATTR_SIZE)) {
3073 pr_warn_once("NFSv4: server %s is incorrectly "
3074 "applying open mode checks to "
3075 "a SETATTR that is not "
3076 "changing file size.\n",
3077 server->nfs_client->cl_hostname);
3078 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003079 if (state && !(state->state & FMODE_WRITE)) {
3080 err = -EBADF;
3081 if (sattr->ia_valid & ATTR_OPEN)
3082 err = -EACCES;
3083 goto out;
3084 }
3085 }
3086 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003088out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return err;
3090}
3091
Peng Tao500d7012015-09-22 11:35:22 +08003092static bool
3093nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3094{
3095 if (inode == NULL || !nfs_have_layout(inode))
3096 return false;
3097
3098 return pnfs_wait_on_layoutreturn(inode, task);
3099}
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101struct nfs4_closedata {
3102 struct inode *inode;
3103 struct nfs4_state *state;
3104 struct nfs_closeargs arg;
3105 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003106 struct {
3107 struct nfs4_layoutreturn_args arg;
3108 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003109 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003110 u32 roc_barrier;
3111 bool roc;
3112 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003113 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003114 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115};
3116
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003117static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003118{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003119 struct nfs4_closedata *calldata = data;
3120 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003121 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003122
Trond Myklebustcf805162016-11-15 14:56:07 -05003123 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003124 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3125 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003126 nfs4_put_open_state(calldata->state);
3127 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003128 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003129 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003130 kfree(calldata);
3131}
3132
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003133static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003135 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003138 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Chuck Levera3ca5652012-03-01 17:00:40 -05003140 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003141 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3142 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003143 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003144
3145 /* Handle Layoutreturn errors */
3146 if (calldata->arg.lr_args && task->tk_status != 0) {
3147 switch (calldata->res.lr_ret) {
3148 default:
3149 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3150 break;
3151 case 0:
3152 calldata->arg.lr_args = NULL;
3153 calldata->res.lr_res = NULL;
3154 break;
3155 case -NFS4ERR_ADMIN_REVOKED:
3156 case -NFS4ERR_DELEG_REVOKED:
3157 case -NFS4ERR_EXPIRED:
3158 case -NFS4ERR_BAD_STATEID:
3159 case -NFS4ERR_OLD_STATEID:
3160 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3161 case -NFS4ERR_WRONG_CRED:
3162 calldata->arg.lr_args = NULL;
3163 calldata->res.lr_res = NULL;
3164 calldata->res.lr_ret = 0;
3165 rpc_restart_call_prepare(task);
3166 return;
3167 }
3168 }
3169
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003170 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 * the state_owner. we keep this around to process errors
3172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 switch (task->tk_status) {
3174 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003175 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003176 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003177 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003178 case -NFS4ERR_ACCESS:
3179 if (calldata->arg.bitmask != NULL) {
3180 calldata->arg.bitmask = NULL;
3181 calldata->res.fattr = NULL;
3182 task->tk_status = 0;
3183 rpc_restart_call_prepare(task);
3184 goto out_release;
3185
3186 }
3187 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003188 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003190 case -NFS4ERR_EXPIRED:
3191 nfs4_free_revoked_stateid(server,
3192 &calldata->arg.stateid,
3193 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003194 case -NFS4ERR_OLD_STATEID:
3195 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003196 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003197 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003198 rpc_restart_call_prepare(task);
3199 goto out_release;
3200 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003201 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003204 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003205 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003206 goto out_release;
3207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003209 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3210 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003211out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003212 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003213 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003214 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003217static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003219 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003220 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003221 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003222 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003223 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003224
Chuck Levera3ca5652012-03-01 17:00:40 -05003225 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003226 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003227 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003228
Trond Myklebust88069f72009-12-08 08:33:16 -05003229 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003230 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003231 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3232 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3233 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003234 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003235 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003236 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003237 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003238 if (state->n_rdonly == 0)
3239 call_close |= is_rdonly;
3240 else if (is_rdonly)
3241 calldata->arg.fmode |= FMODE_READ;
3242 if (state->n_wronly == 0)
3243 call_close |= is_wronly;
3244 else if (is_wronly)
3245 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003246 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3247 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003248 } else if (is_rdwr)
3249 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3250
Trond Myklebust5cc78612016-11-14 11:19:56 -05003251 if (!nfs4_valid_open_stateid(state) ||
3252 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003253 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003254 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003255
3256 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003257 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003258 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003259 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003260
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003261 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003262 nfs_release_seqid(calldata->arg.seqid);
3263 goto out_wait;
3264 }
3265
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003266 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003267 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003268
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003269 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003270 /* Close-to-open cache consistency revalidation */
3271 if (!nfs4_have_delegation(inode, FMODE_READ))
3272 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3273 else
3274 calldata->arg.bitmask = NULL;
3275 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003276
Trond Myklebust6ae37332015-01-30 14:21:14 -05003277 calldata->arg.share_access =
3278 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3279 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003280
Trond Myklebustd8d84982016-12-19 12:14:44 -05003281 if (calldata->res.fattr == NULL)
3282 calldata->arg.bitmask = NULL;
3283 else if (calldata->arg.bitmask == NULL)
3284 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003285 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003286 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003287 &calldata->arg.seq_args,
3288 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003289 task) != 0)
3290 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003291 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003292 return;
3293out_no_action:
3294 task->tk_action = NULL;
3295out_wait:
3296 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003299static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003300 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003301 .rpc_call_done = nfs4_close_done,
3302 .rpc_release = nfs4_free_closedata,
3303};
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305/*
3306 * It is possible for data to be read/written from a mem-mapped file
3307 * after the sys_close call (which hits the vfs layer as a flush).
3308 * This means that we can't safely call nfsv4 close on a file until
3309 * the inode is cleared. This in turn means that we are not good
3310 * NFSv4 citizens - we do not indicate to the server to update the file's
3311 * share state even when we are done with one of the three share
3312 * stateid's in the inode.
3313 *
3314 * NOTE: Caller must be holding the sp->so_owner semaphore!
3315 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003316int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003318 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003319 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003321 struct nfs4_state_owner *sp = state->owner;
3322 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3325 .rpc_cred = state->owner->so_cred,
3326 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003327 struct rpc_task_setup task_setup_data = {
3328 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003329 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003330 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003331 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003332 .flags = RPC_TASK_ASYNC,
3333 };
Trond Myklebust95121352005-10-18 14:20:12 -07003334 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003336 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3337 &task_setup_data.rpc_client, &msg);
3338
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003339 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003341 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003342 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003343 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003345 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003347 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3348 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003349 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003350 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003351 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003352 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003353 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003354 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003355 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003356 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003357 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003358 calldata->lr.roc = pnfs_roc(state->inode,
3359 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3360 if (calldata->lr.roc) {
3361 calldata->arg.lr_args = &calldata->lr.arg;
3362 calldata->res.lr_res = &calldata->lr.res;
3363 }
Al Viro643168c2011-06-22 18:20:23 -04003364 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003365
Trond Myklebust1174dd12010-12-21 10:52:24 -05003366 msg.rpc_argp = &calldata->arg;
3367 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003368 task_setup_data.callback_data = calldata;
3369 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003370 if (IS_ERR(task))
3371 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003372 status = 0;
3373 if (wait)
3374 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003375 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003376 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003377out_free_calldata:
3378 kfree(calldata);
3379out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003380 nfs4_put_open_state(state);
3381 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003382 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}
3384
Trond Myklebust2b484292010-09-17 10:56:51 -04003385static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003386nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3387 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003390 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3391
3392 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Trond Myklebust565277f2007-10-15 18:17:53 -04003394 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003395 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003396
3397 nfs4_label_release_security(label);
3398
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003399 if (IS_ERR(state))
3400 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003401 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Trond Myklebust1185a552009-12-03 15:54:02 -05003404static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003405{
3406 if (ctx->state == NULL)
3407 return;
3408 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003409 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003410 else
Al Viro643168c2011-06-22 18:20:23 -04003411 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Trond Myklebustb944dba2013-11-04 15:20:20 -05003414#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3415#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003416#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3419{
Kinglong Mee8c612822015-08-26 21:12:58 +08003420 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003421 struct nfs4_server_caps_arg args = {
3422 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003423 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003424 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 struct nfs4_server_caps_res res = {};
3426 struct rpc_message msg = {
3427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003428 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 .rpc_resp = &res,
3430 };
3431 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003432 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Kinglong Mee8c612822015-08-26 21:12:58 +08003434 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3435 FATTR4_WORD0_FH_EXPIRE_TYPE |
3436 FATTR4_WORD0_LINK_SUPPORT |
3437 FATTR4_WORD0_SYMLINK_SUPPORT |
3438 FATTR4_WORD0_ACLSUPPORT;
3439 if (minorversion)
3440 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3441
Bryan Schumaker7c513052011-03-24 17:12:24 +00003442 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003444 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003445 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003446 case 0:
3447 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3448 res.attr_bitmask[2] = 0;
3449 break;
3450 case 1:
3451 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3452 break;
3453 case 2:
3454 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003457 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3458 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3459 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3460 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003461 NFS_CAP_CTIME|NFS_CAP_MTIME|
3462 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003463 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3464 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 server->caps |= NFS_CAP_ACLS;
3466 if (res.has_links != 0)
3467 server->caps |= NFS_CAP_HARDLINKS;
3468 if (res.has_symlinks != 0)
3469 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003470 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3471 server->caps |= NFS_CAP_FILEID;
3472 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3473 server->caps |= NFS_CAP_MODE;
3474 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3475 server->caps |= NFS_CAP_NLINK;
3476 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3477 server->caps |= NFS_CAP_OWNER;
3478 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3479 server->caps |= NFS_CAP_OWNER_GROUP;
3480 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3481 server->caps |= NFS_CAP_ATIME;
3482 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3483 server->caps |= NFS_CAP_CTIME;
3484 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3485 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003486#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3487 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3488 server->caps |= NFS_CAP_SECURITY_LABEL;
3489#endif
3490 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3491 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003492 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003493
Trond Myklebusta65318b2009-03-11 14:10:28 -04003494 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3495 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3496 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003497 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003498
3499 /* Avoid a regression due to buggy server */
3500 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3501 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003502 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3503 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003506 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 return status;
3510}
3511
Trond Myklebust55a97592006-06-09 09:34:19 -04003512int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
3514 struct nfs4_exception exception = { };
3515 int err;
3516 do {
3517 err = nfs4_handle_exception(server,
3518 _nfs4_server_capabilities(server, fhandle),
3519 &exception);
3520 } while (exception.retry);
3521 return err;
3522}
3523
3524static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3525 struct nfs_fsinfo *info)
3526{
David Quigleyaa9c2662013-05-22 12:50:44 -04003527 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003529 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 };
3531 struct nfs4_lookup_res res = {
3532 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003533 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 .fh = fhandle,
3535 };
3536 struct rpc_message msg = {
3537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3538 .rpc_argp = &args,
3539 .rpc_resp = &res,
3540 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003541
David Quigleyaa9c2662013-05-22 12:50:44 -04003542 bitmask[0] = nfs4_fattr_bitmap[0];
3543 bitmask[1] = nfs4_fattr_bitmap[1];
3544 /*
3545 * Process the label in the upcoming getfattr
3546 */
3547 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3548
Trond Myklebust0e574af2005-10-27 22:12:38 -04003549 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003550 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
3553static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3554 struct nfs_fsinfo *info)
3555{
3556 struct nfs4_exception exception = { };
3557 int err;
3558 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003559 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003560 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003561 switch (err) {
3562 case 0:
3563 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003564 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003565 default:
3566 err = nfs4_handle_exception(server, err, &exception);
3567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return err;
3571}
3572
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003573static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3574 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3575{
Trond Myklebustc2190662013-08-26 19:23:04 -04003576 struct rpc_auth_create_args auth_args = {
3577 .pseudoflavor = flavor,
3578 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003579 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003580
Trond Myklebustc2190662013-08-26 19:23:04 -04003581 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003582 if (IS_ERR(auth))
3583 return -EACCES;
3584 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003585}
3586
Chuck Lever9a744ba2013-03-16 15:56:02 -04003587/*
3588 * Retry pseudoroot lookup with various security flavors. We do this when:
3589 *
3590 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3591 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3592 *
3593 * Returns zero on success, or a negative NFS4ERR value, or a
3594 * negative errno value.
3595 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003596static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003597 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003599 /* Per 3530bis 15.33.5 */
3600 static const rpc_authflavor_t flav_array[] = {
3601 RPC_AUTH_GSS_KRB5P,
3602 RPC_AUTH_GSS_KRB5I,
3603 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003604 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003605 RPC_AUTH_NULL,
3606 };
3607 int status = -EPERM;
3608 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003610 if (server->auth_info.flavor_len > 0) {
3611 /* try each flavor specified by user */
3612 for (i = 0; i < server->auth_info.flavor_len; i++) {
3613 status = nfs4_lookup_root_sec(server, fhandle, info,
3614 server->auth_info.flavors[i]);
3615 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3616 continue;
3617 break;
3618 }
3619 } else {
3620 /* no flavors specified by user, try default list */
3621 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3622 status = nfs4_lookup_root_sec(server, fhandle, info,
3623 flav_array[i]);
3624 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3625 continue;
3626 break;
3627 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003628 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003629
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003630 /*
3631 * -EACCESS could mean that the user doesn't have correct permissions
3632 * to access the mount. It could also mean that we tried to mount
3633 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3634 * existing mount programs don't handle -EACCES very well so it should
3635 * be mapped to -EPERM instead.
3636 */
3637 if (status == -EACCES)
3638 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003639 return status;
3640}
3641
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003642/**
3643 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3644 * @server: initialized nfs_server handle
3645 * @fhandle: we fill in the pseudo-fs root file handle
3646 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003647 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003648 *
3649 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003650 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003651int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003652 struct nfs_fsinfo *info,
3653 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003654{
Andre Przywarac7757072015-04-23 17:17:40 +01003655 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003656
Andre Przywarac7757072015-04-23 17:17:40 +01003657 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003658 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003659
3660 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003661 status = server->nfs_client->cl_mvops->find_root_sec(server,
3662 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (status == 0)
3665 status = nfs4_server_capabilities(server, fhandle);
3666 if (status == 0)
3667 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003668
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003669 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Bryan Schumakerbae36242012-05-10 15:07:31 -04003672static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3673 struct nfs_fsinfo *info)
3674{
3675 int error;
3676 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003677 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003678
3679 error = nfs4_server_capabilities(server, mntfh);
3680 if (error < 0) {
3681 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3682 return error;
3683 }
3684
David Quigley14c43f72013-05-22 12:50:43 -04003685 label = nfs4_label_alloc(server, GFP_KERNEL);
3686 if (IS_ERR(label))
3687 return PTR_ERR(label);
3688
David Quigley1775fd32013-05-22 12:50:42 -04003689 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003690 if (error < 0) {
3691 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003692 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003693 }
3694
3695 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3696 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3697 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3698
David Quigley14c43f72013-05-22 12:50:43 -04003699err_free_label:
3700 nfs4_label_free(label);
3701
Bryan Schumakerbae36242012-05-10 15:07:31 -04003702 return error;
3703}
3704
Manoj Naik6b97fd32006-06-09 09:34:29 -04003705/*
3706 * Get locations and (maybe) other attributes of a referral.
3707 * Note that we'll actually follow the referral later when
3708 * we detect fsid mismatch in inode revalidation
3709 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003710static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3711 const struct qstr *name, struct nfs_fattr *fattr,
3712 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003713{
3714 int status = -ENOMEM;
3715 struct page *page = NULL;
3716 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003717
3718 page = alloc_page(GFP_KERNEL);
3719 if (page == NULL)
3720 goto out;
3721 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3722 if (locations == NULL)
3723 goto out;
3724
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003725 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003726 if (status != 0)
3727 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003728
3729 /*
3730 * If the fsid didn't change, this is a migration event, not a
3731 * referral. Cause us to drop into the exception handler, which
3732 * will kick off migration recovery.
3733 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003734 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003735 dprintk("%s: server did not return a different fsid for"
3736 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003737 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003738 goto out;
3739 }
Andy Adamson533eb462011-06-13 18:25:56 -04003740 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3741 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003742
Andy Adamson533eb462011-06-13 18:25:56 -04003743 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003744 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003745 memset(fhandle, 0, sizeof(struct nfs_fh));
3746out:
3747 if (page)
3748 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003749 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003750 return status;
3751}
3752
David Quigley1775fd32013-05-22 12:50:42 -04003753static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3754 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 struct nfs4_getattr_arg args = {
3757 .fh = fhandle,
3758 .bitmask = server->attr_bitmask,
3759 };
3760 struct nfs4_getattr_res res = {
3761 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003762 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 .server = server,
3764 };
3765 struct rpc_message msg = {
3766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3767 .rpc_argp = &args,
3768 .rpc_resp = &res,
3769 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003770
3771 args.bitmask = nfs4_bitmask(server, label);
3772
Trond Myklebust0e574af2005-10-27 22:12:38 -04003773 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003774 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
David Quigley1775fd32013-05-22 12:50:42 -04003777static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3778 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
3780 struct nfs4_exception exception = { };
3781 int err;
3782 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003783 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3784 trace_nfs4_getattr(server, fhandle, fattr, err);
3785 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 &exception);
3787 } while (exception.retry);
3788 return err;
3789}
3790
3791/*
3792 * The file is not closed if it is opened due to the a request to change
3793 * the size of the file. The open call will not be needed once the
3794 * VFS layer lookup-intents are implemented.
3795 *
3796 * Close is called when the inode is destroyed.
3797 * If we haven't opened the file for O_WRONLY, we
3798 * need to in the size_change case to obtain a stateid.
3799 *
3800 * Got race?
3801 * Because OPEN is always done by name in nfsv4, it is
3802 * possible that we opened a different file by the same
3803 * name. We can recognize this race condition, but we
3804 * can't do anything about it besides returning an error.
3805 *
3806 * This will be fixed with VFS changes (lookup-intent).
3807 */
3808static int
3809nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3810 struct iattr *sattr)
3811{
David Howells2b0143b2015-03-17 22:25:59 +00003812 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003813 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003814 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003815 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 int status;
3817
Peng Tao88ac8152014-09-12 11:04:10 +08003818 if (pnfs_ld_layoutret_on_setattr(inode) &&
3819 sattr->ia_valid & ATTR_SIZE &&
3820 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003821 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003822
Trond Myklebust0e574af2005-10-27 22:12:38 -04003823 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Andy Adamson26699402012-05-30 16:12:24 -04003825 /* Deal with open(O_TRUNC) */
3826 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003827 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003828
3829 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003830 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003831 return 0;
3832
Trond Myklebustd5308382005-11-04 15:33:38 -05003833 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003834 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003835
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003836 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003837 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003838 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003839 }
3840
David Quigley14c43f72013-05-22 12:50:43 -04003841 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3842 if (IS_ERR(label))
3843 return PTR_ERR(label);
3844
NeilBrown29b59f92016-10-13 15:26:47 +11003845 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003846 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003847 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003848 nfs_setsecurity(inode, fattr, label);
3849 }
David Quigley14c43f72013-05-22 12:50:43 -04003850 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return status;
3852}
3853
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003854static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3855 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003856 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003857{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003858 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003859 int status;
3860 struct nfs4_lookup_arg args = {
3861 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003862 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003863 .name = name,
3864 };
3865 struct nfs4_lookup_res res = {
3866 .server = server,
3867 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003868 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003869 .fh = fhandle,
3870 };
3871 struct rpc_message msg = {
3872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3873 .rpc_argp = &args,
3874 .rpc_resp = &res,
3875 };
3876
David Quigleyaa9c2662013-05-22 12:50:44 -04003877 args.bitmask = nfs4_bitmask(server, label);
3878
David Howells2b3de442006-08-22 20:06:09 -04003879 nfs_fattr_init(fattr);
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003882 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 dprintk("NFS reply lookup: %d\n", status);
3884 return status;
3885}
3886
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003887static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003888{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003889 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003890 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003891 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3892 fattr->nlink = 2;
3893}
3894
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003895static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003896 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003897 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003898{
3899 struct nfs4_exception exception = { };
3900 struct rpc_clnt *client = *clnt;
3901 int err;
3902 do {
David Quigley1775fd32013-05-22 12:50:42 -04003903 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003904 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003905 switch (err) {
3906 case -NFS4ERR_BADNAME:
3907 err = -ENOENT;
3908 goto out;
3909 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003910 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003911 if (err == -NFS4ERR_MOVED)
3912 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003913 goto out;
3914 case -NFS4ERR_WRONGSEC:
3915 err = -EPERM;
3916 if (client != *clnt)
3917 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003918 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003919 if (IS_ERR(client))
3920 return PTR_ERR(client);
3921
3922 exception.retry = 1;
3923 break;
3924 default:
3925 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3926 }
3927 } while (exception.retry);
3928
3929out:
3930 if (err == 0)
3931 *clnt = client;
3932 else if (client != *clnt)
3933 rpc_shutdown_client(client);
3934
3935 return err;
3936}
3937
Al Virobeffb8f2016-07-20 16:34:42 -04003938static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003939 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3940 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003942 int status;
3943 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003944
David Quigley1775fd32013-05-22 12:50:42 -04003945 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003946 if (client != NFS_CLIENT(dir)) {
3947 rpc_shutdown_client(client);
3948 nfs_fixup_secinfo_attributes(fattr);
3949 }
3950 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951}
3952
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003953struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003954nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003955 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3956{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003957 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003958 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003959
David Quigley1775fd32013-05-22 12:50:42 -04003960 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003961 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003962 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003963 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003964}
3965
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003966static int _nfs4_proc_lookupp(struct inode *inode,
3967 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3968 struct nfs4_label *label)
3969{
3970 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3971 struct nfs_server *server = NFS_SERVER(inode);
3972 int status;
3973 struct nfs4_lookupp_arg args = {
3974 .bitmask = server->attr_bitmask,
3975 .fh = NFS_FH(inode),
3976 };
3977 struct nfs4_lookupp_res res = {
3978 .server = server,
3979 .fattr = fattr,
3980 .label = label,
3981 .fh = fhandle,
3982 };
3983 struct rpc_message msg = {
3984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3985 .rpc_argp = &args,
3986 .rpc_resp = &res,
3987 };
3988
3989 args.bitmask = nfs4_bitmask(server, label);
3990
3991 nfs_fattr_init(fattr);
3992
3993 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3994 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3995 &res.seq_res, 0);
3996 dprintk("NFS reply lookupp: %d\n", status);
3997 return status;
3998}
3999
4000static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4001 struct nfs_fattr *fattr, struct nfs4_label *label)
4002{
4003 struct nfs4_exception exception = { };
4004 int err;
4005 do {
4006 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4007 trace_nfs4_lookupp(inode, err);
4008 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4009 &exception);
4010 } while (exception.retry);
4011 return err;
4012}
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4015{
Trond Myklebust76b32992007-08-10 17:45:11 -04004016 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 struct nfs4_accessargs args = {
4018 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004019 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004020 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004022 struct nfs4_accessres res = {
4023 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004024 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 struct rpc_message msg = {
4026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4027 .rpc_argp = &args,
4028 .rpc_resp = &res,
4029 .rpc_cred = entry->cred,
4030 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004031 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Trond Myklebustc407d412010-04-16 16:22:48 -04004033 res.fattr = nfs_alloc_fattr();
4034 if (res.fattr == NULL)
4035 return -ENOMEM;
4036
Bryan Schumaker7c513052011-03-24 17:12:24 +00004037 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004039 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004040 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004042 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return status;
4044}
4045
4046static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4047{
4048 struct nfs4_exception exception = { };
4049 int err;
4050 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004051 err = _nfs4_proc_access(inode, entry);
4052 trace_nfs4_access(inode, err);
4053 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 &exception);
4055 } while (exception.retry);
4056 return err;
4057}
4058
4059/*
4060 * TODO: For the time being, we don't try to get any attributes
4061 * along with any of the zero-copy operations READ, READDIR,
4062 * READLINK, WRITE.
4063 *
4064 * In the case of the first three, we want to put the GETATTR
4065 * after the read-type operation -- this is because it is hard
4066 * to predict the length of a GETATTR response in v4, and thus
4067 * align the READ data correctly. This means that the GETATTR
4068 * may end up partially falling into the page cache, and we should
4069 * shift it into the 'tail' of the xdr_buf before processing.
4070 * To do this efficiently, we need to know the total length
4071 * of data received, which doesn't seem to be available outside
4072 * of the RPC layer.
4073 *
4074 * In the case of WRITE, we also want to put the GETATTR after
4075 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004076 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 *
4078 * Both of these changes to the XDR layer would in fact be quite
4079 * minor, but I decided to leave them for a subsequent patch.
4080 */
4081static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4082 unsigned int pgbase, unsigned int pglen)
4083{
4084 struct nfs4_readlink args = {
4085 .fh = NFS_FH(inode),
4086 .pgbase = pgbase,
4087 .pglen = pglen,
4088 .pages = &page,
4089 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004090 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 struct rpc_message msg = {
4092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4093 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 };
4096
Bryan Schumaker7c513052011-03-24 17:12:24 +00004097 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 -07004098}
4099
4100static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4101 unsigned int pgbase, unsigned int pglen)
4102{
4103 struct nfs4_exception exception = { };
4104 int err;
4105 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004106 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4107 trace_nfs4_readlink(inode, err);
4108 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 &exception);
4110 } while (exception.retry);
4111 return err;
4112}
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004115 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117static int
4118nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004119 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004121 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004122 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004123 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 int status = 0;
4126
NeilBrown532d4de2016-10-13 15:26:47 +11004127 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004128 if (IS_ERR(ctx))
4129 return PTR_ERR(ctx);
4130
David Quigleyaa9c2662013-05-22 12:50:44 -04004131 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4132
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004133 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4134 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004135 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 if (IS_ERR(state)) {
4137 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004141 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004142 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return status;
4144}
4145
Al Virobeffb8f2016-07-20 16:34:42 -04004146static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Trond Myklebust16e42952005-10-27 22:12:44 -04004148 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004149 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004151 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004153 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004154 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4158 .rpc_argp = &args,
4159 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004161 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004162 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004163
Bryan Schumaker7c513052011-03-24 17:12:24 +00004164 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004165 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004166 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return status;
4168}
4169
Al Virobeffb8f2016-07-20 16:34:42 -04004170static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
4172 struct nfs4_exception exception = { };
4173 int err;
4174 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004175 err = _nfs4_proc_remove(dir, name);
4176 trace_nfs4_remove(dir, name, err);
4177 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 &exception);
4179 } while (exception.retry);
4180 return err;
4181}
4182
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004183static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004185 struct nfs_server *server = NFS_SERVER(dir);
4186 struct nfs_removeargs *args = msg->rpc_argp;
4187 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004189 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004191 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004192
4193 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004196static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4197{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004198 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004199 &data->args.seq_args,
4200 &data->res.seq_res,
4201 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004204static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004206 struct nfs_unlinkdata *data = task->tk_calldata;
4207 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004208
Trond Myklebust14516c32010-07-31 14:29:06 -04004209 if (!nfs4_sequence_done(task, &res->seq_res))
4210 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004211 if (nfs4_async_handle_error(task, res->server, NULL,
4212 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004213 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004214 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004215 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004216 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Jeff Laytond3d41522010-09-17 17:31:57 -04004219static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4220{
4221 struct nfs_server *server = NFS_SERVER(dir);
4222 struct nfs_renameargs *arg = msg->rpc_argp;
4223 struct nfs_renameres *res = msg->rpc_resp;
4224
4225 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004226 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004227 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004228}
4229
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004230static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4231{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004232 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004233 &data->args.seq_args,
4234 &data->res.seq_res,
4235 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004236}
4237
4238static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4239 struct inode *new_dir)
4240{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004241 struct nfs_renamedata *data = task->tk_calldata;
4242 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004243
4244 if (!nfs4_sequence_done(task, &res->seq_res))
4245 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004246 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004247 return 0;
4248
Trond Myklebustc733c492017-01-11 12:32:26 -05004249 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004250 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004251 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004252 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004253 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004254 return 1;
4255}
4256
Al Virobeffb8f2016-07-20 16:34:42 -04004257static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004259 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 struct nfs4_link_arg arg = {
4261 .fh = NFS_FH(inode),
4262 .dir_fh = NFS_FH(dir),
4263 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004264 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004266 struct nfs4_link_res res = {
4267 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004268 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4272 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004273 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004275 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Trond Myklebust136f2622010-04-16 16:22:49 -04004277 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004278 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004279 goto out;
4280
David Quigley14c43f72013-05-22 12:50:43 -04004281 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4282 if (IS_ERR(res.label)) {
4283 status = PTR_ERR(res.label);
4284 goto out;
4285 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004286 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004287
Bryan Schumaker7c513052011-03-24 17:12:24 +00004288 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004289 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004290 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004291 status = nfs_post_op_update_inode(inode, res.fattr);
4292 if (!status)
4293 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004294 }
David Quigley14c43f72013-05-22 12:50:43 -04004295
4296
4297 nfs4_label_free(res.label);
4298
Trond Myklebust136f2622010-04-16 16:22:49 -04004299out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004300 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return status;
4302}
4303
Al Virobeffb8f2016-07-20 16:34:42 -04004304static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct nfs4_exception exception = { };
4307 int err;
4308 do {
4309 err = nfs4_handle_exception(NFS_SERVER(inode),
4310 _nfs4_proc_link(inode, dir, name),
4311 &exception);
4312 } while (exception.retry);
4313 return err;
4314}
4315
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004316struct nfs4_createdata {
4317 struct rpc_message msg;
4318 struct nfs4_create_arg arg;
4319 struct nfs4_create_res res;
4320 struct nfs_fh fh;
4321 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004322 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004323};
4324
4325static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004326 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004327{
4328 struct nfs4_createdata *data;
4329
4330 data = kzalloc(sizeof(*data), GFP_KERNEL);
4331 if (data != NULL) {
4332 struct nfs_server *server = NFS_SERVER(dir);
4333
David Quigley14c43f72013-05-22 12:50:43 -04004334 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4335 if (IS_ERR(data->label))
4336 goto out_free;
4337
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004338 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4339 data->msg.rpc_argp = &data->arg;
4340 data->msg.rpc_resp = &data->res;
4341 data->arg.dir_fh = NFS_FH(dir);
4342 data->arg.server = server;
4343 data->arg.name = name;
4344 data->arg.attrs = sattr;
4345 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004346 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004347 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 data->res.server = server;
4349 data->res.fh = &data->fh;
4350 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004351 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004352 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004353 }
4354 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004355out_free:
4356 kfree(data);
4357 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004358}
4359
4360static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4361{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004362 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004363 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004364 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004365 update_changeattr(dir, &data->res.dir_cinfo,
4366 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004367 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004368 }
4369 return status;
4370}
4371
4372static void nfs4_free_createdata(struct nfs4_createdata *data)
4373{
David Quigley14c43f72013-05-22 12:50:43 -04004374 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004375 kfree(data);
4376}
4377
Chuck Lever4f390c12006-08-22 20:06:22 -04004378static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004379 struct page *page, unsigned int len, struct iattr *sattr,
4380 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004382 struct nfs4_createdata *data;
4383 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Chuck Lever94a6d752006-08-22 20:06:23 -04004385 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004387
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004388 status = -ENOMEM;
4389 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4390 if (data == NULL)
4391 goto out;
4392
4393 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4394 data->arg.u.symlink.pages = &page;
4395 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004396 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004398 status = nfs4_do_create(dir, dentry, data);
4399
4400 nfs4_free_createdata(data);
4401out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 return status;
4403}
4404
Chuck Lever4f390c12006-08-22 20:06:22 -04004405static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004406 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
4408 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004409 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004411
4412 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004415 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4416 trace_nfs4_symlink(dir, &dentry->d_name, err);
4417 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 &exception);
4419 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004420
4421 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 return err;
4423}
4424
4425static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004426 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004428 struct nfs4_createdata *data;
4429 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4432 if (data == NULL)
4433 goto out;
4434
David Quigley1775fd32013-05-22 12:50:42 -04004435 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004436 status = nfs4_do_create(dir, dentry, data);
4437
4438 nfs4_free_createdata(data);
4439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return status;
4441}
4442
4443static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4444 struct iattr *sattr)
4445{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004446 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004448 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004450
David Quigleyaa9c2662013-05-22 12:50:44 -04004451 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4452
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004453 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4454 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004456 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4457 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4458 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 &exception);
4460 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004461 nfs4_label_release_security(label);
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return err;
4464}
4465
4466static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004467 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
David Howells2b0143b2015-03-17 22:25:59 +00004469 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 struct nfs4_readdir_arg args = {
4471 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004472 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 .pgbase = 0,
4474 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004475 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004476 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 };
4478 struct nfs4_readdir_res res;
4479 struct rpc_message msg = {
4480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4481 .rpc_argp = &args,
4482 .rpc_resp = &res,
4483 .rpc_cred = cred,
4484 };
4485 int status;
4486
Al Viro6de14722013-09-16 10:53:17 -04004487 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4488 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004489 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004490 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004492 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 -05004493 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004494 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004495 status += args.pgbase;
4496 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004497
4498 nfs_invalidate_atime(dir);
4499
Harvey Harrison3110ff82008-05-02 13:42:44 -07004500 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return status;
4502}
4503
4504static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004505 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
4507 struct nfs4_exception exception = { };
4508 int err;
4509 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004510 err = _nfs4_proc_readdir(dentry, cred, cookie,
4511 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004512 trace_nfs4_readdir(d_inode(dentry), err);
4513 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 &exception);
4515 } while (exception.retry);
4516 return err;
4517}
4518
4519static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004520 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004522 struct nfs4_createdata *data;
4523 int mode = sattr->ia_mode;
4524 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004526 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4527 if (data == NULL)
4528 goto out;
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004531 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004533 data->arg.ftype = NF4BLK;
4534 data->arg.u.device.specdata1 = MAJOR(rdev);
4535 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 }
4537 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004538 data->arg.ftype = NF4CHR;
4539 data->arg.u.device.specdata1 = MAJOR(rdev);
4540 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004541 } else if (!S_ISSOCK(mode)) {
4542 status = -EINVAL;
4543 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 }
David Quigley1775fd32013-05-22 12:50:42 -04004545
David Quigleyaa9c2662013-05-22 12:50:44 -04004546 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004547 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004548out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004549 nfs4_free_createdata(data);
4550out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return status;
4552}
4553
4554static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4555 struct iattr *sattr, dev_t rdev)
4556{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004557 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004559 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004561
David Quigleyaa9c2662013-05-22 12:50:44 -04004562 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4563
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004564 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4565 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004567 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4568 trace_nfs4_mknod(dir, &dentry->d_name, err);
4569 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 &exception);
4571 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004572
4573 nfs4_label_release_security(label);
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return err;
4576}
4577
4578static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4579 struct nfs_fsstat *fsstat)
4580{
4581 struct nfs4_statfs_arg args = {
4582 .fh = fhandle,
4583 .bitmask = server->attr_bitmask,
4584 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004585 struct nfs4_statfs_res res = {
4586 .fsstat = fsstat,
4587 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 struct rpc_message msg = {
4589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4590 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004591 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 };
4593
Trond Myklebust0e574af2005-10-27 22:12:38 -04004594 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004595 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
4598static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4599{
4600 struct nfs4_exception exception = { };
4601 int err;
4602 do {
4603 err = nfs4_handle_exception(server,
4604 _nfs4_proc_statfs(server, fhandle, fsstat),
4605 &exception);
4606 } while (exception.retry);
4607 return err;
4608}
4609
4610static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4611 struct nfs_fsinfo *fsinfo)
4612{
4613 struct nfs4_fsinfo_arg args = {
4614 .fh = fhandle,
4615 .bitmask = server->attr_bitmask,
4616 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004617 struct nfs4_fsinfo_res res = {
4618 .fsinfo = fsinfo,
4619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 struct rpc_message msg = {
4621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4622 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004623 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 };
4625
Bryan Schumaker7c513052011-03-24 17:12:24 +00004626 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627}
4628
4629static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4630{
4631 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004632 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 int err;
4634
4635 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004636 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004637 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004638 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004639 nfs4_set_lease_period(server->nfs_client,
4640 fsinfo->lease_time * HZ,
4641 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004642 break;
4643 }
4644 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 } while (exception.retry);
4646 return err;
4647}
4648
4649static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4650{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004651 int error;
4652
Trond Myklebust0e574af2005-10-27 22:12:38 -04004653 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004654 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004655 if (error == 0) {
4656 /* block layout checks this! */
4657 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004658 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004659 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004660
4661 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
4664static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4665 struct nfs_pathconf *pathconf)
4666{
4667 struct nfs4_pathconf_arg args = {
4668 .fh = fhandle,
4669 .bitmask = server->attr_bitmask,
4670 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004671 struct nfs4_pathconf_res res = {
4672 .pathconf = pathconf,
4673 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 struct rpc_message msg = {
4675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4676 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004677 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 };
4679
4680 /* None of the pathconf attributes are mandatory to implement */
4681 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4682 memset(pathconf, 0, sizeof(*pathconf));
4683 return 0;
4684 }
4685
Trond Myklebust0e574af2005-10-27 22:12:38 -04004686 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004687 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688}
4689
4690static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4691 struct nfs_pathconf *pathconf)
4692{
4693 struct nfs4_exception exception = { };
4694 int err;
4695
4696 do {
4697 err = nfs4_handle_exception(server,
4698 _nfs4_proc_pathconf(server, fhandle, pathconf),
4699 &exception);
4700 } while (exception.retry);
4701 return err;
4702}
4703
Trond Myklebust5521abf2013-03-16 20:54:34 -04004704int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004705 const struct nfs_open_context *ctx,
4706 const struct nfs_lock_context *l_ctx,
4707 fmode_t fmode)
4708{
NeilBrown17393472016-10-13 15:26:47 +11004709 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004710}
4711EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4712
Trond Myklebust5521abf2013-03-16 20:54:34 -04004713static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4714 const struct nfs_open_context *ctx,
4715 const struct nfs_lock_context *l_ctx,
4716 fmode_t fmode)
4717{
4718 nfs4_stateid current_stateid;
4719
Trond Myklebuste1253be2014-03-05 08:44:23 -05004720 /* If the current stateid represents a lost lock, then exit */
4721 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4722 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004723 return nfs4_stateid_match(stateid, &current_stateid);
4724}
4725
4726static bool nfs4_error_stateid_expired(int err)
4727{
4728 switch (err) {
4729 case -NFS4ERR_DELEG_REVOKED:
4730 case -NFS4ERR_ADMIN_REVOKED:
4731 case -NFS4ERR_BAD_STATEID:
4732 case -NFS4ERR_STALE_STATEID:
4733 case -NFS4ERR_OLD_STATEID:
4734 case -NFS4ERR_OPENMODE:
4735 case -NFS4ERR_EXPIRED:
4736 return true;
4737 }
4738 return false;
4739}
4740
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004741static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004745 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004746 if (task->tk_status < 0) {
4747 struct nfs4_exception exception = {
4748 .inode = hdr->inode,
4749 .state = hdr->args.context->state,
4750 .stateid = &hdr->args.stateid,
4751 };
4752 task->tk_status = nfs4_async_handle_exception(task,
4753 server, task->tk_status, &exception);
4754 if (exception.retry) {
4755 rpc_restart_call_prepare(task);
4756 return -EAGAIN;
4757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
Trond Myklebust5521abf2013-03-16 20:54:34 -04004765static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004766 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004767{
4768
4769 if (!nfs4_error_stateid_expired(task->tk_status) ||
4770 nfs4_stateid_is_current(&args->stateid,
4771 args->context,
4772 args->lock_context,
4773 FMODE_READ))
4774 return false;
4775 rpc_restart_call_prepare(task);
4776 return true;
4777}
4778
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004779static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004780{
4781
4782 dprintk("--> %s\n", __func__);
4783
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004784 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004785 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004786 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004787 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004788 if (task->tk_status > 0)
4789 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004790 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4791 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004792}
4793
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004794static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4795 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004797 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004798 if (!hdr->pgio_done_cb)
4799 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004800 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004801 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004804static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4805 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004806{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004807 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808 &hdr->args.seq_args,
4809 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004810 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004811 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004812 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4813 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004814 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004815 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004817 return -EIO;
4818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
4820
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004821static int nfs4_write_done_cb(struct rpc_task *task,
4822 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004824 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004825
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004826 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004827 if (task->tk_status < 0) {
4828 struct nfs4_exception exception = {
4829 .inode = hdr->inode,
4830 .state = hdr->args.context->state,
4831 .stateid = &hdr->args.stateid,
4832 };
4833 task->tk_status = nfs4_async_handle_exception(task,
4834 NFS_SERVER(inode), task->tk_status,
4835 &exception);
4836 if (exception.retry) {
4837 rpc_restart_call_prepare(task);
4838 return -EAGAIN;
4839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004841 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004842 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004843 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004844 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
Trond Myklebust5521abf2013-03-16 20:54:34 -04004848static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004849 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004850{
4851
4852 if (!nfs4_error_stateid_expired(task->tk_status) ||
4853 nfs4_stateid_is_current(&args->stateid,
4854 args->context,
4855 args->lock_context,
4856 FMODE_WRITE))
4857 return false;
4858 rpc_restart_call_prepare(task);
4859 return true;
4860}
4861
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004862static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004863{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004864 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004865 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004866 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004867 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004868 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4869 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004870}
4871
Trond Myklebust5a37f852012-04-28 14:55:16 -04004872static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004873bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004874{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004875 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004876 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004877 return false;
4878 /* Otherwise, request attributes if and only if we don't hold
4879 * a delegation
4880 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004881 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004882}
Fred Isamana69aef12011-03-03 15:13:47 +00004883
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004884static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4885 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004888
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004889 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4890 hdr->args.bitmask = NULL;
4891 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004892 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004893 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004894
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004895 if (!hdr->pgio_done_cb)
4896 hdr->pgio_done_cb = nfs4_write_done_cb;
4897 hdr->res.server = server;
4898 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004900 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004901 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903
Fred Isaman0b7c0152012-04-20 14:47:39 -04004904static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4905{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004906 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004907 &data->args.seq_args,
4908 &data->res.seq_res,
4909 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
Fred Isaman0b7c0152012-04-20 14:47:39 -04004912static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004915
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004916 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004917 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4918 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004919 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004920 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
4924
Fred Isaman0b7c0152012-04-20 14:47:39 -04004925static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004926{
4927 if (!nfs4_sequence_done(task, &data->res.seq_res))
4928 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004929 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004930}
4931
Fred Isaman0b7c0152012-04-20 14:47:39 -04004932static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004934 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004935
Fred Isaman0b7c0152012-04-20 14:47:39 -04004936 if (data->commit_done_cb == NULL)
4937 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004938 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004939 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004940 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004943struct nfs4_renewdata {
4944 struct nfs_client *client;
4945 unsigned long timestamp;
4946};
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948/*
4949 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4950 * standalone procedure for queueing an asynchronous RENEW.
4951 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004952static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004953{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004954 struct nfs4_renewdata *data = calldata;
4955 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004956
Elena Reshetova212bf412017-10-20 12:53:38 +03004957 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004958 nfs4_schedule_state_renewal(clp);
4959 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004960 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004961}
4962
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004963static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004965 struct nfs4_renewdata *data = calldata;
4966 struct nfs_client *clp = data->client;
4967 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004969 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004970 switch (task->tk_status) {
4971 case 0:
4972 break;
4973 case -NFS4ERR_LEASE_MOVED:
4974 nfs4_schedule_lease_moved_recovery(clp);
4975 break;
4976 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004977 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004978 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4979 return;
4980 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004981 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004982 return;
4983 }
4984 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004986 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987}
4988
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004989static const struct rpc_call_ops nfs4_renew_ops = {
4990 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004991 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004992};
4993
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004994static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
4996 struct rpc_message msg = {
4997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4998 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004999 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005001 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005003 if (renew_flags == 0)
5004 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005005 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005006 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005007 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005008 if (data == NULL) {
5009 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005010 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005011 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005012 data->client = clp;
5013 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005014 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005015 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016}
5017
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005018static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019{
5020 struct rpc_message msg = {
5021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5022 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005023 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 };
5025 unsigned long now = jiffies;
5026 int status;
5027
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005028 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 if (status < 0)
5030 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005031 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return 0;
5033}
5034
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005035static inline int nfs4_server_supports_acls(struct nfs_server *server)
5036{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005037 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005038}
5039
Trond Myklebust21f498c2012-08-24 10:59:25 -04005040/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5041 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005042 * the stack.
5043 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005044#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005045
Neil Hormane9e3d722011-03-04 19:26:03 -05005046static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005047 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005048{
5049 struct page *newpage, **spages;
5050 int rc = 0;
5051 size_t len;
5052 spages = pages;
5053
5054 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005055 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005056 newpage = alloc_page(GFP_KERNEL);
5057
5058 if (newpage == NULL)
5059 goto unwind;
5060 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005061 buf += len;
5062 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005063 *pages++ = newpage;
5064 rc++;
5065 } while (buflen != 0);
5066
5067 return rc;
5068
5069unwind:
5070 for(; rc > 0; rc--)
5071 __free_page(spages[rc-1]);
5072 return -ENOMEM;
5073}
5074
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005075struct nfs4_cached_acl {
5076 int cached;
5077 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005078 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005079};
5080
5081static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005082{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005083 struct nfs_inode *nfsi = NFS_I(inode);
5084
5085 spin_lock(&inode->i_lock);
5086 kfree(nfsi->nfs4_acl);
5087 nfsi->nfs4_acl = acl;
5088 spin_unlock(&inode->i_lock);
5089}
5090
5091static void nfs4_zap_acl_attr(struct inode *inode)
5092{
5093 nfs4_set_cached_acl(inode, NULL);
5094}
5095
5096static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5097{
5098 struct nfs_inode *nfsi = NFS_I(inode);
5099 struct nfs4_cached_acl *acl;
5100 int ret = -ENOENT;
5101
5102 spin_lock(&inode->i_lock);
5103 acl = nfsi->nfs4_acl;
5104 if (acl == NULL)
5105 goto out;
5106 if (buf == NULL) /* user is just asking for length */
5107 goto out_len;
5108 if (acl->cached == 0)
5109 goto out;
5110 ret = -ERANGE; /* see getxattr(2) man page */
5111 if (acl->len > buflen)
5112 goto out;
5113 memcpy(buf, acl->data, acl->len);
5114out_len:
5115 ret = acl->len;
5116out:
5117 spin_unlock(&inode->i_lock);
5118 return ret;
5119}
5120
Sachin Prabhu5794d212012-04-17 14:36:40 +01005121static 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 +00005122{
5123 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005124 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005125
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005126 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005127 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005128 if (acl == NULL)
5129 goto out;
5130 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005131 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005132 } else {
5133 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5134 if (acl == NULL)
5135 goto out;
5136 acl->cached = 0;
5137 }
5138 acl->len = acl_len;
5139out:
5140 nfs4_set_cached_acl(inode, acl);
5141}
5142
Andy Adamsonbf118a32011-12-07 11:55:27 -05005143/*
5144 * The getxattr API returns the required buffer length when called with a
5145 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5146 * the required buf. On a NULL buf, we send a page of data to the server
5147 * guessing that the ACL request can be serviced by a page. If so, we cache
5148 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5149 * the cache. If not so, we throw away the page, and cache the required
5150 * length. The next getxattr call will then produce another round trip to
5151 * the server, this time with the input buf of the required size.
5152 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005153static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005154{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005155 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005156 struct nfs_getaclargs args = {
5157 .fh = NFS_FH(inode),
5158 .acl_pages = pages,
5159 .acl_len = buflen,
5160 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005161 struct nfs_getaclres res = {
5162 .acl_len = buflen,
5163 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005164 struct rpc_message msg = {
5165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5166 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005167 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005168 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005169 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005170 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005171
Trond Myklebust21f498c2012-08-24 10:59:25 -04005172 if (npages > ARRAY_SIZE(pages))
5173 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005174
Andy Adamsonbf118a32011-12-07 11:55:27 -05005175 for (i = 0; i < npages; i++) {
5176 pages[i] = alloc_page(GFP_KERNEL);
5177 if (!pages[i])
5178 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005179 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005180
5181 /* for decoding across pages */
5182 res.acl_scratch = alloc_page(GFP_KERNEL);
5183 if (!res.acl_scratch)
5184 goto out_free;
5185
Andy Adamsonbf118a32011-12-07 11:55:27 -05005186 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005187
Peng Taode040be2012-01-10 22:42:47 +08005188 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005189 __func__, buf, buflen, npages, args.acl_len);
5190 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5191 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005192 if (ret)
5193 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005194
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005195 /* Handle the case where the passed-in buffer is too short */
5196 if (res.acl_flags & NFS4_ACL_TRUNC) {
5197 /* Did the user only issue a request for the acl length? */
5198 if (buf == NULL)
5199 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005200 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005201 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005202 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005203 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005204 if (buf) {
5205 if (res.acl_len > buflen) {
5206 ret = -ERANGE;
5207 goto out_free;
5208 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005209 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005210 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005211out_ok:
5212 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005213out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005214 for (i = 0; i < npages; i++)
5215 if (pages[i])
5216 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005217 if (res.acl_scratch)
5218 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005219 return ret;
5220}
5221
Trond Myklebust16b42892006-08-24 12:27:15 -04005222static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5223{
5224 struct nfs4_exception exception = { };
5225 ssize_t ret;
5226 do {
5227 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005228 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005229 if (ret >= 0)
5230 break;
5231 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5232 } while (exception.retry);
5233 return ret;
5234}
5235
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005236static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5237{
5238 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005239 int ret;
5240
5241 if (!nfs4_server_supports_acls(server))
5242 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005243 ret = nfs_revalidate_inode(server, inode);
5244 if (ret < 0)
5245 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005246 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5247 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005248 ret = nfs4_read_cached_acl(inode, buf, buflen);
5249 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005250 /* -ENOENT is returned if there is no ACL or if there is an ACL
5251 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005252 return ret;
5253 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005254}
5255
Trond Myklebust16b42892006-08-24 12:27:15 -04005256static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005257{
5258 struct nfs_server *server = NFS_SERVER(inode);
5259 struct page *pages[NFS4ACL_MAXPAGES];
5260 struct nfs_setaclargs arg = {
5261 .fh = NFS_FH(inode),
5262 .acl_pages = pages,
5263 .acl_len = buflen,
5264 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005265 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005266 struct rpc_message msg = {
5267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5268 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005269 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005270 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005271 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005272 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005273
5274 if (!nfs4_server_supports_acls(server))
5275 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005276 if (npages > ARRAY_SIZE(pages))
5277 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005278 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005279 if (i < 0)
5280 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005281 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005282 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005283
5284 /*
5285 * Free each page after tx, so the only ref left is
5286 * held by the network stack
5287 */
5288 for (; i > 0; i--)
5289 put_page(pages[i-1]);
5290
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005291 /*
5292 * Acl update can result in inode attribute update.
5293 * so mark the attribute cache invalid.
5294 */
5295 spin_lock(&inode->i_lock);
5296 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5297 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005298 nfs_access_zap_cache(inode);
5299 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005300 return ret;
5301}
5302
Trond Myklebust16b42892006-08-24 12:27:15 -04005303static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5304{
5305 struct nfs4_exception exception = { };
5306 int err;
5307 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005308 err = __nfs4_proc_set_acl(inode, buf, buflen);
5309 trace_nfs4_set_acl(inode, err);
5310 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005311 &exception);
5312 } while (exception.retry);
5313 return err;
5314}
5315
David Quigleyaa9c2662013-05-22 12:50:44 -04005316#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5317static int _nfs4_get_security_label(struct inode *inode, void *buf,
5318 size_t buflen)
5319{
5320 struct nfs_server *server = NFS_SERVER(inode);
5321 struct nfs_fattr fattr;
5322 struct nfs4_label label = {0, 0, buflen, buf};
5323
5324 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005325 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005326 .fh = NFS_FH(inode),
5327 .bitmask = bitmask,
5328 };
5329 struct nfs4_getattr_res res = {
5330 .fattr = &fattr,
5331 .label = &label,
5332 .server = server,
5333 };
5334 struct rpc_message msg = {
5335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005336 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005337 .rpc_resp = &res,
5338 };
5339 int ret;
5340
5341 nfs_fattr_init(&fattr);
5342
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005343 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005344 if (ret)
5345 return ret;
5346 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5347 return -ENOENT;
5348 if (buflen < label.len)
5349 return -ERANGE;
5350 return 0;
5351}
5352
5353static int nfs4_get_security_label(struct inode *inode, void *buf,
5354 size_t buflen)
5355{
5356 struct nfs4_exception exception = { };
5357 int err;
5358
5359 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5360 return -EOPNOTSUPP;
5361
5362 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005363 err = _nfs4_get_security_label(inode, buf, buflen);
5364 trace_nfs4_get_security_label(inode, err);
5365 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005366 &exception);
5367 } while (exception.retry);
5368 return err;
5369}
5370
5371static int _nfs4_do_set_security_label(struct inode *inode,
5372 struct nfs4_label *ilabel,
5373 struct nfs_fattr *fattr,
5374 struct nfs4_label *olabel)
5375{
5376
5377 struct iattr sattr = {0};
5378 struct nfs_server *server = NFS_SERVER(inode);
5379 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005380 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005381 .fh = NFS_FH(inode),
5382 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005383 .server = server,
5384 .bitmask = bitmask,
5385 .label = ilabel,
5386 };
5387 struct nfs_setattrres res = {
5388 .fattr = fattr,
5389 .label = olabel,
5390 .server = server,
5391 };
5392 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5394 .rpc_argp = &arg,
5395 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005396 };
5397 int status;
5398
Jeff Layton12207f62013-11-01 10:49:32 -04005399 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005400
Jeff Layton12207f62013-11-01 10:49:32 -04005401 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005402 if (status)
5403 dprintk("%s failed: %d\n", __func__, status);
5404
5405 return status;
5406}
5407
5408static int nfs4_do_set_security_label(struct inode *inode,
5409 struct nfs4_label *ilabel,
5410 struct nfs_fattr *fattr,
5411 struct nfs4_label *olabel)
5412{
5413 struct nfs4_exception exception = { };
5414 int err;
5415
5416 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005417 err = _nfs4_do_set_security_label(inode, ilabel,
5418 fattr, olabel);
5419 trace_nfs4_set_security_label(inode, err);
5420 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005421 &exception);
5422 } while (exception.retry);
5423 return err;
5424}
5425
5426static int
Al Viro59301222016-05-27 10:19:30 -04005427nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005428{
5429 struct nfs4_label ilabel, *olabel = NULL;
5430 struct nfs_fattr fattr;
5431 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005432 int status;
5433
5434 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5435 return -EOPNOTSUPP;
5436
5437 nfs_fattr_init(&fattr);
5438
5439 ilabel.pi = 0;
5440 ilabel.lfs = 0;
5441 ilabel.label = (char *)buf;
5442 ilabel.len = buflen;
5443
5444 cred = rpc_lookup_cred();
5445 if (IS_ERR(cred))
5446 return PTR_ERR(cred);
5447
5448 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5449 if (IS_ERR(olabel)) {
5450 status = -PTR_ERR(olabel);
5451 goto out;
5452 }
5453
5454 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5455 if (status == 0)
5456 nfs_setsecurity(inode, &fattr, olabel);
5457
5458 nfs4_label_free(olabel);
5459out:
5460 put_rpccred(cred);
5461 return status;
5462}
5463#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5464
5465
Chuck Leverf0920752012-05-21 22:45:41 -04005466static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5467 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005468{
5469 __be32 verf[2];
5470
Chuck Lever2c820d92012-05-21 22:45:33 -04005471 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5472 /* An impossible timestamp guarantees this value
5473 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005474 verf[0] = cpu_to_be32(U32_MAX);
5475 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005476 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005477 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005478 u64 ns = ktime_to_ns(nn->boot_time);
5479
5480 verf[0] = cpu_to_be32(ns >> 32);
5481 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005482 }
Chuck Levercd937102012-03-02 17:14:31 -05005483 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5484}
5485
Jeff Laytona3192682015-06-09 19:43:59 -04005486static int
5487nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005488{
Jeff Laytona3192682015-06-09 19:43:59 -04005489 size_t len;
5490 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005491
Trond Myklebustceb3a162015-01-03 15:16:04 -05005492 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005493 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005494
Jeff Laytona3192682015-06-09 19:43:59 -04005495 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005496 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005497 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5498 1 +
5499 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5500 1;
5501 rcu_read_unlock();
5502
5503 if (len > NFS4_OPAQUE_LIMIT + 1)
5504 return -EINVAL;
5505
5506 /*
5507 * Since this string is allocated at mount time, and held until the
5508 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5509 * about a memory-reclaim deadlock.
5510 */
5511 str = kmalloc(len, GFP_KERNEL);
5512 if (!str)
5513 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005514
Chuck Levere984a552012-09-14 17:24:21 -04005515 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005516 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005517 clp->cl_ipaddr,
5518 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5519 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005520 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005521
Jeff Laytona3192682015-06-09 19:43:59 -04005522 clp->cl_owner_id = str;
5523 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005524}
5525
Jeff Layton873e3852015-06-09 19:44:00 -04005526static int
5527nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005528{
Jeff Layton873e3852015-06-09 19:44:00 -04005529 size_t len;
5530 char *str;
5531
5532 len = 10 + 10 + 1 + 10 + 1 +
5533 strlen(nfs4_client_id_uniquifier) + 1 +
5534 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5535
5536 if (len > NFS4_OPAQUE_LIMIT + 1)
5537 return -EINVAL;
5538
5539 /*
5540 * Since this string is allocated at mount time, and held until the
5541 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5542 * about a memory-reclaim deadlock.
5543 */
5544 str = kmalloc(len, GFP_KERNEL);
5545 if (!str)
5546 return -ENOMEM;
5547
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005548 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005549 clp->rpc_ops->version, clp->cl_minorversion,
5550 nfs4_client_id_uniquifier,
5551 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005552 clp->cl_owner_id = str;
5553 return 0;
5554}
5555
5556static int
5557nfs4_init_uniform_client_string(struct nfs_client *clp)
5558{
Jeff Layton873e3852015-06-09 19:44:00 -04005559 size_t len;
5560 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005561
5562 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005563 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005564
5565 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005566 return nfs4_init_uniquifier_client_string(clp);
5567
5568 len = 10 + 10 + 1 + 10 + 1 +
5569 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5570
5571 if (len > NFS4_OPAQUE_LIMIT + 1)
5572 return -EINVAL;
5573
5574 /*
5575 * Since this string is allocated at mount time, and held until the
5576 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5577 * about a memory-reclaim deadlock.
5578 */
5579 str = kmalloc(len, GFP_KERNEL);
5580 if (!str)
5581 return -ENOMEM;
5582
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005583 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005584 clp->rpc_ops->version, clp->cl_minorversion,
5585 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005586 clp->cl_owner_id = str;
5587 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005588}
5589
Chuck Lever706cb8d2014-03-12 12:51:47 -04005590/*
5591 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5592 * services. Advertise one based on the address family of the
5593 * clientaddr.
5594 */
5595static unsigned int
5596nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5597{
5598 if (strchr(clp->cl_ipaddr, ':') != NULL)
5599 return scnprintf(buf, len, "tcp6");
5600 else
5601 return scnprintf(buf, len, "tcp");
5602}
5603
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005604static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5605{
5606 struct nfs4_setclientid *sc = calldata;
5607
5608 if (task->tk_status == 0)
5609 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5610}
5611
5612static const struct rpc_call_ops nfs4_setclientid_ops = {
5613 .rpc_call_done = nfs4_setclientid_done,
5614};
5615
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005616/**
5617 * nfs4_proc_setclientid - Negotiate client ID
5618 * @clp: state data structure
5619 * @program: RPC program for NFSv4 callback service
5620 * @port: IP port number for NFS4 callback service
5621 * @cred: RPC credential to use for this call
5622 * @res: where to place the result
5623 *
5624 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5625 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005626int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5627 unsigned short port, struct rpc_cred *cred,
5628 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629{
5630 nfs4_verifier sc_verifier;
5631 struct nfs4_setclientid setclientid = {
5632 .sc_verifier = &sc_verifier,
5633 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005634 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 };
5636 struct rpc_message msg = {
5637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5638 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005639 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005640 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005642 struct rpc_task *task;
5643 struct rpc_task_setup task_setup_data = {
5644 .rpc_client = clp->cl_rpcclient,
5645 .rpc_message = &msg,
5646 .callback_ops = &nfs4_setclientid_ops,
5647 .callback_data = &setclientid,
5648 .flags = RPC_TASK_TIMEOUT,
5649 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005650 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
Chuck Leverde734832012-07-11 16:30:50 -04005652 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005653 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005654
5655 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5656 status = nfs4_init_uniform_client_string(clp);
5657 else
Jeff Laytona3192682015-06-09 19:43:59 -04005658 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005659
5660 if (status)
5661 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005662
Chuck Leverde734832012-07-11 16:30:50 -04005663 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005664 setclientid.sc_netid_len =
5665 nfs4_init_callback_netid(clp,
5666 setclientid.sc_netid,
5667 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005668 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005669 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 clp->cl_ipaddr, port >> 8, port & 255);
5671
Jeff Layton3a6bb732015-06-09 19:43:57 -04005672 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005673 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005674 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005675 task = rpc_run_task(&task_setup_data);
5676 if (IS_ERR(task)) {
5677 status = PTR_ERR(task);
5678 goto out;
5679 }
5680 status = task->tk_status;
5681 if (setclientid.sc_cred) {
5682 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5683 put_rpccred(setclientid.sc_cred);
5684 }
5685 rpc_put_task(task);
5686out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005687 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005688 dprintk("NFS reply setclientid: %d\n", status);
5689 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
5691
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005692/**
5693 * nfs4_proc_setclientid_confirm - Confirm client ID
5694 * @clp: state data structure
5695 * @res: result of a previous SETCLIENTID
5696 * @cred: RPC credential to use for this call
5697 *
5698 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5699 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005700int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005701 struct nfs4_setclientid_res *arg,
5702 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 struct rpc_message msg = {
5705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005706 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005707 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 int status;
5710
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005711 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5712 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5713 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005714 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005715 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005716 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 return status;
5718}
5719
Trond Myklebustfe650402006-01-03 09:55:18 +01005720struct nfs4_delegreturndata {
5721 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005722 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005723 struct nfs_fh fh;
5724 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005725 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005726 struct {
5727 struct nfs4_layoutreturn_args arg;
5728 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005729 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005730 u32 roc_barrier;
5731 bool roc;
5732 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005733 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005734 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005735 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005736};
5737
Trond Myklebustfe650402006-01-03 09:55:18 +01005738static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5739{
5740 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005741
Trond Myklebust14516c32010-07-31 14:29:06 -04005742 if (!nfs4_sequence_done(task, &data->res.seq_res))
5743 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005744
Trond Myklebustca8acf82013-08-13 10:36:56 -04005745 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005746
5747 /* Handle Layoutreturn errors */
5748 if (data->args.lr_args && task->tk_status != 0) {
5749 switch(data->res.lr_ret) {
5750 default:
5751 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5752 break;
5753 case 0:
5754 data->args.lr_args = NULL;
5755 data->res.lr_res = NULL;
5756 break;
5757 case -NFS4ERR_ADMIN_REVOKED:
5758 case -NFS4ERR_DELEG_REVOKED:
5759 case -NFS4ERR_EXPIRED:
5760 case -NFS4ERR_BAD_STATEID:
5761 case -NFS4ERR_OLD_STATEID:
5762 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5763 case -NFS4ERR_WRONG_CRED:
5764 data->args.lr_args = NULL;
5765 data->res.lr_res = NULL;
5766 data->res.lr_ret = 0;
5767 rpc_restart_call_prepare(task);
5768 return;
5769 }
5770 }
5771
Ricardo Labiaga79708862009-12-07 09:23:21 -05005772 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005773 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005774 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005775 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005776 case -NFS4ERR_ADMIN_REVOKED:
5777 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005778 case -NFS4ERR_EXPIRED:
5779 nfs4_free_revoked_stateid(data->res.server,
5780 data->args.stateid,
5781 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005782 case -NFS4ERR_BAD_STATEID:
5783 case -NFS4ERR_OLD_STATEID:
5784 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005785 task->tk_status = 0;
5786 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005787 case -NFS4ERR_ACCESS:
5788 if (data->args.bitmask) {
5789 data->args.bitmask = NULL;
5790 data->res.fattr = NULL;
5791 task->tk_status = 0;
5792 rpc_restart_call_prepare(task);
5793 return;
5794 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005795 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005796 if (nfs4_async_handle_error(task, data->res.server,
5797 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005798 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005799 return;
5800 }
5801 }
5802 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005803}
5804
5805static void nfs4_delegreturn_release(void *calldata)
5806{
Peng Tao039b7562014-07-03 13:05:02 +08005807 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005808 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005809
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005810 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005811 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005812 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5813 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005814 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005815 nfs_iput_and_deactive(inode);
5816 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005817 kfree(calldata);
5818}
5819
Andy Adamson938e1012009-04-01 09:22:28 -04005820static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5821{
5822 struct nfs4_delegreturndata *d_data;
5823
5824 d_data = (struct nfs4_delegreturndata *)data;
5825
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005826 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005827 return;
5828
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005829 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005830 &d_data->args.seq_args,
5831 &d_data->res.seq_res,
5832 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005833}
Andy Adamson938e1012009-04-01 09:22:28 -04005834
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005835static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005836 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005837 .rpc_call_done = nfs4_delegreturn_done,
5838 .rpc_release = nfs4_delegreturn_release,
5839};
5840
Trond Myklebuste6f81072008-01-24 18:14:34 -05005841static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005842{
5843 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005844 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005845 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005846 struct rpc_message msg = {
5847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5848 .rpc_cred = cred,
5849 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005850 struct rpc_task_setup task_setup_data = {
5851 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005852 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005853 .callback_ops = &nfs4_delegreturn_ops,
5854 .flags = RPC_TASK_ASYNC,
5855 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005856 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005857
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005858 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005859 if (data == NULL)
5860 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005861 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005862
5863 nfs4_state_protect(server->nfs_client,
5864 NFS_SP4_MACH_CRED_CLEANUP,
5865 &task_setup_data.rpc_client, &msg);
5866
Trond Myklebustfe650402006-01-03 09:55:18 +01005867 data->args.fhandle = &data->fh;
5868 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005869 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005870 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005871 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005872 data->res.fattr = &data->fattr;
5873 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005874 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005875 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005876 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005877 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005878 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005879 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005880 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005881 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005882 if (data->lr.roc) {
5883 data->args.lr_args = &data->lr.arg;
5884 data->res.lr_res = &data->lr.res;
5885 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005886 } else if (data->lr.roc) {
5887 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5888 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005889 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005890
Trond Myklebustc970aa82007-07-14 15:39:59 -04005891 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005892 msg.rpc_argp = &data->args;
5893 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005894 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005895 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005896 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005897 if (!issync)
5898 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005899 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005900 if (status != 0)
5901 goto out;
5902 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005903out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005904 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005905 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906}
5907
Trond Myklebuste6f81072008-01-24 18:14:34 -05005908int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 struct nfs_server *server = NFS_SERVER(inode);
5911 struct nfs4_exception exception = { };
5912 int err;
5913 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005914 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005915 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 switch (err) {
5917 case -NFS4ERR_STALE_STATEID:
5918 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 case 0:
5920 return 0;
5921 }
5922 err = nfs4_handle_exception(server, err, &exception);
5923 } while (exception.retry);
5924 return err;
5925}
5926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5928{
5929 struct inode *inode = state->inode;
5930 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005931 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005932 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005934 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005936 struct nfs_lockt_res res = {
5937 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 };
5939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005941 .rpc_argp = &arg,
5942 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 .rpc_cred = state->owner->so_cred,
5944 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 struct nfs4_lock_state *lsp;
5946 int status;
5947
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005948 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005949 status = nfs4_set_lock_state(state, request);
5950 if (status != 0)
5951 goto out;
5952 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005953 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005954 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005955 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005956 switch (status) {
5957 case 0:
5958 request->fl_type = F_UNLCK;
5959 break;
5960 case -NFS4ERR_DENIED:
5961 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005963 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005964 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005965out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 return status;
5967}
5968
5969static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5970{
5971 struct nfs4_exception exception = { };
5972 int err;
5973
5974 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005975 err = _nfs4_proc_getlk(state, cmd, request);
5976 trace_nfs4_get_lock(request, state, cmd, err);
5977 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 &exception);
5979 } while (exception.retry);
5980 return err;
5981}
5982
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005983struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005984 struct nfs_locku_args arg;
5985 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005986 struct nfs4_lock_state *lsp;
5987 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005988 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005989 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005990 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005991 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005992};
5993
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005994static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5995 struct nfs_open_context *ctx,
5996 struct nfs4_lock_state *lsp,
5997 struct nfs_seqid *seqid)
5998{
5999 struct nfs4_unlockdata *p;
6000 struct inode *inode = lsp->ls_state->inode;
6001
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006002 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006003 if (p == NULL)
6004 return NULL;
6005 p->arg.fh = NFS_FH(inode);
6006 p->arg.fl = &p->fl;
6007 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006008 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006009 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006010 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006011 /* Ensure we don't close file until we're done freeing locks! */
6012 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006013 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006014 memcpy(&p->fl, fl, sizeof(p->fl));
6015 p->server = NFS_SERVER(inode);
6016 return p;
6017}
6018
Trond Myklebust06f814a2006-01-03 09:55:07 +01006019static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006020{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006021 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006022 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006023 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006024 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006025 put_nfs_open_context(calldata->ctx);
6026 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006027}
6028
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006029static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006030{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006031 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006032
Trond Myklebust14516c32010-07-31 14:29:06 -04006033 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6034 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006035 switch (task->tk_status) {
6036 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006037 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006038 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006039 if (nfs4_update_lock_stateid(calldata->lsp,
6040 &calldata->res.stateid))
6041 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006042 case -NFS4ERR_ADMIN_REVOKED:
6043 case -NFS4ERR_EXPIRED:
6044 nfs4_free_revoked_stateid(calldata->server,
6045 &calldata->arg.stateid,
6046 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006047 case -NFS4ERR_BAD_STATEID:
6048 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006049 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006050 if (!nfs4_stateid_match(&calldata->arg.stateid,
6051 &calldata->lsp->ls_stateid))
6052 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006053 break;
6054 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006055 if (nfs4_async_handle_error(task, calldata->server,
6056 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006057 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006058 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006059 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006060}
6061
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006062static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006063{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006064 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006066 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6067 nfs_async_iocounter_wait(task, calldata->l_ctx))
6068 return;
6069
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006070 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006071 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006072 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006073 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006074 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006075 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006076 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006077 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006078 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006079 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006080 &calldata->res.seq_res,
6081 task) != 0)
6082 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006083 return;
6084out_no_action:
6085 task->tk_action = NULL;
6086out_wait:
6087 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088}
6089
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006090static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006091 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006092 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006093 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006094};
6095
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6097 struct nfs_open_context *ctx,
6098 struct nfs4_lock_state *lsp,
6099 struct nfs_seqid *seqid)
6100{
6101 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6104 .rpc_cred = ctx->cred,
6105 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006106 struct rpc_task_setup task_setup_data = {
6107 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006108 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006109 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006110 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006111 .flags = RPC_TASK_ASYNC,
6112 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006114 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6115 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6116
Frank Filz137d6ac2007-07-09 15:32:29 -07006117 /* Ensure this is an unlock - when canceling a lock, the
6118 * canceled lock is passed in, and it won't be an unlock.
6119 */
6120 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006121 if (fl->fl_flags & FL_CLOSE)
6122 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006123
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006124 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6125 if (data == NULL) {
6126 nfs_free_seqid(seqid);
6127 return ERR_PTR(-ENOMEM);
6128 }
6129
Chuck Levera9c92d62013-08-09 12:48:18 -04006130 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006131 msg.rpc_argp = &data->arg;
6132 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006133 task_setup_data.callback_data = data;
6134 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135}
6136
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6138{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006139 struct inode *inode = state->inode;
6140 struct nfs4_state_owner *sp = state->owner;
6141 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006142 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006143 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006144 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006145 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006146 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006147 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Trond Myklebust9b073572006-06-29 16:38:34 -04006149 status = nfs4_set_lock_state(state, request);
6150 /* Unlock _before_ we do the RPC call */
6151 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006152 /* Exclude nfs_delegation_claim_locks() */
6153 mutex_lock(&sp->so_delegreturn_mutex);
6154 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006155 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006156 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006157 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006158 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006159 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006160 }
6161 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006162 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006163 if (status != 0)
6164 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006165 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006166 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006167 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6168 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006169 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6170 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006171 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006172 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006173 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006174 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006175 status = PTR_ERR(task);
6176 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006177 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006178 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006179 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006180out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006181 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006182 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006183 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184}
6185
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006186struct nfs4_lockdata {
6187 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006188 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006189 struct nfs4_lock_state *lsp;
6190 struct nfs_open_context *ctx;
6191 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006192 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006193 int rpc_status;
6194 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006195 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006196};
6197
6198static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006199 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6200 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006201{
6202 struct nfs4_lockdata *p;
6203 struct inode *inode = lsp->ls_state->inode;
6204 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006205 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006206
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006207 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006208 if (p == NULL)
6209 return NULL;
6210
6211 p->arg.fh = NFS_FH(inode);
6212 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006213 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006214 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006215 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6217 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006218 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006219 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006220 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006221 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006222 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006223 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006224 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006225 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006226 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006227 p->ctx = get_nfs_open_context(ctx);
6228 memcpy(&p->fl, fl, sizeof(p->fl));
6229 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006230out_free_seqid:
6231 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006232out_free:
6233 kfree(p);
6234 return NULL;
6235}
6236
6237static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6238{
6239 struct nfs4_lockdata *data = calldata;
6240 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241
Harvey Harrison3110ff82008-05-02 13:42:44 -07006242 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006243 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006244 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006245 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006246 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006247 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006248 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006249 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006250 nfs4_stateid_copy(&data->arg.open_stateid,
6251 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006252 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006253 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006254 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006255 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006256 nfs4_stateid_copy(&data->arg.lock_stateid,
6257 &data->lsp->ls_stateid);
6258 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006259 if (!nfs4_valid_open_stateid(state)) {
6260 data->rpc_status = -EBADF;
6261 task->tk_action = NULL;
6262 goto out_release_open_seqid;
6263 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006264 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006265 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006266 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006267 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006268 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006269 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006270out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006271 nfs_release_seqid(data->arg.open_seqid);
6272out_release_lock_seqid:
6273 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006274out_wait:
6275 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006276 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006277}
6278
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006279static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6280{
6281 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006282 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006283
Harvey Harrison3110ff82008-05-02 13:42:44 -07006284 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285
Trond Myklebust14516c32010-07-31 14:29:06 -04006286 if (!nfs4_sequence_done(task, &data->res.seq_res))
6287 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006288
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006289 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006290 switch (task->tk_status) {
6291 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006292 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006293 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006294 if (data->arg.new_lock) {
6295 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006296 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006297 rpc_restart_call_prepare(task);
6298 break;
6299 }
6300 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006301 if (data->arg.new_lock_owner != 0) {
6302 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6303 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6304 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6305 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6306 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006307 break;
6308 case -NFS4ERR_BAD_STATEID:
6309 case -NFS4ERR_OLD_STATEID:
6310 case -NFS4ERR_STALE_STATEID:
6311 case -NFS4ERR_EXPIRED:
6312 if (data->arg.new_lock_owner != 0) {
6313 if (!nfs4_stateid_match(&data->arg.open_stateid,
6314 &lsp->ls_state->open_stateid))
6315 rpc_restart_call_prepare(task);
6316 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6317 &lsp->ls_stateid))
6318 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006319 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006320 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006321}
6322
6323static void nfs4_lock_release(void *calldata)
6324{
6325 struct nfs4_lockdata *data = calldata;
6326
Harvey Harrison3110ff82008-05-02 13:42:44 -07006327 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006328 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006329 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006330 struct rpc_task *task;
6331 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6332 data->arg.lock_seqid);
6333 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006334 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006335 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006336 } else
6337 nfs_free_seqid(data->arg.lock_seqid);
6338 nfs4_put_lock_state(data->lsp);
6339 put_nfs_open_context(data->ctx);
6340 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006341 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006342}
6343
6344static const struct rpc_call_ops nfs4_lock_ops = {
6345 .rpc_call_prepare = nfs4_lock_prepare,
6346 .rpc_call_done = nfs4_lock_done,
6347 .rpc_release = nfs4_lock_release,
6348};
6349
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006350static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6351{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006352 switch (error) {
6353 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006354 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006355 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006356 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006357 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006358 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006359 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006360 break;
6361 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006362 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006363 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006364 };
6365}
6366
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006367static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006368{
6369 struct nfs4_lockdata *data;
6370 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006371 struct rpc_message msg = {
6372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6373 .rpc_cred = state->owner->so_cred,
6374 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006375 struct rpc_task_setup task_setup_data = {
6376 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006377 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006378 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006379 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006380 .flags = RPC_TASK_ASYNC,
6381 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006382 int ret;
6383
Harvey Harrison3110ff82008-05-02 13:42:44 -07006384 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006385 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006386 fl->fl_u.nfs4_fl.owner,
6387 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006388 if (data == NULL)
6389 return -ENOMEM;
6390 if (IS_SETLKW(cmd))
6391 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006392 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006393 msg.rpc_argp = &data->arg;
6394 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006395 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006396 if (recovery_type > NFS_LOCK_NEW) {
6397 if (recovery_type == NFS_LOCK_RECLAIM)
6398 data->arg.reclaim = NFS_LOCK_RECLAIM;
6399 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006400 } else
6401 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006402 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006403 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006404 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006405 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006406 if (ret == 0) {
6407 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006408 if (ret)
6409 nfs4_handle_setlk_error(data->server, data->lsp,
6410 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006411 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006412 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006413 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006414 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006415 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417}
6418
6419static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6420{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006421 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006422 struct nfs4_exception exception = {
6423 .inode = state->inode,
6424 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006425 int err;
6426
6427 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006428 /* Cache the lock if possible... */
6429 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6430 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006431 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006432 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006433 break;
6434 nfs4_handle_exception(server, err, &exception);
6435 } while (exception.retry);
6436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437}
6438
6439static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6440{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006441 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006442 struct nfs4_exception exception = {
6443 .inode = state->inode,
6444 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006445 int err;
6446
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006447 err = nfs4_set_lock_state(state, request);
6448 if (err != 0)
6449 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006450 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006451 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6452 return 0;
6453 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006454 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006455 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6456 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006457 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006458 switch (err) {
6459 default:
6460 goto out;
6461 case -NFS4ERR_GRACE:
6462 case -NFS4ERR_DELAY:
6463 nfs4_handle_exception(server, err, &exception);
6464 err = 0;
6465 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006466 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006467out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469}
6470
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006471#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006472static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6473{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006474 struct nfs4_lock_state *lsp;
6475 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006476
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006477 status = nfs4_set_lock_state(state, request);
6478 if (status != 0)
6479 return status;
6480 lsp = request->fl_u.nfs4_fl.owner;
6481 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6482 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6483 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006484 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006485}
6486#endif
6487
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6489{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006490 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006491 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006492 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006493 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
Trond Myklebust01c3b862006-06-29 16:38:39 -04006495 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006496 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006497 if (status < 0)
6498 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006499 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006500 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006501 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006502 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006503 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006504 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006505 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006506 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006507 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006508 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006509 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006510 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006511 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006512 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006513out:
6514 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 return status;
6516}
6517
6518static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6519{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006520 struct nfs4_exception exception = {
6521 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006522 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006523 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 int err;
6525
6526 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006527 err = _nfs4_proc_setlk(state, cmd, request);
6528 if (err == -NFS4ERR_DENIED)
6529 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006531 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 } while (exception.retry);
6533 return err;
6534}
6535
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006536#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6537#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6538
6539static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006540nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6541 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006542{
6543 int status = -ERESTARTSYS;
6544 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6545
6546 while(!signalled()) {
6547 status = nfs4_proc_setlk(state, cmd, request);
6548 if ((status != -EAGAIN) || IS_SETLK(cmd))
6549 break;
6550 freezable_schedule_timeout_interruptible(timeout);
6551 timeout *= 2;
6552 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6553 status = -ERESTARTSYS;
6554 }
6555 return status;
6556}
6557
Jeff Laytona1d617d82016-09-17 18:17:39 -04006558#ifdef CONFIG_NFS_V4_1
6559struct nfs4_lock_waiter {
6560 struct task_struct *task;
6561 struct inode *inode;
6562 struct nfs_lowner *owner;
6563 bool notified;
6564};
6565
6566static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006567nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006568{
6569 int ret;
6570 struct cb_notify_lock_args *cbnl = key;
6571 struct nfs4_lock_waiter *waiter = wait->private;
6572 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6573 *wowner = waiter->owner;
6574
6575 /* Only wake if the callback was for the same owner */
6576 if (lowner->clientid != wowner->clientid ||
6577 lowner->id != wowner->id ||
6578 lowner->s_dev != wowner->s_dev)
6579 return 0;
6580
6581 /* Make sure it's for the right inode */
6582 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6583 return 0;
6584
6585 waiter->notified = true;
6586
6587 /* override "private" so we can use default_wake_function */
6588 wait->private = waiter->task;
6589 ret = autoremove_wake_function(wait, mode, flags, key);
6590 wait->private = waiter;
6591 return ret;
6592}
6593
6594static int
6595nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6596{
6597 int status = -ERESTARTSYS;
6598 unsigned long flags;
6599 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6600 struct nfs_server *server = NFS_SERVER(state->inode);
6601 struct nfs_client *clp = server->nfs_client;
6602 wait_queue_head_t *q = &clp->cl_lock_waitq;
6603 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6604 .id = lsp->ls_seqid.owner_id,
6605 .s_dev = server->s_dev };
6606 struct nfs4_lock_waiter waiter = { .task = current,
6607 .inode = state->inode,
6608 .owner = &owner,
6609 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006610 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006611
6612 /* Don't bother with waitqueue if we don't expect a callback */
6613 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6614 return nfs4_retry_setlk_simple(state, cmd, request);
6615
6616 init_wait(&wait);
6617 wait.private = &waiter;
6618 wait.func = nfs4_wake_lock_waiter;
6619 add_wait_queue(q, &wait);
6620
6621 while(!signalled()) {
6622 status = nfs4_proc_setlk(state, cmd, request);
6623 if ((status != -EAGAIN) || IS_SETLK(cmd))
6624 break;
6625
6626 status = -ERESTARTSYS;
6627 spin_lock_irqsave(&q->lock, flags);
6628 if (waiter.notified) {
6629 spin_unlock_irqrestore(&q->lock, flags);
6630 continue;
6631 }
6632 set_current_state(TASK_INTERRUPTIBLE);
6633 spin_unlock_irqrestore(&q->lock, flags);
6634
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006635 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006636 }
6637
6638 finish_wait(q, &wait);
6639 return status;
6640}
6641#else /* !CONFIG_NFS_V4_1 */
6642static inline int
6643nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6644{
6645 return nfs4_retry_setlk_simple(state, cmd, request);
6646}
6647#endif
6648
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649static int
6650nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6651{
6652 struct nfs_open_context *ctx;
6653 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 int status;
6655
6656 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006657 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 state = ctx->state;
6659
Trond Myklebustd9531262009-07-21 19:22:38 -04006660 if (IS_GETLK(cmd)) {
6661 if (state != NULL)
6662 return nfs4_proc_getlk(state, F_GETLK, request);
6663 return 0;
6664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
6666 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6667 return -EINVAL;
6668
Trond Myklebustd9531262009-07-21 19:22:38 -04006669 if (request->fl_type == F_UNLCK) {
6670 if (state != NULL)
6671 return nfs4_proc_unlck(state, cmd, request);
6672 return 0;
6673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
Trond Myklebustd9531262009-07-21 19:22:38 -04006675 if (state == NULL)
6676 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006677
6678 if ((request->fl_flags & FL_POSIX) &&
6679 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6680 return -ENOLCK;
6681
Jeff Layton1ea67db2016-09-17 18:17:37 -04006682 status = nfs4_set_lock_state(state, request);
6683 if (status != 0)
6684 return status;
6685
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006686 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687}
6688
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006689int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006690{
6691 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006692 int err;
6693
6694 err = nfs4_set_lock_state(state, fl);
6695 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006696 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006697 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006698 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006699}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006700
Trond Myklebustcf470c32012-03-07 13:49:12 -05006701struct nfs_release_lockowner_data {
6702 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006703 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006704 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006705 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006706 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006707};
6708
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006709static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6710{
6711 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006712 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006713 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6714 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006715 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006716 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006717}
6718
6719static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6720{
6721 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006722 struct nfs_server *server = data->server;
6723
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006724 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006725
6726 switch (task->tk_status) {
6727 case 0:
6728 renew_lease(server, data->timestamp);
6729 break;
6730 case -NFS4ERR_STALE_CLIENTID:
6731 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006732 nfs4_schedule_lease_recovery(server->nfs_client);
6733 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006734 case -NFS4ERR_LEASE_MOVED:
6735 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006736 if (nfs4_async_handle_error(task, server,
6737 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006738 rpc_restart_call_prepare(task);
6739 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006740}
6741
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006742static void nfs4_release_lockowner_release(void *calldata)
6743{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006744 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006745 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006746 kfree(calldata);
6747}
6748
Trond Myklebust17280172012-03-11 13:11:00 -04006749static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006750 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6751 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006752 .rpc_release = nfs4_release_lockowner_release,
6753};
6754
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006755static void
6756nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006757{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006758 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006759 struct rpc_message msg = {
6760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6761 };
6762
6763 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006764 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006765
Trond Myklebustcf470c32012-03-07 13:49:12 -05006766 data = kmalloc(sizeof(*data), GFP_NOFS);
6767 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006768 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006769 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006770 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006771 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6772 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6773 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006774
Trond Myklebustcf470c32012-03-07 13:49:12 -05006775 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006776 msg.rpc_resp = &data->res;
6777 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006778 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006779}
6780
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006781#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6782
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006783static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006784 struct dentry *unused, struct inode *inode,
6785 const char *key, const void *buf,
6786 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006787{
Al Viro59301222016-05-27 10:19:30 -04006788 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006789}
6790
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006791static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006792 struct dentry *unused, struct inode *inode,
6793 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006794{
Al Virob2968212016-04-10 20:48:24 -04006795 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006796}
6797
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006798static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006799{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006800 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006801}
6802
David Quigleyc9bccef2013-05-22 12:50:45 -04006803#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006804
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006805static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006806 struct dentry *unused, struct inode *inode,
6807 const char *key, const void *buf,
6808 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006809{
6810 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006811 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006812
6813 return -EOPNOTSUPP;
6814}
6815
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006816static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006817 struct dentry *unused, struct inode *inode,
6818 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006819{
6820 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006821 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006822 return -EOPNOTSUPP;
6823}
6824
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006825static ssize_t
6826nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006827{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006828 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006829
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006830 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6831 len = security_inode_listsecurity(inode, list, list_len);
6832 if (list_len && len > list_len)
6833 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006834 }
6835 return len;
6836}
6837
6838static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6839 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006840 .get = nfs4_xattr_get_nfs4_label,
6841 .set = nfs4_xattr_set_nfs4_label,
6842};
David Quigleyc9bccef2013-05-22 12:50:45 -04006843
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006844#else
6845
6846static ssize_t
6847nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6848{
6849 return 0;
6850}
6851
6852#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006853
Andy Adamson533eb462011-06-13 18:25:56 -04006854/*
6855 * nfs_fhget will use either the mounted_on_fileid or the fileid
6856 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006857static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6858{
Andy Adamson533eb462011-06-13 18:25:56 -04006859 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6860 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6861 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006862 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006863 return;
6864
6865 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006866 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006867 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6868 fattr->nlink = 2;
6869}
6870
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006871static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6872 const struct qstr *name,
6873 struct nfs4_fs_locations *fs_locations,
6874 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006875{
6876 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006877 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006878 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006879 };
6880 struct nfs4_fs_locations_arg args = {
6881 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006882 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006883 .page = page,
6884 .bitmask = bitmask,
6885 };
Benny Halevy22958462009-04-01 09:22:02 -04006886 struct nfs4_fs_locations_res res = {
6887 .fs_locations = fs_locations,
6888 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006889 struct rpc_message msg = {
6890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6891 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006892 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006893 };
6894 int status;
6895
Harvey Harrison3110ff82008-05-02 13:42:44 -07006896 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006897
6898 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6899 * is not supported */
6900 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6901 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6902 else
6903 bitmask[0] |= FATTR4_WORD0_FILEID;
6904
Trond Myklebustc228fd32007-01-13 02:28:11 -05006905 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006906 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006907 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006908 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006909 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006910 return status;
6911}
6912
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006913int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6914 const struct qstr *name,
6915 struct nfs4_fs_locations *fs_locations,
6916 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006917{
6918 struct nfs4_exception exception = { };
6919 int err;
6920 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006921 err = _nfs4_proc_fs_locations(client, dir, name,
6922 fs_locations, page);
6923 trace_nfs4_get_fs_locations(dir, name, err);
6924 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006925 &exception);
6926 } while (exception.retry);
6927 return err;
6928}
6929
Chuck Leverb03d7352013-10-17 14:12:50 -04006930/*
6931 * This operation also signals the server that this client is
6932 * performing migration recovery. The server can stop returning
6933 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6934 * appended to this compound to identify the client ID which is
6935 * performing recovery.
6936 */
6937static int _nfs40_proc_get_locations(struct inode *inode,
6938 struct nfs4_fs_locations *locations,
6939 struct page *page, struct rpc_cred *cred)
6940{
6941 struct nfs_server *server = NFS_SERVER(inode);
6942 struct rpc_clnt *clnt = server->client;
6943 u32 bitmask[2] = {
6944 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6945 };
6946 struct nfs4_fs_locations_arg args = {
6947 .clientid = server->nfs_client->cl_clientid,
6948 .fh = NFS_FH(inode),
6949 .page = page,
6950 .bitmask = bitmask,
6951 .migration = 1, /* skip LOOKUP */
6952 .renew = 1, /* append RENEW */
6953 };
6954 struct nfs4_fs_locations_res res = {
6955 .fs_locations = locations,
6956 .migration = 1,
6957 .renew = 1,
6958 };
6959 struct rpc_message msg = {
6960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6961 .rpc_argp = &args,
6962 .rpc_resp = &res,
6963 .rpc_cred = cred,
6964 };
6965 unsigned long now = jiffies;
6966 int status;
6967
6968 nfs_fattr_init(&locations->fattr);
6969 locations->server = server;
6970 locations->nlocations = 0;
6971
6972 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6973 nfs4_set_sequence_privileged(&args.seq_args);
6974 status = nfs4_call_sync_sequence(clnt, server, &msg,
6975 &args.seq_args, &res.seq_res);
6976 if (status)
6977 return status;
6978
6979 renew_lease(server, now);
6980 return 0;
6981}
6982
6983#ifdef CONFIG_NFS_V4_1
6984
6985/*
6986 * This operation also signals the server that this client is
6987 * performing migration recovery. The server can stop asserting
6988 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6989 * performing this operation is identified in the SEQUENCE
6990 * operation in this compound.
6991 *
6992 * When the client supports GETATTR(fs_locations_info), it can
6993 * be plumbed in here.
6994 */
6995static int _nfs41_proc_get_locations(struct inode *inode,
6996 struct nfs4_fs_locations *locations,
6997 struct page *page, struct rpc_cred *cred)
6998{
6999 struct nfs_server *server = NFS_SERVER(inode);
7000 struct rpc_clnt *clnt = server->client;
7001 u32 bitmask[2] = {
7002 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7003 };
7004 struct nfs4_fs_locations_arg args = {
7005 .fh = NFS_FH(inode),
7006 .page = page,
7007 .bitmask = bitmask,
7008 .migration = 1, /* skip LOOKUP */
7009 };
7010 struct nfs4_fs_locations_res res = {
7011 .fs_locations = locations,
7012 .migration = 1,
7013 };
7014 struct rpc_message msg = {
7015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7016 .rpc_argp = &args,
7017 .rpc_resp = &res,
7018 .rpc_cred = cred,
7019 };
7020 int status;
7021
7022 nfs_fattr_init(&locations->fattr);
7023 locations->server = server;
7024 locations->nlocations = 0;
7025
7026 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7027 nfs4_set_sequence_privileged(&args.seq_args);
7028 status = nfs4_call_sync_sequence(clnt, server, &msg,
7029 &args.seq_args, &res.seq_res);
7030 if (status == NFS4_OK &&
7031 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7032 status = -NFS4ERR_LEASE_MOVED;
7033 return status;
7034}
7035
7036#endif /* CONFIG_NFS_V4_1 */
7037
7038/**
7039 * nfs4_proc_get_locations - discover locations for a migrated FSID
7040 * @inode: inode on FSID that is migrating
7041 * @locations: result of query
7042 * @page: buffer
7043 * @cred: credential to use for this operation
7044 *
7045 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7046 * operation failed, or a negative errno if a local error occurred.
7047 *
7048 * On success, "locations" is filled in, but if the server has
7049 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7050 * asserted.
7051 *
7052 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7053 * from this client that require migration recovery.
7054 */
7055int nfs4_proc_get_locations(struct inode *inode,
7056 struct nfs4_fs_locations *locations,
7057 struct page *page, struct rpc_cred *cred)
7058{
7059 struct nfs_server *server = NFS_SERVER(inode);
7060 struct nfs_client *clp = server->nfs_client;
7061 const struct nfs4_mig_recovery_ops *ops =
7062 clp->cl_mvops->mig_recovery_ops;
7063 struct nfs4_exception exception = { };
7064 int status;
7065
7066 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7067 (unsigned long long)server->fsid.major,
7068 (unsigned long long)server->fsid.minor,
7069 clp->cl_hostname);
7070 nfs_display_fhandle(NFS_FH(inode), __func__);
7071
7072 do {
7073 status = ops->get_locations(inode, locations, page, cred);
7074 if (status != -NFS4ERR_DELAY)
7075 break;
7076 nfs4_handle_exception(server, status, &exception);
7077 } while (exception.retry);
7078 return status;
7079}
7080
Chuck Lever44c99932013-10-17 14:13:30 -04007081/*
7082 * This operation also signals the server that this client is
7083 * performing "lease moved" recovery. The server can stop
7084 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7085 * is appended to this compound to identify the client ID which is
7086 * performing recovery.
7087 */
7088static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7089{
7090 struct nfs_server *server = NFS_SERVER(inode);
7091 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7092 struct rpc_clnt *clnt = server->client;
7093 struct nfs4_fsid_present_arg args = {
7094 .fh = NFS_FH(inode),
7095 .clientid = clp->cl_clientid,
7096 .renew = 1, /* append RENEW */
7097 };
7098 struct nfs4_fsid_present_res res = {
7099 .renew = 1,
7100 };
7101 struct rpc_message msg = {
7102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7103 .rpc_argp = &args,
7104 .rpc_resp = &res,
7105 .rpc_cred = cred,
7106 };
7107 unsigned long now = jiffies;
7108 int status;
7109
7110 res.fh = nfs_alloc_fhandle();
7111 if (res.fh == NULL)
7112 return -ENOMEM;
7113
7114 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7115 nfs4_set_sequence_privileged(&args.seq_args);
7116 status = nfs4_call_sync_sequence(clnt, server, &msg,
7117 &args.seq_args, &res.seq_res);
7118 nfs_free_fhandle(res.fh);
7119 if (status)
7120 return status;
7121
7122 do_renew_lease(clp, now);
7123 return 0;
7124}
7125
7126#ifdef CONFIG_NFS_V4_1
7127
7128/*
7129 * This operation also signals the server that this client is
7130 * performing "lease moved" recovery. The server can stop asserting
7131 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7132 * this operation is identified in the SEQUENCE operation in this
7133 * compound.
7134 */
7135static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7136{
7137 struct nfs_server *server = NFS_SERVER(inode);
7138 struct rpc_clnt *clnt = server->client;
7139 struct nfs4_fsid_present_arg args = {
7140 .fh = NFS_FH(inode),
7141 };
7142 struct nfs4_fsid_present_res res = {
7143 };
7144 struct rpc_message msg = {
7145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7146 .rpc_argp = &args,
7147 .rpc_resp = &res,
7148 .rpc_cred = cred,
7149 };
7150 int status;
7151
7152 res.fh = nfs_alloc_fhandle();
7153 if (res.fh == NULL)
7154 return -ENOMEM;
7155
7156 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7157 nfs4_set_sequence_privileged(&args.seq_args);
7158 status = nfs4_call_sync_sequence(clnt, server, &msg,
7159 &args.seq_args, &res.seq_res);
7160 nfs_free_fhandle(res.fh);
7161 if (status == NFS4_OK &&
7162 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7163 status = -NFS4ERR_LEASE_MOVED;
7164 return status;
7165}
7166
7167#endif /* CONFIG_NFS_V4_1 */
7168
7169/**
7170 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7171 * @inode: inode on FSID to check
7172 * @cred: credential to use for this operation
7173 *
7174 * Server indicates whether the FSID is present, moved, or not
7175 * recognized. This operation is necessary to clear a LEASE_MOVED
7176 * condition for this client ID.
7177 *
7178 * Returns NFS4_OK if the FSID is present on this server,
7179 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7180 * NFS4ERR code if some error occurred on the server, or a
7181 * negative errno if a local failure occurred.
7182 */
7183int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7184{
7185 struct nfs_server *server = NFS_SERVER(inode);
7186 struct nfs_client *clp = server->nfs_client;
7187 const struct nfs4_mig_recovery_ops *ops =
7188 clp->cl_mvops->mig_recovery_ops;
7189 struct nfs4_exception exception = { };
7190 int status;
7191
7192 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7193 (unsigned long long)server->fsid.major,
7194 (unsigned long long)server->fsid.minor,
7195 clp->cl_hostname);
7196 nfs_display_fhandle(NFS_FH(inode), __func__);
7197
7198 do {
7199 status = ops->fsid_present(inode, cred);
7200 if (status != -NFS4ERR_DELAY)
7201 break;
7202 nfs4_handle_exception(server, status, &exception);
7203 } while (exception.retry);
7204 return status;
7205}
7206
Andy Adamson5ec16a82013-08-08 10:57:55 -04007207/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007208 * If 'use_integrity' is true and the state managment nfs_client
7209 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7210 * and the machine credential as per RFC3530bis and RFC5661 Security
7211 * Considerations sections. Otherwise, just use the user cred with the
7212 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007213 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007214static 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 +00007215{
7216 int status;
7217 struct nfs4_secinfo_arg args = {
7218 .dir_fh = NFS_FH(dir),
7219 .name = name,
7220 };
7221 struct nfs4_secinfo_res res = {
7222 .flavors = flavors,
7223 };
7224 struct rpc_message msg = {
7225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7226 .rpc_argp = &args,
7227 .rpc_resp = &res,
7228 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007229 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007230 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007231
7232 if (use_integrity) {
7233 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007234 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7235 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007236 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007237
7238 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007239
7240 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7241 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7242
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007243 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7244 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007245 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007246
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007247 if (cred)
7248 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007249
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007250 return status;
7251}
7252
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007253int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7254 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007255{
7256 struct nfs4_exception exception = { };
7257 int err;
7258 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007259 err = -NFS4ERR_WRONGSEC;
7260
7261 /* try to use integrity protection with machine cred */
7262 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7263 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7264
7265 /*
7266 * if unable to use integrity protection, or SECINFO with
7267 * integrity protection returns NFS4ERR_WRONGSEC (which is
7268 * disallowed by spec, but exists in deployed servers) use
7269 * the current filesystem's rpc_client and the user cred.
7270 */
7271 if (err == -NFS4ERR_WRONGSEC)
7272 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7273
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007274 trace_nfs4_secinfo(dir, name, err);
7275 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007276 &exception);
7277 } while (exception.retry);
7278 return err;
7279}
7280
Andy Adamson557134a2009-04-01 09:21:53 -04007281#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007282/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007283 * Check the exchange flags returned by the server for invalid flags, having
7284 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7285 * DS flags set.
7286 */
7287static int nfs4_check_cl_exchange_flags(u32 flags)
7288{
7289 if (flags & ~EXCHGID4_FLAG_MASK_R)
7290 goto out_inval;
7291 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7292 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7293 goto out_inval;
7294 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7295 goto out_inval;
7296 return NFS_OK;
7297out_inval:
7298 return -NFS4ERR_INVAL;
7299}
7300
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007301static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007302nfs41_same_server_scope(struct nfs41_server_scope *a,
7303 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007304{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007305 if (a->server_scope_sz != b->server_scope_sz)
7306 return false;
7307 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007308}
7309
Andy Adamson02a95de2016-02-05 16:08:37 -05007310static void
7311nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7312{
7313}
7314
7315static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7316 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7317};
7318
Andy Adamson357f54d2010-12-14 10:11:57 -05007319/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007320 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007321 *
7322 * The 4.1 client currently uses the same TCP connection for the
7323 * fore and backchannel.
7324 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007325static
7326int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7327 struct rpc_xprt *xprt,
7328 struct nfs_client *clp,
7329 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007330{
7331 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007332 struct nfs41_bind_conn_to_session_args args = {
7333 .client = clp,
7334 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7335 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007336 struct nfs41_bind_conn_to_session_res res;
7337 struct rpc_message msg = {
7338 .rpc_proc =
7339 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007340 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007341 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007342 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007343 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007344 struct rpc_task_setup task_setup_data = {
7345 .rpc_client = clnt,
7346 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007347 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007348 .rpc_message = &msg,
7349 .flags = RPC_TASK_TIMEOUT,
7350 };
7351 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007352
Trond Myklebust71a097c2015-02-18 09:27:18 -08007353 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7354 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7355 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007356
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007357 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7358 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7359 args.dir = NFS4_CDFC4_FORE;
7360
7361 task = rpc_run_task(&task_setup_data);
7362 if (!IS_ERR(task)) {
7363 status = task->tk_status;
7364 rpc_put_task(task);
7365 } else
7366 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007367 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007368 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007369 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007370 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7371 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007372 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007373 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007374 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007375 dprintk("NFS: %s: Unexpected direction from server\n",
7376 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007377 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007378 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007379 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007380 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7381 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007382 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007383 }
7384 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007385
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007386 return status;
7387}
7388
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007389struct rpc_bind_conn_calldata {
7390 struct nfs_client *clp;
7391 struct rpc_cred *cred;
7392};
7393
7394static int
7395nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7396 struct rpc_xprt *xprt,
7397 void *calldata)
7398{
7399 struct rpc_bind_conn_calldata *p = calldata;
7400
7401 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7402}
7403
7404int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7405{
7406 struct rpc_bind_conn_calldata data = {
7407 .clp = clp,
7408 .cred = cred,
7409 };
7410 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7411 nfs4_proc_bind_conn_to_session_callback, &data);
7412}
7413
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007414/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007415 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7416 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007417 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007418static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7419 .how = SP4_MACH_CRED,
7420 .enforce.u.words = {
7421 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7422 1 << (OP_EXCHANGE_ID - 32) |
7423 1 << (OP_CREATE_SESSION - 32) |
7424 1 << (OP_DESTROY_SESSION - 32) |
7425 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007426 },
7427 .allow.u.words = {
7428 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007429 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007430 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007431 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007432 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007433 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007434 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007435 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007436 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007437 1 << (OP_FREE_STATEID - 32) |
7438 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007439 }
7440};
7441
7442/*
7443 * Select the state protection mode for client `clp' given the server results
7444 * from exchange_id in `sp'.
7445 *
7446 * Returns 0 on success, negative errno otherwise.
7447 */
7448static int nfs4_sp4_select_mode(struct nfs_client *clp,
7449 struct nfs41_state_protection *sp)
7450{
7451 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7452 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7453 1 << (OP_EXCHANGE_ID - 32) |
7454 1 << (OP_CREATE_SESSION - 32) |
7455 1 << (OP_DESTROY_SESSION - 32) |
7456 1 << (OP_DESTROY_CLIENTID - 32)
7457 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007458 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007459 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007460 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007461
7462 if (sp->how == SP4_MACH_CRED) {
7463 /* Print state protect result */
7464 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7465 for (i = 0; i <= LAST_NFS4_OP; i++) {
7466 if (test_bit(i, sp->enforce.u.longs))
7467 dfprintk(MOUNT, " enforce op %d\n", i);
7468 if (test_bit(i, sp->allow.u.longs))
7469 dfprintk(MOUNT, " allow op %d\n", i);
7470 }
7471
7472 /* make sure nothing is on enforce list that isn't supported */
7473 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7474 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7475 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007476 ret = -EINVAL;
7477 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007478 }
7479 }
7480
7481 /*
7482 * Minimal mode - state operations are allowed to use machine
7483 * credential. Note this already happens by default, so the
7484 * client doesn't have to do anything more than the negotiation.
7485 *
7486 * NOTE: we don't care if EXCHANGE_ID is in the list -
7487 * we're already using the machine cred for exchange_id
7488 * and will never use a different cred.
7489 */
7490 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7491 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7492 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7493 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7494 dfprintk(MOUNT, "sp4_mach_cred:\n");
7495 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007496 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007497 } else {
7498 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007499 ret = -EINVAL;
7500 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007501 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007502
7503 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007504 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7505 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007506 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7507 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007508 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007509 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007510
Andrew Elble99ade3c2015-12-02 09:39:51 -05007511 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7512 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007513 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007514 }
7515
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007516 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7517 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7518 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007519 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007520 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007521
7522 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7523 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7524 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007525 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007526 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007527
7528 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7529 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007530 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007531 }
7532
7533 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7534 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007535 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007536 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007537 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007538out:
7539 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007540 return 0;
7541}
7542
Andy Adamson8d89bd72016-09-09 09:22:18 -04007543struct nfs41_exchange_id_data {
7544 struct nfs41_exchange_id_res res;
7545 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007546};
7547
Andy Adamson8d89bd72016-09-09 09:22:18 -04007548static void nfs4_exchange_id_release(void *data)
7549{
7550 struct nfs41_exchange_id_data *cdata =
7551 (struct nfs41_exchange_id_data *)data;
7552
Olga Kornievskaia63513232017-03-13 10:36:19 -04007553 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007554 kfree(cdata->res.impl_id);
7555 kfree(cdata->res.server_scope);
7556 kfree(cdata->res.server_owner);
7557 kfree(cdata);
7558}
7559
7560static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007561 .rpc_release = nfs4_exchange_id_release,
7562};
7563
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007564/*
7565 * _nfs4_proc_exchange_id()
7566 *
7567 * Wrapper for EXCHANGE_ID operation.
7568 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007569static struct rpc_task *
7570nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007571 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007572{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007573 struct rpc_message msg = {
7574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007575 .rpc_cred = cred,
7576 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007577 struct rpc_task_setup task_setup_data = {
7578 .rpc_client = clp->cl_rpcclient,
7579 .callback_ops = &nfs4_exchange_id_call_ops,
7580 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007581 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582 };
7583 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007584 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007585
Elena Reshetova212bf412017-10-20 12:53:38 +03007586 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007587 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007588
Trond Myklebust9c760d12017-07-31 18:38:50 -04007589 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007590 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007591 if (!calldata)
7592 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007593
Trond Myklebustfd405592017-08-01 16:02:47 -04007594 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007595
7596 status = nfs4_init_uniform_client_string(clp);
7597 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007598 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007599
Andy Adamson8d89bd72016-09-09 09:22:18 -04007600 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7601 GFP_NOFS);
7602 status = -ENOMEM;
7603 if (unlikely(calldata->res.server_owner == NULL))
7604 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007605
Andy Adamson8d89bd72016-09-09 09:22:18 -04007606 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007607 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007608 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007609 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007610
Andy Adamson8d89bd72016-09-09 09:22:18 -04007611 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7612 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007613 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007614
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007615 switch (sp4_how) {
7616 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007617 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007618 break;
7619
7620 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007622 break;
7623
7624 default:
7625 /* unsupported! */
7626 WARN_ON_ONCE(1);
7627 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007628 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007629 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007630 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007631 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007632 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007633 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7634 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007635 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007636 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007637 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7638 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007639#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007640 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007641#endif
7642 msg.rpc_argp = &calldata->args;
7643 msg.rpc_resp = &calldata->res;
7644 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007645
Trond Myklebust9c760d12017-07-31 18:38:50 -04007646 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007647
7648out_impl_id:
7649 kfree(calldata->res.impl_id);
7650out_server_scope:
7651 kfree(calldata->res.server_scope);
7652out_server_owner:
7653 kfree(calldata->res.server_owner);
7654out_calldata:
7655 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007656out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007657 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007658 return ERR_PTR(status);
7659}
7660
7661/*
7662 * _nfs4_proc_exchange_id()
7663 *
7664 * Wrapper for EXCHANGE_ID operation.
7665 */
7666static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7667 u32 sp4_how)
7668{
7669 struct rpc_task *task;
7670 struct nfs41_exchange_id_args *argp;
7671 struct nfs41_exchange_id_res *resp;
7672 int status;
7673
7674 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7675 if (IS_ERR(task))
7676 return PTR_ERR(task);
7677
7678 argp = task->tk_msg.rpc_argp;
7679 resp = task->tk_msg.rpc_resp;
7680 status = task->tk_status;
7681 if (status != 0)
7682 goto out;
7683
7684 status = nfs4_check_cl_exchange_flags(resp->flags);
7685 if (status != 0)
7686 goto out;
7687
7688 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7689 if (status != 0)
7690 goto out;
7691
7692 clp->cl_clientid = resp->clientid;
7693 clp->cl_exchange_flags = resp->flags;
7694 clp->cl_seqid = resp->seqid;
7695 /* Client ID is not confirmed */
7696 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7697 clear_bit(NFS4_SESSION_ESTABLISHED,
7698 &clp->cl_session->session_state);
7699
7700 if (clp->cl_serverscope != NULL &&
7701 !nfs41_same_server_scope(clp->cl_serverscope,
7702 resp->server_scope)) {
7703 dprintk("%s: server_scope mismatch detected\n",
7704 __func__);
7705 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7706 }
7707
7708 swap(clp->cl_serverowner, resp->server_owner);
7709 swap(clp->cl_serverscope, resp->server_scope);
7710 swap(clp->cl_implid, resp->impl_id);
7711
7712 /* Save the EXCHANGE_ID verifier session trunk tests */
7713 memcpy(clp->cl_confirm.data, argp->verifier.data,
7714 sizeof(clp->cl_confirm.data));
7715out:
7716 trace_nfs4_exchange_id(clp, status);
7717 rpc_put_task(task);
7718 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007719}
7720
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007721/*
7722 * nfs4_proc_exchange_id()
7723 *
7724 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7725 *
7726 * Since the clientid has expired, all compounds using sessions
7727 * associated with the stale clientid will be returning
7728 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7729 * be in some phase of session reset.
7730 *
7731 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7732 */
7733int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7734{
7735 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7736 int status;
7737
7738 /* try SP4_MACH_CRED if krb5i/p */
7739 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7740 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007741 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007742 if (!status)
7743 return 0;
7744 }
7745
7746 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007747 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007748}
7749
Andy Adamson04fa2c62016-09-09 09:22:29 -04007750/**
7751 * nfs4_test_session_trunk
7752 *
7753 * This is an add_xprt_test() test function called from
7754 * rpc_clnt_setup_test_and_add_xprt.
7755 *
7756 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7757 * and is dereferrenced in nfs4_exchange_id_release
7758 *
7759 * Upon success, add the new transport to the rpc_clnt
7760 *
7761 * @clnt: struct rpc_clnt to get new transport
7762 * @xprt: the rpc_xprt to test
7763 * @data: call data for _nfs4_proc_exchange_id.
7764 */
7765int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7766 void *data)
7767{
7768 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007769 struct rpc_task *task;
7770 int status;
7771
Andy Adamson04fa2c62016-09-09 09:22:29 -04007772 u32 sp4_how;
7773
7774 dprintk("--> %s try %s\n", __func__,
7775 xprt->address_strings[RPC_DISPLAY_ADDR]);
7776
7777 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7778
7779 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007780 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7781 if (IS_ERR(task))
7782 return PTR_ERR(task);
7783
7784 status = task->tk_status;
7785 if (status == 0)
7786 status = nfs4_detect_session_trunking(adata->clp,
7787 task->tk_msg.rpc_resp, xprt);
7788
7789 rpc_put_task(task);
7790 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007791}
7792EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7793
Trond Myklebust66245532012-05-25 17:18:09 -04007794static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7795 struct rpc_cred *cred)
7796{
7797 struct rpc_message msg = {
7798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7799 .rpc_argp = clp,
7800 .rpc_cred = cred,
7801 };
7802 int status;
7803
7804 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007805 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007806 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007807 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007808 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7809 return status;
7810}
7811
7812static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7813 struct rpc_cred *cred)
7814{
7815 unsigned int loop;
7816 int ret;
7817
7818 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7819 ret = _nfs4_proc_destroy_clientid(clp, cred);
7820 switch (ret) {
7821 case -NFS4ERR_DELAY:
7822 case -NFS4ERR_CLIENTID_BUSY:
7823 ssleep(1);
7824 break;
7825 default:
7826 return ret;
7827 }
7828 }
7829 return 0;
7830}
7831
7832int nfs4_destroy_clientid(struct nfs_client *clp)
7833{
7834 struct rpc_cred *cred;
7835 int ret = 0;
7836
7837 if (clp->cl_mvops->minor_version < 1)
7838 goto out;
7839 if (clp->cl_exchange_flags == 0)
7840 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007841 if (clp->cl_preserve_clid)
7842 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007843 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007844 ret = nfs4_proc_destroy_clientid(clp, cred);
7845 if (cred)
7846 put_rpccred(cred);
7847 switch (ret) {
7848 case 0:
7849 case -NFS4ERR_STALE_CLIENTID:
7850 clp->cl_exchange_flags = 0;
7851 }
7852out:
7853 return ret;
7854}
7855
Andy Adamson2050f0c2009-04-01 09:22:30 -04007856struct nfs4_get_lease_time_data {
7857 struct nfs4_get_lease_time_args *args;
7858 struct nfs4_get_lease_time_res *res;
7859 struct nfs_client *clp;
7860};
7861
7862static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7863 void *calldata)
7864{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007865 struct nfs4_get_lease_time_data *data =
7866 (struct nfs4_get_lease_time_data *)calldata;
7867
7868 dprintk("--> %s\n", __func__);
7869 /* just setup sequence, do not trigger session recovery
7870 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007871 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007872 &data->args->la_seq_args,
7873 &data->res->lr_seq_res,
7874 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007875 dprintk("<-- %s\n", __func__);
7876}
7877
7878/*
7879 * Called from nfs4_state_manager thread for session setup, so don't recover
7880 * from sequence operation or clientid errors.
7881 */
7882static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7883{
7884 struct nfs4_get_lease_time_data *data =
7885 (struct nfs4_get_lease_time_data *)calldata;
7886
7887 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007888 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7889 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007890 switch (task->tk_status) {
7891 case -NFS4ERR_DELAY:
7892 case -NFS4ERR_GRACE:
7893 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7894 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7895 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007896 /* fall through */
7897 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007898 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007899 return;
7900 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007901 dprintk("<-- %s\n", __func__);
7902}
7903
Trond Myklebust17280172012-03-11 13:11:00 -04007904static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007905 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7906 .rpc_call_done = nfs4_get_lease_time_done,
7907};
7908
7909int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7910{
7911 struct rpc_task *task;
7912 struct nfs4_get_lease_time_args args;
7913 struct nfs4_get_lease_time_res res = {
7914 .lr_fsinfo = fsinfo,
7915 };
7916 struct nfs4_get_lease_time_data data = {
7917 .args = &args,
7918 .res = &res,
7919 .clp = clp,
7920 };
7921 struct rpc_message msg = {
7922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7923 .rpc_argp = &args,
7924 .rpc_resp = &res,
7925 };
7926 struct rpc_task_setup task_setup = {
7927 .rpc_client = clp->cl_rpcclient,
7928 .rpc_message = &msg,
7929 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007930 .callback_data = &data,
7931 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007932 };
7933 int status;
7934
Chuck Levera9c92d62013-08-09 12:48:18 -04007935 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007936 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007937 task = rpc_run_task(&task_setup);
7938
7939 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007940 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007941
Anna Schumakerf6148712017-04-07 14:15:23 -04007942 status = task->tk_status;
7943 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007944 return status;
7945}
7946
Andy Adamsonfc931582009-04-01 09:22:31 -04007947/*
7948 * Initialize the values to be used by the client in CREATE_SESSION
7949 * If nfs4_init_session set the fore channel request and response sizes,
7950 * use them.
7951 *
7952 * Set the back channel max_resp_sz_cached to zero to force the client to
7953 * always set csa_cachethis to FALSE because the current implementation
7954 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7955 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007956static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7957 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007958{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007959 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007960 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007961
Andy Adamson18aad3d2013-06-26 12:21:49 -04007962 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7963 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7964
Andy Adamsonfc931582009-04-01 09:22:31 -04007965 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007966 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7967 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007968 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007969 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007970
7971 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007972 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007973 __func__,
7974 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007975 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007976
7977 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007978 args->bc_attrs.max_rqst_sz = max_bc_payload;
7979 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007980 args->bc_attrs.max_resp_sz_cached = 0;
7981 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007982 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007983
7984 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7985 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7986 __func__,
7987 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7988 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7989 args->bc_attrs.max_reqs);
7990}
7991
Trond Myklebust79969dd2015-02-18 11:30:18 -08007992static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7993 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007994{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007995 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007996 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007997
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007998 if (rcvd->max_resp_sz > sent->max_resp_sz)
7999 return -EINVAL;
8000 /*
8001 * Our requested max_ops is the minimum we need; we're not
8002 * prepared to break up compounds into smaller pieces than that.
8003 * So, no point even trying to continue if the server won't
8004 * cooperate:
8005 */
8006 if (rcvd->max_ops < sent->max_ops)
8007 return -EINVAL;
8008 if (rcvd->max_reqs == 0)
8009 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008010 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8011 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008012 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008013}
8014
Trond Myklebust79969dd2015-02-18 11:30:18 -08008015static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8016 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008017{
8018 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008019 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008020
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008021 if (!(res->flags & SESSION4_BACK_CHAN))
8022 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008023 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8024 return -EINVAL;
8025 if (rcvd->max_resp_sz < sent->max_resp_sz)
8026 return -EINVAL;
8027 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8028 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008029 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008030 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008031 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008032 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008033out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008034 return 0;
8035}
Andy Adamson8d353012009-04-01 09:22:32 -04008036
Andy Adamson8d353012009-04-01 09:22:32 -04008037static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008038 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008039{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008040 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008041
Trond Myklebust79969dd2015-02-18 11:30:18 -08008042 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008043 if (ret)
8044 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008045 return nfs4_verify_back_channel_attrs(args, res);
8046}
8047
8048static void nfs4_update_session(struct nfs4_session *session,
8049 struct nfs41_create_session_res *res)
8050{
8051 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008052 /* Mark client id and session as being confirmed */
8053 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8054 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008055 session->flags = res->flags;
8056 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008057 if (res->flags & SESSION4_BACK_CHAN)
8058 memcpy(&session->bc_attrs, &res->bc_attrs,
8059 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008060}
8061
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008062static int _nfs4_proc_create_session(struct nfs_client *clp,
8063 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008064{
8065 struct nfs4_session *session = clp->cl_session;
8066 struct nfs41_create_session_args args = {
8067 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008068 .clientid = clp->cl_clientid,
8069 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008070 .cb_program = NFS4_CALLBACK,
8071 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008072 struct nfs41_create_session_res res;
8073
Andy Adamsonfc931582009-04-01 09:22:31 -04008074 struct rpc_message msg = {
8075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8076 .rpc_argp = &args,
8077 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008078 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008079 };
8080 int status;
8081
Chuck Lever6b26cc82016-05-02 14:40:40 -04008082 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008083 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008084
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008085 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008086 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008087
Trond Myklebustb519d402016-09-11 14:50:01 -04008088 switch (status) {
8089 case -NFS4ERR_STALE_CLIENTID:
8090 case -NFS4ERR_DELAY:
8091 case -ETIMEDOUT:
8092 case -EACCES:
8093 case -EAGAIN:
8094 goto out;
8095 };
8096
8097 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008098 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008099 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008100 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008101 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008102 if (status)
8103 goto out;
8104 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008105 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008106out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008107 return status;
8108}
8109
8110/*
8111 * Issues a CREATE_SESSION operation to the server.
8112 * It is the responsibility of the caller to verify the session is
8113 * expired before calling this routine.
8114 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008115int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008116{
8117 int status;
8118 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008119 struct nfs4_session *session = clp->cl_session;
8120
8121 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8122
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008123 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008124 if (status)
8125 goto out;
8126
Andy Adamsonaacd5532011-11-09 13:58:21 -05008127 /* Init or reset the session slot tables */
8128 status = nfs4_setup_session_slot_tables(session);
8129 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008130 if (status)
8131 goto out;
8132
8133 ptr = (unsigned *)&session->sess_id.data[0];
8134 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8135 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008136out:
8137 dprintk("<-- %s\n", __func__);
8138 return status;
8139}
8140
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008141/*
8142 * Issue the over-the-wire RPC DESTROY_SESSION.
8143 * The caller must serialize access to this routine.
8144 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008145int nfs4_proc_destroy_session(struct nfs4_session *session,
8146 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008147{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008148 struct rpc_message msg = {
8149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8150 .rpc_argp = session,
8151 .rpc_cred = cred,
8152 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008153 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008154
8155 dprintk("--> nfs4_proc_destroy_session\n");
8156
8157 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008158 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8159 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008160
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008161 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008162 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008163
8164 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008165 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008166 "Session has been destroyed regardless...\n", status);
8167
8168 dprintk("<-- nfs4_proc_destroy_session\n");
8169 return status;
8170}
8171
Trond Myklebust7b38c362012-05-23 13:23:31 -04008172/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008173 * Renew the cl_session lease.
8174 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008175struct nfs4_sequence_data {
8176 struct nfs_client *clp;
8177 struct nfs4_sequence_args args;
8178 struct nfs4_sequence_res res;
8179};
8180
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008181static void nfs41_sequence_release(void *data)
8182{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008183 struct nfs4_sequence_data *calldata = data;
8184 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008185
Elena Reshetova212bf412017-10-20 12:53:38 +03008186 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008187 nfs4_schedule_state_renewal(clp);
8188 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008189 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008190}
8191
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008192static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8193{
8194 switch(task->tk_status) {
8195 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008196 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8197 return -EAGAIN;
8198 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008199 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008200 }
8201 return 0;
8202}
8203
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008204static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008205{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008206 struct nfs4_sequence_data *calldata = data;
8207 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008208
Trond Myklebust14516c32010-07-31 14:29:06 -04008209 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8210 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008211
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008212 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008213 if (task->tk_status < 0) {
8214 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008215 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008216 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008217
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008218 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8219 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008220 return;
8221 }
8222 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008223 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008224out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008225 dprintk("<-- %s\n", __func__);
8226}
8227
8228static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8229{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008230 struct nfs4_sequence_data *calldata = data;
8231 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008232 struct nfs4_sequence_args *args;
8233 struct nfs4_sequence_res *res;
8234
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008235 args = task->tk_msg.rpc_argp;
8236 res = task->tk_msg.rpc_resp;
8237
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008238 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008239}
8240
8241static const struct rpc_call_ops nfs41_sequence_ops = {
8242 .rpc_call_done = nfs41_sequence_call_done,
8243 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008244 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008245};
8246
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008247static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8248 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008249 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008250 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008251{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008252 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008253 struct rpc_message msg = {
8254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8255 .rpc_cred = cred,
8256 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008257 struct rpc_task_setup task_setup_data = {
8258 .rpc_client = clp->cl_rpcclient,
8259 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008260 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008261 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008262 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008263 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008264
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008265 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008266 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008267 goto out_err;
8268
8269 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008270 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008271 if (calldata == NULL)
8272 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008273 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008274 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008275 if (is_privileged)
8276 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008277 msg.rpc_argp = &calldata->args;
8278 msg.rpc_resp = &calldata->res;
8279 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008280 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008281
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008282 ret = rpc_run_task(&task_setup_data);
8283 if (IS_ERR(ret))
8284 goto out_err;
8285 return ret;
8286out_put_clp:
8287 nfs_put_client(clp);
8288out_err:
8289 nfs41_release_slot(slot);
8290 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008291}
8292
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008293static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008294{
8295 struct rpc_task *task;
8296 int ret = 0;
8297
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008298 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008299 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008300 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008301 if (IS_ERR(task))
8302 ret = PTR_ERR(task);
8303 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008304 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008305 dprintk("<-- %s status=%d\n", __func__, ret);
8306 return ret;
8307}
8308
8309static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8310{
8311 struct rpc_task *task;
8312 int ret;
8313
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008314 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008315 if (IS_ERR(task)) {
8316 ret = PTR_ERR(task);
8317 goto out;
8318 }
8319 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008320 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008321 ret = task->tk_status;
8322 rpc_put_task(task);
8323out:
8324 dprintk("<-- %s status=%d\n", __func__, ret);
8325 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008326}
8327
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008328struct nfs4_reclaim_complete_data {
8329 struct nfs_client *clp;
8330 struct nfs41_reclaim_complete_args arg;
8331 struct nfs41_reclaim_complete_res res;
8332};
8333
8334static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8335{
8336 struct nfs4_reclaim_complete_data *calldata = data;
8337
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008338 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008339 &calldata->arg.seq_args,
8340 &calldata->res.seq_res,
8341 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008342}
8343
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008344static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8345{
8346 switch(task->tk_status) {
8347 case 0:
8348 case -NFS4ERR_COMPLETE_ALREADY:
8349 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8350 break;
8351 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008352 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008353 /* fall through */
8354 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008355 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008356 case -NFS4ERR_BADSESSION:
8357 case -NFS4ERR_DEADSESSION:
8358 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8359 nfs4_schedule_session_recovery(clp->cl_session,
8360 task->tk_status);
8361 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008362 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008363 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008364 }
8365 return 0;
8366}
8367
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008368static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8369{
8370 struct nfs4_reclaim_complete_data *calldata = data;
8371 struct nfs_client *clp = calldata->clp;
8372 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8373
8374 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008375 if (!nfs41_sequence_done(task, res))
8376 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008377
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008378 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008379 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8380 rpc_restart_call_prepare(task);
8381 return;
8382 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008383 dprintk("<-- %s\n", __func__);
8384}
8385
8386static void nfs4_free_reclaim_complete_data(void *data)
8387{
8388 struct nfs4_reclaim_complete_data *calldata = data;
8389
8390 kfree(calldata);
8391}
8392
8393static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8394 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8395 .rpc_call_done = nfs4_reclaim_complete_done,
8396 .rpc_release = nfs4_free_reclaim_complete_data,
8397};
8398
8399/*
8400 * Issue a global reclaim complete.
8401 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008402static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8403 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008404{
8405 struct nfs4_reclaim_complete_data *calldata;
8406 struct rpc_task *task;
8407 struct rpc_message msg = {
8408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008409 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008410 };
8411 struct rpc_task_setup task_setup_data = {
8412 .rpc_client = clp->cl_rpcclient,
8413 .rpc_message = &msg,
8414 .callback_ops = &nfs4_reclaim_complete_call_ops,
8415 .flags = RPC_TASK_ASYNC,
8416 };
8417 int status = -ENOMEM;
8418
8419 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008420 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008421 if (calldata == NULL)
8422 goto out;
8423 calldata->clp = clp;
8424 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008425
Chuck Levera9c92d62013-08-09 12:48:18 -04008426 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008427 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008428 msg.rpc_argp = &calldata->arg;
8429 msg.rpc_resp = &calldata->res;
8430 task_setup_data.callback_data = calldata;
8431 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008432 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008433 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008434 goto out;
8435 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008436 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008437 if (status == 0)
8438 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008439 rpc_put_task(task);
8440out:
8441 dprintk("<-- %s status=%d\n", __func__, status);
8442 return status;
8443}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008444
8445static void
8446nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8447{
8448 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008449 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008450
8451 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008452 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008453 &lgp->res.seq_res, task);
8454 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455}
8456
8457static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8458{
8459 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008460
8461 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008462 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008463 dprintk("<-- %s\n", __func__);
8464}
8465
8466static int
8467nfs4_layoutget_handle_exception(struct rpc_task *task,
8468 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8469{
Trond Myklebustee314c22012-10-01 17:25:48 -07008470 struct inode *inode = lgp->args.inode;
8471 struct nfs_server *server = NFS_SERVER(inode);
8472 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008473 int nfs4err = task->tk_status;
8474 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008475 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008476
Boaz Harroshed7e5422014-01-22 20:34:54 +02008477 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008479 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008481 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008482
8483 /*
8484 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8485 * on the file. set tk_status to -ENODATA to tell upper layer to
8486 * retry go inband.
8487 */
8488 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008489 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008490 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008491 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008492 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8493 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8494 */
8495 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008496 status = -EOVERFLOW;
8497 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008498 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008499 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008500 * (or clients) writing to the same RAID stripe except when
8501 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008502 *
8503 * Treat it like we would RECALLCONFLICT -- we retry for a little
8504 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008505 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008506 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008507 if (lgp->args.minlength == 0) {
8508 status = -EOVERFLOW;
8509 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008510 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008511 status = -EBUSY;
8512 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008513 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008514 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008515 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008516 case -NFS4ERR_DELEG_REVOKED:
8517 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008518 case -NFS4ERR_EXPIRED:
8519 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008520 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008521 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008522 lo = NFS_I(inode)->layout;
8523 /* If the open stateid was bad, then recover it. */
8524 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008525 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008526 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008527 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008528 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008529 break;
8530 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008531
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008532 /*
8533 * Mark the bad layout state as invalid, then retry
8534 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008535 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008536 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008537 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008538 pnfs_free_lseg_list(&head);
8539 status = -EAGAIN;
8540 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008541 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008542
Trond Myklebust8ac09252017-01-23 22:44:12 -05008543 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008544 err = nfs4_handle_exception(server, nfs4err, exception);
8545 if (!status) {
8546 if (exception->retry)
8547 status = -EAGAIN;
8548 else
8549 status = err;
8550 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008551out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008552 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008553 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008554}
8555
Idan Kedar85541162012-08-02 11:47:10 +03008556static size_t max_response_pages(struct nfs_server *server)
8557{
8558 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8559 return nfs_page_array_len(0, max_resp_sz);
8560}
8561
8562static void nfs4_free_pages(struct page **pages, size_t size)
8563{
8564 int i;
8565
8566 if (!pages)
8567 return;
8568
8569 for (i = 0; i < size; i++) {
8570 if (!pages[i])
8571 break;
8572 __free_page(pages[i]);
8573 }
8574 kfree(pages);
8575}
8576
8577static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8578{
8579 struct page **pages;
8580 int i;
8581
8582 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8583 if (!pages) {
8584 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8585 return NULL;
8586 }
8587
8588 for (i = 0; i < size; i++) {
8589 pages[i] = alloc_page(gfp_flags);
8590 if (!pages[i]) {
8591 dprintk("%s: failed to allocate page\n", __func__);
8592 nfs4_free_pages(pages, size);
8593 return NULL;
8594 }
8595 }
8596
8597 return pages;
8598}
8599
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008600static void nfs4_layoutget_release(void *calldata)
8601{
8602 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008603 struct inode *inode = lgp->args.inode;
8604 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008605 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008606
8607 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008608 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008609 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008610 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611 put_nfs_open_context(lgp->args.ctx);
8612 kfree(calldata);
8613 dprintk("<-- %s\n", __func__);
8614}
8615
8616static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8617 .rpc_call_prepare = nfs4_layoutget_prepare,
8618 .rpc_call_done = nfs4_layoutget_done,
8619 .rpc_release = nfs4_layoutget_release,
8620};
8621
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008622struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008623nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008624{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008625 struct inode *inode = lgp->args.inode;
8626 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008627 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008628 struct rpc_task *task;
8629 struct rpc_message msg = {
8630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8631 .rpc_argp = &lgp->args,
8632 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008633 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008634 };
8635 struct rpc_task_setup task_setup_data = {
8636 .rpc_client = server->client,
8637 .rpc_message = &msg,
8638 .callback_ops = &nfs4_layoutget_call_ops,
8639 .callback_data = lgp,
8640 .flags = RPC_TASK_ASYNC,
8641 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008642 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008643 struct nfs4_exception exception = {
8644 .inode = inode,
8645 .timeout = *timeout,
8646 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008647 int status = 0;
8648
8649 dprintk("--> %s\n", __func__);
8650
Peng Tao4bd5a982014-11-17 11:05:17 +08008651 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8652 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8653
Idan Kedar85541162012-08-02 11:47:10 +03008654 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8655 if (!lgp->args.layout.pages) {
8656 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008657 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008658 }
8659 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8660
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008661 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008662 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008663 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008664
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008665 task = rpc_run_task(&task_setup_data);
8666 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008667 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008668 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008669 if (status == 0) {
8670 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8671 *timeout = exception.timeout;
8672 }
8673
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008674 trace_nfs4_layoutget(lgp->args.ctx,
8675 &lgp->args.range,
8676 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008677 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008678 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008679
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008680 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8681 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008682 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 rpc_put_task(task);
8684 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008685 if (status)
8686 return ERR_PTR(status);
8687 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008688}
8689
Benny Halevycbe82602011-05-22 19:52:37 +03008690static void
8691nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8692{
8693 struct nfs4_layoutreturn *lrp = calldata;
8694
8695 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008696 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008697 &lrp->args.seq_args,
8698 &lrp->res.seq_res,
8699 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008700}
8701
8702static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8703{
8704 struct nfs4_layoutreturn *lrp = calldata;
8705 struct nfs_server *server;
8706
8707 dprintk("--> %s\n", __func__);
8708
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008709 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008710 return;
8711
8712 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008713 switch (task->tk_status) {
8714 default:
8715 task->tk_status = 0;
8716 case 0:
8717 break;
8718 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008719 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008720 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008721 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008722 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008723 return;
8724 }
Benny Halevycbe82602011-05-22 19:52:37 +03008725 dprintk("<-- %s\n", __func__);
8726}
8727
8728static void nfs4_layoutreturn_release(void *calldata)
8729{
8730 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008731 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008732
8733 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008734 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008735 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008736 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008737 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8738 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008739 pnfs_put_layout_hdr(lrp->args.layout);
8740 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008741 kfree(calldata);
8742 dprintk("<-- %s\n", __func__);
8743}
8744
8745static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8746 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8747 .rpc_call_done = nfs4_layoutreturn_done,
8748 .rpc_release = nfs4_layoutreturn_release,
8749};
8750
Peng Tao6c166052014-11-17 09:30:40 +08008751int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008752{
8753 struct rpc_task *task;
8754 struct rpc_message msg = {
8755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8756 .rpc_argp = &lrp->args,
8757 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008758 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008759 };
8760 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008761 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008762 .rpc_message = &msg,
8763 .callback_ops = &nfs4_layoutreturn_call_ops,
8764 .callback_data = lrp,
8765 };
Peng Tao6c166052014-11-17 09:30:40 +08008766 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008767
Andrew Elble99ade3c2015-12-02 09:39:51 -05008768 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8769 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8770 &task_setup_data.rpc_client, &msg);
8771
Benny Halevycbe82602011-05-22 19:52:37 +03008772 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008773 if (!sync) {
8774 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8775 if (!lrp->inode) {
8776 nfs4_layoutreturn_release(lrp);
8777 return -EAGAIN;
8778 }
8779 task_setup_data.flags |= RPC_TASK_ASYNC;
8780 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008781 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008782 task = rpc_run_task(&task_setup_data);
8783 if (IS_ERR(task))
8784 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008785 if (sync)
8786 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008787 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008788 dprintk("<-- %s status=%d\n", __func__, status);
8789 rpc_put_task(task);
8790 return status;
8791}
8792
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008793static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008794_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8795 struct pnfs_device *pdev,
8796 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008797{
8798 struct nfs4_getdeviceinfo_args args = {
8799 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008800 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8801 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008802 };
8803 struct nfs4_getdeviceinfo_res res = {
8804 .pdev = pdev,
8805 };
8806 struct rpc_message msg = {
8807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8808 .rpc_argp = &args,
8809 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008810 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008811 };
8812 int status;
8813
8814 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008815 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008816 if (res.notification & ~args.notify_types)
8817 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008818 if (res.notification != args.notify_types)
8819 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008820
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008821 dprintk("<-- %s status=%d\n", __func__, status);
8822
8823 return status;
8824}
8825
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008826int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8827 struct pnfs_device *pdev,
8828 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008829{
8830 struct nfs4_exception exception = { };
8831 int err;
8832
8833 do {
8834 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008835 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008836 &exception);
8837 } while (exception.retry);
8838 return err;
8839}
8840EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8841
Andy Adamson863a3c62011-03-23 13:27:54 +00008842static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8843{
8844 struct nfs4_layoutcommit_data *data = calldata;
8845 struct nfs_server *server = NFS_SERVER(data->args.inode);
8846
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008847 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008848 &data->args.seq_args,
8849 &data->res.seq_res,
8850 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008851}
8852
8853static void
8854nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8855{
8856 struct nfs4_layoutcommit_data *data = calldata;
8857 struct nfs_server *server = NFS_SERVER(data->args.inode);
8858
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008859 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008860 return;
8861
8862 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008863 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8864 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8865 case -NFS4ERR_BADLAYOUT: /* no layout */
8866 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008867 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008868 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008869 break;
8870 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008871 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008872 rpc_restart_call_prepare(task);
8873 return;
8874 }
8875 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008876}
8877
8878static void nfs4_layoutcommit_release(void *calldata)
8879{
8880 struct nfs4_layoutcommit_data *data = calldata;
8881
Andy Adamsondb29c082011-07-30 20:52:38 -04008882 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008883 nfs_post_op_update_inode_force_wcc(data->args.inode,
8884 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008885 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008886 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008887 kfree(data);
8888}
8889
8890static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8891 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8892 .rpc_call_done = nfs4_layoutcommit_done,
8893 .rpc_release = nfs4_layoutcommit_release,
8894};
8895
8896int
Andy Adamsonef311532011-03-12 02:58:10 -05008897nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008898{
8899 struct rpc_message msg = {
8900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8901 .rpc_argp = &data->args,
8902 .rpc_resp = &data->res,
8903 .rpc_cred = data->cred,
8904 };
8905 struct rpc_task_setup task_setup_data = {
8906 .task = &data->task,
8907 .rpc_client = NFS_CLIENT(data->args.inode),
8908 .rpc_message = &msg,
8909 .callback_ops = &nfs4_layoutcommit_ops,
8910 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008911 };
8912 struct rpc_task *task;
8913 int status = 0;
8914
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008915 dprintk("NFS: initiating layoutcommit call. sync %d "
8916 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008917 data->args.lastbytewritten,
8918 data->args.inode->i_ino);
8919
Trond Myklebust472e2592015-02-05 16:50:30 -05008920 if (!sync) {
8921 data->inode = nfs_igrab_and_active(data->args.inode);
8922 if (data->inode == NULL) {
8923 nfs4_layoutcommit_release(data);
8924 return -EAGAIN;
8925 }
8926 task_setup_data.flags = RPC_TASK_ASYNC;
8927 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008928 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008929 task = rpc_run_task(&task_setup_data);
8930 if (IS_ERR(task))
8931 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008932 if (sync)
8933 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008934 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008935 dprintk("%s: status %d\n", __func__, status);
8936 rpc_put_task(task);
8937 return status;
8938}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008939
Andy Adamson97431202013-08-08 10:57:56 -04008940/**
8941 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8942 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8943 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008944static int
8945_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008946 struct nfs_fsinfo *info,
8947 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008948{
8949 struct nfs41_secinfo_no_name_args args = {
8950 .style = SECINFO_STYLE_CURRENT_FH,
8951 };
8952 struct nfs4_secinfo_res res = {
8953 .flavors = flavors,
8954 };
8955 struct rpc_message msg = {
8956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8957 .rpc_argp = &args,
8958 .rpc_resp = &res,
8959 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008960 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008961 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008962 int status;
8963
8964 if (use_integrity) {
8965 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008966 cred = nfs4_get_clid_cred(server->nfs_client);
8967 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008968 }
8969
8970 dprintk("--> %s\n", __func__);
8971 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8972 &res.seq_res, 0);
8973 dprintk("<-- %s status=%d\n", __func__, status);
8974
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008975 if (cred)
8976 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008977
8978 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008979}
8980
8981static int
8982nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8983 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8984{
8985 struct nfs4_exception exception = { };
8986 int err;
8987 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008988 /* first try using integrity protection */
8989 err = -NFS4ERR_WRONGSEC;
8990
8991 /* try to use integrity protection with machine cred */
8992 if (_nfs4_is_integrity_protected(server->nfs_client))
8993 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8994 flavors, true);
8995
8996 /*
8997 * if unable to use integrity protection, or SECINFO with
8998 * integrity protection returns NFS4ERR_WRONGSEC (which is
8999 * disallowed by spec, but exists in deployed servers) use
9000 * the current filesystem's rpc_client and the user cred.
9001 */
9002 if (err == -NFS4ERR_WRONGSEC)
9003 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9004 flavors, false);
9005
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009006 switch (err) {
9007 case 0:
9008 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009009 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009010 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009011 default:
9012 err = nfs4_handle_exception(server, err, &exception);
9013 }
9014 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009015out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009016 return err;
9017}
9018
9019static int
9020nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9021 struct nfs_fsinfo *info)
9022{
9023 int err;
9024 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009025 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009026 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009027 struct nfs4_secinfo4 *secinfo;
9028 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009029
9030 page = alloc_page(GFP_KERNEL);
9031 if (!page) {
9032 err = -ENOMEM;
9033 goto out;
9034 }
9035
9036 flavors = page_address(page);
9037 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9038
9039 /*
9040 * Fall back on "guess and check" method if
9041 * the server doesn't support SECINFO_NO_NAME
9042 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009043 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009044 err = nfs4_find_root_sec(server, fhandle, info);
9045 goto out_freepage;
9046 }
9047 if (err)
9048 goto out_freepage;
9049
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009050 for (i = 0; i < flavors->num_flavors; i++) {
9051 secinfo = &flavors->flavors[i];
9052
9053 switch (secinfo->flavor) {
9054 case RPC_AUTH_NULL:
9055 case RPC_AUTH_UNIX:
9056 case RPC_AUTH_GSS:
9057 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9058 &secinfo->flavor_info);
9059 break;
9060 default:
9061 flavor = RPC_AUTH_MAXFLAVOR;
9062 break;
9063 }
9064
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009065 if (!nfs_auth_info_match(&server->auth_info, flavor))
9066 flavor = RPC_AUTH_MAXFLAVOR;
9067
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009068 if (flavor != RPC_AUTH_MAXFLAVOR) {
9069 err = nfs4_lookup_root_sec(server, fhandle,
9070 info, flavor);
9071 if (!err)
9072 break;
9073 }
9074 }
9075
9076 if (flavor == RPC_AUTH_MAXFLAVOR)
9077 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009078
9079out_freepage:
9080 put_page(page);
9081 if (err == -EACCES)
9082 return -EPERM;
9083out:
9084 return err;
9085}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009086
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009087static int _nfs41_test_stateid(struct nfs_server *server,
9088 nfs4_stateid *stateid,
9089 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009090{
9091 int status;
9092 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009093 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009094 };
9095 struct nfs41_test_stateid_res res;
9096 struct rpc_message msg = {
9097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9098 .rpc_argp = &args,
9099 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009100 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009101 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009102 struct rpc_clnt *rpc_client = server->client;
9103
9104 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9105 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009106
Chuck Lever38527b12012-07-11 16:30:23 -04009107 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009108 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009109 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009110 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009111 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009112 if (status != NFS_OK) {
9113 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009114 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009115 }
9116 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009117 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009118}
9119
Trond Myklebust43912bb2016-09-22 13:38:56 -04009120static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9121 int err, struct nfs4_exception *exception)
9122{
9123 exception->retry = 0;
9124 switch(err) {
9125 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009126 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009127 nfs4_handle_exception(server, err, exception);
9128 break;
9129 case -NFS4ERR_BADSESSION:
9130 case -NFS4ERR_BADSLOT:
9131 case -NFS4ERR_BAD_HIGH_SLOT:
9132 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9133 case -NFS4ERR_DEADSESSION:
9134 nfs4_do_handle_exception(server, err, exception);
9135 }
9136}
9137
Chuck Lever38527b12012-07-11 16:30:23 -04009138/**
9139 * nfs41_test_stateid - perform a TEST_STATEID operation
9140 *
9141 * @server: server / transport on which to perform the operation
9142 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009143 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009144 *
9145 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9146 * Otherwise a negative NFS4ERR value is returned if the operation
9147 * failed or the state ID is not currently valid.
9148 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009149static int nfs41_test_stateid(struct nfs_server *server,
9150 nfs4_stateid *stateid,
9151 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009152{
9153 struct nfs4_exception exception = { };
9154 int err;
9155 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009156 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009157 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009158 } while (exception.retry);
9159 return err;
9160}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009161
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009162struct nfs_free_stateid_data {
9163 struct nfs_server *server;
9164 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009165 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009166};
9167
9168static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9169{
9170 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009171 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009172 &data->args.seq_args,
9173 &data->res.seq_res,
9174 task);
9175}
9176
9177static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9178{
9179 struct nfs_free_stateid_data *data = calldata;
9180
9181 nfs41_sequence_done(task, &data->res.seq_res);
9182
9183 switch (task->tk_status) {
9184 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009185 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009186 rpc_restart_call_prepare(task);
9187 }
9188}
9189
9190static void nfs41_free_stateid_release(void *calldata)
9191{
9192 kfree(calldata);
9193}
9194
Trond Myklebust17f26b12013-08-21 15:48:42 -04009195static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009196 .rpc_call_prepare = nfs41_free_stateid_prepare,
9197 .rpc_call_done = nfs41_free_stateid_done,
9198 .rpc_release = nfs41_free_stateid_release,
9199};
9200
9201static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009202 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009203 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009204 bool privileged)
9205{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009206 struct rpc_message msg = {
9207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009208 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009209 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009210 struct rpc_task_setup task_setup = {
9211 .rpc_client = server->client,
9212 .rpc_message = &msg,
9213 .callback_ops = &nfs41_free_stateid_ops,
9214 .flags = RPC_TASK_ASYNC,
9215 };
9216 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009217
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009218 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9219 &task_setup.rpc_client, &msg);
9220
Chuck Lever38527b12012-07-11 16:30:23 -04009221 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009222 data = kmalloc(sizeof(*data), GFP_NOFS);
9223 if (!data)
9224 return ERR_PTR(-ENOMEM);
9225 data->server = server;
9226 nfs4_stateid_copy(&data->args.stateid, stateid);
9227
9228 task_setup.callback_data = data;
9229
9230 msg.rpc_argp = &data->args;
9231 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009232 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009233 if (privileged)
9234 nfs4_set_sequence_privileged(&data->args.seq_args);
9235
9236 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009237}
9238
Chuck Lever38527b12012-07-11 16:30:23 -04009239/**
9240 * nfs41_free_stateid - perform a FREE_STATEID operation
9241 *
9242 * @server: server / transport on which to perform the operation
9243 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009244 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009245 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009246 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009247 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009248 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009249static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009250 const nfs4_stateid *stateid,
9251 struct rpc_cred *cred,
9252 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009253{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009254 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009255
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009256 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009257 if (IS_ERR(task))
9258 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009259 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009260 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009261}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009262
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009263static void
9264nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009265{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009266 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009267
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009268 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009269 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009270}
9271
Trond Myklebust36281ca2012-03-04 18:13:56 -05009272static bool nfs41_match_stateid(const nfs4_stateid *s1,
9273 const nfs4_stateid *s2)
9274{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009275 if (s1->type != s2->type)
9276 return false;
9277
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009278 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009279 return false;
9280
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009281 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009282 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009283
Anna Schumaker045c5512017-01-11 16:59:48 -05009284 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009285}
9286
Andy Adamson557134a2009-04-01 09:21:53 -04009287#endif /* CONFIG_NFS_V4_1 */
9288
Trond Myklebust36281ca2012-03-04 18:13:56 -05009289static bool nfs4_match_stateid(const nfs4_stateid *s1,
9290 const nfs4_stateid *s2)
9291{
Trond Myklebustf597c532012-03-04 18:13:56 -05009292 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009293}
9294
9295
Trond Myklebust17280172012-03-11 13:11:00 -04009296static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009297 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009298 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 .recover_open = nfs4_open_reclaim,
9300 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009301 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009302 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303};
9304
Andy Adamson591d71c2009-04-01 09:22:47 -04009305#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009306static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009307 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9308 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9309 .recover_open = nfs4_open_reclaim,
9310 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009311 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009312 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009313 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009314};
9315#endif /* CONFIG_NFS_V4_1 */
9316
Trond Myklebust17280172012-03-11 13:11:00 -04009317static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009318 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009319 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009320 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009322 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323};
9324
Andy Adamson591d71c2009-04-01 09:22:47 -04009325#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009326static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009327 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9328 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009329 .recover_open = nfs41_open_expired,
9330 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009331 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009332};
9333#endif /* CONFIG_NFS_V4_1 */
9334
Trond Myklebust17280172012-03-11 13:11:00 -04009335static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009336 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009337 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009338 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009339};
9340
9341#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009342static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009343 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009344 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009345 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009346};
9347#endif
9348
Chuck Leverec011fe2013-10-17 14:12:39 -04009349static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009350 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009351 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009352};
9353
9354#if defined(CONFIG_NFS_V4_1)
9355static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009356 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009357 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009358};
9359#endif /* CONFIG_NFS_V4_1 */
9360
Trond Myklebust97dc1352010-06-16 09:52:26 -04009361static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9362 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009363 .init_caps = NFS_CAP_READDIRPLUS
9364 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009365 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009366 .init_client = nfs40_init_client,
9367 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009368 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009369 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009370 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009371 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009372 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009373 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009374 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9375 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9376 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009377 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009378};
9379
9380#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009381static struct nfs_seqid *
9382nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9383{
9384 return NULL;
9385}
9386
Trond Myklebust97dc1352010-06-16 09:52:26 -04009387static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9388 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009389 .init_caps = NFS_CAP_READDIRPLUS
9390 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009391 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009392 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009393 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009394 .init_client = nfs41_init_client,
9395 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009396 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009397 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009398 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009399 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009400 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009401 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009402 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009403 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9404 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9405 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009406 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009407};
9408#endif
9409
Steve Dickson42c2c422013-05-22 12:50:38 -04009410#if defined(CONFIG_NFS_V4_2)
9411static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9412 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009413 .init_caps = NFS_CAP_READDIRPLUS
9414 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009415 | NFS_CAP_POSIX_LOCK
9416 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009417 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009418 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009419 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009420 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009421 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009422 | NFS_CAP_LAYOUTSTATS
9423 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009424 .init_client = nfs41_init_client,
9425 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009426 .match_stateid = nfs41_match_stateid,
9427 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009428 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009429 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009430 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009431 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009432 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009433 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9434 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9435 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009436 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009437};
9438#endif
9439
Trond Myklebust97dc1352010-06-16 09:52:26 -04009440const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9441 [0] = &nfs_v4_0_minor_ops,
9442#if defined(CONFIG_NFS_V4_1)
9443 [1] = &nfs_v4_1_minor_ops,
9444#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009445#if defined(CONFIG_NFS_V4_2)
9446 [2] = &nfs_v4_2_minor_ops,
9447#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009448};
9449
Trond Myklebust13997822016-07-24 17:10:52 -04009450static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009451{
9452 ssize_t error, error2;
9453
9454 error = generic_listxattr(dentry, list, size);
9455 if (error < 0)
9456 return error;
9457 if (list) {
9458 list += error;
9459 size -= error;
9460 }
9461
9462 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9463 if (error2 < 0)
9464 return error2;
9465 return error + error2;
9466}
9467
Trond Myklebust17f26b12013-08-21 15:48:42 -04009468static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009469 .create = nfs_create,
9470 .lookup = nfs_lookup,
9471 .atomic_open = nfs_atomic_open,
9472 .link = nfs_link,
9473 .unlink = nfs_unlink,
9474 .symlink = nfs_symlink,
9475 .mkdir = nfs_mkdir,
9476 .rmdir = nfs_rmdir,
9477 .mknod = nfs_mknod,
9478 .rename = nfs_rename,
9479 .permission = nfs_permission,
9480 .getattr = nfs_getattr,
9481 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009482 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009483};
9484
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009485static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009486 .permission = nfs_permission,
9487 .getattr = nfs_getattr,
9488 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009489 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009490};
9491
David Howells509de812006-08-22 20:06:11 -04009492const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 .version = 4, /* protocol version */
9494 .dentry_ops = &nfs4_dentry_operations,
9495 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009496 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009497 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009499 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009500 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009501 .getattr = nfs4_proc_getattr,
9502 .setattr = nfs4_proc_setattr,
9503 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009504 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009505 .access = nfs4_proc_access,
9506 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 .create = nfs4_proc_create,
9508 .remove = nfs4_proc_remove,
9509 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009510 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009511 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009512 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009513 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009514 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009515 .link = nfs4_proc_link,
9516 .symlink = nfs4_proc_symlink,
9517 .mkdir = nfs4_proc_mkdir,
9518 .rmdir = nfs4_proc_remove,
9519 .readdir = nfs4_proc_readdir,
9520 .mknod = nfs4_proc_mknod,
9521 .statfs = nfs4_proc_statfs,
9522 .fsinfo = nfs4_proc_fsinfo,
9523 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009524 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009526 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009527 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009528 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009530 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009532 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009533 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009535 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009536 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009537 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009538 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009539 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009540 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009541 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009542 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009543 .create_server = nfs4_create_server,
9544 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545};
9546
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009547static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009548 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009549 .list = nfs4_xattr_list_nfs4_acl,
9550 .get = nfs4_xattr_get_nfs4_acl,
9551 .set = nfs4_xattr_set_nfs4_acl,
9552};
9553
9554const struct xattr_handler *nfs4_xattr_handlers[] = {
9555 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009556#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9557 &nfs4_xattr_nfs4_label_handler,
9558#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009559 NULL
9560};
9561
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562/*
9563 * Local variables:
9564 * c-basic-offset: 8
9565 * End:
9566 */