blob: 56fa5a16e0978e73d898b4f32cd4db44b9c18221 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -040099static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
100 struct rpc_cred *cred,
101 struct nfs4_slot *slot,
102 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400103static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
104 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400105static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
106 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400107#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400108
109#ifdef CONFIG_NFS_V4_SECURITY_LABEL
110static inline struct nfs4_label *
111nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
112 struct iattr *sattr, struct nfs4_label *label)
113{
114 int err;
115
116 if (label == NULL)
117 return NULL;
118
119 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
120 return NULL;
121
David Quigleyaa9c2662013-05-22 12:50:44 -0400122 err = security_dentry_init_security(dentry, sattr->ia_mode,
123 &dentry->d_name, (void **)&label->label, &label->len);
124 if (err == 0)
125 return label;
126
127 return NULL;
128}
129static inline void
130nfs4_label_release_security(struct nfs4_label *label)
131{
132 if (label)
133 security_release_secctx(label->label, label->len);
134}
135static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
136{
137 if (label)
138 return server->attr_bitmask;
139
140 return server->attr_bitmask_nl;
141}
142#else
143static inline struct nfs4_label *
144nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
145 struct iattr *sattr, struct nfs4_label *l)
146{ return NULL; }
147static inline void
148nfs4_label_release_security(struct nfs4_label *label)
149{ return; }
150static inline u32 *
151nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
152{ return server->attr_bitmask; }
153#endif
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500156static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Trond Myklebust52567b02009-10-23 14:46:42 -0400158 if (err >= -1000)
159 return err;
160 switch (err) {
161 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500162 case -NFS4ERR_LAYOUTTRYLATER:
163 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000165 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400166 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800168 case -NFS4ERR_BADOWNER:
169 case -NFS4ERR_BADNAME:
170 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400171 case -NFS4ERR_SHARE_DENIED:
172 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400173 case -NFS4ERR_MINOR_VERS_MISMATCH:
174 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400175 case -NFS4ERR_FILE_OPEN:
176 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700179 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * This is our standard bitmap for GETATTR requests.
187 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400188const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID,
194 FATTR4_WORD1_MODE
195 | FATTR4_WORD1_NUMLINKS
196 | FATTR4_WORD1_OWNER
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400204#ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Trond Myklebust1549210f2012-06-05 09:16:47 -0400209static const u32 nfs4_pnfs_open_bitmap[3] = {
210 FATTR4_WORD0_TYPE
211 | FATTR4_WORD0_CHANGE
212 | FATTR4_WORD0_SIZE
213 | FATTR4_WORD0_FSID
214 | FATTR4_WORD0_FILEID,
215 FATTR4_WORD1_MODE
216 | FATTR4_WORD1_NUMLINKS
217 | FATTR4_WORD1_OWNER
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY,
224 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500225#ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
227#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400228};
229
Andy Adamsone23008e2012-10-02 21:07:32 -0400230static const u32 nfs4_open_noattr_bitmap[3] = {
231 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400232 | FATTR4_WORD0_FILEID,
233};
234
David Quigleya09df2c2013-05-22 12:50:41 -0400235const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_MAXLINK
246 | FATTR4_WORD0_MAXNAME,
247 0
248};
249
Fred Isamandae100c2011-07-30 20:52:37 -0400250const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700254 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400255 | FATTR4_WORD1_FS_LAYOUT_TYPES,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800257 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
David Quigleya09df2c2013-05-22 12:50:41 -0400260const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500261 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400262 | FATTR4_WORD0_SIZE
263 | FATTR4_WORD0_FSID
264 | FATTR4_WORD0_FILEID
265 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500266 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400274};
275
Al Virobc4785c2006-10-19 23:28:51 -0700276static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nfs4_readdir_arg *readdir)
278{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400279 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400310 *p++ = htonl(attrs); /* bitmap */
311 *p++ = htonl(12); /* attribute buffer length */
312 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000313 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 *p++ = xdr_one; /* next */
317 *p++ = xdr_zero; /* cookie, first word */
318 *p++ = xdr_two; /* cookie, second word */
319 *p++ = xdr_two; /* entry len */
320 memcpy(p, "..\0\0", 4); /* entry */
321 p++;
322 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400323 *p++ = htonl(attrs); /* bitmap */
324 *p++ = htonl(12); /* attribute buffer length */
325 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000326 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 readdir->pgbase = (char *)p - (char *)start;
329 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800330 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Trond Myklebust26d36302016-09-22 13:39:05 -0400333static void nfs4_test_and_free_stateid(struct nfs_server *server,
334 nfs4_stateid *stateid,
335 struct rpc_cred *cred)
336{
337 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
338
339 ops->test_and_free_expired(server, stateid, cred);
340}
341
342static void __nfs4_free_revoked_stateid(struct nfs_server *server,
343 nfs4_stateid *stateid,
344 struct rpc_cred *cred)
345{
346 stateid->type = NFS4_REVOKED_STATEID_TYPE;
347 nfs4_test_and_free_stateid(server, stateid, cred);
348}
349
350static void nfs4_free_revoked_stateid(struct nfs_server *server,
351 const nfs4_stateid *stateid,
352 struct rpc_cred *cred)
353{
354 nfs4_stateid tmp;
355
356 nfs4_stateid_copy(&tmp, stateid);
357 __nfs4_free_revoked_stateid(server, &tmp, cred);
358}
359
NeilBrown8478eaa2014-09-18 16:09:27 +1000360static long nfs4_update_delay(long *timeout)
361{
362 long ret;
363 if (!timeout)
364 return NFS4_POLL_RETRY_MAX;
365 if (*timeout <= 0)
366 *timeout = NFS4_POLL_RETRY_MIN;
367 if (*timeout > NFS4_POLL_RETRY_MAX)
368 *timeout = NFS4_POLL_RETRY_MAX;
369 ret = *timeout;
370 *timeout <<= 1;
371 return ret;
372}
373
Trond Myklebust65de8722008-12-23 15:21:44 -0500374static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
375{
376 int res = 0;
377
378 might_sleep();
379
NeilBrown8478eaa2014-09-18 16:09:27 +1000380 freezable_schedule_timeout_killable_unsafe(
381 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 if (fatal_signal_pending(current))
383 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500384 return res;
385}
386
387/* This is the error handling routine for processes that are allowed
388 * to sleep.
389 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400390static int nfs4_do_handle_exception(struct nfs_server *server,
391 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500392{
393 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500394 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400395 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500396 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 int ret = errorcode;
398
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400399 exception->delay = 0;
400 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500401 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400402
403 if (stateid == NULL && state != NULL)
404 stateid = &state->stateid;
405
Trond Myklebust65de8722008-12-23 15:21:44 -0500406 switch(errorcode) {
407 case 0:
408 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400409 case -NFS4ERR_DELEG_REVOKED:
410 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400412 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400413 if (inode != NULL && stateid != NULL) {
414 nfs_inode_find_state_and_recover(inode,
415 stateid);
416 goto wait_on_recovery;
417 }
418 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400419 if (inode) {
420 int err;
421
422 err = nfs_async_inode_return_delegation(inode,
423 stateid);
424 if (err == 0)
425 goto wait_on_recovery;
426 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
427 exception->retry = 1;
428 break;
429 }
430 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500431 if (state == NULL)
432 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400433 ret = nfs4_schedule_stateid_recovery(server, state);
434 if (ret < 0)
435 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500436 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500439 nfs4_schedule_lease_recovery(clp);
440 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400441 case -NFS4ERR_MOVED:
442 ret = nfs4_schedule_migration_recovery(server);
443 if (ret < 0)
444 break;
445 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400446 case -NFS4ERR_LEASE_MOVED:
447 nfs4_schedule_lease_moved_recovery(clp);
448 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500449#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400450 case -NFS4ERR_BADSESSION:
451 case -NFS4ERR_BADSLOT:
452 case -NFS4ERR_BAD_HIGH_SLOT:
453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
454 case -NFS4ERR_DEADSESSION:
455 case -NFS4ERR_SEQ_FALSE_RETRY:
456 case -NFS4ERR_SEQ_MISORDERED:
457 dprintk("%s ERROR: %d Reset session\n", __func__,
458 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400459 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400460 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500461#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500462 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500463 if (exception->timeout > HZ) {
464 /* We have retried a decent amount, time to
465 * fail
466 */
467 ret = -EBUSY;
468 break;
469 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500470 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400471 nfs_inc_server_stats(server, NFSIOS_DELAY);
472 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400473 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400474 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400475 exception->delay = 1;
476 return 0;
477
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400478 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500479 case -NFS4ERR_OLD_STATEID:
480 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800481 break;
482 case -NFS4ERR_BADOWNER:
483 /* The following works around a Linux server bug! */
484 case -NFS4ERR_BADNAME:
485 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
486 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
487 exception->retry = 1;
488 printk(KERN_WARNING "NFS: v4 server %s "
489 "does not accept raw "
490 "uid/gids. "
491 "Reenabling the idmapper.\n",
492 server->nfs_client->cl_hostname);
493 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500494 }
495 /* We failed to handle the error */
496 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500497wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400498 exception->recovering = 1;
499 return 0;
500}
501
502/* This is the error handling routine for processes that are allowed
503 * to sleep.
504 */
505int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
506{
507 struct nfs_client *clp = server->nfs_client;
508 int ret;
509
510 ret = nfs4_do_handle_exception(server, errorcode, exception);
511 if (exception->delay) {
512 ret = nfs4_delay(server->client, &exception->timeout);
513 goto out_retry;
514 }
515 if (exception->recovering) {
516 ret = nfs4_wait_clnt_recover(clp);
517 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
518 return -EIO;
519 goto out_retry;
520 }
521 return ret;
522out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500523 if (ret == 0)
524 exception->retry = 1;
525 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500526}
527
Trond Myklebust037fc982015-09-20 15:51:00 -0400528static int
529nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
530 int errorcode, struct nfs4_exception *exception)
531{
532 struct nfs_client *clp = server->nfs_client;
533 int ret;
534
535 ret = nfs4_do_handle_exception(server, errorcode, exception);
536 if (exception->delay) {
537 rpc_delay(task, nfs4_update_delay(&exception->timeout));
538 goto out_retry;
539 }
540 if (exception->recovering) {
541 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
542 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
543 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
544 goto out_retry;
545 }
546 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
547 ret = -EIO;
548 return ret;
549out_retry:
550 if (ret == 0)
551 exception->retry = 1;
552 return ret;
553}
554
555static int
556nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
557 struct nfs4_state *state, long *timeout)
558{
559 struct nfs4_exception exception = {
560 .state = state,
561 };
562
563 if (task->tk_status >= 0)
564 return 0;
565 if (timeout)
566 exception.timeout = *timeout;
567 task->tk_status = nfs4_async_handle_exception(task, server,
568 task->tk_status,
569 &exception);
570 if (exception.delay && timeout)
571 *timeout = exception.timeout;
572 if (exception.retry)
573 return -EAGAIN;
574 return 0;
575}
576
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400577/*
578 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
579 * or 'false' otherwise.
580 */
581static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
582{
583 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500584 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400585}
Trond Myklebust65de8722008-12-23 15:21:44 -0500586
Trond Myklebust452e9352010-07-31 14:29:06 -0400587static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 spin_lock(&clp->cl_lock);
590 if (time_before(clp->cl_last_renewal,timestamp))
591 clp->cl_last_renewal = timestamp;
592 spin_unlock(&clp->cl_lock);
593}
594
Trond Myklebust452e9352010-07-31 14:29:06 -0400595static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
596{
Trond Myklebustbe824162015-07-05 14:50:46 -0400597 struct nfs_client *clp = server->nfs_client;
598
599 if (!nfs4_has_session(clp))
600 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400601}
602
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400603struct nfs4_call_sync_data {
604 const struct nfs_server *seq_server;
605 struct nfs4_sequence_args *seq_args;
606 struct nfs4_sequence_res *seq_res;
607};
608
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800609void nfs4_init_sequence(struct nfs4_sequence_args *args,
610 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400611{
612 args->sa_slot = NULL;
613 args->sa_cache_this = cache_reply;
614 args->sa_privileged = 0;
615
616 res->sr_slot = NULL;
617}
618
619static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
620{
621 args->sa_privileged = 1;
622}
623
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400624static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400625{
626 struct nfs4_slot *slot = res->sr_slot;
627 struct nfs4_slot_table *tbl;
628
Chuck Lever3bd23842013-08-09 12:49:19 -0400629 tbl = slot->table;
630 spin_lock(&tbl->slot_tbl_lock);
631 if (!nfs41_wake_and_assign_slot(tbl, slot))
632 nfs4_free_slot(tbl, slot);
633 spin_unlock(&tbl->slot_tbl_lock);
634
635 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400636}
637
638static int nfs40_sequence_done(struct rpc_task *task,
639 struct nfs4_sequence_res *res)
640{
641 if (res->sr_slot != NULL)
642 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 return 1;
644}
645
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400646#if defined(CONFIG_NFS_V4_1)
647
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400648static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400649{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500650 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400651 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500652 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400653
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400654 if (!slot)
655 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500656 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500657 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500658
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400659 /* Bump the slot sequence number */
660 if (slot->seq_done)
661 slot->seq_nr++;
662 slot->seq_done = 0;
663
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500664 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500665 /* Be nice to the server: try to ensure that the last transmitted
666 * value for highest_user_slotid <= target_highest_slotid
667 */
668 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
669 send_new_highest_used_slotid = true;
670
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500671 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500672 send_new_highest_used_slotid = false;
673 goto out_unlock;
674 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500675 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500676
677 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
678 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500679out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500680 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500681 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400682 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400683 if (waitqueue_active(&tbl->slot_waitq))
684 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400685}
686
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400687static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
688{
689 nfs41_release_slot(res->sr_slot);
690 res->sr_slot = NULL;
691}
692
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400693static int nfs41_sequence_process(struct rpc_task *task,
694 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400695{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500696 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500699 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500700 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400701
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500702 if (slot == NULL)
703 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400704 /* don't increment the sequence number if the task wasn't sent */
705 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400706 goto out;
707
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500708 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500709
Trond Myklebustac20d162012-12-15 15:36:07 -0500710 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400711 if (res->sr_status != -NFS4ERR_DELAY)
712 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500713 interrupted = true;
714 }
715
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400716 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500717 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400718 switch (res->sr_status) {
719 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400720 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400721 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500723 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500724 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
726 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500727 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 case 1:
730 /*
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
733 * operation..
734 * Mark the slot as having hosted an interrupted RPC call.
735 */
736 slot->interrupted = 1;
737 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 case -NFS4ERR_DELAY:
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
741 * of RFC5661.
742 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500746 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400747 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500748 case -NFS4ERR_BADSLOT:
749 /*
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
752 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400753 if (slot->seq_nr < slot->table->target_highest_slotid)
754 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500755 goto retry_nowait;
756 case -NFS4ERR_SEQ_MISORDERED:
757 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500758 * Was the last operation on this sequence interrupted?
759 * If so, retry after bumping the sequence number.
760 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400761 if (interrupted)
762 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
766 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 if (slot->seq_nr != 1) {
768 slot->seq_nr = 1;
769 goto retry_nowait;
770 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400771 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500772 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400773 if (interrupted)
774 goto retry_new_seq;
775 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 default:
777 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400778 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779 }
780out:
781 /* The session may be reset by one of the error handlers. */
782 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500783out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500784 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400785session_recover:
786 nfs4_schedule_session_recovery(session, res->sr_status);
787 goto retry_nowait;
788retry_new_seq:
789 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500790retry_nowait:
791 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400792 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500793 task->tk_status = 0;
794 ret = 0;
795 }
796 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400797out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400798 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400799 goto out;
800 rpc_delay(task, NFS4_POLL_RETRY_MAX);
801 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400802}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400803
804int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
805{
806 if (!nfs41_sequence_process(task, res))
807 return 0;
808 if (res->sr_slot != NULL)
809 nfs41_sequence_free_slot(res);
810 return 1;
811
812}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500813EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400814
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400815static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
816{
817 if (res->sr_slot == NULL)
818 return 1;
819 if (res->sr_slot->table->session != NULL)
820 return nfs41_sequence_process(task, res);
821 return nfs40_sequence_done(task, res);
822}
823
824static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
825{
826 if (res->sr_slot != NULL) {
827 if (res->sr_slot->table->session != NULL)
828 nfs41_sequence_free_slot(res);
829 else
830 nfs40_sequence_free_slot(res);
831 }
832}
833
Peng Tao2c4b1312014-06-11 05:24:16 +0800834int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400835{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500836 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400837 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400838 if (!res->sr_slot->table->session)
839 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400841}
Peng Tao2c4b1312014-06-11 05:24:16 +0800842EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400843
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
845{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400846 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400847
Trond Myklebust035168ab2010-06-16 09:52:26 -0400848 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
849
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500850 nfs4_setup_sequence(data->seq_server->nfs_client,
851 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852}
853
Andy Adamson69ab40c2009-04-01 09:22:19 -0400854static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
855{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400856 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857
Trond Myklebust14516c32010-07-31 14:29:06 -0400858 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400859}
860
Trond Myklebust17280172012-03-11 13:11:00 -0400861static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400862 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400863 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400864};
865
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400866static void
867nfs4_sequence_process_interrupted(struct nfs_client *client,
868 struct nfs4_slot *slot, struct rpc_cred *cred)
869{
870 struct rpc_task *task;
871
872 task = _nfs41_proc_sequence(client, cred, slot, true);
873 if (!IS_ERR(task))
874 rpc_put_task_async(task);
875}
876
Chuck Lever3bd23842013-08-09 12:49:19 -0400877#else /* !CONFIG_NFS_V4_1 */
878
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400879static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
880{
881 return nfs40_sequence_done(task, res);
882}
883
884static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
885{
886 if (res->sr_slot != NULL)
887 nfs40_sequence_free_slot(res);
888}
889
Peng Tao2c4b1312014-06-11 05:24:16 +0800890int nfs4_sequence_done(struct rpc_task *task,
891 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400892{
Chuck Lever3bd23842013-08-09 12:49:19 -0400893 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400894}
Peng Tao2c4b1312014-06-11 05:24:16 +0800895EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400896
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400897static void
898nfs4_sequence_process_interrupted(struct nfs_client *client,
899 struct nfs4_slot *slot, struct rpc_cred *cred)
900{
901 WARN_ON_ONCE(1);
902 slot->interrupted = 0;
903}
904
Chuck Lever3bd23842013-08-09 12:49:19 -0400905#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400906
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400907static
908void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
909 struct nfs4_sequence_res *res,
910 struct nfs4_slot *slot)
911{
912 if (!slot)
913 return;
914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 res->sr_timestamp = jiffies;
919 res->sr_status_flags = 0;
920 res->sr_status = 1;
921
922}
923
924int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500925 struct nfs4_sequence_args *args,
926 struct nfs4_sequence_res *res,
927 struct rpc_task *task)
928{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500929 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500930 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500931 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500932
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500933 /* slot already allocated? */
934 if (res->sr_slot != NULL)
935 goto out_start;
936
Anna Schumaker76ee0352017-01-10 16:49:31 -0500937 if (session) {
938 tbl = &session->fc_slot_table;
939 task->tk_timeout = 0;
940 }
941
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400942 for (;;) {
943 spin_lock(&tbl->slot_tbl_lock);
944 /* The state manager will wait until the slot table is empty */
945 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
946 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500947
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400948 slot = nfs4_alloc_slot(tbl);
949 if (IS_ERR(slot)) {
950 /* Try again in 1/4 second */
951 if (slot == ERR_PTR(-ENOMEM))
952 task->tk_timeout = HZ >> 2;
953 goto out_sleep;
954 }
955 spin_unlock(&tbl->slot_tbl_lock);
956
957 if (likely(!slot->interrupted))
958 break;
959 nfs4_sequence_process_interrupted(client,
960 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500961 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500962
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400963 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500964
Anna Schumakerad05cc02017-01-11 13:37:06 -0500965 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500966out_start:
967 rpc_call_start(task);
968 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500969
970out_sleep:
971 if (args->sa_privileged)
972 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
973 NULL, RPC_PRIORITY_PRIVILEGED);
974 else
975 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
976 spin_unlock(&tbl->slot_tbl_lock);
977 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978}
979EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
980
Chuck Lever9915ea72013-08-09 12:48:27 -0400981static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
982{
983 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500984 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400985 data->seq_args, data->seq_res, task);
986}
987
988static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
989{
990 struct nfs4_call_sync_data *data = calldata;
991 nfs4_sequence_done(task, data->seq_res);
992}
993
994static const struct rpc_call_ops nfs40_call_sync_ops = {
995 .rpc_call_prepare = nfs40_call_sync_prepare,
996 .rpc_call_done = nfs40_call_sync_done,
997};
998
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400999static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001000 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001004{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 int ret;
1006 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001007 struct nfs_client *clp = server->nfs_client;
1008 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 .seq_server = server,
1010 .seq_args = args,
1011 .seq_res = res,
1012 };
1013 struct rpc_task_setup task_setup = {
1014 .rpc_client = clnt,
1015 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001016 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001017 .callback_data = &data
1018 };
1019
1020 task = rpc_run_task(&task_setup);
1021 if (IS_ERR(task))
1022 ret = PTR_ERR(task);
1023 else {
1024 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001025 rpc_put_task(task);
1026 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001027 return ret;
1028}
1029
Bryan Schumaker7c513052011-03-24 17:12:24 +00001030int nfs4_call_sync(struct rpc_clnt *clnt,
1031 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001032 struct rpc_message *msg,
1033 struct nfs4_sequence_args *args,
1034 struct nfs4_sequence_res *res,
1035 int cache_reply)
1036{
Chuck Levera9c92d62013-08-09 12:48:18 -04001037 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001038 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001041static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1042 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct nfs_inode *nfsi = NFS_I(dir);
1045
1046 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001047 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001048 if (cinfo->atomic && cinfo->before == dir->i_version) {
1049 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1050 nfsi->attrtimeo_timestamp = jiffies;
1051 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001053 if (cinfo->before != dir->i_version)
1054 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1055 NFS_INO_INVALID_ACL;
1056 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001057 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001058 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001059 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001060 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_unlock(&dir->i_lock);
1062}
1063
1064struct nfs4_opendata {
1065 struct kref kref;
1066 struct nfs_openargs o_arg;
1067 struct nfs_openres o_res;
1068 struct nfs_open_confirmargs c_arg;
1069 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001070 struct nfs4_string owner_name;
1071 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001072 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001074 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001076 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct nfs4_state_owner *owner;
1078 struct nfs4_state *state;
1079 struct iattr attrs;
1080 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001081 bool rpc_done;
1082 bool file_created;
1083 bool is_recover;
1084 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001085 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001087
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001088struct nfs4_open_createattrs {
1089 struct nfs4_label *label;
1090 struct iattr *sattr;
1091 const __u32 verf[2];
1092};
1093
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001094static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1095 int err, struct nfs4_exception *exception)
1096{
1097 if (err != -EINVAL)
1098 return false;
1099 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1100 return false;
1101 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1102 exception->retry = 1;
1103 return true;
1104}
1105
Trond Myklebust6ae37332015-01-30 14:21:14 -05001106static u32
1107nfs4_map_atomic_open_share(struct nfs_server *server,
1108 fmode_t fmode, int openflags)
1109{
1110 u32 res = 0;
1111
1112 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1113 case FMODE_READ:
1114 res = NFS4_SHARE_ACCESS_READ;
1115 break;
1116 case FMODE_WRITE:
1117 res = NFS4_SHARE_ACCESS_WRITE;
1118 break;
1119 case FMODE_READ|FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_BOTH;
1121 }
1122 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1123 goto out;
1124 /* Want no delegation if we're using O_DIRECT */
1125 if (openflags & O_DIRECT)
1126 res |= NFS4_SHARE_WANT_NO_DELEG;
1127out:
1128 return res;
1129}
1130
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001131static enum open_claim_type4
1132nfs4_map_atomic_open_claim(struct nfs_server *server,
1133 enum open_claim_type4 claim)
1134{
1135 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1136 return claim;
1137 switch (claim) {
1138 default:
1139 return claim;
1140 case NFS4_OPEN_CLAIM_FH:
1141 return NFS4_OPEN_CLAIM_NULL;
1142 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1143 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1144 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1145 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1146 }
1147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150{
1151 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001152 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153 p->o_res.seqid = p->o_arg.seqid;
1154 p->c_res.seqid = p->c_arg.seqid;
1155 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001156 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001157 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001158 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159}
1160
Al Viro82a2c1b2011-06-22 18:30:55 -04001161static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001163 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001164 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001165 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166{
Al Viro82a2c1b2011-06-22 18:30:55 -04001167 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001168 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001170 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001171 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs4_opendata *p;
1173
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001174 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 if (p == NULL)
1176 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001177
1178 p->f_label = nfs4_label_alloc(server, gfp_mask);
1179 if (IS_ERR(p->f_label))
1180 goto err_free_p;
1181
Kinglong Meea49c2692015-07-27 15:31:38 +08001182 p->a_label = nfs4_label_alloc(server, gfp_mask);
1183 if (IS_ERR(p->a_label))
1184 goto err_free_f;
1185
Trond Myklebust63f5f792015-01-23 19:19:25 -05001186 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1187 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001188 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001189 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001190 nfs_sb_active(dentry->d_sb);
1191 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 p->dir = parent;
1193 p->owner = sp;
1194 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 p->o_arg.open_flags = flags;
1196 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001197 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001198 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001199 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1200 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001201 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1202 * will return permission denied for all bits until close */
1203 if (!(flags & O_EXCL)) {
1204 /* ask server to check for all possible rights as results
1205 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001206 switch (p->o_arg.claim) {
1207 default:
1208 break;
1209 case NFS4_OPEN_CLAIM_NULL:
1210 case NFS4_OPEN_CLAIM_FH:
1211 p->o_arg.access = NFS4_ACCESS_READ |
1212 NFS4_ACCESS_MODIFY |
1213 NFS4_ACCESS_EXTEND |
1214 NFS4_ACCESS_EXECUTE;
1215 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001216 }
David Howells7539bba2006-08-22 20:06:09 -04001217 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001218 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1219 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001222 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001223 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001224 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001225 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001226 case NFS4_OPEN_CLAIM_NULL:
1227 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1228 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1229 p->o_arg.fh = NFS_FH(dir);
1230 break;
1231 case NFS4_OPEN_CLAIM_PREVIOUS:
1232 case NFS4_OPEN_CLAIM_FH:
1233 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001235 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001236 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001237 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001238 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001239 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001240
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001241 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001242 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001243 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001244 p->c_arg.fh = &p->o_res.fh;
1245 p->c_arg.stateid = &p->o_res.stateid;
1246 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001250
1251err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001252 nfs4_label_free(p->a_label);
1253err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001254 nfs4_label_free(p->f_label);
1255err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001256 kfree(p);
1257err:
1258 dput(parent);
1259 return NULL;
1260}
1261
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001263{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001264 struct nfs4_opendata *p = container_of(kref,
1265 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001266 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267
1268 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001269 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001270 if (p->state != NULL)
1271 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001273
Kinglong Meea49c2692015-07-27 15:31:38 +08001274 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001275 nfs4_label_free(p->f_label);
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001278 dput(p->dentry);
1279 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001280 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001281 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282 kfree(p);
1283}
1284
1285static void nfs4_opendata_put(struct nfs4_opendata *p)
1286{
1287 if (p != NULL)
1288 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289}
1290
Trond Myklebust24311f82015-09-20 10:50:17 -04001291static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1292 fmode_t fmode)
1293{
1294 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1295 case FMODE_READ|FMODE_WRITE:
1296 return state->n_rdwr != 0;
1297 case FMODE_WRITE:
1298 return state->n_wronly != 0;
1299 case FMODE_READ:
1300 return state->n_rdonly != 0;
1301 }
1302 WARN_ON_ONCE(1);
1303 return false;
1304}
1305
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001307{
1308 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309
Trond Myklebust536e43d2012-01-17 22:04:26 -05001310 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 goto out;
1312 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001314 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1315 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 break;
1317 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001318 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1319 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001320 break;
1321 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001322 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1323 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001325out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001326 return ret;
1327}
1328
Trond Myklebust2a606182015-08-19 22:30:00 -05001329static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1330 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001331{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001332 if (delegation == NULL)
1333 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001336 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1337 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001338 switch (claim) {
1339 case NFS4_OPEN_CLAIM_NULL:
1340 case NFS4_OPEN_CLAIM_FH:
1341 break;
1342 case NFS4_OPEN_CLAIM_PREVIOUS:
1343 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1344 break;
1345 default:
1346 return 0;
1347 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001348 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001349 return 1;
1350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001353{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001355 case FMODE_WRITE:
1356 state->n_wronly++;
1357 break;
1358 case FMODE_READ:
1359 state->n_rdonly++;
1360 break;
1361 case FMODE_READ|FMODE_WRITE:
1362 state->n_rdwr++;
1363 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001365}
1366
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001367#ifdef CONFIG_NFS_V4_1
1368static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1369{
1370 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1371 return true;
1372 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1373 return true;
1374 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1375 return true;
1376 return false;
1377}
1378#endif /* CONFIG_NFS_V4_1 */
1379
Trond Myklebustc9399f22017-11-06 15:28:01 -05001380static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1381{
1382 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1383 wake_up_all(&state->waitq);
1384}
1385
1386static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1387 const nfs4_stateid *stateid)
1388{
1389 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1390 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1391
1392 if (stateid_seqid == state_seqid + 1U ||
1393 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1394 nfs_state_log_update_open_stateid(state);
1395 else
1396 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1397}
1398
Trond Myklebust4f14c192014-02-12 19:15:06 -05001399static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001400{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001401 struct nfs_client *clp = state->owner->so_server->nfs_client;
1402 bool need_recover = false;
1403
1404 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1405 need_recover = true;
1406 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1407 need_recover = true;
1408 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1409 need_recover = true;
1410 if (need_recover)
1411 nfs4_state_mark_reclaim_nograce(clp, state);
1412}
1413
Trond Myklebustc9399f22017-11-06 15:28:01 -05001414/*
1415 * Check for whether or not the caller may update the open stateid
1416 * to the value passed in by stateid.
1417 *
1418 * Note: This function relies heavily on the server implementing
1419 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1420 * correctly.
1421 * i.e. The stateid seqids have to be initialised to 1, and
1422 * are then incremented on every state transition.
1423 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001424static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001425 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001426{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001427 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1428 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1429 if (stateid->seqid == cpu_to_be32(1))
1430 nfs_state_log_update_open_stateid(state);
1431 else
1432 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001434 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001435
1436 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1437 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001439 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001440 return false;
1441}
1442
Trond Myklebustf95549c2015-01-23 18:06:09 -05001443static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1444{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001445 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1446 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447 if (state->n_wronly)
1448 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1449 if (state->n_rdonly)
1450 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1451 if (state->n_rdwr)
1452 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001454}
1455
Trond Myklebust226056c52014-02-11 10:41:07 -05001456static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1457 nfs4_stateid *stateid, fmode_t fmode)
1458{
1459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1460 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1461 case FMODE_WRITE:
1462 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 break;
1464 case FMODE_READ:
1465 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 break;
1467 case 0:
1468 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 clear_bit(NFS_OPEN_STATE, &state->flags);
1471 }
1472 if (stateid == NULL)
1473 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001474 /* Handle OPEN+OPEN_DOWNGRADE races */
1475 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1476 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001477 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001478 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 nfs4_stateid_copy(&state->stateid, stateid);
1482 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001483 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001484out:
1485 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c52014-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 Myklebust226056c52014-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 Myklebust226056c52014-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 Myklebust226056c52014-02-11 10:41:07 -05001499}
1500
Trond Myklebust1393d962016-09-22 13:39:13 -04001501static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001502 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001503{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001504 DEFINE_WAIT(wait);
1505 int status = 0;
1506 for (;;) {
1507
1508 if (!nfs_need_update_open_stateid(state, stateid))
1509 return;
1510 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001511 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001512 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001513 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001514 /* Rely on seqids for serialisation with NFSv4.0 */
1515 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1516 break;
1517
1518 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1519 /*
1520 * Ensure we process the state changes in the same order
1521 * in which the server processed them by delaying the
1522 * update of the stateid until we are in sequence.
1523 */
1524 write_sequnlock(&state->seqlock);
1525 spin_unlock(&state->owner->so_lock);
1526 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001527 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001528 if (!signal_pending(current)) {
1529 if (schedule_timeout(5*HZ) == 0)
1530 status = -EAGAIN;
1531 else
1532 status = 0;
1533 } else
1534 status = -EINTR;
1535 finish_wait(&state->waitq, &wait);
1536 rcu_read_lock();
1537 spin_lock(&state->owner->so_lock);
1538 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001539 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001540
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001541 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1542 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543 nfs4_stateid_copy(freeme, &state->open_stateid);
1544 nfs_test_and_clear_all_open_stateid(state);
1545 }
1546
Trond Myklebuste999e802014-02-10 18:20:47 -05001547 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1548 nfs4_stateid_copy(&state->stateid, stateid);
1549 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001550 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001552}
1553
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001556 fmode_t fmode,
1557 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001559 /*
1560 * Protect the call to nfs4_state_set_mode_locked and
1561 * serialise the stateid update
1562 */
1563 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001564 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1565 switch (fmode) {
1566 case FMODE_READ:
1567 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1568 break;
1569 case FMODE_WRITE:
1570 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_READ|FMODE_WRITE:
1573 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001574 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001576 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001577}
1578
1579static void nfs_state_set_delegation(struct nfs4_state *state,
1580 const nfs4_stateid *deleg_stateid,
1581 fmode_t fmode)
1582{
1583 /*
1584 * Protect the call to nfs4_state_set_mode_locked and
1585 * serialise the stateid update
1586 */
1587 write_seqlock(&state->seqlock);
1588 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1589 set_bit(NFS_DELEGATED_STATE, &state->flags);
1590 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Trond Myklebust1393d962016-09-22 13:39:13 -04001593static int update_open_stateid(struct nfs4_state *state,
1594 const nfs4_stateid *open_stateid,
1595 const nfs4_stateid *delegation,
1596 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001597{
Trond Myklebust1393d962016-09-22 13:39:13 -04001598 struct nfs_server *server = NFS_SERVER(state->inode);
1599 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001600 struct nfs_inode *nfsi = NFS_I(state->inode);
1601 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001602 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001603 int ret = 0;
1604
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001605 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001606
1607 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001608 spin_lock(&state->owner->so_lock);
1609 if (open_stateid != NULL) {
1610 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1611 ret = 1;
1612 }
1613
Trond Myklebust34310432008-12-23 15:21:38 -05001614 deleg_cur = rcu_dereference(nfsi->delegation);
1615 if (deleg_cur == NULL)
1616 goto no_delegation;
1617
1618 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001619 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001620 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001621 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001622 goto no_delegation_unlock;
1623
1624 if (delegation == NULL)
1625 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001626 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001627 goto no_delegation_unlock;
1628
Trond Myklebustb7391f42008-12-23 15:21:52 -05001629 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001631 ret = 1;
1632no_delegation_unlock:
1633 spin_unlock(&deleg_cur->lock);
1634no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001635 if (ret)
1636 update_open_stateflags(state, fmode);
1637 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001638 rcu_read_unlock();
1639
Trond Myklebust4f14c192014-02-12 19:15:06 -05001640 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001641 nfs4_schedule_state_manager(clp);
1642 if (freeme.type != 0)
1643 nfs4_test_and_free_stateid(server, &freeme,
1644 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001645
1646 return ret;
1647}
1648
Trond Myklebust39071e62015-01-24 15:07:56 -05001649static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1650 const nfs4_stateid *stateid)
1651{
1652 struct nfs4_state *state = lsp->ls_state;
1653 bool ret = false;
1654
1655 spin_lock(&state->state_lock);
1656 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1657 goto out_noupdate;
1658 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1659 goto out_noupdate;
1660 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1661 ret = true;
1662out_noupdate:
1663 spin_unlock(&state->state_lock);
1664 return ret;
1665}
Trond Myklebust34310432008-12-23 15:21:38 -05001666
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668{
1669 struct nfs_delegation *delegation;
1670
1671 rcu_read_lock();
1672 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 rcu_read_unlock();
1675 return;
1676 }
1677 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001678 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679}
1680
Trond Myklebust6ee41262007-07-08 14:11:36 -04001681static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682{
1683 struct nfs4_state *state = opendata->state;
1684 struct nfs_inode *nfsi = NFS_I(state->inode);
1685 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001686 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001687 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001688 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 nfs4_stateid stateid;
1690 int ret = -EAGAIN;
1691
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001693 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001696 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001700 rcu_read_lock();
1701 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001702 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001703 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001704 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001705 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001707 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001709 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001710 if (!opendata->is_recover) {
1711 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1712 if (ret != 0)
1713 goto out;
1714 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001716
1717 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001719 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721out:
1722 return ERR_PTR(ret);
1723out_return_state:
1724 atomic_inc(&state->count);
1725 return state;
1726}
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728static void
1729nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1730{
1731 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1732 struct nfs_delegation *delegation;
1733 int delegation_flags = 0;
1734
1735 rcu_read_lock();
1736 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1737 if (delegation)
1738 delegation_flags = delegation->flags;
1739 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001740 switch (data->o_arg.claim) {
1741 default:
1742 break;
1743 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1744 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001745 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1746 "returning a delegation for "
1747 "OPEN(CLAIM_DELEGATE_CUR)\n",
1748 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001749 return;
1750 }
1751 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001752 nfs_inode_set_delegation(state->inode,
1753 data->owner->so_cred,
1754 &data->o_res);
1755 else
1756 nfs_inode_reclaim_delegation(state->inode,
1757 data->owner->so_cred,
1758 &data->o_res);
1759}
1760
1761/*
1762 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1763 * and update the nfs4_state.
1764 */
1765static struct nfs4_state *
1766_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1767{
1768 struct inode *inode = data->state->inode;
1769 struct nfs4_state *state = data->state;
1770 int ret;
1771
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001772 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001773 if (data->rpc_status)
1774 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001775 /* cached opens have already been processed */
1776 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001777 }
1778
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 ret = nfs_refresh_inode(inode, &data->f_attr);
1780 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001781 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001782
1783 if (data->o_res.delegation_type != 0)
1784 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001785update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001786 update_open_stateid(state, &data->o_res.stateid, NULL,
1787 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001788 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001789
1790 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001791}
1792
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001793static struct inode *
1794nfs4_opendata_get_inode(struct nfs4_opendata *data)
1795{
1796 struct inode *inode;
1797
1798 switch (data->o_arg.claim) {
1799 case NFS4_OPEN_CLAIM_NULL:
1800 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1801 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1802 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1803 return ERR_PTR(-EAGAIN);
1804 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1805 &data->f_attr, data->f_label);
1806 break;
1807 default:
1808 inode = d_inode(data->dentry);
1809 ihold(inode);
1810 nfs_refresh_inode(inode, &data->f_attr);
1811 }
1812 return inode;
1813}
1814
Andy Adamsone23008e2012-10-02 21:07:32 -04001815static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001816nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1817{
1818 struct nfs4_state *state;
1819 struct inode *inode;
1820
1821 inode = nfs4_opendata_get_inode(data);
1822 if (IS_ERR(inode))
1823 return ERR_CAST(inode);
1824 if (data->state != NULL && data->state->inode == inode) {
1825 state = data->state;
1826 atomic_inc(&state->count);
1827 } else
1828 state = nfs4_get_open_state(inode, data->owner);
1829 iput(inode);
1830 if (state == NULL)
1831 state = ERR_PTR(-ENOMEM);
1832 return state;
1833}
1834
Andy Adamsone23008e2012-10-02 21:07:32 -04001835static struct nfs4_state *
1836_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001838 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001841 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001842 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001843 goto out;
1844 }
1845
Trond Myklebust75e8c482017-08-08 10:38:07 -04001846 state = nfs4_opendata_find_nfs4_state(data);
1847 if (IS_ERR(state))
1848 goto out;
1849
Andy Adamsone23008e2012-10-02 21:07:32 -04001850 if (data->o_res.delegation_type != 0)
1851 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001852 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001853 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001855 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 return state;
1857}
1858
Andy Adamsone23008e2012-10-02 21:07:32 -04001859static struct nfs4_state *
1860nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1861{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001862 struct nfs4_state *ret;
1863
Andy Adamsone23008e2012-10-02 21:07:32 -04001864 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001865 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1866 else
1867 ret = _nfs4_opendata_to_nfs4_state(data);
1868 nfs4_sequence_free_slot(&data->o_res.seq_res);
1869 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001870}
1871
Trond Myklebust864472e2006-01-03 09:55:15 +01001872static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1873{
1874 struct nfs_inode *nfsi = NFS_I(state->inode);
1875 struct nfs_open_context *ctx;
1876
1877 spin_lock(&state->inode->i_lock);
1878 list_for_each_entry(ctx, &nfsi->open_files, list) {
1879 if (ctx->state != state)
1880 continue;
1881 get_nfs_open_context(ctx);
1882 spin_unlock(&state->inode->i_lock);
1883 return ctx;
1884 }
1885 spin_unlock(&state->inode->i_lock);
1886 return ERR_PTR(-ENOENT);
1887}
1888
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001889static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1890 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001891{
1892 struct nfs4_opendata *opendata;
1893
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001894 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001895 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001896 if (opendata == NULL)
1897 return ERR_PTR(-ENOMEM);
1898 opendata->state = state;
1899 atomic_inc(&state->count);
1900 return opendata;
1901}
1902
Trond Myklebust24311f82015-09-20 10:50:17 -04001903static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1904 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001905{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001906 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001907 int ret;
1908
Trond Myklebust24311f82015-09-20 10:50:17 -04001909 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001910 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001911 opendata->o_arg.open_flags = 0;
1912 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001913 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1914 NFS_SB(opendata->dentry->d_sb),
1915 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001916 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1917 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1918 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001919 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001920 if (ret != 0)
1921 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001922 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001923 if (IS_ERR(newstate))
1924 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001925 if (newstate != opendata->state)
1926 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001927 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001928 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001929}
1930
1931static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1932{
Trond Myklebust864472e2006-01-03 09:55:15 +01001933 int ret;
1934
Trond Myklebust4f14c192014-02-12 19:15:06 -05001935 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1936 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1937 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1938 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001939 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001940 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001941 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001943 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1944 if (ret != 0)
1945 return ret;
1946 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1947 if (ret != 0)
1948 return ret;
1949 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1950 if (ret != 0)
1951 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001952 /*
1953 * We may have performed cached opens for all three recoveries.
1954 * Check if we need to update the current stateid.
1955 */
1956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001957 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001958 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001959 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001960 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001961 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001962 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001963 return 0;
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * OPEN_RECLAIM:
1968 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001970static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001972 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001973 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001974 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int status;
1976
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1978 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001981 rcu_read_lock();
1982 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001983 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001984 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001985 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001986 opendata->o_arg.u.delegation_type = delegation_type;
1987 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001988 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return status;
1990}
1991
Trond Myklebust539cd032007-06-05 11:46:42 -04001992static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct nfs_server *server = NFS_SERVER(state->inode);
1995 struct nfs4_exception exception = { };
1996 int err;
1997 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001998 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001999 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002000 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2001 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002002 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002003 break;
2004 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } while (exception.retry);
2006 return err;
2007}
2008
Trond Myklebust864472e2006-01-03 09:55:15 +01002009static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2010{
2011 struct nfs_open_context *ctx;
2012 int ret;
2013
2014 ctx = nfs4_state_find_open_context(state);
2015 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002016 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002017 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002018 put_nfs_open_context(ctx);
2019 return ret;
2020}
2021
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002022static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002024 switch (err) {
2025 default:
2026 printk(KERN_ERR "NFS: %s: unhandled error "
2027 "%d.\n", __func__, err);
2028 case 0:
2029 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002030 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002031 case -ESTALE:
2032 break;
2033 case -NFS4ERR_BADSESSION:
2034 case -NFS4ERR_BADSLOT:
2035 case -NFS4ERR_BAD_HIGH_SLOT:
2036 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2037 case -NFS4ERR_DEADSESSION:
2038 set_bit(NFS_DELEGATED_STATE, &state->flags);
2039 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2040 return -EAGAIN;
2041 case -NFS4ERR_STALE_CLIENTID:
2042 case -NFS4ERR_STALE_STATEID:
2043 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002044 /* Don't recall a delegation if it was lost */
2045 nfs4_schedule_lease_recovery(server->nfs_client);
2046 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002047 case -NFS4ERR_MOVED:
2048 nfs4_schedule_migration_recovery(server);
2049 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002050 case -NFS4ERR_LEASE_MOVED:
2051 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2052 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002053 case -NFS4ERR_DELEG_REVOKED:
2054 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002055 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002056 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002057 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 nfs_inode_find_state_and_recover(state->inode,
2059 stateid);
2060 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002061 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002062 case -NFS4ERR_DELAY:
2063 case -NFS4ERR_GRACE:
2064 set_bit(NFS_DELEGATED_STATE, &state->flags);
2065 ssleep(1);
2066 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002067 case -ENOMEM:
2068 case -NFS4ERR_DENIED:
2069 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2070 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return err;
2073}
2074
Trond Myklebust24311f82015-09-20 10:50:17 -04002075int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2076 struct nfs4_state *state, const nfs4_stateid *stateid,
2077 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002078{
2079 struct nfs_server *server = NFS_SERVER(state->inode);
2080 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002081 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002082
2083 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2084 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2085 if (IS_ERR(opendata))
2086 return PTR_ERR(opendata);
2087 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b5322015-10-02 13:14:37 -04002088 write_seqlock(&state->seqlock);
2089 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2090 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002091 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2092 switch (type & (FMODE_READ|FMODE_WRITE)) {
2093 case FMODE_READ|FMODE_WRITE:
2094 case FMODE_WRITE:
2095 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2096 if (err)
2097 break;
2098 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2099 if (err)
2100 break;
2101 case FMODE_READ:
2102 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2103 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002104 nfs4_opendata_put(opendata);
2105 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2106}
2107
Chuck Leverbe05c862013-08-09 12:49:47 -04002108static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2109{
2110 struct nfs4_opendata *data = calldata;
2111
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002112 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2113 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002114}
2115
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002116static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2117{
2118 struct nfs4_opendata *data = calldata;
2119
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002120 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002121
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002122 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002124 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002125 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002127 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002129}
2130
2131static void nfs4_open_confirm_release(void *calldata)
2132{
2133 struct nfs4_opendata *data = calldata;
2134 struct nfs4_state *state = NULL;
2135
2136 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002137 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002138 goto out_free;
2139 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002140 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002141 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002142 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002143 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002144 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002145out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002146 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147}
2148
2149static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002150 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002151 .rpc_call_done = nfs4_open_confirm_done,
2152 .rpc_release = nfs4_open_confirm_release,
2153};
2154
2155/*
2156 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2157 */
2158static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2159{
David Howells2b0143b2015-03-17 22:25:59 +00002160 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002161 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2164 .rpc_argp = &data->c_arg,
2165 .rpc_resp = &data->c_res,
2166 .rpc_cred = data->owner->so_cred,
2167 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002168 struct rpc_task_setup task_setup_data = {
2169 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002170 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002171 .callback_ops = &nfs4_open_confirm_ops,
2172 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002173 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002174 .flags = RPC_TASK_ASYNC,
2175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int status;
2177
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002178 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002179 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002180 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002181 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002183 if (data->is_recover)
2184 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002185 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002186 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002187 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002188 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002189 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002190 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002191 smp_wmb();
2192 } else
2193 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002194 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return status;
2196}
2197
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200 struct nfs4_opendata *data = calldata;
2201 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002202 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002203 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002204
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002206 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002207 /*
2208 * Check if we still need to send an OPEN call, or if we can use
2209 * a delegation instead.
2210 */
2211 if (data->state != NULL) {
2212 struct nfs_delegation *delegation;
2213
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002214 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002215 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002216 rcu_read_lock();
2217 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002218 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002219 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002220 rcu_read_unlock();
2221 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002222 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002223 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002224 switch (claim) {
2225 default:
2226 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002227 case NFS4_OPEN_CLAIM_PREVIOUS:
2228 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2229 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002230 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002231 case NFS4_OPEN_CLAIM_FH:
2232 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002233 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002234 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002235 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002236 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002237 &data->o_res.seq_res,
2238 task) != 0)
2239 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002240
2241 /* Set the create mode (note dependency on the session type) */
2242 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2243 if (data->o_arg.open_flags & O_EXCL) {
2244 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2245 if (nfs4_has_persistent_session(clp))
2246 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2247 else if (clp->cl_mvops->minor_version > 0)
2248 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2249 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002250 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002251unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002252 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002253 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002254out_no_action:
2255 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002256out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002257 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002258}
2259
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260static void nfs4_open_done(struct rpc_task *task, void *calldata)
2261{
2262 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002264 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002265
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002266 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002267 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002268
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002269 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002270 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2271 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002272 case S_IFREG:
2273 break;
2274 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002276 break;
2277 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002279 break;
2280 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002282 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002283 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002284 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002285 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2286 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002287 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002288 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002289}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002290
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291static void nfs4_open_release(void *calldata)
2292{
2293 struct nfs4_opendata *data = calldata;
2294 struct nfs4_state *state = NULL;
2295
2296 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002297 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298 goto out_free;
2299 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002300 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 goto out_free;
2302 /* In case we need an open_confirm, no cleanup! */
2303 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2304 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002306 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002307 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002309 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310}
2311
2312static const struct rpc_call_ops nfs4_open_ops = {
2313 .rpc_call_prepare = nfs4_open_prepare,
2314 .rpc_call_done = nfs4_open_done,
2315 .rpc_release = nfs4_open_release,
2316};
2317
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002318static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002319{
David Howells2b0143b2015-03-17 22:25:59 +00002320 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321 struct nfs_server *server = NFS_SERVER(dir);
2322 struct nfs_openargs *o_arg = &data->o_arg;
2323 struct nfs_openres *o_res = &data->o_res;
2324 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2327 .rpc_argp = o_arg,
2328 .rpc_resp = o_res,
2329 .rpc_cred = data->owner->so_cred,
2330 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002333 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 .callback_ops = &nfs4_open_ops,
2335 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002336 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 .flags = RPC_TASK_ASYNC,
2338 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002339 int status;
2340
Chuck Levera9c92d62013-08-09 12:48:18 -04002341 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002342 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002343 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002344 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002345 data->cancelled = false;
2346 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002347 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002348 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002350 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002351 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002352 if (IS_ERR(task))
2353 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002354 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002355 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 smp_wmb();
2358 } else
2359 status = data->rpc_status;
2360 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002361
2362 return status;
2363}
2364
2365static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2366{
David Howells2b0143b2015-03-17 22:25:59 +00002367 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002368 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002369 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370
2371 status = nfs4_run_open_task(data, 1);
2372 if (status != 0 || !data->rpc_done)
2373 return status;
2374
Trond Myklebust6926afd12012-01-07 13:22:46 -05002375 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2376
Anna Schumakerd7e98252017-01-11 16:13:29 -05002377 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002378 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002379
2380 return status;
2381}
2382
Trond Myklebustf3792d62014-07-10 08:54:32 -04002383/*
2384 * Additional permission checks in order to distinguish between an
2385 * open for read, and an open for execute. This works around the
2386 * fact that NFSv4 OPEN treats read and execute permissions as being
2387 * the same.
2388 * Note that in the non-execute case, we want to turn off permission
2389 * checking if we just created a new file (POSIX open() semantics).
2390 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002391static int nfs4_opendata_access(struct rpc_cred *cred,
2392 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002393 struct nfs4_state *state, fmode_t fmode,
2394 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002395{
2396 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002397 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002398
2399 /* access call failed or for some reason the server doesn't
2400 * support any access modes -- defer access call until later */
2401 if (opendata->o_res.access_supported == 0)
2402 return 0;
2403
2404 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002405 /*
2406 * Use openflags to check for exec, because fmode won't
2407 * always have FMODE_EXEC set when file open for exec.
2408 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002409 if (openflags & __FMODE_EXEC) {
2410 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002411 if (S_ISDIR(state->inode->i_mode))
2412 mask = NFS4_ACCESS_LOOKUP;
2413 else
2414 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002415 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002416 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002417
2418 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002419 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2420 nfs_access_add_cache(state->inode, &cache);
2421
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002422 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2423 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424 return 0;
2425
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002426 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427}
2428
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002429/*
2430 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2431 */
2432static int _nfs4_proc_open(struct nfs4_opendata *data)
2433{
David Howells2b0143b2015-03-17 22:25:59 +00002434 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002435 struct nfs_server *server = NFS_SERVER(dir);
2436 struct nfs_openargs *o_arg = &data->o_arg;
2437 struct nfs_openres *o_res = &data->o_res;
2438 int status;
2439
2440 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002441 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002442 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002443 if (status != 0) {
2444 if (status == -NFS4ERR_BADNAME &&
2445 !(o_arg->open_flags & O_CREAT))
2446 return -ENOENT;
2447 return status;
2448 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002449
Trond Myklebust6926afd12012-01-07 13:22:46 -05002450 nfs_fattr_map_and_free_names(server, &data->f_attr);
2451
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002452 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002453 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002454 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002455 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002456 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002457 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002458 update_changeattr(dir, &o_res->cinfo,
2459 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002460 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002461 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2462 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002464 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002466 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002468 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2469 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002470 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002471 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475/*
2476 * OPEN_EXPIRED:
2477 * reclaim state on the server after a network partition.
2478 * Assumes caller holds the appropriate lock
2479 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002480static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002482 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002483 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002485 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002486 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002487 if (IS_ERR(opendata))
2488 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002489 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002490 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002491 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002492 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002496static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002497{
Trond Myklebust539cd032007-06-05 11:46:42 -04002498 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002499 struct nfs4_exception exception = { };
2500 int err;
2501
2502 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002503 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002504 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002505 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2506 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002507 switch (err) {
2508 default:
2509 goto out;
2510 case -NFS4ERR_GRACE:
2511 case -NFS4ERR_DELAY:
2512 nfs4_handle_exception(server, err, &exception);
2513 err = 0;
2514 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002515 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002516out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002517 return err;
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002523 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Trond Myklebust864472e2006-01-03 09:55:15 +01002525 ctx = nfs4_state_find_open_context(state);
2526 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002527 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002528 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002529 put_nfs_open_context(ctx);
2530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Trond Myklebust41020b62016-09-22 13:38:58 -04002533static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2534 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002535{
Trond Myklebust41020b62016-09-22 13:38:58 -04002536 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002537 write_seqlock(&state->seqlock);
2538 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2539 write_sequnlock(&state->seqlock);
2540 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2541}
2542
2543static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2544{
2545 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002546 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002547}
2548
2549static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2550{
2551 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2552 nfs40_clear_delegation_stateid(state);
2553 return nfs4_open_expired(sp, state);
2554}
2555
Trond Myklebust45870d62016-09-22 13:38:59 -04002556static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2557 nfs4_stateid *stateid,
2558 struct rpc_cred *cred)
2559{
2560 return -NFS4ERR_BAD_STATEID;
2561}
2562
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002563#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002564static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2565 nfs4_stateid *stateid,
2566 struct rpc_cred *cred)
2567{
2568 int status;
2569
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002570 switch (stateid->type) {
2571 default:
2572 break;
2573 case NFS4_INVALID_STATEID_TYPE:
2574 case NFS4_SPECIAL_STATEID_TYPE:
2575 return -NFS4ERR_BAD_STATEID;
2576 case NFS4_REVOKED_STATEID_TYPE:
2577 goto out_free;
2578 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002579
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002580 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002581 switch (status) {
2582 case -NFS4ERR_EXPIRED:
2583 case -NFS4ERR_ADMIN_REVOKED:
2584 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002585 break;
2586 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002587 return status;
2588 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002589out_free:
2590 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002591 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002592 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002593}
2594
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002595static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002596{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002597 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002598 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002599 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002600 struct rpc_cred *cred;
2601 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002602
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002603 /* Get the delegation credential for use by test/free_stateid */
2604 rcu_read_lock();
2605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002606 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002607 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002608 return;
2609 }
2610
2611 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002612 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2613 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2614 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002615 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002616 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002617 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002618 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002619
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 cred = get_rpccred(delegation->cred);
2621 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002622 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002623 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002624 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002625 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002626
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002627 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628}
2629
2630/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002631 * nfs41_check_expired_locks - possibly free a lock stateid
2632 *
2633 * @state: NFSv4 state for an inode
2634 *
2635 * Returns NFS_OK if recovery for this stateid is now finished.
2636 * Otherwise a negative NFS4ERR value is returned.
2637 */
2638static int nfs41_check_expired_locks(struct nfs4_state *state)
2639{
2640 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002641 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002642 struct nfs_server *server = NFS_SERVER(state->inode);
2643
2644 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2645 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002646
2647 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002648 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2649 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2650 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2651
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002652 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002653 spin_unlock(&state->state_lock);
2654
2655 nfs4_put_lock_state(prev);
2656 prev = lsp;
2657
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002658 status = nfs41_test_and_free_expired_stateid(server,
2659 &lsp->ls_stateid,
2660 cred);
2661 trace_nfs4_test_lock_stateid(state, lsp, status);
2662 if (status == -NFS4ERR_EXPIRED ||
2663 status == -NFS4ERR_BAD_STATEID) {
2664 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002665 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002666 if (!recover_lost_locks)
2667 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2668 } else if (status != NFS_OK) {
2669 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002670 nfs4_put_lock_state(prev);
2671 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002672 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002673 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002675 }
2676 spin_unlock(&state->state_lock);
2677 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002678out:
2679 return ret;
2680}
2681
2682/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002683 * nfs41_check_open_stateid - possibly free an open stateid
2684 *
2685 * @state: NFSv4 state for an inode
2686 *
2687 * Returns NFS_OK if recovery for this stateid is now finished.
2688 * Otherwise a negative NFS4ERR value is returned.
2689 */
2690static int nfs41_check_open_stateid(struct nfs4_state *state)
2691{
2692 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002693 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002694 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002695 int status;
2696
Trond Myklebustb134fc42016-09-22 13:39:16 -04002697 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002698 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2699 if (nfs4_have_delegation(state->inode, state->state))
2700 return NFS_OK;
2701 return -NFS4ERR_OPENMODE;
2702 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002703 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002704 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002705 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002706 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002707 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002708 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2709 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2710 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002711 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002712 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002713 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002714 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002715 if (nfs_open_stateid_recover_openmode(state))
2716 return -NFS4ERR_OPENMODE;
2717 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002718}
2719
2720static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2721{
Chuck Levereb64cf92012-07-11 16:30:05 -04002722 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002723
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002724 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002725 status = nfs41_check_expired_locks(state);
2726 if (status != NFS_OK)
2727 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002728 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002729 if (status != NFS_OK)
2730 status = nfs4_open_expired(sp, state);
2731 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002732}
2733#endif
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002736 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2737 * fields corresponding to attributes that were used to store the verifier.
2738 * Make sure we clobber those fields in the later setattr call
2739 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002740static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2741 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002742{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002743 const u32 *attrset = opendata->o_res.attrset;
2744
2745 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002746 !(sattr->ia_valid & ATTR_ATIME_SET))
2747 sattr->ia_valid |= ATTR_ATIME;
2748
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002749 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002750 !(sattr->ia_valid & ATTR_MTIME_SET))
2751 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752
2753 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002754 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002755 (attrset[1] & FATTR4_WORD1_MODE ||
2756 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002757 sattr->ia_valid &= ~ATTR_MODE;
2758
2759 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2760 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002761}
2762
Trond Myklebustc21443c2013-02-07 14:26:21 -05002763static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2764 fmode_t fmode,
2765 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002766 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002767{
2768 struct nfs4_state_owner *sp = opendata->owner;
2769 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002770 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002771 struct nfs4_state *state;
2772 unsigned int seq;
2773 int ret;
2774
2775 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2776
2777 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002778 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002779 goto out;
2780
2781 state = nfs4_opendata_to_nfs4_state(opendata);
2782 ret = PTR_ERR(state);
2783 if (IS_ERR(state))
2784 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002785 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002786 if (server->caps & NFS_CAP_POSIX_LOCK)
2787 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002788 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2789 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002790
Trond Myklebust275bb302013-05-29 13:11:28 -04002791 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002792 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002793 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002794 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002795 alias = d_exact_alias(dentry, state->inode);
2796 if (!alias)
2797 alias = d_splice_alias(igrab(state->inode), dentry);
2798 /* d_splice_alias() can't fail here - it's a non-directory */
2799 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002800 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002801 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002802 }
2803 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002804 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002805 }
2806
Trond Myklebustc21443c2013-02-07 14:26:21 -05002807 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2808 if (ret != 0)
2809 goto out;
2810
David Howells2b0143b2015-03-17 22:25:59 +00002811 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002812 nfs_inode_attach_open_context(ctx);
2813 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2814 nfs4_schedule_stateid_recovery(server, state);
2815 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002816out:
2817 return ret;
2818}
2819
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002820/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002821 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Andy Adamson82be4172012-05-23 05:02:35 -04002823static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002824 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002825 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002826 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002827 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct nfs4_state_owner *sp;
2830 struct nfs4_state *state = NULL;
2831 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002832 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002833 struct dentry *dentry = ctx->dentry;
2834 struct rpc_cred *cred = ctx->cred;
2835 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2836 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002837 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002838 struct iattr *sattr = c->sattr;
2839 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002840 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002841 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002845 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2846 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2848 goto out_err;
2849 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002850 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002851 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002852 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002853 if (d_really_is_positive(dentry))
2854 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002855 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002856 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002857 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002858 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2859 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002860 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002861 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
David Quigley14c43f72013-05-22 12:50:43 -04002863 if (label) {
2864 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2865 if (IS_ERR(olabel)) {
2866 status = PTR_ERR(olabel);
2867 goto err_opendata_put;
2868 }
2869 }
2870
Trond Myklebuste911b812014-03-26 13:24:37 -07002871 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2872 if (!opendata->f_attr.mdsthreshold) {
2873 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2874 if (!opendata->f_attr.mdsthreshold)
2875 goto err_free_label;
2876 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002877 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002878 }
David Howells2b0143b2015-03-17 22:25:59 +00002879 if (d_really_is_positive(dentry))
2880 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002881
Trond Myklebust3efb9722013-05-29 13:17:04 -04002882 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002883 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002884 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002885 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002886
NeilBrownefcbc042015-07-30 13:00:56 +10002887 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002888 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002889 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002890 /*
2891 * send create attributes which was not set by open
2892 * with an extra setattr.
2893 */
2894 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2895 nfs_fattr_init(opendata->o_res.f_attr);
2896 status = nfs4_do_setattr(state->inode, cred,
2897 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002898 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002899 if (status == 0) {
2900 nfs_setattr_update_inode(state->inode, sattr,
2901 opendata->o_res.f_attr);
2902 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2903 }
David Quigley1775fd32013-05-22 12:50:42 -04002904 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002905 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002906 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002907 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002908
Trond Myklebuste911b812014-03-26 13:24:37 -07002909 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002910 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002911 opendata->f_attr.mdsthreshold = NULL;
2912 }
Andy Adamson82be4172012-05-23 05:02:35 -04002913
David Quigley14c43f72013-05-22 12:50:43 -04002914 nfs4_label_free(olabel);
2915
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002916 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002919err_free_label:
2920 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002921err_opendata_put:
2922 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002923err_put_state_owner:
2924 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
2929
Andy Adamson82be4172012-05-23 05:02:35 -04002930static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002931 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002932 int flags,
2933 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002934 struct nfs4_label *label,
2935 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002937 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct nfs4_exception exception = { };
2939 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002940 struct nfs4_open_createattrs c = {
2941 .label = label,
2942 .sattr = sattr,
2943 .verf = {
2944 [0] = (__u32)jiffies,
2945 [1] = (__u32)current->pid,
2946 },
2947 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 int status;
2949
2950 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002951 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002952 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002953 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (status == 0)
2955 break;
2956 /* NOTE: BAD_SEQID means the server and client disagree about the
2957 * book-keeping w.r.t. state-changing operations
2958 * (OPEN/CLOSE/LOCK/LOCKU...)
2959 * It is actually a sign of a bug on the client or on the server.
2960 *
2961 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002962 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 * have unhashed the old state_owner for us, and that we can
2964 * therefore safely retry using a new one. We should still warn
2965 * the user though...
2966 */
2967 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002968 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002969 " returned a bad sequence-id error!\n",
2970 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 exception.retry = 1;
2972 continue;
2973 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002974 /*
2975 * BAD_STATEID on OPEN means that the server cancelled our
2976 * state before it received the OPEN_CONFIRM.
2977 * Recover by retrying the request as per the discussion
2978 * on Page 181 of RFC3530.
2979 */
2980 if (status == -NFS4ERR_BAD_STATEID) {
2981 exception.retry = 1;
2982 continue;
2983 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002984 if (status == -EAGAIN) {
2985 /* We must have found a delegation */
2986 exception.retry = 1;
2987 continue;
2988 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002989 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2990 continue;
2991 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 status, &exception));
2993 } while (exception.retry);
2994 return res;
2995}
2996
Trond Myklebust8487c472016-06-26 08:44:35 -04002997static int _nfs4_do_setattr(struct inode *inode,
2998 struct nfs_setattrargs *arg,
2999 struct nfs_setattrres *res,
3000 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003001 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003002{
3003 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003004 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3006 .rpc_argp = arg,
3007 .rpc_resp = res,
3008 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003009 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003010 struct rpc_cred *delegation_cred = NULL;
3011 unsigned long timestamp = jiffies;
3012 fmode_t fmode;
3013 bool truncate;
3014 int status;
3015
3016 nfs_fattr_init(res->fattr);
3017
3018 /* Servers should only apply open mode checks for file size changes */
3019 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3020 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3021
3022 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3023 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003024 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003025 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003026 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003027 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003028 l_ctx = nfs_get_lock_context(ctx);
3029 if (IS_ERR(l_ctx))
3030 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003031 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3032 &arg->stateid, &delegation_cred);
3033 nfs_put_lock_context(l_ctx);
3034 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003035 return -EBADF;
3036 } else
3037 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3038 if (delegation_cred)
3039 msg.rpc_cred = delegation_cred;
3040
3041 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3042
3043 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003044 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003045 renew_lease(server, timestamp);
3046 trace_nfs4_setattr(inode, &arg->stateid, status);
3047 return status;
3048}
3049
3050static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3051 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003052 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003053 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003055 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003056 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003057 struct nfs_setattrargs arg = {
3058 .fh = NFS_FH(inode),
3059 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 .server = server,
3061 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003062 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003063 };
3064 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003066 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003068 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003069 struct nfs4_exception exception = {
3070 .state = state,
3071 .inode = inode,
3072 .stateid = &arg.stateid,
3073 };
3074 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
David Quigleyaa9c2662013-05-22 12:50:44 -04003076 arg.bitmask = nfs4_bitmask(server, ilabel);
3077 if (ilabel)
3078 arg.bitmask = nfs4_bitmask(server, olabel);
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003081 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003082 switch (err) {
3083 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003084 if (!(sattr->ia_valid & ATTR_SIZE)) {
3085 pr_warn_once("NFSv4: server %s is incorrectly "
3086 "applying open mode checks to "
3087 "a SETATTR that is not "
3088 "changing file size.\n",
3089 server->nfs_client->cl_hostname);
3090 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003091 if (state && !(state->state & FMODE_WRITE)) {
3092 err = -EBADF;
3093 if (sattr->ia_valid & ATTR_OPEN)
3094 err = -EACCES;
3095 goto out;
3096 }
3097 }
3098 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return err;
3102}
3103
Peng Tao500d7012015-09-22 11:35:22 +08003104static bool
3105nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3106{
3107 if (inode == NULL || !nfs_have_layout(inode))
3108 return false;
3109
3110 return pnfs_wait_on_layoutreturn(inode, task);
3111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113struct nfs4_closedata {
3114 struct inode *inode;
3115 struct nfs4_state *state;
3116 struct nfs_closeargs arg;
3117 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003118 struct {
3119 struct nfs4_layoutreturn_args arg;
3120 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003121 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003122 u32 roc_barrier;
3123 bool roc;
3124 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003125 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003126 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127};
3128
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003129static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003130{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131 struct nfs4_closedata *calldata = data;
3132 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003133 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003134
Trond Myklebustcf805162016-11-15 14:56:07 -05003135 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003136 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3137 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003138 nfs4_put_open_state(calldata->state);
3139 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003140 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003141 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003142 kfree(calldata);
3143}
3144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003145static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003150 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Chuck Levera3ca5652012-03-01 17:00:40 -05003152 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003153 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3154 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003155 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003156
3157 /* Handle Layoutreturn errors */
3158 if (calldata->arg.lr_args && task->tk_status != 0) {
3159 switch (calldata->res.lr_ret) {
3160 default:
3161 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3162 break;
3163 case 0:
3164 calldata->arg.lr_args = NULL;
3165 calldata->res.lr_res = NULL;
3166 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003167 case -NFS4ERR_OLD_STATEID:
3168 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003169 calldata->inode))
3170 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003171 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003172 case -NFS4ERR_ADMIN_REVOKED:
3173 case -NFS4ERR_DELEG_REVOKED:
3174 case -NFS4ERR_EXPIRED:
3175 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003176 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3177 case -NFS4ERR_WRONG_CRED:
3178 calldata->arg.lr_args = NULL;
3179 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003180 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003181 }
3182 }
3183
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003184 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 * the state_owner. we keep this around to process errors
3186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 switch (task->tk_status) {
3188 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003189 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003190 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003191 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003192 case -NFS4ERR_ACCESS:
3193 if (calldata->arg.bitmask != NULL) {
3194 calldata->arg.bitmask = NULL;
3195 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003196 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003197
3198 }
3199 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003200 case -NFS4ERR_OLD_STATEID:
3201 /* Did we race with OPEN? */
3202 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003203 state))
3204 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003205 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003206 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003208 case -NFS4ERR_EXPIRED:
3209 nfs4_free_revoked_stateid(server,
3210 &calldata->arg.stateid,
3211 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003212 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003213 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 default:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003216 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3217 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003219 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3220 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003221out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003222 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003223 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003224 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003225 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003226 return;
3227lr_restart:
3228 calldata->res.lr_ret = 0;
3229out_restart:
3230 task->tk_status = 0;
3231 rpc_restart_call_prepare(task);
3232 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003235static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003237 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003238 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003239 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003240 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003241 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003242
Chuck Levera3ca5652012-03-01 17:00:40 -05003243 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003244 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003245 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003246
Trond Myklebust88069f72009-12-08 08:33:16 -05003247 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003248 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003249 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3250 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3251 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003252 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003253 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003254 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003255 if (state->n_rdonly == 0)
3256 call_close |= is_rdonly;
3257 else if (is_rdonly)
3258 calldata->arg.fmode |= FMODE_READ;
3259 if (state->n_wronly == 0)
3260 call_close |= is_wronly;
3261 else if (is_wronly)
3262 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003263 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3264 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003265 } else if (is_rdwr)
3266 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3267
Trond Myklebust5cc78612016-11-14 11:19:56 -05003268 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003269 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003270 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003271 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003272
3273 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003274 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003275 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003276 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003277
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003278 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003279 nfs_release_seqid(calldata->arg.seqid);
3280 goto out_wait;
3281 }
3282
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003283 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003284 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003285
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003286 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003287 /* Close-to-open cache consistency revalidation */
3288 if (!nfs4_have_delegation(inode, FMODE_READ))
3289 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3290 else
3291 calldata->arg.bitmask = NULL;
3292 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003293
Trond Myklebust6ae37332015-01-30 14:21:14 -05003294 calldata->arg.share_access =
3295 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3296 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003297
Trond Myklebustd8d84982016-12-19 12:14:44 -05003298 if (calldata->res.fattr == NULL)
3299 calldata->arg.bitmask = NULL;
3300 else if (calldata->arg.bitmask == NULL)
3301 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003302 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003303 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003304 &calldata->arg.seq_args,
3305 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003306 task) != 0)
3307 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003308 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003309 return;
3310out_no_action:
3311 task->tk_action = NULL;
3312out_wait:
3313 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003316static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003317 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003318 .rpc_call_done = nfs4_close_done,
3319 .rpc_release = nfs4_free_closedata,
3320};
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322/*
3323 * It is possible for data to be read/written from a mem-mapped file
3324 * after the sys_close call (which hits the vfs layer as a flush).
3325 * This means that we can't safely call nfsv4 close on a file until
3326 * the inode is cleared. This in turn means that we are not good
3327 * NFSv4 citizens - we do not indicate to the server to update the file's
3328 * share state even when we are done with one of the three share
3329 * stateid's in the inode.
3330 *
3331 * NOTE: Caller must be holding the sp->so_owner semaphore!
3332 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003333int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003335 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003336 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003338 struct nfs4_state_owner *sp = state->owner;
3339 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003340 struct rpc_message msg = {
3341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3342 .rpc_cred = state->owner->so_cred,
3343 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003344 struct rpc_task_setup task_setup_data = {
3345 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003346 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003347 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003349 .flags = RPC_TASK_ASYNC,
3350 };
Trond Myklebust95121352005-10-18 14:20:12 -07003351 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003353 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3354 &task_setup_data.rpc_client, &msg);
3355
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003356 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003358 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003359 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003360 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003362 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003363 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3364 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003366 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3367 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003368 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003369 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003370 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003371 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003372 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003373 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003374 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003375 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003376 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003377 calldata->lr.roc = pnfs_roc(state->inode,
3378 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3379 if (calldata->lr.roc) {
3380 calldata->arg.lr_args = &calldata->lr.arg;
3381 calldata->res.lr_res = &calldata->lr.res;
3382 }
Al Viro643168c2011-06-22 18:20:23 -04003383 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003384
Trond Myklebust1174dd12010-12-21 10:52:24 -05003385 msg.rpc_argp = &calldata->arg;
3386 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003387 task_setup_data.callback_data = calldata;
3388 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003389 if (IS_ERR(task))
3390 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003391 status = 0;
3392 if (wait)
3393 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003394 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003395 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003396out_free_calldata:
3397 kfree(calldata);
3398out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003399 nfs4_put_open_state(state);
3400 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003401 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Trond Myklebust2b484292010-09-17 10:56:51 -04003404static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003405nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3406 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003409 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3410
3411 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Trond Myklebust565277f2007-10-15 18:17:53 -04003413 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003414 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003415
3416 nfs4_label_release_security(label);
3417
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003418 if (IS_ERR(state))
3419 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003420 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
Trond Myklebust1185a552009-12-03 15:54:02 -05003423static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003424{
3425 if (ctx->state == NULL)
3426 return;
3427 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003428 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003429 else
Al Viro643168c2011-06-22 18:20:23 -04003430 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Trond Myklebustb944dba2013-11-04 15:20:20 -05003433#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3434#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003435#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3438{
Kinglong Mee8c612822015-08-26 21:12:58 +08003439 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003440 struct nfs4_server_caps_arg args = {
3441 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003442 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 struct nfs4_server_caps_res res = {};
3445 struct rpc_message msg = {
3446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003447 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 .rpc_resp = &res,
3449 };
3450 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003451 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Kinglong Mee8c612822015-08-26 21:12:58 +08003453 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3454 FATTR4_WORD0_FH_EXPIRE_TYPE |
3455 FATTR4_WORD0_LINK_SUPPORT |
3456 FATTR4_WORD0_SYMLINK_SUPPORT |
3457 FATTR4_WORD0_ACLSUPPORT;
3458 if (minorversion)
3459 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3460
Bryan Schumaker7c513052011-03-24 17:12:24 +00003461 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003463 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003464 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003465 case 0:
3466 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3467 res.attr_bitmask[2] = 0;
3468 break;
3469 case 1:
3470 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3471 break;
3472 case 2:
3473 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003476 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3477 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3478 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3479 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003480 NFS_CAP_CTIME|NFS_CAP_MTIME|
3481 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003482 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3483 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 server->caps |= NFS_CAP_ACLS;
3485 if (res.has_links != 0)
3486 server->caps |= NFS_CAP_HARDLINKS;
3487 if (res.has_symlinks != 0)
3488 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003489 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3490 server->caps |= NFS_CAP_FILEID;
3491 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3492 server->caps |= NFS_CAP_MODE;
3493 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3494 server->caps |= NFS_CAP_NLINK;
3495 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3496 server->caps |= NFS_CAP_OWNER;
3497 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3498 server->caps |= NFS_CAP_OWNER_GROUP;
3499 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3500 server->caps |= NFS_CAP_ATIME;
3501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3502 server->caps |= NFS_CAP_CTIME;
3503 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3504 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003505#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3506 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3507 server->caps |= NFS_CAP_SECURITY_LABEL;
3508#endif
3509 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3510 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003511 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003512
Trond Myklebusta65318b2009-03-11 14:10:28 -04003513 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3514 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3515 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003516 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003517
3518 /* Avoid a regression due to buggy server */
3519 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3520 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003521 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3522 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003525 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return status;
3529}
3530
Trond Myklebust55a97592006-06-09 09:34:19 -04003531int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 struct nfs4_exception exception = { };
3534 int err;
3535 do {
3536 err = nfs4_handle_exception(server,
3537 _nfs4_server_capabilities(server, fhandle),
3538 &exception);
3539 } while (exception.retry);
3540 return err;
3541}
3542
3543static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3544 struct nfs_fsinfo *info)
3545{
David Quigleyaa9c2662013-05-22 12:50:44 -04003546 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003548 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 };
3550 struct nfs4_lookup_res res = {
3551 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003552 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 .fh = fhandle,
3554 };
3555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3557 .rpc_argp = &args,
3558 .rpc_resp = &res,
3559 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003560
David Quigleyaa9c2662013-05-22 12:50:44 -04003561 bitmask[0] = nfs4_fattr_bitmap[0];
3562 bitmask[1] = nfs4_fattr_bitmap[1];
3563 /*
3564 * Process the label in the upcoming getfattr
3565 */
3566 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3567
Trond Myklebust0e574af2005-10-27 22:12:38 -04003568 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003569 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
3572static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3573 struct nfs_fsinfo *info)
3574{
3575 struct nfs4_exception exception = { };
3576 int err;
3577 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003578 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003579 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003580 switch (err) {
3581 case 0:
3582 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003583 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003584 default:
3585 err = nfs4_handle_exception(server, err, &exception);
3586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003588out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return err;
3590}
3591
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003592static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3593 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3594{
Trond Myklebustc2190662013-08-26 19:23:04 -04003595 struct rpc_auth_create_args auth_args = {
3596 .pseudoflavor = flavor,
3597 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003598 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003599
Trond Myklebustc2190662013-08-26 19:23:04 -04003600 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003601 if (IS_ERR(auth))
3602 return -EACCES;
3603 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003604}
3605
Chuck Lever9a744ba2013-03-16 15:56:02 -04003606/*
3607 * Retry pseudoroot lookup with various security flavors. We do this when:
3608 *
3609 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3610 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3611 *
3612 * Returns zero on success, or a negative NFS4ERR value, or a
3613 * negative errno value.
3614 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003615static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003616 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003618 /* Per 3530bis 15.33.5 */
3619 static const rpc_authflavor_t flav_array[] = {
3620 RPC_AUTH_GSS_KRB5P,
3621 RPC_AUTH_GSS_KRB5I,
3622 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003623 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003624 RPC_AUTH_NULL,
3625 };
3626 int status = -EPERM;
3627 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003629 if (server->auth_info.flavor_len > 0) {
3630 /* try each flavor specified by user */
3631 for (i = 0; i < server->auth_info.flavor_len; i++) {
3632 status = nfs4_lookup_root_sec(server, fhandle, info,
3633 server->auth_info.flavors[i]);
3634 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3635 continue;
3636 break;
3637 }
3638 } else {
3639 /* no flavors specified by user, try default list */
3640 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3641 status = nfs4_lookup_root_sec(server, fhandle, info,
3642 flav_array[i]);
3643 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3644 continue;
3645 break;
3646 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003647 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003648
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003649 /*
3650 * -EACCESS could mean that the user doesn't have correct permissions
3651 * to access the mount. It could also mean that we tried to mount
3652 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3653 * existing mount programs don't handle -EACCES very well so it should
3654 * be mapped to -EPERM instead.
3655 */
3656 if (status == -EACCES)
3657 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003658 return status;
3659}
3660
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003661/**
3662 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3663 * @server: initialized nfs_server handle
3664 * @fhandle: we fill in the pseudo-fs root file handle
3665 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003666 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003667 *
3668 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003669 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003670int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003671 struct nfs_fsinfo *info,
3672 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003673{
Andre Przywarac7757072015-04-23 17:17:40 +01003674 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003675
Andre Przywarac7757072015-04-23 17:17:40 +01003676 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003677 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003678
3679 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c937b2016-07-25 13:31:14 -04003680 status = server->nfs_client->cl_mvops->find_root_sec(server,
3681 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 if (status == 0)
3684 status = nfs4_server_capabilities(server, fhandle);
3685 if (status == 0)
3686 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003687
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003688 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
Bryan Schumakerbae36242012-05-10 15:07:31 -04003691static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3692 struct nfs_fsinfo *info)
3693{
3694 int error;
3695 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003696 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003697
3698 error = nfs4_server_capabilities(server, mntfh);
3699 if (error < 0) {
3700 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3701 return error;
3702 }
3703
David Quigley14c43f72013-05-22 12:50:43 -04003704 label = nfs4_label_alloc(server, GFP_KERNEL);
3705 if (IS_ERR(label))
3706 return PTR_ERR(label);
3707
David Quigley1775fd32013-05-22 12:50:42 -04003708 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003709 if (error < 0) {
3710 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003711 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003712 }
3713
3714 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3715 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3716 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3717
David Quigley14c43f72013-05-22 12:50:43 -04003718err_free_label:
3719 nfs4_label_free(label);
3720
Bryan Schumakerbae36242012-05-10 15:07:31 -04003721 return error;
3722}
3723
Manoj Naik6b97fd32006-06-09 09:34:29 -04003724/*
3725 * Get locations and (maybe) other attributes of a referral.
3726 * Note that we'll actually follow the referral later when
3727 * we detect fsid mismatch in inode revalidation
3728 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003729static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3730 const struct qstr *name, struct nfs_fattr *fattr,
3731 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003732{
3733 int status = -ENOMEM;
3734 struct page *page = NULL;
3735 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003736
3737 page = alloc_page(GFP_KERNEL);
3738 if (page == NULL)
3739 goto out;
3740 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3741 if (locations == NULL)
3742 goto out;
3743
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003744 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003745 if (status != 0)
3746 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003747
3748 /*
3749 * If the fsid didn't change, this is a migration event, not a
3750 * referral. Cause us to drop into the exception handler, which
3751 * will kick off migration recovery.
3752 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003753 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003754 dprintk("%s: server did not return a different fsid for"
3755 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003756 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003757 goto out;
3758 }
Andy Adamson533eb462011-06-13 18:25:56 -04003759 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3760 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003761
Andy Adamson533eb462011-06-13 18:25:56 -04003762 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003763 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764 memset(fhandle, 0, sizeof(struct nfs_fh));
3765out:
3766 if (page)
3767 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003768 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003769 return status;
3770}
3771
David Quigley1775fd32013-05-22 12:50:42 -04003772static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3773 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 struct nfs4_getattr_arg args = {
3776 .fh = fhandle,
3777 .bitmask = server->attr_bitmask,
3778 };
3779 struct nfs4_getattr_res res = {
3780 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003781 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 .server = server,
3783 };
3784 struct rpc_message msg = {
3785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3786 .rpc_argp = &args,
3787 .rpc_resp = &res,
3788 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003789
3790 args.bitmask = nfs4_bitmask(server, label);
3791
Trond Myklebust0e574af2005-10-27 22:12:38 -04003792 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003793 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
David Quigley1775fd32013-05-22 12:50:42 -04003796static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3797 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 struct nfs4_exception exception = { };
3800 int err;
3801 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003802 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3803 trace_nfs4_getattr(server, fhandle, fattr, err);
3804 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 &exception);
3806 } while (exception.retry);
3807 return err;
3808}
3809
3810/*
3811 * The file is not closed if it is opened due to the a request to change
3812 * the size of the file. The open call will not be needed once the
3813 * VFS layer lookup-intents are implemented.
3814 *
3815 * Close is called when the inode is destroyed.
3816 * If we haven't opened the file for O_WRONLY, we
3817 * need to in the size_change case to obtain a stateid.
3818 *
3819 * Got race?
3820 * Because OPEN is always done by name in nfsv4, it is
3821 * possible that we opened a different file by the same
3822 * name. We can recognize this race condition, but we
3823 * can't do anything about it besides returning an error.
3824 *
3825 * This will be fixed with VFS changes (lookup-intent).
3826 */
3827static int
3828nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3829 struct iattr *sattr)
3830{
David Howells2b0143b2015-03-17 22:25:59 +00003831 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003832 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003833 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003834 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 int status;
3836
Peng Tao88ac8152014-09-12 11:04:10 +08003837 if (pnfs_ld_layoutret_on_setattr(inode) &&
3838 sattr->ia_valid & ATTR_SIZE &&
3839 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003840 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003841
Trond Myklebust0e574af2005-10-27 22:12:38 -04003842 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Andy Adamson26699402012-05-30 16:12:24 -04003844 /* Deal with open(O_TRUNC) */
3845 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003846 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003847
3848 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003849 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003850 return 0;
3851
Trond Myklebustd5308382005-11-04 15:33:38 -05003852 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003853 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003854
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003855 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003856 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003857 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003858 }
3859
David Quigley14c43f72013-05-22 12:50:43 -04003860 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3861 if (IS_ERR(label))
3862 return PTR_ERR(label);
3863
NeilBrown29b59f92016-10-13 15:26:47 +11003864 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003865 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003866 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003867 nfs_setsecurity(inode, fattr, label);
3868 }
David Quigley14c43f72013-05-22 12:50:43 -04003869 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return status;
3871}
3872
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003873static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3874 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003875 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003876{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003877 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003878 int status;
3879 struct nfs4_lookup_arg args = {
3880 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003881 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003882 .name = name,
3883 };
3884 struct nfs4_lookup_res res = {
3885 .server = server,
3886 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003888 .fh = fhandle,
3889 };
3890 struct rpc_message msg = {
3891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3892 .rpc_argp = &args,
3893 .rpc_resp = &res,
3894 };
3895
David Quigleyaa9c2662013-05-22 12:50:44 -04003896 args.bitmask = nfs4_bitmask(server, label);
3897
David Howells2b3de442006-08-22 20:06:09 -04003898 nfs_fattr_init(fattr);
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003901 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 dprintk("NFS reply lookup: %d\n", status);
3903 return status;
3904}
3905
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003906static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003907{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003908 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003909 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003910 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3911 fattr->nlink = 2;
3912}
3913
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003914static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003915 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003916 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003917{
3918 struct nfs4_exception exception = { };
3919 struct rpc_clnt *client = *clnt;
3920 int err;
3921 do {
David Quigley1775fd32013-05-22 12:50:42 -04003922 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003923 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003924 switch (err) {
3925 case -NFS4ERR_BADNAME:
3926 err = -ENOENT;
3927 goto out;
3928 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003929 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003930 if (err == -NFS4ERR_MOVED)
3931 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003932 goto out;
3933 case -NFS4ERR_WRONGSEC:
3934 err = -EPERM;
3935 if (client != *clnt)
3936 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003937 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003938 if (IS_ERR(client))
3939 return PTR_ERR(client);
3940
3941 exception.retry = 1;
3942 break;
3943 default:
3944 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3945 }
3946 } while (exception.retry);
3947
3948out:
3949 if (err == 0)
3950 *clnt = client;
3951 else if (client != *clnt)
3952 rpc_shutdown_client(client);
3953
3954 return err;
3955}
3956
Al Virobeffb8f2016-07-20 16:34:42 -04003957static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003958 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3959 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003961 int status;
3962 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003963
David Quigley1775fd32013-05-22 12:50:42 -04003964 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003965 if (client != NFS_CLIENT(dir)) {
3966 rpc_shutdown_client(client);
3967 nfs_fixup_secinfo_attributes(fattr);
3968 }
3969 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003972struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003973nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003974 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3975{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003976 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003977 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003978
David Quigley1775fd32013-05-22 12:50:42 -04003979 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003980 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003981 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003982 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003983}
3984
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003985static int _nfs4_proc_lookupp(struct inode *inode,
3986 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3987 struct nfs4_label *label)
3988{
3989 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3990 struct nfs_server *server = NFS_SERVER(inode);
3991 int status;
3992 struct nfs4_lookupp_arg args = {
3993 .bitmask = server->attr_bitmask,
3994 .fh = NFS_FH(inode),
3995 };
3996 struct nfs4_lookupp_res res = {
3997 .server = server,
3998 .fattr = fattr,
3999 .label = label,
4000 .fh = fhandle,
4001 };
4002 struct rpc_message msg = {
4003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4004 .rpc_argp = &args,
4005 .rpc_resp = &res,
4006 };
4007
4008 args.bitmask = nfs4_bitmask(server, label);
4009
4010 nfs_fattr_init(fattr);
4011
4012 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4013 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4014 &res.seq_res, 0);
4015 dprintk("NFS reply lookupp: %d\n", status);
4016 return status;
4017}
4018
4019static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4020 struct nfs_fattr *fattr, struct nfs4_label *label)
4021{
4022 struct nfs4_exception exception = { };
4023 int err;
4024 do {
4025 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4026 trace_nfs4_lookupp(inode, err);
4027 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4028 &exception);
4029 } while (exception.retry);
4030 return err;
4031}
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4034{
Trond Myklebust76b32992007-08-10 17:45:11 -04004035 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 struct nfs4_accessargs args = {
4037 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004038 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004039 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004041 struct nfs4_accessres res = {
4042 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4046 .rpc_argp = &args,
4047 .rpc_resp = &res,
4048 .rpc_cred = entry->cred,
4049 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004050 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Trond Myklebustc407d412010-04-16 16:22:48 -04004052 res.fattr = nfs_alloc_fattr();
4053 if (res.fattr == NULL)
4054 return -ENOMEM;
4055
Bryan Schumaker7c513052011-03-24 17:12:24 +00004056 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004058 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004059 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004061 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return status;
4063}
4064
4065static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4066{
4067 struct nfs4_exception exception = { };
4068 int err;
4069 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004070 err = _nfs4_proc_access(inode, entry);
4071 trace_nfs4_access(inode, err);
4072 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 &exception);
4074 } while (exception.retry);
4075 return err;
4076}
4077
4078/*
4079 * TODO: For the time being, we don't try to get any attributes
4080 * along with any of the zero-copy operations READ, READDIR,
4081 * READLINK, WRITE.
4082 *
4083 * In the case of the first three, we want to put the GETATTR
4084 * after the read-type operation -- this is because it is hard
4085 * to predict the length of a GETATTR response in v4, and thus
4086 * align the READ data correctly. This means that the GETATTR
4087 * may end up partially falling into the page cache, and we should
4088 * shift it into the 'tail' of the xdr_buf before processing.
4089 * To do this efficiently, we need to know the total length
4090 * of data received, which doesn't seem to be available outside
4091 * of the RPC layer.
4092 *
4093 * In the case of WRITE, we also want to put the GETATTR after
4094 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004095 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 *
4097 * Both of these changes to the XDR layer would in fact be quite
4098 * minor, but I decided to leave them for a subsequent patch.
4099 */
4100static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4101 unsigned int pgbase, unsigned int pglen)
4102{
4103 struct nfs4_readlink args = {
4104 .fh = NFS_FH(inode),
4105 .pgbase = pgbase,
4106 .pglen = pglen,
4107 .pages = &page,
4108 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004109 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 struct rpc_message msg = {
4111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4112 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004113 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 };
4115
Bryan Schumaker7c513052011-03-24 17:12:24 +00004116 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 -07004117}
4118
4119static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4120 unsigned int pgbase, unsigned int pglen)
4121{
4122 struct nfs4_exception exception = { };
4123 int err;
4124 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004125 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4126 trace_nfs4_readlink(inode, err);
4127 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 &exception);
4129 } while (exception.retry);
4130 return err;
4131}
4132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004134 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136static int
4137nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004138 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004140 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004141 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004142 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 int status = 0;
4145
NeilBrown532d4de2016-10-13 15:26:47 +11004146 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004147 if (IS_ERR(ctx))
4148 return PTR_ERR(ctx);
4149
David Quigleyaa9c2662013-05-22 12:50:44 -04004150 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4151
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004152 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4153 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004154 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (IS_ERR(state)) {
4156 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004160 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004161 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return status;
4163}
4164
Al Virobeffb8f2016-07-20 16:34:42 -04004165static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Trond Myklebust16e42952005-10-27 22:12:44 -04004167 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004168 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004170 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004172 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004173 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004174 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4177 .rpc_argp = &args,
4178 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004180 unsigned long timestamp = jiffies;
Trond Myklebust778d28172012-04-27 13:48:19 -04004181 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004182
Bryan Schumaker7c513052011-03-24 17:12:24 +00004183 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d28172012-04-27 13:48:19 -04004184 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004185 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return status;
4187}
4188
Al Virobeffb8f2016-07-20 16:34:42 -04004189static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
4191 struct nfs4_exception exception = { };
4192 int err;
4193 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004194 err = _nfs4_proc_remove(dir, name);
4195 trace_nfs4_remove(dir, name, err);
4196 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 &exception);
4198 } while (exception.retry);
4199 return err;
4200}
4201
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004202static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004204 struct nfs_server *server = NFS_SERVER(dir);
4205 struct nfs_removeargs *args = msg->rpc_argp;
4206 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004208 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004210 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004211
4212 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004215static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4216{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004217 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004218 &data->args.seq_args,
4219 &data->res.seq_res,
4220 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004223static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004225 struct nfs_unlinkdata *data = task->tk_calldata;
4226 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004227
Trond Myklebust14516c32010-07-31 14:29:06 -04004228 if (!nfs4_sequence_done(task, &res->seq_res))
4229 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004230 if (nfs4_async_handle_error(task, res->server, NULL,
4231 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004232 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004233 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004234 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004235 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236}
4237
Jeff Laytond3d41522010-09-17 17:31:57 -04004238static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4239{
4240 struct nfs_server *server = NFS_SERVER(dir);
4241 struct nfs_renameargs *arg = msg->rpc_argp;
4242 struct nfs_renameres *res = msg->rpc_resp;
4243
4244 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004245 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004246 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004247}
4248
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004249static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4250{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004251 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004252 &data->args.seq_args,
4253 &data->res.seq_res,
4254 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004255}
4256
4257static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4258 struct inode *new_dir)
4259{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004260 struct nfs_renamedata *data = task->tk_calldata;
4261 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004262
4263 if (!nfs4_sequence_done(task, &res->seq_res))
4264 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004265 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004266 return 0;
4267
Trond Myklebustc733c492017-01-11 12:32:26 -05004268 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004269 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004270 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004271 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004272 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004273 return 1;
4274}
4275
Al Virobeffb8f2016-07-20 16:34:42 -04004276static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004278 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 struct nfs4_link_arg arg = {
4280 .fh = NFS_FH(inode),
4281 .dir_fh = NFS_FH(dir),
4282 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004283 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004285 struct nfs4_link_res res = {
4286 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004287 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004288 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4291 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004292 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004294 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Trond Myklebust136f2622010-04-16 16:22:49 -04004296 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d28172012-04-27 13:48:19 -04004297 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004298 goto out;
4299
David Quigley14c43f72013-05-22 12:50:43 -04004300 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4301 if (IS_ERR(res.label)) {
4302 status = PTR_ERR(res.label);
4303 goto out;
4304 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004305 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004306
Bryan Schumaker7c513052011-03-24 17:12:24 +00004307 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004308 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004309 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004310 status = nfs_post_op_update_inode(inode, res.fattr);
4311 if (!status)
4312 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004313 }
David Quigley14c43f72013-05-22 12:50:43 -04004314
4315
4316 nfs4_label_free(res.label);
4317
Trond Myklebust136f2622010-04-16 16:22:49 -04004318out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004319 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return status;
4321}
4322
Al Virobeffb8f2016-07-20 16:34:42 -04004323static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
4325 struct nfs4_exception exception = { };
4326 int err;
4327 do {
4328 err = nfs4_handle_exception(NFS_SERVER(inode),
4329 _nfs4_proc_link(inode, dir, name),
4330 &exception);
4331 } while (exception.retry);
4332 return err;
4333}
4334
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004335struct nfs4_createdata {
4336 struct rpc_message msg;
4337 struct nfs4_create_arg arg;
4338 struct nfs4_create_res res;
4339 struct nfs_fh fh;
4340 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004341 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004342};
4343
4344static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004345 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004346{
4347 struct nfs4_createdata *data;
4348
4349 data = kzalloc(sizeof(*data), GFP_KERNEL);
4350 if (data != NULL) {
4351 struct nfs_server *server = NFS_SERVER(dir);
4352
David Quigley14c43f72013-05-22 12:50:43 -04004353 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4354 if (IS_ERR(data->label))
4355 goto out_free;
4356
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004357 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4358 data->msg.rpc_argp = &data->arg;
4359 data->msg.rpc_resp = &data->res;
4360 data->arg.dir_fh = NFS_FH(dir);
4361 data->arg.server = server;
4362 data->arg.name = name;
4363 data->arg.attrs = sattr;
4364 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004365 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004366 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004367 data->res.server = server;
4368 data->res.fh = &data->fh;
4369 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004370 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004371 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004372 }
4373 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004374out_free:
4375 kfree(data);
4376 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004377}
4378
4379static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4380{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004381 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004382 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004383 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004384 update_changeattr(dir, &data->res.dir_cinfo,
4385 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004386 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004387 }
4388 return status;
4389}
4390
4391static void nfs4_free_createdata(struct nfs4_createdata *data)
4392{
David Quigley14c43f72013-05-22 12:50:43 -04004393 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004394 kfree(data);
4395}
4396
Chuck Lever4f390c12006-08-22 20:06:22 -04004397static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004398 struct page *page, unsigned int len, struct iattr *sattr,
4399 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 struct nfs4_createdata *data;
4402 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Chuck Lever94a6d752006-08-22 20:06:23 -04004404 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004405 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004406
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 status = -ENOMEM;
4408 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4409 if (data == NULL)
4410 goto out;
4411
4412 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4413 data->arg.u.symlink.pages = &page;
4414 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004415 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004417 status = nfs4_do_create(dir, dentry, data);
4418
4419 nfs4_free_createdata(data);
4420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 return status;
4422}
4423
Chuck Lever4f390c12006-08-22 20:06:22 -04004424static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004425 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
4427 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004428 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004430
4431 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004434 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4435 trace_nfs4_symlink(dir, &dentry->d_name, err);
4436 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 &exception);
4438 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004439
4440 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 return err;
4442}
4443
4444static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004445 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004447 struct nfs4_createdata *data;
4448 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4451 if (data == NULL)
4452 goto out;
4453
David Quigley1775fd32013-05-22 12:50:42 -04004454 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004455 status = nfs4_do_create(dir, dentry, data);
4456
4457 nfs4_free_createdata(data);
4458out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 return status;
4460}
4461
4462static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4463 struct iattr *sattr)
4464{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004465 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004467 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004469
David Quigleyaa9c2662013-05-22 12:50:44 -04004470 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4471
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004472 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4473 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004475 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4476 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4477 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 &exception);
4479 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004480 nfs4_label_release_security(label);
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 return err;
4483}
4484
4485static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004486 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
David Howells2b0143b2015-03-17 22:25:59 +00004488 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct nfs4_readdir_arg args = {
4490 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004491 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 .pgbase = 0,
4493 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004494 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004495 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 };
4497 struct nfs4_readdir_res res;
4498 struct rpc_message msg = {
4499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4500 .rpc_argp = &args,
4501 .rpc_resp = &res,
4502 .rpc_cred = cred,
4503 };
4504 int status;
4505
Al Viro6de14722013-09-16 10:53:17 -04004506 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4507 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004508 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004509 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004511 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 -05004512 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004513 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004514 status += args.pgbase;
4515 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004516
4517 nfs_invalidate_atime(dir);
4518
Harvey Harrison3110ff82008-05-02 13:42:44 -07004519 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return status;
4521}
4522
4523static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004524 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
4526 struct nfs4_exception exception = { };
4527 int err;
4528 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004529 err = _nfs4_proc_readdir(dentry, cred, cookie,
4530 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004531 trace_nfs4_readdir(d_inode(dentry), err);
4532 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 &exception);
4534 } while (exception.retry);
4535 return err;
4536}
4537
4538static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004539 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004541 struct nfs4_createdata *data;
4542 int mode = sattr->ia_mode;
4543 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004545 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4546 if (data == NULL)
4547 goto out;
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004550 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data->arg.ftype = NF4BLK;
4553 data->arg.u.device.specdata1 = MAJOR(rdev);
4554 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004557 data->arg.ftype = NF4CHR;
4558 data->arg.u.device.specdata1 = MAJOR(rdev);
4559 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004560 } else if (!S_ISSOCK(mode)) {
4561 status = -EINVAL;
4562 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
David Quigley1775fd32013-05-22 12:50:42 -04004564
David Quigleyaa9c2662013-05-22 12:50:44 -04004565 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004566 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004567out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004568 nfs4_free_createdata(data);
4569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return status;
4571}
4572
4573static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4574 struct iattr *sattr, dev_t rdev)
4575{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004576 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004578 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004580
David Quigleyaa9c2662013-05-22 12:50:44 -04004581 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4582
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004583 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4584 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004586 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4587 trace_nfs4_mknod(dir, &dentry->d_name, err);
4588 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 &exception);
4590 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004591
4592 nfs4_label_release_security(label);
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return err;
4595}
4596
4597static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4598 struct nfs_fsstat *fsstat)
4599{
4600 struct nfs4_statfs_arg args = {
4601 .fh = fhandle,
4602 .bitmask = server->attr_bitmask,
4603 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004604 struct nfs4_statfs_res res = {
4605 .fsstat = fsstat,
4606 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 struct rpc_message msg = {
4608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4609 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004610 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 };
4612
Trond Myklebust0e574af2005-10-27 22:12:38 -04004613 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004614 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
4617static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4618{
4619 struct nfs4_exception exception = { };
4620 int err;
4621 do {
4622 err = nfs4_handle_exception(server,
4623 _nfs4_proc_statfs(server, fhandle, fsstat),
4624 &exception);
4625 } while (exception.retry);
4626 return err;
4627}
4628
4629static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4630 struct nfs_fsinfo *fsinfo)
4631{
4632 struct nfs4_fsinfo_arg args = {
4633 .fh = fhandle,
4634 .bitmask = server->attr_bitmask,
4635 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004636 struct nfs4_fsinfo_res res = {
4637 .fsinfo = fsinfo,
4638 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4641 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004642 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 };
4644
Bryan Schumaker7c513052011-03-24 17:12:24 +00004645 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
4647
4648static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4649{
4650 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004651 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 int err;
4653
4654 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004655 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004656 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004657 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004658 nfs4_set_lease_period(server->nfs_client,
4659 fsinfo->lease_time * HZ,
4660 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004661 break;
4662 }
4663 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 } while (exception.retry);
4665 return err;
4666}
4667
4668static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4669{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004670 int error;
4671
Trond Myklebust0e574af2005-10-27 22:12:38 -04004672 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004673 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004674 if (error == 0) {
4675 /* block layout checks this! */
4676 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004677 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004678 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004679
4680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
4683static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4684 struct nfs_pathconf *pathconf)
4685{
4686 struct nfs4_pathconf_arg args = {
4687 .fh = fhandle,
4688 .bitmask = server->attr_bitmask,
4689 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004690 struct nfs4_pathconf_res res = {
4691 .pathconf = pathconf,
4692 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4695 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004696 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 };
4698
4699 /* None of the pathconf attributes are mandatory to implement */
4700 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4701 memset(pathconf, 0, sizeof(*pathconf));
4702 return 0;
4703 }
4704
Trond Myklebust0e574af2005-10-27 22:12:38 -04004705 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004706 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
4709static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4710 struct nfs_pathconf *pathconf)
4711{
4712 struct nfs4_exception exception = { };
4713 int err;
4714
4715 do {
4716 err = nfs4_handle_exception(server,
4717 _nfs4_proc_pathconf(server, fhandle, pathconf),
4718 &exception);
4719 } while (exception.retry);
4720 return err;
4721}
4722
Trond Myklebust5521abf2013-03-16 20:54:34 -04004723int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004724 const struct nfs_open_context *ctx,
4725 const struct nfs_lock_context *l_ctx,
4726 fmode_t fmode)
4727{
NeilBrown17393472016-10-13 15:26:47 +11004728 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004729}
4730EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4731
Trond Myklebust5521abf2013-03-16 20:54:34 -04004732static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4733 const struct nfs_open_context *ctx,
4734 const struct nfs_lock_context *l_ctx,
4735 fmode_t fmode)
4736{
4737 nfs4_stateid current_stateid;
4738
Trond Myklebuste1253be2014-03-05 08:44:23 -05004739 /* If the current stateid represents a lost lock, then exit */
4740 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4741 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004742 return nfs4_stateid_match(stateid, &current_stateid);
4743}
4744
4745static bool nfs4_error_stateid_expired(int err)
4746{
4747 switch (err) {
4748 case -NFS4ERR_DELEG_REVOKED:
4749 case -NFS4ERR_ADMIN_REVOKED:
4750 case -NFS4ERR_BAD_STATEID:
4751 case -NFS4ERR_STALE_STATEID:
4752 case -NFS4ERR_OLD_STATEID:
4753 case -NFS4ERR_OPENMODE:
4754 case -NFS4ERR_EXPIRED:
4755 return true;
4756 }
4757 return false;
4758}
4759
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004760static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004765 if (task->tk_status < 0) {
4766 struct nfs4_exception exception = {
4767 .inode = hdr->inode,
4768 .state = hdr->args.context->state,
4769 .stateid = &hdr->args.stateid,
4770 };
4771 task->tk_status = nfs4_async_handle_exception(task,
4772 server, task->tk_status, &exception);
4773 if (exception.retry) {
4774 rpc_restart_call_prepare(task);
4775 return -EAGAIN;
4776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
Trond Myklebust5521abf2013-03-16 20:54:34 -04004784static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004785 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786{
4787
4788 if (!nfs4_error_stateid_expired(task->tk_status) ||
4789 nfs4_stateid_is_current(&args->stateid,
4790 args->context,
4791 args->lock_context,
4792 FMODE_READ))
4793 return false;
4794 rpc_restart_call_prepare(task);
4795 return true;
4796}
4797
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004798static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004799{
4800
4801 dprintk("--> %s\n", __func__);
4802
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004803 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004804 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004806 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004807 if (task->tk_status > 0)
4808 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004809 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4810 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004811}
4812
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004813static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4814 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004817 if (!hdr->pgio_done_cb)
4818 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004819 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004820 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004823static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4824 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004825{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004826 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004827 &hdr->args.seq_args,
4828 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004829 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004830 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004831 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4832 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004833 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004834 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004835 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004836 return -EIO;
4837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838}
4839
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004840static int nfs4_write_done_cb(struct rpc_task *task,
4841 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004843 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004844
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004845 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004846 if (task->tk_status < 0) {
4847 struct nfs4_exception exception = {
4848 .inode = hdr->inode,
4849 .state = hdr->args.context->state,
4850 .stateid = &hdr->args.stateid,
4851 };
4852 task->tk_status = nfs4_async_handle_exception(task,
4853 NFS_SERVER(inode), task->tk_status,
4854 &exception);
4855 if (exception.retry) {
4856 rpc_restart_call_prepare(task);
4857 return -EAGAIN;
4858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 }
Trond Myklebust4f9838c72005-10-27 22:12:44 -04004860 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004861 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004862 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c72005-10-27 22:12:44 -04004863 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Trond Myklebust5521abf2013-03-16 20:54:34 -04004867static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004868 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004869{
4870
4871 if (!nfs4_error_stateid_expired(task->tk_status) ||
4872 nfs4_stateid_is_current(&args->stateid,
4873 args->context,
4874 args->lock_context,
4875 FMODE_WRITE))
4876 return false;
4877 rpc_restart_call_prepare(task);
4878 return true;
4879}
4880
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004881static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004882{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004884 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004885 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004886 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4888 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004889}
4890
Trond Myklebust5a37f852012-04-28 14:55:16 -04004891static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004892bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004893{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004894 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004895 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004896 return false;
4897 /* Otherwise, request attributes if and only if we don't hold
4898 * a delegation
4899 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004900 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004901}
Fred Isamana69aef12011-03-03 15:13:47 +00004902
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004903static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4904 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004907
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4909 hdr->args.bitmask = NULL;
4910 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004911 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004912 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004913
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004914 if (!hdr->pgio_done_cb)
4915 hdr->pgio_done_cb = nfs4_write_done_cb;
4916 hdr->res.server = server;
4917 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004919 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004920 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
Fred Isaman0b7c0152012-04-20 14:47:39 -04004923static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4924{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004925 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004926 &data->args.seq_args,
4927 &data->res.seq_res,
4928 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
Fred Isaman0b7c0152012-04-20 14:47:39 -04004931static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004934
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004935 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004936 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4937 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004938 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004939 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
Fred Isaman0b7c0152012-04-20 14:47:39 -04004944static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004945{
4946 if (!nfs4_sequence_done(task, &data->res.seq_res))
4947 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004948 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004949}
4950
Fred Isaman0b7c0152012-04-20 14:47:39 -04004951static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004953 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004954
Fred Isaman0b7c0152012-04-20 14:47:39 -04004955 if (data->commit_done_cb == NULL)
4956 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c72005-10-27 22:12:44 -04004957 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004958 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004959 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004962struct nfs4_renewdata {
4963 struct nfs_client *client;
4964 unsigned long timestamp;
4965};
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967/*
4968 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4969 * standalone procedure for queueing an asynchronous RENEW.
4970 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004971static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004972{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004973 struct nfs4_renewdata *data = calldata;
4974 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004975
Elena Reshetova212bf412017-10-20 12:53:38 +03004976 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004977 nfs4_schedule_state_renewal(clp);
4978 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004979 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004980}
4981
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004982static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004984 struct nfs4_renewdata *data = calldata;
4985 struct nfs_client *clp = data->client;
4986 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004988 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004989 switch (task->tk_status) {
4990 case 0:
4991 break;
4992 case -NFS4ERR_LEASE_MOVED:
4993 nfs4_schedule_lease_moved_recovery(clp);
4994 break;
4995 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004996 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004997 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4998 return;
4999 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005000 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005001 return;
5002 }
5003 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005005 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006}
5007
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005008static const struct rpc_call_ops nfs4_renew_ops = {
5009 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005010 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005011};
5012
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005013static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014{
5015 struct rpc_message msg = {
5016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5017 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005018 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005020 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005022 if (renew_flags == 0)
5023 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005024 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005025 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005026 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005027 if (data == NULL) {
5028 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005029 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005030 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005031 data->client = clp;
5032 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005033 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005034 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005037static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038{
5039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5041 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005042 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 };
5044 unsigned long now = jiffies;
5045 int status;
5046
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005047 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 if (status < 0)
5049 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005050 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 return 0;
5052}
5053
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054static inline int nfs4_server_supports_acls(struct nfs_server *server)
5055{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005056 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005057}
5058
Trond Myklebust21f498c2012-08-24 10:59:25 -04005059/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5060 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005061 * the stack.
5062 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005063#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005064
Neil Hormane9e3d722011-03-04 19:26:03 -05005065static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005066 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005067{
5068 struct page *newpage, **spages;
5069 int rc = 0;
5070 size_t len;
5071 spages = pages;
5072
5073 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005074 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005075 newpage = alloc_page(GFP_KERNEL);
5076
5077 if (newpage == NULL)
5078 goto unwind;
5079 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005080 buf += len;
5081 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005082 *pages++ = newpage;
5083 rc++;
5084 } while (buflen != 0);
5085
5086 return rc;
5087
5088unwind:
5089 for(; rc > 0; rc--)
5090 __free_page(spages[rc-1]);
5091 return -ENOMEM;
5092}
5093
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094struct nfs4_cached_acl {
5095 int cached;
5096 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005097 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005098};
5099
5100static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005101{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102 struct nfs_inode *nfsi = NFS_I(inode);
5103
5104 spin_lock(&inode->i_lock);
5105 kfree(nfsi->nfs4_acl);
5106 nfsi->nfs4_acl = acl;
5107 spin_unlock(&inode->i_lock);
5108}
5109
5110static void nfs4_zap_acl_attr(struct inode *inode)
5111{
5112 nfs4_set_cached_acl(inode, NULL);
5113}
5114
5115static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5116{
5117 struct nfs_inode *nfsi = NFS_I(inode);
5118 struct nfs4_cached_acl *acl;
5119 int ret = -ENOENT;
5120
5121 spin_lock(&inode->i_lock);
5122 acl = nfsi->nfs4_acl;
5123 if (acl == NULL)
5124 goto out;
5125 if (buf == NULL) /* user is just asking for length */
5126 goto out_len;
5127 if (acl->cached == 0)
5128 goto out;
5129 ret = -ERANGE; /* see getxattr(2) man page */
5130 if (acl->len > buflen)
5131 goto out;
5132 memcpy(buf, acl->data, acl->len);
5133out_len:
5134 ret = acl->len;
5135out:
5136 spin_unlock(&inode->i_lock);
5137 return ret;
5138}
5139
Sachin Prabhu5794d212012-04-17 14:36:40 +01005140static 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 +00005141{
5142 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005143 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005144
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005145 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005146 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005147 if (acl == NULL)
5148 goto out;
5149 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005150 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005151 } else {
5152 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5153 if (acl == NULL)
5154 goto out;
5155 acl->cached = 0;
5156 }
5157 acl->len = acl_len;
5158out:
5159 nfs4_set_cached_acl(inode, acl);
5160}
5161
Andy Adamsonbf118a32011-12-07 11:55:27 -05005162/*
5163 * The getxattr API returns the required buffer length when called with a
5164 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5165 * the required buf. On a NULL buf, we send a page of data to the server
5166 * guessing that the ACL request can be serviced by a page. If so, we cache
5167 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5168 * the cache. If not so, we throw away the page, and cache the required
5169 * length. The next getxattr call will then produce another round trip to
5170 * the server, this time with the input buf of the required size.
5171 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005172static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005173{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005174 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005175 struct nfs_getaclargs args = {
5176 .fh = NFS_FH(inode),
5177 .acl_pages = pages,
5178 .acl_len = buflen,
5179 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005180 struct nfs_getaclres res = {
5181 .acl_len = buflen,
5182 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005183 struct rpc_message msg = {
5184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5185 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005186 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005187 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005188 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005189 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005190
Trond Myklebust21f498c2012-08-24 10:59:25 -04005191 if (npages > ARRAY_SIZE(pages))
5192 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005193
Andy Adamsonbf118a32011-12-07 11:55:27 -05005194 for (i = 0; i < npages; i++) {
5195 pages[i] = alloc_page(GFP_KERNEL);
5196 if (!pages[i])
5197 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005198 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005199
5200 /* for decoding across pages */
5201 res.acl_scratch = alloc_page(GFP_KERNEL);
5202 if (!res.acl_scratch)
5203 goto out_free;
5204
Andy Adamsonbf118a32011-12-07 11:55:27 -05005205 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005206
Peng Taode040be2012-01-10 22:42:47 +08005207 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005208 __func__, buf, buflen, npages, args.acl_len);
5209 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5210 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005211 if (ret)
5212 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005213
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005214 /* Handle the case where the passed-in buffer is too short */
5215 if (res.acl_flags & NFS4_ACL_TRUNC) {
5216 /* Did the user only issue a request for the acl length? */
5217 if (buf == NULL)
5218 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005219 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005220 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005222 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005223 if (buf) {
5224 if (res.acl_len > buflen) {
5225 ret = -ERANGE;
5226 goto out_free;
5227 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005228 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005229 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005230out_ok:
5231 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005232out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005233 for (i = 0; i < npages; i++)
5234 if (pages[i])
5235 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005236 if (res.acl_scratch)
5237 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005238 return ret;
5239}
5240
Trond Myklebust16b42892006-08-24 12:27:15 -04005241static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5242{
5243 struct nfs4_exception exception = { };
5244 ssize_t ret;
5245 do {
5246 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005247 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005248 if (ret >= 0)
5249 break;
5250 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5251 } while (exception.retry);
5252 return ret;
5253}
5254
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005255static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5256{
5257 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005258 int ret;
5259
5260 if (!nfs4_server_supports_acls(server))
5261 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005262 ret = nfs_revalidate_inode(server, inode);
5263 if (ret < 0)
5264 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005265 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5266 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005267 ret = nfs4_read_cached_acl(inode, buf, buflen);
5268 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005269 /* -ENOENT is returned if there is no ACL or if there is an ACL
5270 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005271 return ret;
5272 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005273}
5274
Trond Myklebust16b42892006-08-24 12:27:15 -04005275static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005276{
5277 struct nfs_server *server = NFS_SERVER(inode);
5278 struct page *pages[NFS4ACL_MAXPAGES];
5279 struct nfs_setaclargs arg = {
5280 .fh = NFS_FH(inode),
5281 .acl_pages = pages,
5282 .acl_len = buflen,
5283 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005284 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005285 struct rpc_message msg = {
5286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5287 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005288 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005289 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005290 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005291 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005292
5293 if (!nfs4_server_supports_acls(server))
5294 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005295 if (npages > ARRAY_SIZE(pages))
5296 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005297 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005298 if (i < 0)
5299 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005300 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005301 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005302
5303 /*
5304 * Free each page after tx, so the only ref left is
5305 * held by the network stack
5306 */
5307 for (; i > 0; i--)
5308 put_page(pages[i-1]);
5309
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005310 /*
5311 * Acl update can result in inode attribute update.
5312 * so mark the attribute cache invalid.
5313 */
5314 spin_lock(&inode->i_lock);
5315 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5316 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005317 nfs_access_zap_cache(inode);
5318 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005319 return ret;
5320}
5321
Trond Myklebust16b42892006-08-24 12:27:15 -04005322static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5323{
5324 struct nfs4_exception exception = { };
5325 int err;
5326 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005327 err = __nfs4_proc_set_acl(inode, buf, buflen);
5328 trace_nfs4_set_acl(inode, err);
5329 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005330 &exception);
5331 } while (exception.retry);
5332 return err;
5333}
5334
David Quigleyaa9c2662013-05-22 12:50:44 -04005335#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5336static int _nfs4_get_security_label(struct inode *inode, void *buf,
5337 size_t buflen)
5338{
5339 struct nfs_server *server = NFS_SERVER(inode);
5340 struct nfs_fattr fattr;
5341 struct nfs4_label label = {0, 0, buflen, buf};
5342
5343 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005344 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005345 .fh = NFS_FH(inode),
5346 .bitmask = bitmask,
5347 };
5348 struct nfs4_getattr_res res = {
5349 .fattr = &fattr,
5350 .label = &label,
5351 .server = server,
5352 };
5353 struct rpc_message msg = {
5354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005355 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005356 .rpc_resp = &res,
5357 };
5358 int ret;
5359
5360 nfs_fattr_init(&fattr);
5361
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005362 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005363 if (ret)
5364 return ret;
5365 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5366 return -ENOENT;
5367 if (buflen < label.len)
5368 return -ERANGE;
5369 return 0;
5370}
5371
5372static int nfs4_get_security_label(struct inode *inode, void *buf,
5373 size_t buflen)
5374{
5375 struct nfs4_exception exception = { };
5376 int err;
5377
5378 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5379 return -EOPNOTSUPP;
5380
5381 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005382 err = _nfs4_get_security_label(inode, buf, buflen);
5383 trace_nfs4_get_security_label(inode, err);
5384 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005385 &exception);
5386 } while (exception.retry);
5387 return err;
5388}
5389
5390static int _nfs4_do_set_security_label(struct inode *inode,
5391 struct nfs4_label *ilabel,
5392 struct nfs_fattr *fattr,
5393 struct nfs4_label *olabel)
5394{
5395
5396 struct iattr sattr = {0};
5397 struct nfs_server *server = NFS_SERVER(inode);
5398 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005399 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005400 .fh = NFS_FH(inode),
5401 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005402 .server = server,
5403 .bitmask = bitmask,
5404 .label = ilabel,
5405 };
5406 struct nfs_setattrres res = {
5407 .fattr = fattr,
5408 .label = olabel,
5409 .server = server,
5410 };
5411 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5413 .rpc_argp = &arg,
5414 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005415 };
5416 int status;
5417
Jeff Layton12207f62013-11-01 10:49:32 -04005418 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005419
Jeff Layton12207f62013-11-01 10:49:32 -04005420 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005421 if (status)
5422 dprintk("%s failed: %d\n", __func__, status);
5423
5424 return status;
5425}
5426
5427static int nfs4_do_set_security_label(struct inode *inode,
5428 struct nfs4_label *ilabel,
5429 struct nfs_fattr *fattr,
5430 struct nfs4_label *olabel)
5431{
5432 struct nfs4_exception exception = { };
5433 int err;
5434
5435 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005436 err = _nfs4_do_set_security_label(inode, ilabel,
5437 fattr, olabel);
5438 trace_nfs4_set_security_label(inode, err);
5439 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005440 &exception);
5441 } while (exception.retry);
5442 return err;
5443}
5444
5445static int
Al Viro59301222016-05-27 10:19:30 -04005446nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005447{
5448 struct nfs4_label ilabel, *olabel = NULL;
5449 struct nfs_fattr fattr;
5450 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005451 int status;
5452
5453 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5454 return -EOPNOTSUPP;
5455
5456 nfs_fattr_init(&fattr);
5457
5458 ilabel.pi = 0;
5459 ilabel.lfs = 0;
5460 ilabel.label = (char *)buf;
5461 ilabel.len = buflen;
5462
5463 cred = rpc_lookup_cred();
5464 if (IS_ERR(cred))
5465 return PTR_ERR(cred);
5466
5467 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5468 if (IS_ERR(olabel)) {
5469 status = -PTR_ERR(olabel);
5470 goto out;
5471 }
5472
5473 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5474 if (status == 0)
5475 nfs_setsecurity(inode, &fattr, olabel);
5476
5477 nfs4_label_free(olabel);
5478out:
5479 put_rpccred(cred);
5480 return status;
5481}
5482#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5483
5484
Chuck Leverf0920752012-05-21 22:45:41 -04005485static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5486 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005487{
5488 __be32 verf[2];
5489
Chuck Lever2c820d92012-05-21 22:45:33 -04005490 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5491 /* An impossible timestamp guarantees this value
5492 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005493 verf[0] = cpu_to_be32(U32_MAX);
5494 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005495 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005496 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005497 u64 ns = ktime_to_ns(nn->boot_time);
5498
5499 verf[0] = cpu_to_be32(ns >> 32);
5500 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005501 }
Chuck Levercd937102012-03-02 17:14:31 -05005502 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5503}
5504
Jeff Laytona3192682015-06-09 19:43:59 -04005505static int
5506nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005507{
Jeff Laytona3192682015-06-09 19:43:59 -04005508 size_t len;
5509 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005510
Trond Myklebustceb3a162015-01-03 15:16:04 -05005511 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005512 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005513
Jeff Laytona3192682015-06-09 19:43:59 -04005514 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005515 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005516 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5517 1 +
5518 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5519 1;
5520 rcu_read_unlock();
5521
5522 if (len > NFS4_OPAQUE_LIMIT + 1)
5523 return -EINVAL;
5524
5525 /*
5526 * Since this string is allocated at mount time, and held until the
5527 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5528 * about a memory-reclaim deadlock.
5529 */
5530 str = kmalloc(len, GFP_KERNEL);
5531 if (!str)
5532 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005533
Chuck Levere984a552012-09-14 17:24:21 -04005534 rcu_read_lock();
Trond Myklebustf2dd436e2015-10-08 11:33:17 -04005535 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005536 clp->cl_ipaddr,
5537 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5538 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005539 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005540
Jeff Laytona3192682015-06-09 19:43:59 -04005541 clp->cl_owner_id = str;
5542 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005543}
5544
Jeff Layton873e3852015-06-09 19:44:00 -04005545static int
5546nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005547{
Jeff Layton873e3852015-06-09 19:44:00 -04005548 size_t len;
5549 char *str;
5550
5551 len = 10 + 10 + 1 + 10 + 1 +
5552 strlen(nfs4_client_id_uniquifier) + 1 +
5553 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5554
5555 if (len > NFS4_OPAQUE_LIMIT + 1)
5556 return -EINVAL;
5557
5558 /*
5559 * Since this string is allocated at mount time, and held until the
5560 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5561 * about a memory-reclaim deadlock.
5562 */
5563 str = kmalloc(len, GFP_KERNEL);
5564 if (!str)
5565 return -ENOMEM;
5566
Trond Myklebustf2dd436e2015-10-08 11:33:17 -04005567 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005568 clp->rpc_ops->version, clp->cl_minorversion,
5569 nfs4_client_id_uniquifier,
5570 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005571 clp->cl_owner_id = str;
5572 return 0;
5573}
5574
5575static int
5576nfs4_init_uniform_client_string(struct nfs_client *clp)
5577{
Jeff Layton873e3852015-06-09 19:44:00 -04005578 size_t len;
5579 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005580
5581 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005582 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005583
5584 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005585 return nfs4_init_uniquifier_client_string(clp);
5586
5587 len = 10 + 10 + 1 + 10 + 1 +
5588 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5589
5590 if (len > NFS4_OPAQUE_LIMIT + 1)
5591 return -EINVAL;
5592
5593 /*
5594 * Since this string is allocated at mount time, and held until the
5595 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5596 * about a memory-reclaim deadlock.
5597 */
5598 str = kmalloc(len, GFP_KERNEL);
5599 if (!str)
5600 return -ENOMEM;
5601
Trond Myklebustf2dd436e2015-10-08 11:33:17 -04005602 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005603 clp->rpc_ops->version, clp->cl_minorversion,
5604 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005605 clp->cl_owner_id = str;
5606 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005607}
5608
Chuck Lever706cb8d2014-03-12 12:51:47 -04005609/*
5610 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5611 * services. Advertise one based on the address family of the
5612 * clientaddr.
5613 */
5614static unsigned int
5615nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5616{
5617 if (strchr(clp->cl_ipaddr, ':') != NULL)
5618 return scnprintf(buf, len, "tcp6");
5619 else
5620 return scnprintf(buf, len, "tcp");
5621}
5622
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005623static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5624{
5625 struct nfs4_setclientid *sc = calldata;
5626
5627 if (task->tk_status == 0)
5628 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5629}
5630
5631static const struct rpc_call_ops nfs4_setclientid_ops = {
5632 .rpc_call_done = nfs4_setclientid_done,
5633};
5634
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005635/**
5636 * nfs4_proc_setclientid - Negotiate client ID
5637 * @clp: state data structure
5638 * @program: RPC program for NFSv4 callback service
5639 * @port: IP port number for NFS4 callback service
5640 * @cred: RPC credential to use for this call
5641 * @res: where to place the result
5642 *
5643 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5644 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005645int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5646 unsigned short port, struct rpc_cred *cred,
5647 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
5649 nfs4_verifier sc_verifier;
5650 struct nfs4_setclientid setclientid = {
5651 .sc_verifier = &sc_verifier,
5652 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005653 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 };
5655 struct rpc_message msg = {
5656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5657 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005658 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005659 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005661 struct rpc_task *task;
5662 struct rpc_task_setup task_setup_data = {
5663 .rpc_client = clp->cl_rpcclient,
5664 .rpc_message = &msg,
5665 .callback_ops = &nfs4_setclientid_ops,
5666 .callback_data = &setclientid,
5667 .flags = RPC_TASK_TIMEOUT,
5668 };
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005669 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Chuck Leverde734832012-07-11 16:30:50 -04005671 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005672 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005673
5674 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5675 status = nfs4_init_uniform_client_string(clp);
5676 else
Jeff Laytona3192682015-06-09 19:43:59 -04005677 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005678
5679 if (status)
5680 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005681
Chuck Leverde734832012-07-11 16:30:50 -04005682 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005683 setclientid.sc_netid_len =
5684 nfs4_init_callback_netid(clp,
5685 setclientid.sc_netid,
5686 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005687 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005688 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 clp->cl_ipaddr, port >> 8, port & 255);
5690
Jeff Layton3a6bb732015-06-09 19:43:57 -04005691 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005692 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005693 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005694 task = rpc_run_task(&task_setup_data);
5695 if (IS_ERR(task)) {
5696 status = PTR_ERR(task);
5697 goto out;
5698 }
5699 status = task->tk_status;
5700 if (setclientid.sc_cred) {
5701 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5702 put_rpccred(setclientid.sc_cred);
5703 }
5704 rpc_put_task(task);
5705out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005706 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005707 dprintk("NFS reply setclientid: %d\n", status);
5708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709}
5710
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005711/**
5712 * nfs4_proc_setclientid_confirm - Confirm client ID
5713 * @clp: state data structure
5714 * @res: result of a previous SETCLIENTID
5715 * @cred: RPC credential to use for this call
5716 *
5717 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5718 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005719int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005720 struct nfs4_setclientid_res *arg,
5721 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 struct rpc_message msg = {
5724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005725 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005726 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 int status;
5729
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005730 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5731 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5732 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005733 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005734 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04005735 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 return status;
5737}
5738
Trond Myklebustfe650402006-01-03 09:55:18 +01005739struct nfs4_delegreturndata {
5740 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005741 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005742 struct nfs_fh fh;
5743 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005744 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005745 struct {
5746 struct nfs4_layoutreturn_args arg;
5747 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005748 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005749 u32 roc_barrier;
5750 bool roc;
5751 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005752 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005753 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005754 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005755};
5756
Trond Myklebustfe650402006-01-03 09:55:18 +01005757static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5758{
5759 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005760
Trond Myklebust14516c32010-07-31 14:29:06 -04005761 if (!nfs4_sequence_done(task, &data->res.seq_res))
5762 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005763
Trond Myklebustca8acf82013-08-13 10:36:56 -04005764 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005765
5766 /* Handle Layoutreturn errors */
5767 if (data->args.lr_args && task->tk_status != 0) {
5768 switch(data->res.lr_ret) {
5769 default:
5770 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5771 break;
5772 case 0:
5773 data->args.lr_args = NULL;
5774 data->res.lr_res = NULL;
5775 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005776 case -NFS4ERR_OLD_STATEID:
5777 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005778 data->inode))
5779 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005780 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005781 case -NFS4ERR_ADMIN_REVOKED:
5782 case -NFS4ERR_DELEG_REVOKED:
5783 case -NFS4ERR_EXPIRED:
5784 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005785 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5786 case -NFS4ERR_WRONG_CRED:
5787 data->args.lr_args = NULL;
5788 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005789 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005790 }
5791 }
5792
Ricardo Labiaga79708862009-12-07 09:23:21 -05005793 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005794 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005795 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005796 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005797 case -NFS4ERR_ADMIN_REVOKED:
5798 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005799 case -NFS4ERR_EXPIRED:
5800 nfs4_free_revoked_stateid(data->res.server,
5801 data->args.stateid,
5802 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005803 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005804 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005805 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005806 task->tk_status = 0;
5807 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005808 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005809 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5810 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005811 task->tk_status = 0;
5812 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005813 case -NFS4ERR_ACCESS:
5814 if (data->args.bitmask) {
5815 data->args.bitmask = NULL;
5816 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005817 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005818 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005819 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005820 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005821 if (nfs4_async_handle_error(task, data->res.server,
5822 NULL, NULL) == -EAGAIN) {
Trond Myklebust140087fd2017-11-06 15:28:10 -05005823 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005824 }
5825 }
5826 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005827 return;
5828lr_restart:
5829 data->res.lr_ret = 0;
5830out_restart:
5831 task->tk_status = 0;
5832 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005833}
5834
5835static void nfs4_delegreturn_release(void *calldata)
5836{
Peng Tao039b7562014-07-03 13:05:02 +08005837 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005838 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005839
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005840 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005841 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005842 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5843 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005844 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005845 nfs_iput_and_deactive(inode);
5846 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005847 kfree(calldata);
5848}
5849
Andy Adamson938e1012009-04-01 09:22:28 -04005850static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5851{
5852 struct nfs4_delegreturndata *d_data;
5853
5854 d_data = (struct nfs4_delegreturndata *)data;
5855
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005856 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005857 return;
5858
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005859 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005860 &d_data->args.seq_args,
5861 &d_data->res.seq_res,
5862 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005863}
Andy Adamson938e1012009-04-01 09:22:28 -04005864
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005865static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005866 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005867 .rpc_call_done = nfs4_delegreturn_done,
5868 .rpc_release = nfs4_delegreturn_release,
5869};
5870
Trond Myklebuste6f81072008-01-24 18:14:34 -05005871static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005872{
5873 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005874 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005875 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005876 struct rpc_message msg = {
5877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5878 .rpc_cred = cred,
5879 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005880 struct rpc_task_setup task_setup_data = {
5881 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005882 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005883 .callback_ops = &nfs4_delegreturn_ops,
5884 .flags = RPC_TASK_ASYNC,
5885 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005886 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005887
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005888 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005889 if (data == NULL)
5890 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005891 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005892
5893 nfs4_state_protect(server->nfs_client,
5894 NFS_SP4_MACH_CRED_CLEANUP,
5895 &task_setup_data.rpc_client, &msg);
5896
Trond Myklebustfe650402006-01-03 09:55:18 +01005897 data->args.fhandle = &data->fh;
5898 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005899 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005900 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005901 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005902 data->res.fattr = &data->fattr;
5903 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005904 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005905 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005906 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005907 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005908 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005909 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005910 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005911 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005912 if (data->lr.roc) {
5913 data->args.lr_args = &data->lr.arg;
5914 data->res.lr_res = &data->lr.res;
5915 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005916 } else if (data->lr.roc) {
5917 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5918 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005919 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005920
Trond Myklebustc970aa82007-07-14 15:39:59 -04005921 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005922 msg.rpc_argp = &data->args;
5923 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005924 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005925 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005926 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005927 if (!issync)
5928 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005929 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005930 if (status != 0)
5931 goto out;
5932 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005933out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005934 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005935 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936}
5937
Trond Myklebuste6f81072008-01-24 18:14:34 -05005938int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939{
5940 struct nfs_server *server = NFS_SERVER(inode);
5941 struct nfs4_exception exception = { };
5942 int err;
5943 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005944 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005945 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 switch (err) {
5947 case -NFS4ERR_STALE_STATEID:
5948 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 case 0:
5950 return 0;
5951 }
5952 err = nfs4_handle_exception(server, err, &exception);
5953 } while (exception.retry);
5954 return err;
5955}
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5958{
5959 struct inode *inode = state->inode;
5960 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005961 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005962 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005964 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005966 struct nfs_lockt_res res = {
5967 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 };
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005971 .rpc_argp = &arg,
5972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 .rpc_cred = state->owner->so_cred,
5974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 struct nfs4_lock_state *lsp;
5976 int status;
5977
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005978 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005979 status = nfs4_set_lock_state(state, request);
5980 if (status != 0)
5981 goto out;
5982 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005983 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005984 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005986 switch (status) {
5987 case 0:
5988 request->fl_type = F_UNLCK;
5989 break;
5990 case -NFS4ERR_DENIED:
5991 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005993 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005994 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005995out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 return status;
5997}
5998
5999static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6000{
6001 struct nfs4_exception exception = { };
6002 int err;
6003
6004 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006005 err = _nfs4_proc_getlk(state, cmd, request);
6006 trace_nfs4_get_lock(request, state, cmd, err);
6007 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 &exception);
6009 } while (exception.retry);
6010 return err;
6011}
6012
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006013struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006014 struct nfs_locku_args arg;
6015 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006016 struct nfs4_lock_state *lsp;
6017 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006018 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006019 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006020 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006021 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006022};
6023
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006024static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6025 struct nfs_open_context *ctx,
6026 struct nfs4_lock_state *lsp,
6027 struct nfs_seqid *seqid)
6028{
6029 struct nfs4_unlockdata *p;
6030 struct inode *inode = lsp->ls_state->inode;
6031
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006032 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006033 if (p == NULL)
6034 return NULL;
6035 p->arg.fh = NFS_FH(inode);
6036 p->arg.fl = &p->fl;
6037 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006038 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006039 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006040 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006041 /* Ensure we don't close file until we're done freeing locks! */
6042 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006043 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006044 memcpy(&p->fl, fl, sizeof(p->fl));
6045 p->server = NFS_SERVER(inode);
6046 return p;
6047}
6048
Trond Myklebust06f814a2006-01-03 09:55:07 +01006049static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006050{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006051 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006052 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006053 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006054 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006055 put_nfs_open_context(calldata->ctx);
6056 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006057}
6058
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006059static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006060{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006061 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006062
Trond Myklebust14516c32010-07-31 14:29:06 -04006063 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6064 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006065 switch (task->tk_status) {
6066 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006067 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006068 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006069 if (nfs4_update_lock_stateid(calldata->lsp,
6070 &calldata->res.stateid))
6071 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006072 case -NFS4ERR_ADMIN_REVOKED:
6073 case -NFS4ERR_EXPIRED:
6074 nfs4_free_revoked_stateid(calldata->server,
6075 &calldata->arg.stateid,
6076 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006077 case -NFS4ERR_BAD_STATEID:
6078 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006079 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006080 if (!nfs4_stateid_match(&calldata->arg.stateid,
6081 &calldata->lsp->ls_stateid))
6082 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006083 break;
6084 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006085 if (nfs4_async_handle_error(task, calldata->server,
6086 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006087 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006088 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006089 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006090}
6091
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006092static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006093{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006094 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006096 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6097 nfs_async_iocounter_wait(task, calldata->l_ctx))
6098 return;
6099
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006101 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006102 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006103 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006104 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006105 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006106 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006107 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006108 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006109 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006110 &calldata->res.seq_res,
6111 task) != 0)
6112 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006113 return;
6114out_no_action:
6115 task->tk_action = NULL;
6116out_wait:
6117 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118}
6119
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006120static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006121 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006122 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006123 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006124};
6125
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006126static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6127 struct nfs_open_context *ctx,
6128 struct nfs4_lock_state *lsp,
6129 struct nfs_seqid *seqid)
6130{
6131 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006132 struct rpc_message msg = {
6133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6134 .rpc_cred = ctx->cred,
6135 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006136 struct rpc_task_setup task_setup_data = {
6137 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006138 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006139 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006140 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006141 .flags = RPC_TASK_ASYNC,
6142 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006143
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006144 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6145 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6146
Frank Filz137d6ac2007-07-09 15:32:29 -07006147 /* Ensure this is an unlock - when canceling a lock, the
6148 * canceled lock is passed in, and it won't be an unlock.
6149 */
6150 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006151 if (fl->fl_flags & FL_CLOSE)
6152 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006153
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006154 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6155 if (data == NULL) {
6156 nfs_free_seqid(seqid);
6157 return ERR_PTR(-ENOMEM);
6158 }
6159
Chuck Levera9c92d62013-08-09 12:48:18 -04006160 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006161 msg.rpc_argp = &data->arg;
6162 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006163 task_setup_data.callback_data = data;
6164 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006165}
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6168{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006169 struct inode *inode = state->inode;
6170 struct nfs4_state_owner *sp = state->owner;
6171 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006172 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006173 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006174 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006175 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006176 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006177 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Trond Myklebust9b073572006-06-29 16:38:34 -04006179 status = nfs4_set_lock_state(state, request);
6180 /* Unlock _before_ we do the RPC call */
6181 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006182 /* Exclude nfs_delegation_claim_locks() */
6183 mutex_lock(&sp->so_delegreturn_mutex);
6184 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006185 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006186 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006187 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006188 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006189 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006190 }
6191 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006192 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006193 if (status != 0)
6194 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006195 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006196 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006197 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6198 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006199 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6200 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006201 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006202 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006203 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006204 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205 status = PTR_ERR(task);
6206 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006207 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006208 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006209 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006210out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006211 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006212 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006213 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214}
6215
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006216struct nfs4_lockdata {
6217 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006218 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219 struct nfs4_lock_state *lsp;
6220 struct nfs_open_context *ctx;
6221 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006222 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006223 int rpc_status;
6224 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006225 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006226};
6227
6228static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006229 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6230 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231{
6232 struct nfs4_lockdata *p;
6233 struct inode *inode = lsp->ls_state->inode;
6234 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006235 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006236
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006237 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006238 if (p == NULL)
6239 return NULL;
6240
6241 p->arg.fh = NFS_FH(inode);
6242 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006243 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006244 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006245 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006246 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6247 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006248 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006249 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006250 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006251 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006252 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006253 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006255 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006256 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006257 p->ctx = get_nfs_open_context(ctx);
6258 memcpy(&p->fl, fl, sizeof(p->fl));
6259 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006260out_free_seqid:
6261 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006262out_free:
6263 kfree(p);
6264 return NULL;
6265}
6266
6267static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6268{
6269 struct nfs4_lockdata *data = calldata;
6270 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
Harvey Harrison3110ff82008-05-02 13:42:44 -07006272 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006273 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006274 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006275 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006276 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006277 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006278 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006279 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006280 nfs4_stateid_copy(&data->arg.open_stateid,
6281 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006283 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006284 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006285 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006286 nfs4_stateid_copy(&data->arg.lock_stateid,
6287 &data->lsp->ls_stateid);
6288 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006289 if (!nfs4_valid_open_stateid(state)) {
6290 data->rpc_status = -EBADF;
6291 task->tk_action = NULL;
6292 goto out_release_open_seqid;
6293 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006294 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006295 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006296 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006297 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006298 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006299 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006300out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006301 nfs_release_seqid(data->arg.open_seqid);
6302out_release_lock_seqid:
6303 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006304out_wait:
6305 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006306 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006307}
6308
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006309static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6310{
6311 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006312 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006313
Harvey Harrison3110ff82008-05-02 13:42:44 -07006314 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315
Trond Myklebust14516c32010-07-31 14:29:06 -04006316 if (!nfs4_sequence_done(task, &data->res.seq_res))
6317 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006318
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006319 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006320 switch (task->tk_status) {
6321 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006322 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006323 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006324 if (data->arg.new_lock) {
6325 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006326 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006327 rpc_restart_call_prepare(task);
6328 break;
6329 }
6330 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006331 if (data->arg.new_lock_owner != 0) {
6332 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6333 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6334 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6335 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6336 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006337 break;
6338 case -NFS4ERR_BAD_STATEID:
6339 case -NFS4ERR_OLD_STATEID:
6340 case -NFS4ERR_STALE_STATEID:
6341 case -NFS4ERR_EXPIRED:
6342 if (data->arg.new_lock_owner != 0) {
6343 if (!nfs4_stateid_match(&data->arg.open_stateid,
6344 &lsp->ls_state->open_stateid))
6345 rpc_restart_call_prepare(task);
6346 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6347 &lsp->ls_stateid))
6348 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006349 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006350 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351}
6352
6353static void nfs4_lock_release(void *calldata)
6354{
6355 struct nfs4_lockdata *data = calldata;
6356
Harvey Harrison3110ff82008-05-02 13:42:44 -07006357 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006358 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006359 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006360 struct rpc_task *task;
6361 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6362 data->arg.lock_seqid);
6363 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006364 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006365 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006366 } else
6367 nfs_free_seqid(data->arg.lock_seqid);
6368 nfs4_put_lock_state(data->lsp);
6369 put_nfs_open_context(data->ctx);
6370 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006371 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006372}
6373
6374static const struct rpc_call_ops nfs4_lock_ops = {
6375 .rpc_call_prepare = nfs4_lock_prepare,
6376 .rpc_call_done = nfs4_lock_done,
6377 .rpc_release = nfs4_lock_release,
6378};
6379
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006380static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6381{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006382 switch (error) {
6383 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006384 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006385 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006386 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006387 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006388 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006389 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006390 break;
6391 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006392 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006393 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006394 };
6395}
6396
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006397static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006398{
6399 struct nfs4_lockdata *data;
6400 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6403 .rpc_cred = state->owner->so_cred,
6404 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006405 struct rpc_task_setup task_setup_data = {
6406 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006407 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006408 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006409 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006410 .flags = RPC_TASK_ASYNC,
6411 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006412 int ret;
6413
Harvey Harrison3110ff82008-05-02 13:42:44 -07006414 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006415 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006416 fl->fl_u.nfs4_fl.owner,
6417 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006418 if (data == NULL)
6419 return -ENOMEM;
6420 if (IS_SETLKW(cmd))
6421 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006422 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006423 msg.rpc_argp = &data->arg;
6424 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006425 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006426 if (recovery_type > NFS_LOCK_NEW) {
6427 if (recovery_type == NFS_LOCK_RECLAIM)
6428 data->arg.reclaim = NFS_LOCK_RECLAIM;
6429 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006430 } else
6431 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006432 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006433 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006434 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006435 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006436 if (ret == 0) {
6437 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006438 if (ret)
6439 nfs4_handle_setlk_error(data->server, data->lsp,
6440 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006441 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006442 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006443 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006444 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006445 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447}
6448
6449static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6450{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006451 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006452 struct nfs4_exception exception = {
6453 .inode = state->inode,
6454 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006455 int err;
6456
6457 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006458 /* Cache the lock if possible... */
6459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6460 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006461 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006462 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006463 break;
6464 nfs4_handle_exception(server, err, &exception);
6465 } while (exception.retry);
6466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467}
6468
6469static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6470{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006471 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006472 struct nfs4_exception exception = {
6473 .inode = state->inode,
6474 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006475 int err;
6476
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006477 err = nfs4_set_lock_state(state, request);
6478 if (err != 0)
6479 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006480 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006481 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6482 return 0;
6483 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006484 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6486 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006487 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006488 switch (err) {
6489 default:
6490 goto out;
6491 case -NFS4ERR_GRACE:
6492 case -NFS4ERR_DELAY:
6493 nfs4_handle_exception(server, err, &exception);
6494 err = 0;
6495 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006496 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006497out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499}
6500
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006501#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006502static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6503{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006504 struct nfs4_lock_state *lsp;
6505 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006506
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006507 status = nfs4_set_lock_state(state, request);
6508 if (status != 0)
6509 return status;
6510 lsp = request->fl_u.nfs4_fl.owner;
6511 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6512 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6513 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006514 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006515}
6516#endif
6517
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6519{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006520 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006521 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006522 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006523 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
Trond Myklebust01c3b862006-06-29 16:38:39 -04006525 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006526 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006527 if (status < 0)
6528 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006529 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006530 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006531 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006532 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006533 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006534 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006535 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006536 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006537 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006538 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006539 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006540 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006541 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006542 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006543out:
6544 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 return status;
6546}
6547
6548static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6549{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006550 struct nfs4_exception exception = {
6551 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006552 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006553 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 int err;
6555
6556 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006557 err = _nfs4_proc_setlk(state, cmd, request);
6558 if (err == -NFS4ERR_DENIED)
6559 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006561 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 } while (exception.retry);
6563 return err;
6564}
6565
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006566#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6567#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6568
6569static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006570nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6571 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006572{
6573 int status = -ERESTARTSYS;
6574 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6575
6576 while(!signalled()) {
6577 status = nfs4_proc_setlk(state, cmd, request);
6578 if ((status != -EAGAIN) || IS_SETLK(cmd))
6579 break;
6580 freezable_schedule_timeout_interruptible(timeout);
6581 timeout *= 2;
6582 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6583 status = -ERESTARTSYS;
6584 }
6585 return status;
6586}
6587
Jeff Laytona1d617d82016-09-17 18:17:39 -04006588#ifdef CONFIG_NFS_V4_1
6589struct nfs4_lock_waiter {
6590 struct task_struct *task;
6591 struct inode *inode;
6592 struct nfs_lowner *owner;
6593 bool notified;
6594};
6595
6596static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006597nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006598{
6599 int ret;
6600 struct cb_notify_lock_args *cbnl = key;
6601 struct nfs4_lock_waiter *waiter = wait->private;
6602 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6603 *wowner = waiter->owner;
6604
6605 /* Only wake if the callback was for the same owner */
6606 if (lowner->clientid != wowner->clientid ||
6607 lowner->id != wowner->id ||
6608 lowner->s_dev != wowner->s_dev)
6609 return 0;
6610
6611 /* Make sure it's for the right inode */
6612 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6613 return 0;
6614
6615 waiter->notified = true;
6616
6617 /* override "private" so we can use default_wake_function */
6618 wait->private = waiter->task;
6619 ret = autoremove_wake_function(wait, mode, flags, key);
6620 wait->private = waiter;
6621 return ret;
6622}
6623
6624static int
6625nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6626{
6627 int status = -ERESTARTSYS;
6628 unsigned long flags;
6629 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6630 struct nfs_server *server = NFS_SERVER(state->inode);
6631 struct nfs_client *clp = server->nfs_client;
6632 wait_queue_head_t *q = &clp->cl_lock_waitq;
6633 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6634 .id = lsp->ls_seqid.owner_id,
6635 .s_dev = server->s_dev };
6636 struct nfs4_lock_waiter waiter = { .task = current,
6637 .inode = state->inode,
6638 .owner = &owner,
6639 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006640 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006641
6642 /* Don't bother with waitqueue if we don't expect a callback */
6643 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6644 return nfs4_retry_setlk_simple(state, cmd, request);
6645
6646 init_wait(&wait);
6647 wait.private = &waiter;
6648 wait.func = nfs4_wake_lock_waiter;
6649 add_wait_queue(q, &wait);
6650
6651 while(!signalled()) {
6652 status = nfs4_proc_setlk(state, cmd, request);
6653 if ((status != -EAGAIN) || IS_SETLK(cmd))
6654 break;
6655
6656 status = -ERESTARTSYS;
6657 spin_lock_irqsave(&q->lock, flags);
6658 if (waiter.notified) {
6659 spin_unlock_irqrestore(&q->lock, flags);
6660 continue;
6661 }
6662 set_current_state(TASK_INTERRUPTIBLE);
6663 spin_unlock_irqrestore(&q->lock, flags);
6664
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006665 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006666 }
6667
6668 finish_wait(q, &wait);
6669 return status;
6670}
6671#else /* !CONFIG_NFS_V4_1 */
6672static inline int
6673nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6674{
6675 return nfs4_retry_setlk_simple(state, cmd, request);
6676}
6677#endif
6678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679static int
6680nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6681{
6682 struct nfs_open_context *ctx;
6683 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 int status;
6685
6686 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006687 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 state = ctx->state;
6689
Trond Myklebustd9531262009-07-21 19:22:38 -04006690 if (IS_GETLK(cmd)) {
6691 if (state != NULL)
6692 return nfs4_proc_getlk(state, F_GETLK, request);
6693 return 0;
6694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695
6696 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6697 return -EINVAL;
6698
Trond Myklebustd9531262009-07-21 19:22:38 -04006699 if (request->fl_type == F_UNLCK) {
6700 if (state != NULL)
6701 return nfs4_proc_unlck(state, cmd, request);
6702 return 0;
6703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Trond Myklebustd9531262009-07-21 19:22:38 -04006705 if (state == NULL)
6706 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006707
6708 if ((request->fl_flags & FL_POSIX) &&
6709 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6710 return -ENOLCK;
6711
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006712 /*
6713 * Don't rely on the VFS having checked the file open mode,
6714 * since it won't do this for flock() locks.
6715 */
6716 switch (request->fl_type) {
6717 case F_RDLCK:
6718 if (!(filp->f_mode & FMODE_READ))
6719 return -EBADF;
6720 break;
6721 case F_WRLCK:
6722 if (!(filp->f_mode & FMODE_WRITE))
6723 return -EBADF;
6724 }
6725
Jeff Layton1ea67db2016-09-17 18:17:37 -04006726 status = nfs4_set_lock_state(state, request);
6727 if (status != 0)
6728 return status;
6729
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006730 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731}
6732
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006733int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006734{
6735 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006736 int err;
6737
6738 err = nfs4_set_lock_state(state, fl);
6739 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006740 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006741 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006742 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006743}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006744
Trond Myklebustcf470c32012-03-07 13:49:12 -05006745struct nfs_release_lockowner_data {
6746 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006747 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006748 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006749 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006750 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006751};
6752
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006753static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6754{
6755 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006756 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006757 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6758 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006759 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006760 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006761}
6762
6763static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6764{
6765 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006766 struct nfs_server *server = data->server;
6767
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006768 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006769
6770 switch (task->tk_status) {
6771 case 0:
6772 renew_lease(server, data->timestamp);
6773 break;
6774 case -NFS4ERR_STALE_CLIENTID:
6775 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006776 nfs4_schedule_lease_recovery(server->nfs_client);
6777 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006778 case -NFS4ERR_LEASE_MOVED:
6779 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006780 if (nfs4_async_handle_error(task, server,
6781 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006782 rpc_restart_call_prepare(task);
6783 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006784}
6785
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006786static void nfs4_release_lockowner_release(void *calldata)
6787{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006788 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006789 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006790 kfree(calldata);
6791}
6792
Trond Myklebust17280172012-03-11 13:11:00 -04006793static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006794 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6795 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006796 .rpc_release = nfs4_release_lockowner_release,
6797};
6798
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006799static void
6800nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006801{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006802 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006803 struct rpc_message msg = {
6804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6805 };
6806
6807 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006808 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006809
Trond Myklebustcf470c32012-03-07 13:49:12 -05006810 data = kmalloc(sizeof(*data), GFP_NOFS);
6811 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006812 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006813 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006814 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006815 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6816 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6817 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006818
Trond Myklebustcf470c32012-03-07 13:49:12 -05006819 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006820 msg.rpc_resp = &data->res;
6821 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006822 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006823}
6824
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006825#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6826
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006827static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006828 struct dentry *unused, struct inode *inode,
6829 const char *key, const void *buf,
6830 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006831{
Al Viro59301222016-05-27 10:19:30 -04006832 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006833}
6834
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006835static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006836 struct dentry *unused, struct inode *inode,
6837 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006838{
Al Virob2968212016-04-10 20:48:24 -04006839 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006840}
6841
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006842static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006843{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006844 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006845}
6846
David Quigleyc9bccef2013-05-22 12:50:45 -04006847#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006848
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006849static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006850 struct dentry *unused, struct inode *inode,
6851 const char *key, const void *buf,
6852 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006853{
6854 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006855 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006856
6857 return -EOPNOTSUPP;
6858}
6859
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006860static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006861 struct dentry *unused, struct inode *inode,
6862 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006863{
6864 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006865 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006866 return -EOPNOTSUPP;
6867}
6868
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006869static ssize_t
6870nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006871{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006872 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006873
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006874 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6875 len = security_inode_listsecurity(inode, list, list_len);
6876 if (list_len && len > list_len)
6877 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006878 }
6879 return len;
6880}
6881
6882static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6883 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006884 .get = nfs4_xattr_get_nfs4_label,
6885 .set = nfs4_xattr_set_nfs4_label,
6886};
David Quigleyc9bccef2013-05-22 12:50:45 -04006887
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006888#else
6889
6890static ssize_t
6891nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6892{
6893 return 0;
6894}
6895
6896#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006897
Andy Adamson533eb462011-06-13 18:25:56 -04006898/*
6899 * nfs_fhget will use either the mounted_on_fileid or the fileid
6900 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006901static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6902{
Andy Adamson533eb462011-06-13 18:25:56 -04006903 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6904 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6905 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006906 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006907 return;
6908
6909 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006910 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006911 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6912 fattr->nlink = 2;
6913}
6914
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006915static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6916 const struct qstr *name,
6917 struct nfs4_fs_locations *fs_locations,
6918 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006919{
6920 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006921 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006922 struct nfs4_fs_locations_arg args = {
6923 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006924 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006925 .page = page,
6926 .bitmask = bitmask,
6927 };
Benny Halevy22958462009-04-01 09:22:02 -04006928 struct nfs4_fs_locations_res res = {
6929 .fs_locations = fs_locations,
6930 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006931 struct rpc_message msg = {
6932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6933 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006934 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006935 };
6936 int status;
6937
Harvey Harrison3110ff82008-05-02 13:42:44 -07006938 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006939
Chuck Leverc05cefc2017-11-05 15:45:22 -05006940 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6941 bitmask[1] = nfs4_fattr_bitmap[1];
6942
Andy Adamson533eb462011-06-13 18:25:56 -04006943 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6944 * is not supported */
6945 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05006946 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006947 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05006948 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006949
Trond Myklebustc228fd32007-01-13 02:28:11 -05006950 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006951 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006952 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006953 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006954 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006955 return status;
6956}
6957
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006958int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6959 const struct qstr *name,
6960 struct nfs4_fs_locations *fs_locations,
6961 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006962{
6963 struct nfs4_exception exception = { };
6964 int err;
6965 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006966 err = _nfs4_proc_fs_locations(client, dir, name,
6967 fs_locations, page);
6968 trace_nfs4_get_fs_locations(dir, name, err);
6969 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006970 &exception);
6971 } while (exception.retry);
6972 return err;
6973}
6974
Chuck Leverb03d7352013-10-17 14:12:50 -04006975/*
6976 * This operation also signals the server that this client is
6977 * performing migration recovery. The server can stop returning
6978 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6979 * appended to this compound to identify the client ID which is
6980 * performing recovery.
6981 */
6982static int _nfs40_proc_get_locations(struct inode *inode,
6983 struct nfs4_fs_locations *locations,
6984 struct page *page, struct rpc_cred *cred)
6985{
6986 struct nfs_server *server = NFS_SERVER(inode);
6987 struct rpc_clnt *clnt = server->client;
6988 u32 bitmask[2] = {
6989 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6990 };
6991 struct nfs4_fs_locations_arg args = {
6992 .clientid = server->nfs_client->cl_clientid,
6993 .fh = NFS_FH(inode),
6994 .page = page,
6995 .bitmask = bitmask,
6996 .migration = 1, /* skip LOOKUP */
6997 .renew = 1, /* append RENEW */
6998 };
6999 struct nfs4_fs_locations_res res = {
7000 .fs_locations = locations,
7001 .migration = 1,
7002 .renew = 1,
7003 };
7004 struct rpc_message msg = {
7005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7006 .rpc_argp = &args,
7007 .rpc_resp = &res,
7008 .rpc_cred = cred,
7009 };
7010 unsigned long now = jiffies;
7011 int status;
7012
7013 nfs_fattr_init(&locations->fattr);
7014 locations->server = server;
7015 locations->nlocations = 0;
7016
7017 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7018 nfs4_set_sequence_privileged(&args.seq_args);
7019 status = nfs4_call_sync_sequence(clnt, server, &msg,
7020 &args.seq_args, &res.seq_res);
7021 if (status)
7022 return status;
7023
7024 renew_lease(server, now);
7025 return 0;
7026}
7027
7028#ifdef CONFIG_NFS_V4_1
7029
7030/*
7031 * This operation also signals the server that this client is
7032 * performing migration recovery. The server can stop asserting
7033 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7034 * performing this operation is identified in the SEQUENCE
7035 * operation in this compound.
7036 *
7037 * When the client supports GETATTR(fs_locations_info), it can
7038 * be plumbed in here.
7039 */
7040static int _nfs41_proc_get_locations(struct inode *inode,
7041 struct nfs4_fs_locations *locations,
7042 struct page *page, struct rpc_cred *cred)
7043{
7044 struct nfs_server *server = NFS_SERVER(inode);
7045 struct rpc_clnt *clnt = server->client;
7046 u32 bitmask[2] = {
7047 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7048 };
7049 struct nfs4_fs_locations_arg args = {
7050 .fh = NFS_FH(inode),
7051 .page = page,
7052 .bitmask = bitmask,
7053 .migration = 1, /* skip LOOKUP */
7054 };
7055 struct nfs4_fs_locations_res res = {
7056 .fs_locations = locations,
7057 .migration = 1,
7058 };
7059 struct rpc_message msg = {
7060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7061 .rpc_argp = &args,
7062 .rpc_resp = &res,
7063 .rpc_cred = cred,
7064 };
7065 int status;
7066
7067 nfs_fattr_init(&locations->fattr);
7068 locations->server = server;
7069 locations->nlocations = 0;
7070
7071 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7072 nfs4_set_sequence_privileged(&args.seq_args);
7073 status = nfs4_call_sync_sequence(clnt, server, &msg,
7074 &args.seq_args, &res.seq_res);
7075 if (status == NFS4_OK &&
7076 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7077 status = -NFS4ERR_LEASE_MOVED;
7078 return status;
7079}
7080
7081#endif /* CONFIG_NFS_V4_1 */
7082
7083/**
7084 * nfs4_proc_get_locations - discover locations for a migrated FSID
7085 * @inode: inode on FSID that is migrating
7086 * @locations: result of query
7087 * @page: buffer
7088 * @cred: credential to use for this operation
7089 *
7090 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7091 * operation failed, or a negative errno if a local error occurred.
7092 *
7093 * On success, "locations" is filled in, but if the server has
7094 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7095 * asserted.
7096 *
7097 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7098 * from this client that require migration recovery.
7099 */
7100int nfs4_proc_get_locations(struct inode *inode,
7101 struct nfs4_fs_locations *locations,
7102 struct page *page, struct rpc_cred *cred)
7103{
7104 struct nfs_server *server = NFS_SERVER(inode);
7105 struct nfs_client *clp = server->nfs_client;
7106 const struct nfs4_mig_recovery_ops *ops =
7107 clp->cl_mvops->mig_recovery_ops;
7108 struct nfs4_exception exception = { };
7109 int status;
7110
7111 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7112 (unsigned long long)server->fsid.major,
7113 (unsigned long long)server->fsid.minor,
7114 clp->cl_hostname);
7115 nfs_display_fhandle(NFS_FH(inode), __func__);
7116
7117 do {
7118 status = ops->get_locations(inode, locations, page, cred);
7119 if (status != -NFS4ERR_DELAY)
7120 break;
7121 nfs4_handle_exception(server, status, &exception);
7122 } while (exception.retry);
7123 return status;
7124}
7125
Chuck Lever44c99932013-10-17 14:13:30 -04007126/*
7127 * This operation also signals the server that this client is
7128 * performing "lease moved" recovery. The server can stop
7129 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7130 * is appended to this compound to identify the client ID which is
7131 * performing recovery.
7132 */
7133static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7134{
7135 struct nfs_server *server = NFS_SERVER(inode);
7136 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7137 struct rpc_clnt *clnt = server->client;
7138 struct nfs4_fsid_present_arg args = {
7139 .fh = NFS_FH(inode),
7140 .clientid = clp->cl_clientid,
7141 .renew = 1, /* append RENEW */
7142 };
7143 struct nfs4_fsid_present_res res = {
7144 .renew = 1,
7145 };
7146 struct rpc_message msg = {
7147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7148 .rpc_argp = &args,
7149 .rpc_resp = &res,
7150 .rpc_cred = cred,
7151 };
7152 unsigned long now = jiffies;
7153 int status;
7154
7155 res.fh = nfs_alloc_fhandle();
7156 if (res.fh == NULL)
7157 return -ENOMEM;
7158
7159 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7160 nfs4_set_sequence_privileged(&args.seq_args);
7161 status = nfs4_call_sync_sequence(clnt, server, &msg,
7162 &args.seq_args, &res.seq_res);
7163 nfs_free_fhandle(res.fh);
7164 if (status)
7165 return status;
7166
7167 do_renew_lease(clp, now);
7168 return 0;
7169}
7170
7171#ifdef CONFIG_NFS_V4_1
7172
7173/*
7174 * This operation also signals the server that this client is
7175 * performing "lease moved" recovery. The server can stop asserting
7176 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7177 * this operation is identified in the SEQUENCE operation in this
7178 * compound.
7179 */
7180static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7181{
7182 struct nfs_server *server = NFS_SERVER(inode);
7183 struct rpc_clnt *clnt = server->client;
7184 struct nfs4_fsid_present_arg args = {
7185 .fh = NFS_FH(inode),
7186 };
7187 struct nfs4_fsid_present_res res = {
7188 };
7189 struct rpc_message msg = {
7190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7191 .rpc_argp = &args,
7192 .rpc_resp = &res,
7193 .rpc_cred = cred,
7194 };
7195 int status;
7196
7197 res.fh = nfs_alloc_fhandle();
7198 if (res.fh == NULL)
7199 return -ENOMEM;
7200
7201 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7202 nfs4_set_sequence_privileged(&args.seq_args);
7203 status = nfs4_call_sync_sequence(clnt, server, &msg,
7204 &args.seq_args, &res.seq_res);
7205 nfs_free_fhandle(res.fh);
7206 if (status == NFS4_OK &&
7207 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7208 status = -NFS4ERR_LEASE_MOVED;
7209 return status;
7210}
7211
7212#endif /* CONFIG_NFS_V4_1 */
7213
7214/**
7215 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7216 * @inode: inode on FSID to check
7217 * @cred: credential to use for this operation
7218 *
7219 * Server indicates whether the FSID is present, moved, or not
7220 * recognized. This operation is necessary to clear a LEASE_MOVED
7221 * condition for this client ID.
7222 *
7223 * Returns NFS4_OK if the FSID is present on this server,
7224 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7225 * NFS4ERR code if some error occurred on the server, or a
7226 * negative errno if a local failure occurred.
7227 */
7228int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7229{
7230 struct nfs_server *server = NFS_SERVER(inode);
7231 struct nfs_client *clp = server->nfs_client;
7232 const struct nfs4_mig_recovery_ops *ops =
7233 clp->cl_mvops->mig_recovery_ops;
7234 struct nfs4_exception exception = { };
7235 int status;
7236
7237 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7238 (unsigned long long)server->fsid.major,
7239 (unsigned long long)server->fsid.minor,
7240 clp->cl_hostname);
7241 nfs_display_fhandle(NFS_FH(inode), __func__);
7242
7243 do {
7244 status = ops->fsid_present(inode, cred);
7245 if (status != -NFS4ERR_DELAY)
7246 break;
7247 nfs4_handle_exception(server, status, &exception);
7248 } while (exception.retry);
7249 return status;
7250}
7251
Andy Adamson5ec16a82013-08-08 10:57:55 -04007252/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007253 * If 'use_integrity' is true and the state managment nfs_client
7254 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7255 * and the machine credential as per RFC3530bis and RFC5661 Security
7256 * Considerations sections. Otherwise, just use the user cred with the
7257 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007258 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007259static 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 +00007260{
7261 int status;
7262 struct nfs4_secinfo_arg args = {
7263 .dir_fh = NFS_FH(dir),
7264 .name = name,
7265 };
7266 struct nfs4_secinfo_res res = {
7267 .flavors = flavors,
7268 };
7269 struct rpc_message msg = {
7270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7271 .rpc_argp = &args,
7272 .rpc_resp = &res,
7273 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007274 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007275 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007276
7277 if (use_integrity) {
7278 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007279 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7280 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007281 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007282
7283 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007284
7285 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7286 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7287
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007288 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7289 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007290 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007291
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007292 if (cred)
7293 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007294
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007295 return status;
7296}
7297
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007298int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7299 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007300{
7301 struct nfs4_exception exception = { };
7302 int err;
7303 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007304 err = -NFS4ERR_WRONGSEC;
7305
7306 /* try to use integrity protection with machine cred */
7307 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7308 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7309
7310 /*
7311 * if unable to use integrity protection, or SECINFO with
7312 * integrity protection returns NFS4ERR_WRONGSEC (which is
7313 * disallowed by spec, but exists in deployed servers) use
7314 * the current filesystem's rpc_client and the user cred.
7315 */
7316 if (err == -NFS4ERR_WRONGSEC)
7317 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7318
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007319 trace_nfs4_secinfo(dir, name, err);
7320 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007321 &exception);
7322 } while (exception.retry);
7323 return err;
7324}
7325
Andy Adamson557134a2009-04-01 09:21:53 -04007326#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007327/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007328 * Check the exchange flags returned by the server for invalid flags, having
7329 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7330 * DS flags set.
7331 */
7332static int nfs4_check_cl_exchange_flags(u32 flags)
7333{
7334 if (flags & ~EXCHGID4_FLAG_MASK_R)
7335 goto out_inval;
7336 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7337 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7338 goto out_inval;
7339 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7340 goto out_inval;
7341 return NFS_OK;
7342out_inval:
7343 return -NFS4ERR_INVAL;
7344}
7345
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007346static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007347nfs41_same_server_scope(struct nfs41_server_scope *a,
7348 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007349{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007350 if (a->server_scope_sz != b->server_scope_sz)
7351 return false;
7352 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007353}
7354
Andy Adamson02a95de2016-02-05 16:08:37 -05007355static void
7356nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7357{
7358}
7359
7360static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7361 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7362};
7363
Andy Adamson357f54d2010-12-14 10:11:57 -05007364/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007365 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007366 *
7367 * The 4.1 client currently uses the same TCP connection for the
7368 * fore and backchannel.
7369 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007370static
7371int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7372 struct rpc_xprt *xprt,
7373 struct nfs_client *clp,
7374 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007375{
7376 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007377 struct nfs41_bind_conn_to_session_args args = {
7378 .client = clp,
7379 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7380 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007381 struct nfs41_bind_conn_to_session_res res;
7382 struct rpc_message msg = {
7383 .rpc_proc =
7384 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007385 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007386 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007387 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007388 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007389 struct rpc_task_setup task_setup_data = {
7390 .rpc_client = clnt,
7391 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007392 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007393 .rpc_message = &msg,
7394 .flags = RPC_TASK_TIMEOUT,
7395 };
7396 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007397
Trond Myklebust71a097c2015-02-18 09:27:18 -08007398 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7399 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7400 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007401
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007402 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7403 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7404 args.dir = NFS4_CDFC4_FORE;
7405
7406 task = rpc_run_task(&task_setup_data);
7407 if (!IS_ERR(task)) {
7408 status = task->tk_status;
7409 rpc_put_task(task);
7410 } else
7411 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007412 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007413 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007414 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007415 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7416 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007417 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007418 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007419 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007420 dprintk("NFS: %s: Unexpected direction from server\n",
7421 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007422 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007423 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007424 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007425 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7426 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007427 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007428 }
7429 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007430
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007431 return status;
7432}
7433
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007434struct rpc_bind_conn_calldata {
7435 struct nfs_client *clp;
7436 struct rpc_cred *cred;
7437};
7438
7439static int
7440nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7441 struct rpc_xprt *xprt,
7442 void *calldata)
7443{
7444 struct rpc_bind_conn_calldata *p = calldata;
7445
7446 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7447}
7448
7449int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7450{
7451 struct rpc_bind_conn_calldata data = {
7452 .clp = clp,
7453 .cred = cred,
7454 };
7455 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7456 nfs4_proc_bind_conn_to_session_callback, &data);
7457}
7458
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007459/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007460 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7461 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007462 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007463static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7464 .how = SP4_MACH_CRED,
7465 .enforce.u.words = {
7466 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7467 1 << (OP_EXCHANGE_ID - 32) |
7468 1 << (OP_CREATE_SESSION - 32) |
7469 1 << (OP_DESTROY_SESSION - 32) |
7470 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007471 },
7472 .allow.u.words = {
7473 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007474 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007475 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007476 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007477 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007478 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007479 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007480 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007481 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007482 1 << (OP_FREE_STATEID - 32) |
7483 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007484 }
7485};
7486
7487/*
7488 * Select the state protection mode for client `clp' given the server results
7489 * from exchange_id in `sp'.
7490 *
7491 * Returns 0 on success, negative errno otherwise.
7492 */
7493static int nfs4_sp4_select_mode(struct nfs_client *clp,
7494 struct nfs41_state_protection *sp)
7495{
7496 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7497 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7498 1 << (OP_EXCHANGE_ID - 32) |
7499 1 << (OP_CREATE_SESSION - 32) |
7500 1 << (OP_DESTROY_SESSION - 32) |
7501 1 << (OP_DESTROY_CLIENTID - 32)
7502 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007503 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007504 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007505 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007506
7507 if (sp->how == SP4_MACH_CRED) {
7508 /* Print state protect result */
7509 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7510 for (i = 0; i <= LAST_NFS4_OP; i++) {
7511 if (test_bit(i, sp->enforce.u.longs))
7512 dfprintk(MOUNT, " enforce op %d\n", i);
7513 if (test_bit(i, sp->allow.u.longs))
7514 dfprintk(MOUNT, " allow op %d\n", i);
7515 }
7516
7517 /* make sure nothing is on enforce list that isn't supported */
7518 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7519 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7520 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007521 ret = -EINVAL;
7522 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007523 }
7524 }
7525
7526 /*
7527 * Minimal mode - state operations are allowed to use machine
7528 * credential. Note this already happens by default, so the
7529 * client doesn't have to do anything more than the negotiation.
7530 *
7531 * NOTE: we don't care if EXCHANGE_ID is in the list -
7532 * we're already using the machine cred for exchange_id
7533 * and will never use a different cred.
7534 */
7535 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7536 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7537 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7538 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7539 dfprintk(MOUNT, "sp4_mach_cred:\n");
7540 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007541 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007542 } else {
7543 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007544 ret = -EINVAL;
7545 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007546 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007547
7548 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007549 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7550 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007551 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7552 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007553 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007554 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007555
Andrew Elble99ade3c2015-12-02 09:39:51 -05007556 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7557 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007558 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007559 }
7560
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007561 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7562 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7563 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007564 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007565 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007566
7567 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7568 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7569 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007570 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007571 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007572
7573 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7574 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007575 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007576 }
7577
7578 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7579 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007580 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007581 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007582 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007583out:
7584 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007585 return 0;
7586}
7587
Andy Adamson8d89bd72016-09-09 09:22:18 -04007588struct nfs41_exchange_id_data {
7589 struct nfs41_exchange_id_res res;
7590 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591};
7592
Andy Adamson8d89bd72016-09-09 09:22:18 -04007593static void nfs4_exchange_id_release(void *data)
7594{
7595 struct nfs41_exchange_id_data *cdata =
7596 (struct nfs41_exchange_id_data *)data;
7597
Olga Kornievskaia63513232017-03-13 10:36:19 -04007598 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007599 kfree(cdata->res.impl_id);
7600 kfree(cdata->res.server_scope);
7601 kfree(cdata->res.server_owner);
7602 kfree(cdata);
7603}
7604
7605static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007606 .rpc_release = nfs4_exchange_id_release,
7607};
7608
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007609/*
7610 * _nfs4_proc_exchange_id()
7611 *
7612 * Wrapper for EXCHANGE_ID operation.
7613 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007614static struct rpc_task *
7615nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a72016-09-09 09:22:28 -04007616 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007617{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007618 struct rpc_message msg = {
7619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007620 .rpc_cred = cred,
7621 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007622 struct rpc_task_setup task_setup_data = {
7623 .rpc_client = clp->cl_rpcclient,
7624 .callback_ops = &nfs4_exchange_id_call_ops,
7625 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007626 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007627 };
7628 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007629 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630
Elena Reshetova212bf412017-10-20 12:53:38 +03007631 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007632 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007633
Trond Myklebust9c760d12017-07-31 18:38:50 -04007634 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007635 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007636 if (!calldata)
7637 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007638
Trond Myklebustfd405592017-08-01 16:02:47 -04007639 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007640
7641 status = nfs4_init_uniform_client_string(clp);
7642 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007643 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007644
Andy Adamson8d89bd72016-09-09 09:22:18 -04007645 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7646 GFP_NOFS);
7647 status = -ENOMEM;
7648 if (unlikely(calldata->res.server_owner == NULL))
7649 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007650
Andy Adamson8d89bd72016-09-09 09:22:18 -04007651 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007652 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007653 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007654 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007655
Andy Adamson8d89bd72016-09-09 09:22:18 -04007656 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7657 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007658 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007659
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007660 switch (sp4_how) {
7661 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007662 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007663 break;
7664
7665 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007666 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007667 break;
7668
7669 default:
7670 /* unsupported! */
7671 WARN_ON_ONCE(1);
7672 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007673 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007674 }
Andy Adamsonad0849a72016-09-09 09:22:28 -04007675 if (xprt) {
Andy Adamsonad0849a72016-09-09 09:22:28 -04007676 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007677 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007678 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7679 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a72016-09-09 09:22:28 -04007680 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007681 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007682 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7683 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007684#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007685 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007686#endif
7687 msg.rpc_argp = &calldata->args;
7688 msg.rpc_resp = &calldata->res;
7689 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007690
Trond Myklebust9c760d12017-07-31 18:38:50 -04007691 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007692
7693out_impl_id:
7694 kfree(calldata->res.impl_id);
7695out_server_scope:
7696 kfree(calldata->res.server_scope);
7697out_server_owner:
7698 kfree(calldata->res.server_owner);
7699out_calldata:
7700 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007701out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007702 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007703 return ERR_PTR(status);
7704}
7705
7706/*
7707 * _nfs4_proc_exchange_id()
7708 *
7709 * Wrapper for EXCHANGE_ID operation.
7710 */
7711static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7712 u32 sp4_how)
7713{
7714 struct rpc_task *task;
7715 struct nfs41_exchange_id_args *argp;
7716 struct nfs41_exchange_id_res *resp;
7717 int status;
7718
7719 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7720 if (IS_ERR(task))
7721 return PTR_ERR(task);
7722
7723 argp = task->tk_msg.rpc_argp;
7724 resp = task->tk_msg.rpc_resp;
7725 status = task->tk_status;
7726 if (status != 0)
7727 goto out;
7728
7729 status = nfs4_check_cl_exchange_flags(resp->flags);
7730 if (status != 0)
7731 goto out;
7732
7733 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7734 if (status != 0)
7735 goto out;
7736
7737 clp->cl_clientid = resp->clientid;
7738 clp->cl_exchange_flags = resp->flags;
7739 clp->cl_seqid = resp->seqid;
7740 /* Client ID is not confirmed */
7741 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7742 clear_bit(NFS4_SESSION_ESTABLISHED,
7743 &clp->cl_session->session_state);
7744
7745 if (clp->cl_serverscope != NULL &&
7746 !nfs41_same_server_scope(clp->cl_serverscope,
7747 resp->server_scope)) {
7748 dprintk("%s: server_scope mismatch detected\n",
7749 __func__);
7750 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7751 }
7752
7753 swap(clp->cl_serverowner, resp->server_owner);
7754 swap(clp->cl_serverscope, resp->server_scope);
7755 swap(clp->cl_implid, resp->impl_id);
7756
7757 /* Save the EXCHANGE_ID verifier session trunk tests */
7758 memcpy(clp->cl_confirm.data, argp->verifier.data,
7759 sizeof(clp->cl_confirm.data));
7760out:
7761 trace_nfs4_exchange_id(clp, status);
7762 rpc_put_task(task);
7763 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007764}
7765
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007766/*
7767 * nfs4_proc_exchange_id()
7768 *
7769 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7770 *
7771 * Since the clientid has expired, all compounds using sessions
7772 * associated with the stale clientid will be returning
7773 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7774 * be in some phase of session reset.
7775 *
7776 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7777 */
7778int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7779{
7780 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7781 int status;
7782
7783 /* try SP4_MACH_CRED if krb5i/p */
7784 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7785 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007786 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007787 if (!status)
7788 return 0;
7789 }
7790
7791 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007792 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007793}
7794
Andy Adamson04fa2c62016-09-09 09:22:29 -04007795/**
7796 * nfs4_test_session_trunk
7797 *
7798 * This is an add_xprt_test() test function called from
7799 * rpc_clnt_setup_test_and_add_xprt.
7800 *
7801 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7802 * and is dereferrenced in nfs4_exchange_id_release
7803 *
7804 * Upon success, add the new transport to the rpc_clnt
7805 *
7806 * @clnt: struct rpc_clnt to get new transport
7807 * @xprt: the rpc_xprt to test
7808 * @data: call data for _nfs4_proc_exchange_id.
7809 */
7810int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7811 void *data)
7812{
7813 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007814 struct rpc_task *task;
7815 int status;
7816
Andy Adamson04fa2c62016-09-09 09:22:29 -04007817 u32 sp4_how;
7818
7819 dprintk("--> %s try %s\n", __func__,
7820 xprt->address_strings[RPC_DISPLAY_ADDR]);
7821
7822 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7823
7824 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007825 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7826 if (IS_ERR(task))
7827 return PTR_ERR(task);
7828
7829 status = task->tk_status;
7830 if (status == 0)
7831 status = nfs4_detect_session_trunking(adata->clp,
7832 task->tk_msg.rpc_resp, xprt);
7833
7834 rpc_put_task(task);
7835 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007836}
7837EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7838
Trond Myklebust66245532012-05-25 17:18:09 -04007839static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7840 struct rpc_cred *cred)
7841{
7842 struct rpc_message msg = {
7843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7844 .rpc_argp = clp,
7845 .rpc_cred = cred,
7846 };
7847 int status;
7848
7849 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007850 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007851 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007852 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007853 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7854 return status;
7855}
7856
7857static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7858 struct rpc_cred *cred)
7859{
7860 unsigned int loop;
7861 int ret;
7862
7863 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7864 ret = _nfs4_proc_destroy_clientid(clp, cred);
7865 switch (ret) {
7866 case -NFS4ERR_DELAY:
7867 case -NFS4ERR_CLIENTID_BUSY:
7868 ssleep(1);
7869 break;
7870 default:
7871 return ret;
7872 }
7873 }
7874 return 0;
7875}
7876
7877int nfs4_destroy_clientid(struct nfs_client *clp)
7878{
7879 struct rpc_cred *cred;
7880 int ret = 0;
7881
7882 if (clp->cl_mvops->minor_version < 1)
7883 goto out;
7884 if (clp->cl_exchange_flags == 0)
7885 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007886 if (clp->cl_preserve_clid)
7887 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007888 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007889 ret = nfs4_proc_destroy_clientid(clp, cred);
7890 if (cred)
7891 put_rpccred(cred);
7892 switch (ret) {
7893 case 0:
7894 case -NFS4ERR_STALE_CLIENTID:
7895 clp->cl_exchange_flags = 0;
7896 }
7897out:
7898 return ret;
7899}
7900
Andy Adamson2050f0c2009-04-01 09:22:30 -04007901struct nfs4_get_lease_time_data {
7902 struct nfs4_get_lease_time_args *args;
7903 struct nfs4_get_lease_time_res *res;
7904 struct nfs_client *clp;
7905};
7906
7907static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7908 void *calldata)
7909{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007910 struct nfs4_get_lease_time_data *data =
7911 (struct nfs4_get_lease_time_data *)calldata;
7912
7913 dprintk("--> %s\n", __func__);
7914 /* just setup sequence, do not trigger session recovery
7915 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007916 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007917 &data->args->la_seq_args,
7918 &data->res->lr_seq_res,
7919 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007920 dprintk("<-- %s\n", __func__);
7921}
7922
7923/*
7924 * Called from nfs4_state_manager thread for session setup, so don't recover
7925 * from sequence operation or clientid errors.
7926 */
7927static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7928{
7929 struct nfs4_get_lease_time_data *data =
7930 (struct nfs4_get_lease_time_data *)calldata;
7931
7932 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007933 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7934 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007935 switch (task->tk_status) {
7936 case -NFS4ERR_DELAY:
7937 case -NFS4ERR_GRACE:
7938 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7939 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7940 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007941 /* fall through */
7942 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007943 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007944 return;
7945 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007946 dprintk("<-- %s\n", __func__);
7947}
7948
Trond Myklebust17280172012-03-11 13:11:00 -04007949static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007950 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7951 .rpc_call_done = nfs4_get_lease_time_done,
7952};
7953
7954int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7955{
7956 struct rpc_task *task;
7957 struct nfs4_get_lease_time_args args;
7958 struct nfs4_get_lease_time_res res = {
7959 .lr_fsinfo = fsinfo,
7960 };
7961 struct nfs4_get_lease_time_data data = {
7962 .args = &args,
7963 .res = &res,
7964 .clp = clp,
7965 };
7966 struct rpc_message msg = {
7967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7968 .rpc_argp = &args,
7969 .rpc_resp = &res,
7970 };
7971 struct rpc_task_setup task_setup = {
7972 .rpc_client = clp->cl_rpcclient,
7973 .rpc_message = &msg,
7974 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007975 .callback_data = &data,
7976 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007977 };
7978 int status;
7979
Chuck Levera9c92d62013-08-09 12:48:18 -04007980 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007981 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007982 task = rpc_run_task(&task_setup);
7983
7984 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007985 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007986
Anna Schumakerf6148712017-04-07 14:15:23 -04007987 status = task->tk_status;
7988 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007989 return status;
7990}
7991
Andy Adamsonfc931582009-04-01 09:22:31 -04007992/*
7993 * Initialize the values to be used by the client in CREATE_SESSION
7994 * If nfs4_init_session set the fore channel request and response sizes,
7995 * use them.
7996 *
7997 * Set the back channel max_resp_sz_cached to zero to force the client to
7998 * always set csa_cachethis to FALSE because the current implementation
7999 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8000 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008001static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8002 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008003{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008004 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008005 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008006
Andy Adamson18aad3d2013-06-26 12:21:49 -04008007 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8008 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8009
Andy Adamsonfc931582009-04-01 09:22:31 -04008010 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008011 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8012 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008013 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008014 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008015
8016 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008017 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008018 __func__,
8019 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008020 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008021
8022 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008023 args->bc_attrs.max_rqst_sz = max_bc_payload;
8024 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008025 args->bc_attrs.max_resp_sz_cached = 0;
8026 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008027 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008028
8029 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8030 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8031 __func__,
8032 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8033 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8034 args->bc_attrs.max_reqs);
8035}
8036
Trond Myklebust79969dd12015-02-18 11:30:18 -08008037static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8038 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008039{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008040 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008041 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008042
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008043 if (rcvd->max_resp_sz > sent->max_resp_sz)
8044 return -EINVAL;
8045 /*
8046 * Our requested max_ops is the minimum we need; we're not
8047 * prepared to break up compounds into smaller pieces than that.
8048 * So, no point even trying to continue if the server won't
8049 * cooperate:
8050 */
8051 if (rcvd->max_ops < sent->max_ops)
8052 return -EINVAL;
8053 if (rcvd->max_reqs == 0)
8054 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008055 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8056 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008057 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008058}
8059
Trond Myklebust79969dd12015-02-18 11:30:18 -08008060static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8061 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008062{
8063 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008064 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008065
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008066 if (!(res->flags & SESSION4_BACK_CHAN))
8067 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008068 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8069 return -EINVAL;
8070 if (rcvd->max_resp_sz < sent->max_resp_sz)
8071 return -EINVAL;
8072 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8073 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008074 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008075 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008076 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008077 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008078out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008079 return 0;
8080}
Andy Adamson8d353012009-04-01 09:22:32 -04008081
Andy Adamson8d353012009-04-01 09:22:32 -04008082static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008083 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008084{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008085 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008086
Trond Myklebust79969dd12015-02-18 11:30:18 -08008087 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008088 if (ret)
8089 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008090 return nfs4_verify_back_channel_attrs(args, res);
8091}
8092
8093static void nfs4_update_session(struct nfs4_session *session,
8094 struct nfs41_create_session_res *res)
8095{
8096 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008097 /* Mark client id and session as being confirmed */
8098 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8099 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008100 session->flags = res->flags;
8101 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008102 if (res->flags & SESSION4_BACK_CHAN)
8103 memcpy(&session->bc_attrs, &res->bc_attrs,
8104 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008105}
8106
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008107static int _nfs4_proc_create_session(struct nfs_client *clp,
8108 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008109{
8110 struct nfs4_session *session = clp->cl_session;
8111 struct nfs41_create_session_args args = {
8112 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008113 .clientid = clp->cl_clientid,
8114 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008115 .cb_program = NFS4_CALLBACK,
8116 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008117 struct nfs41_create_session_res res;
8118
Andy Adamsonfc931582009-04-01 09:22:31 -04008119 struct rpc_message msg = {
8120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8121 .rpc_argp = &args,
8122 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008123 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008124 };
8125 int status;
8126
Chuck Lever6b26cc82016-05-02 14:40:40 -04008127 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008128 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008129
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008130 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008131 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008132
Trond Myklebustb519d402016-09-11 14:50:01 -04008133 switch (status) {
8134 case -NFS4ERR_STALE_CLIENTID:
8135 case -NFS4ERR_DELAY:
8136 case -ETIMEDOUT:
8137 case -EACCES:
8138 case -EAGAIN:
8139 goto out;
8140 };
8141
8142 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008143 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008144 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008145 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008146 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008147 if (status)
8148 goto out;
8149 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008150 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008151out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008152 return status;
8153}
8154
8155/*
8156 * Issues a CREATE_SESSION operation to the server.
8157 * It is the responsibility of the caller to verify the session is
8158 * expired before calling this routine.
8159 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008160int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008161{
8162 int status;
8163 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008164 struct nfs4_session *session = clp->cl_session;
8165
8166 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8167
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008168 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008169 if (status)
8170 goto out;
8171
Andy Adamsonaacd5532011-11-09 13:58:21 -05008172 /* Init or reset the session slot tables */
8173 status = nfs4_setup_session_slot_tables(session);
8174 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008175 if (status)
8176 goto out;
8177
8178 ptr = (unsigned *)&session->sess_id.data[0];
8179 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8180 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008181out:
8182 dprintk("<-- %s\n", __func__);
8183 return status;
8184}
8185
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008186/*
8187 * Issue the over-the-wire RPC DESTROY_SESSION.
8188 * The caller must serialize access to this routine.
8189 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008190int nfs4_proc_destroy_session(struct nfs4_session *session,
8191 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008192{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008193 struct rpc_message msg = {
8194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8195 .rpc_argp = session,
8196 .rpc_cred = cred,
8197 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008198 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008199
8200 dprintk("--> nfs4_proc_destroy_session\n");
8201
8202 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008203 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8204 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008205
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008206 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008207 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008208
8209 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008210 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008211 "Session has been destroyed regardless...\n", status);
8212
8213 dprintk("<-- nfs4_proc_destroy_session\n");
8214 return status;
8215}
8216
Trond Myklebust7b38c362012-05-23 13:23:31 -04008217/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008218 * Renew the cl_session lease.
8219 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008220struct nfs4_sequence_data {
8221 struct nfs_client *clp;
8222 struct nfs4_sequence_args args;
8223 struct nfs4_sequence_res res;
8224};
8225
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008226static void nfs41_sequence_release(void *data)
8227{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008228 struct nfs4_sequence_data *calldata = data;
8229 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008230
Elena Reshetova212bf412017-10-20 12:53:38 +03008231 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008232 nfs4_schedule_state_renewal(clp);
8233 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008234 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008235}
8236
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008237static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8238{
8239 switch(task->tk_status) {
8240 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008241 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8242 return -EAGAIN;
8243 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008244 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008245 }
8246 return 0;
8247}
8248
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008249static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008250{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008251 struct nfs4_sequence_data *calldata = data;
8252 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008253
Trond Myklebust14516c32010-07-31 14:29:06 -04008254 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8255 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008256
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008257 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008258 if (task->tk_status < 0) {
8259 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008260 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008261 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008262
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008263 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8264 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008265 return;
8266 }
8267 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008268 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008269out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008270 dprintk("<-- %s\n", __func__);
8271}
8272
8273static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8274{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008275 struct nfs4_sequence_data *calldata = data;
8276 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008277 struct nfs4_sequence_args *args;
8278 struct nfs4_sequence_res *res;
8279
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008280 args = task->tk_msg.rpc_argp;
8281 res = task->tk_msg.rpc_resp;
8282
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008283 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008284}
8285
8286static const struct rpc_call_ops nfs41_sequence_ops = {
8287 .rpc_call_done = nfs41_sequence_call_done,
8288 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008289 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008290};
8291
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008292static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8293 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008294 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008295 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008296{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008297 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008298 struct rpc_message msg = {
8299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8300 .rpc_cred = cred,
8301 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008302 struct rpc_task_setup task_setup_data = {
8303 .rpc_client = clp->cl_rpcclient,
8304 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008305 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008306 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008307 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008308 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008309
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008310 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008311 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008312 goto out_err;
8313
8314 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008315 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008316 if (calldata == NULL)
8317 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008318 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008319 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008320 if (is_privileged)
8321 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008322 msg.rpc_argp = &calldata->args;
8323 msg.rpc_resp = &calldata->res;
8324 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008325 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008326
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008327 ret = rpc_run_task(&task_setup_data);
8328 if (IS_ERR(ret))
8329 goto out_err;
8330 return ret;
8331out_put_clp:
8332 nfs_put_client(clp);
8333out_err:
8334 nfs41_release_slot(slot);
8335 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008336}
8337
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008338static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008339{
8340 struct rpc_task *task;
8341 int ret = 0;
8342
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008343 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008344 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008345 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008346 if (IS_ERR(task))
8347 ret = PTR_ERR(task);
8348 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008349 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008350 dprintk("<-- %s status=%d\n", __func__, ret);
8351 return ret;
8352}
8353
8354static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8355{
8356 struct rpc_task *task;
8357 int ret;
8358
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008359 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008360 if (IS_ERR(task)) {
8361 ret = PTR_ERR(task);
8362 goto out;
8363 }
8364 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008365 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008366 ret = task->tk_status;
8367 rpc_put_task(task);
8368out:
8369 dprintk("<-- %s status=%d\n", __func__, ret);
8370 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008371}
8372
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008373struct nfs4_reclaim_complete_data {
8374 struct nfs_client *clp;
8375 struct nfs41_reclaim_complete_args arg;
8376 struct nfs41_reclaim_complete_res res;
8377};
8378
8379static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8380{
8381 struct nfs4_reclaim_complete_data *calldata = data;
8382
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008383 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008384 &calldata->arg.seq_args,
8385 &calldata->res.seq_res,
8386 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008387}
8388
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008389static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8390{
8391 switch(task->tk_status) {
8392 case 0:
8393 case -NFS4ERR_COMPLETE_ALREADY:
8394 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8395 break;
8396 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008397 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008398 /* fall through */
8399 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008400 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008401 case -NFS4ERR_BADSESSION:
8402 case -NFS4ERR_DEADSESSION:
8403 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8404 nfs4_schedule_session_recovery(clp->cl_session,
8405 task->tk_status);
8406 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008407 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008408 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008409 }
8410 return 0;
8411}
8412
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008413static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8414{
8415 struct nfs4_reclaim_complete_data *calldata = data;
8416 struct nfs_client *clp = calldata->clp;
8417 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8418
8419 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008420 if (!nfs41_sequence_done(task, res))
8421 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008422
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008423 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008424 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8425 rpc_restart_call_prepare(task);
8426 return;
8427 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008428 dprintk("<-- %s\n", __func__);
8429}
8430
8431static void nfs4_free_reclaim_complete_data(void *data)
8432{
8433 struct nfs4_reclaim_complete_data *calldata = data;
8434
8435 kfree(calldata);
8436}
8437
8438static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8439 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8440 .rpc_call_done = nfs4_reclaim_complete_done,
8441 .rpc_release = nfs4_free_reclaim_complete_data,
8442};
8443
8444/*
8445 * Issue a global reclaim complete.
8446 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008447static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8448 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008449{
8450 struct nfs4_reclaim_complete_data *calldata;
8451 struct rpc_task *task;
8452 struct rpc_message msg = {
8453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008454 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008455 };
8456 struct rpc_task_setup task_setup_data = {
8457 .rpc_client = clp->cl_rpcclient,
8458 .rpc_message = &msg,
8459 .callback_ops = &nfs4_reclaim_complete_call_ops,
8460 .flags = RPC_TASK_ASYNC,
8461 };
8462 int status = -ENOMEM;
8463
8464 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008465 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008466 if (calldata == NULL)
8467 goto out;
8468 calldata->clp = clp;
8469 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008470
Chuck Levera9c92d62013-08-09 12:48:18 -04008471 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008472 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008473 msg.rpc_argp = &calldata->arg;
8474 msg.rpc_resp = &calldata->res;
8475 task_setup_data.callback_data = calldata;
8476 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008477 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008478 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008479 goto out;
8480 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008481 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008482 if (status == 0)
8483 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008484 rpc_put_task(task);
8485out:
8486 dprintk("<-- %s status=%d\n", __func__, status);
8487 return status;
8488}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008489
8490static void
8491nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8492{
8493 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008494 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008495
8496 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008497 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008498 &lgp->res.seq_res, task);
8499 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008500}
8501
8502static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8503{
8504 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008505
8506 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008507 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008508 dprintk("<-- %s\n", __func__);
8509}
8510
8511static int
8512nfs4_layoutget_handle_exception(struct rpc_task *task,
8513 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8514{
Trond Myklebustee314c22012-10-01 17:25:48 -07008515 struct inode *inode = lgp->args.inode;
8516 struct nfs_server *server = NFS_SERVER(inode);
8517 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008518 int nfs4err = task->tk_status;
8519 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008520 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008521
Boaz Harroshed7e5422014-01-22 20:34:54 +02008522 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008523
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008524 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008525 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008526 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008527
8528 /*
8529 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8530 * on the file. set tk_status to -ENODATA to tell upper layer to
8531 * retry go inband.
8532 */
8533 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008534 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008535 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008536 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008537 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8538 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8539 */
8540 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008541 status = -EOVERFLOW;
8542 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008543 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008544 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008545 * (or clients) writing to the same RAID stripe except when
8546 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008547 *
8548 * Treat it like we would RECALLCONFLICT -- we retry for a little
8549 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008550 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008551 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008552 if (lgp->args.minlength == 0) {
8553 status = -EOVERFLOW;
8554 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008555 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008556 status = -EBUSY;
8557 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008558 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008559 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008560 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008561 case -NFS4ERR_DELEG_REVOKED:
8562 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008563 case -NFS4ERR_EXPIRED:
8564 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008565 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008566 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008567 lo = NFS_I(inode)->layout;
8568 /* If the open stateid was bad, then recover it. */
8569 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008570 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008571 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008572 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008573 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008574 break;
8575 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008576
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008577 /*
8578 * Mark the bad layout state as invalid, then retry
8579 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008580 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008581 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008582 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008583 pnfs_free_lseg_list(&head);
8584 status = -EAGAIN;
8585 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008586 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008587
Trond Myklebust8ac09252017-01-23 22:44:12 -05008588 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008589 err = nfs4_handle_exception(server, nfs4err, exception);
8590 if (!status) {
8591 if (exception->retry)
8592 status = -EAGAIN;
8593 else
8594 status = err;
8595 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008596out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008597 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008598 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008599}
8600
Idan Kedar85541162012-08-02 11:47:10 +03008601static size_t max_response_pages(struct nfs_server *server)
8602{
8603 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8604 return nfs_page_array_len(0, max_resp_sz);
8605}
8606
8607static void nfs4_free_pages(struct page **pages, size_t size)
8608{
8609 int i;
8610
8611 if (!pages)
8612 return;
8613
8614 for (i = 0; i < size; i++) {
8615 if (!pages[i])
8616 break;
8617 __free_page(pages[i]);
8618 }
8619 kfree(pages);
8620}
8621
8622static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8623{
8624 struct page **pages;
8625 int i;
8626
8627 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8628 if (!pages) {
8629 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8630 return NULL;
8631 }
8632
8633 for (i = 0; i < size; i++) {
8634 pages[i] = alloc_page(gfp_flags);
8635 if (!pages[i]) {
8636 dprintk("%s: failed to allocate page\n", __func__);
8637 nfs4_free_pages(pages, size);
8638 return NULL;
8639 }
8640 }
8641
8642 return pages;
8643}
8644
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008645static void nfs4_layoutget_release(void *calldata)
8646{
8647 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008648 struct inode *inode = lgp->args.inode;
8649 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008650 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008651
8652 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008653 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008654 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008655 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008656 put_nfs_open_context(lgp->args.ctx);
8657 kfree(calldata);
8658 dprintk("<-- %s\n", __func__);
8659}
8660
8661static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8662 .rpc_call_prepare = nfs4_layoutget_prepare,
8663 .rpc_call_done = nfs4_layoutget_done,
8664 .rpc_release = nfs4_layoutget_release,
8665};
8666
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008667struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008668nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008669{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008670 struct inode *inode = lgp->args.inode;
8671 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008672 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008673 struct rpc_task *task;
8674 struct rpc_message msg = {
8675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8676 .rpc_argp = &lgp->args,
8677 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008678 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008679 };
8680 struct rpc_task_setup task_setup_data = {
8681 .rpc_client = server->client,
8682 .rpc_message = &msg,
8683 .callback_ops = &nfs4_layoutget_call_ops,
8684 .callback_data = lgp,
8685 .flags = RPC_TASK_ASYNC,
8686 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008687 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008688 struct nfs4_exception exception = {
8689 .inode = inode,
8690 .timeout = *timeout,
8691 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008692 int status = 0;
8693
8694 dprintk("--> %s\n", __func__);
8695
Peng Tao4bd5a982014-11-17 11:05:17 +08008696 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8697 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8698
Idan Kedar85541162012-08-02 11:47:10 +03008699 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8700 if (!lgp->args.layout.pages) {
8701 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008702 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008703 }
8704 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8705
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008706 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008707 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008708 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008709
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008710 task = rpc_run_task(&task_setup_data);
8711 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008712 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008713 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008714 if (status == 0) {
8715 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8716 *timeout = exception.timeout;
8717 }
8718
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008719 trace_nfs4_layoutget(lgp->args.ctx,
8720 &lgp->args.range,
8721 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008722 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008723 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008724
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008725 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8726 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008727 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008728 rpc_put_task(task);
8729 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008730 if (status)
8731 return ERR_PTR(status);
8732 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008733}
8734
Benny Halevycbe82602011-05-22 19:52:37 +03008735static void
8736nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8737{
8738 struct nfs4_layoutreturn *lrp = calldata;
8739
8740 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008741 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008742 &lrp->args.seq_args,
8743 &lrp->res.seq_res,
8744 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008745}
8746
8747static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8748{
8749 struct nfs4_layoutreturn *lrp = calldata;
8750 struct nfs_server *server;
8751
8752 dprintk("--> %s\n", __func__);
8753
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008754 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008755 return;
8756
8757 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008758 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008759 case -NFS4ERR_OLD_STATEID:
8760 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8761 lrp->args.inode))
8762 goto out_restart;
8763 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008764 default:
8765 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008766 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008767 case 0:
8768 break;
8769 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008770 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008771 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008772 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008773 }
Benny Halevycbe82602011-05-22 19:52:37 +03008774 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008775 return;
8776out_restart:
8777 task->tk_status = 0;
8778 nfs4_sequence_free_slot(&lrp->res.seq_res);
8779 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008780}
8781
8782static void nfs4_layoutreturn_release(void *calldata)
8783{
8784 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008785 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008786
8787 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008788 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008789 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008790 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008791 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8792 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008793 pnfs_put_layout_hdr(lrp->args.layout);
8794 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008795 kfree(calldata);
8796 dprintk("<-- %s\n", __func__);
8797}
8798
8799static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8800 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8801 .rpc_call_done = nfs4_layoutreturn_done,
8802 .rpc_release = nfs4_layoutreturn_release,
8803};
8804
Peng Tao6c166052014-11-17 09:30:40 +08008805int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008806{
8807 struct rpc_task *task;
8808 struct rpc_message msg = {
8809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8810 .rpc_argp = &lrp->args,
8811 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008812 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008813 };
8814 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04008815 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008816 .rpc_message = &msg,
8817 .callback_ops = &nfs4_layoutreturn_call_ops,
8818 .callback_data = lrp,
8819 };
Peng Tao6c166052014-11-17 09:30:40 +08008820 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008821
Andrew Elble99ade3c2015-12-02 09:39:51 -05008822 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8823 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8824 &task_setup_data.rpc_client, &msg);
8825
Benny Halevycbe82602011-05-22 19:52:37 +03008826 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008827 if (!sync) {
8828 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8829 if (!lrp->inode) {
8830 nfs4_layoutreturn_release(lrp);
8831 return -EAGAIN;
8832 }
8833 task_setup_data.flags |= RPC_TASK_ASYNC;
8834 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008835 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008836 task = rpc_run_task(&task_setup_data);
8837 if (IS_ERR(task))
8838 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008839 if (sync)
8840 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008841 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008842 dprintk("<-- %s status=%d\n", __func__, status);
8843 rpc_put_task(task);
8844 return status;
8845}
8846
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008847static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008848_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8849 struct pnfs_device *pdev,
8850 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008851{
8852 struct nfs4_getdeviceinfo_args args = {
8853 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008854 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8855 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008856 };
8857 struct nfs4_getdeviceinfo_res res = {
8858 .pdev = pdev,
8859 };
8860 struct rpc_message msg = {
8861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8862 .rpc_argp = &args,
8863 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008864 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008865 };
8866 int status;
8867
8868 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008869 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008870 if (res.notification & ~args.notify_types)
8871 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008872 if (res.notification != args.notify_types)
8873 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008874
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008875 dprintk("<-- %s status=%d\n", __func__, status);
8876
8877 return status;
8878}
8879
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008880int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8881 struct pnfs_device *pdev,
8882 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008883{
8884 struct nfs4_exception exception = { };
8885 int err;
8886
8887 do {
8888 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008889 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008890 &exception);
8891 } while (exception.retry);
8892 return err;
8893}
8894EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8895
Andy Adamson863a3c62011-03-23 13:27:54 +00008896static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8897{
8898 struct nfs4_layoutcommit_data *data = calldata;
8899 struct nfs_server *server = NFS_SERVER(data->args.inode);
8900
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008901 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008902 &data->args.seq_args,
8903 &data->res.seq_res,
8904 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008905}
8906
8907static void
8908nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8909{
8910 struct nfs4_layoutcommit_data *data = calldata;
8911 struct nfs_server *server = NFS_SERVER(data->args.inode);
8912
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008913 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008914 return;
8915
8916 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008917 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8918 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8919 case -NFS4ERR_BADLAYOUT: /* no layout */
8920 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008921 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008922 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008923 break;
8924 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008925 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008926 rpc_restart_call_prepare(task);
8927 return;
8928 }
8929 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008930}
8931
8932static void nfs4_layoutcommit_release(void *calldata)
8933{
8934 struct nfs4_layoutcommit_data *data = calldata;
8935
Andy Adamsondb29c082011-07-30 20:52:38 -04008936 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008937 nfs_post_op_update_inode_force_wcc(data->args.inode,
8938 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008939 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008940 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008941 kfree(data);
8942}
8943
8944static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8945 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8946 .rpc_call_done = nfs4_layoutcommit_done,
8947 .rpc_release = nfs4_layoutcommit_release,
8948};
8949
8950int
Andy Adamsonef311532011-03-12 02:58:10 -05008951nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008952{
8953 struct rpc_message msg = {
8954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8955 .rpc_argp = &data->args,
8956 .rpc_resp = &data->res,
8957 .rpc_cred = data->cred,
8958 };
8959 struct rpc_task_setup task_setup_data = {
8960 .task = &data->task,
8961 .rpc_client = NFS_CLIENT(data->args.inode),
8962 .rpc_message = &msg,
8963 .callback_ops = &nfs4_layoutcommit_ops,
8964 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008965 };
8966 struct rpc_task *task;
8967 int status = 0;
8968
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008969 dprintk("NFS: initiating layoutcommit call. sync %d "
8970 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008971 data->args.lastbytewritten,
8972 data->args.inode->i_ino);
8973
Trond Myklebust472e2592015-02-05 16:50:30 -05008974 if (!sync) {
8975 data->inode = nfs_igrab_and_active(data->args.inode);
8976 if (data->inode == NULL) {
8977 nfs4_layoutcommit_release(data);
8978 return -EAGAIN;
8979 }
8980 task_setup_data.flags = RPC_TASK_ASYNC;
8981 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008982 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008983 task = rpc_run_task(&task_setup_data);
8984 if (IS_ERR(task))
8985 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008986 if (sync)
8987 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008988 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008989 dprintk("%s: status %d\n", __func__, status);
8990 rpc_put_task(task);
8991 return status;
8992}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008993
Andy Adamson97431202013-08-08 10:57:56 -04008994/**
8995 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8996 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8997 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008998static int
8999_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009000 struct nfs_fsinfo *info,
9001 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009002{
9003 struct nfs41_secinfo_no_name_args args = {
9004 .style = SECINFO_STYLE_CURRENT_FH,
9005 };
9006 struct nfs4_secinfo_res res = {
9007 .flavors = flavors,
9008 };
9009 struct rpc_message msg = {
9010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9011 .rpc_argp = &args,
9012 .rpc_resp = &res,
9013 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009014 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009015 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009016 int status;
9017
9018 if (use_integrity) {
9019 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009020 cred = nfs4_get_clid_cred(server->nfs_client);
9021 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009022 }
9023
9024 dprintk("--> %s\n", __func__);
9025 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9026 &res.seq_res, 0);
9027 dprintk("<-- %s status=%d\n", __func__, status);
9028
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009029 if (cred)
9030 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009031
9032 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009033}
9034
9035static int
9036nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9037 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9038{
9039 struct nfs4_exception exception = { };
9040 int err;
9041 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009042 /* first try using integrity protection */
9043 err = -NFS4ERR_WRONGSEC;
9044
9045 /* try to use integrity protection with machine cred */
9046 if (_nfs4_is_integrity_protected(server->nfs_client))
9047 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9048 flavors, true);
9049
9050 /*
9051 * if unable to use integrity protection, or SECINFO with
9052 * integrity protection returns NFS4ERR_WRONGSEC (which is
9053 * disallowed by spec, but exists in deployed servers) use
9054 * the current filesystem's rpc_client and the user cred.
9055 */
9056 if (err == -NFS4ERR_WRONGSEC)
9057 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9058 flavors, false);
9059
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009060 switch (err) {
9061 case 0:
9062 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009063 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009064 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009065 default:
9066 err = nfs4_handle_exception(server, err, &exception);
9067 }
9068 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009069out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009070 return err;
9071}
9072
9073static int
9074nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9075 struct nfs_fsinfo *info)
9076{
9077 int err;
9078 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009079 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009080 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009081 struct nfs4_secinfo4 *secinfo;
9082 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009083
9084 page = alloc_page(GFP_KERNEL);
9085 if (!page) {
9086 err = -ENOMEM;
9087 goto out;
9088 }
9089
9090 flavors = page_address(page);
9091 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9092
9093 /*
9094 * Fall back on "guess and check" method if
9095 * the server doesn't support SECINFO_NO_NAME
9096 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009097 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009098 err = nfs4_find_root_sec(server, fhandle, info);
9099 goto out_freepage;
9100 }
9101 if (err)
9102 goto out_freepage;
9103
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009104 for (i = 0; i < flavors->num_flavors; i++) {
9105 secinfo = &flavors->flavors[i];
9106
9107 switch (secinfo->flavor) {
9108 case RPC_AUTH_NULL:
9109 case RPC_AUTH_UNIX:
9110 case RPC_AUTH_GSS:
9111 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9112 &secinfo->flavor_info);
9113 break;
9114 default:
9115 flavor = RPC_AUTH_MAXFLAVOR;
9116 break;
9117 }
9118
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009119 if (!nfs_auth_info_match(&server->auth_info, flavor))
9120 flavor = RPC_AUTH_MAXFLAVOR;
9121
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009122 if (flavor != RPC_AUTH_MAXFLAVOR) {
9123 err = nfs4_lookup_root_sec(server, fhandle,
9124 info, flavor);
9125 if (!err)
9126 break;
9127 }
9128 }
9129
9130 if (flavor == RPC_AUTH_MAXFLAVOR)
9131 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009132
9133out_freepage:
9134 put_page(page);
9135 if (err == -EACCES)
9136 return -EPERM;
9137out:
9138 return err;
9139}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009140
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009141static int _nfs41_test_stateid(struct nfs_server *server,
9142 nfs4_stateid *stateid,
9143 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009144{
9145 int status;
9146 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009147 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009148 };
9149 struct nfs41_test_stateid_res res;
9150 struct rpc_message msg = {
9151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9152 .rpc_argp = &args,
9153 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009154 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009155 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009156 struct rpc_clnt *rpc_client = server->client;
9157
9158 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9159 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009160
Chuck Lever38527b12012-07-11 16:30:23 -04009161 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009162 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009163 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009164 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009165 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009166 if (status != NFS_OK) {
9167 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009168 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009169 }
9170 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009171 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009172}
9173
Trond Myklebust43912bb2016-09-22 13:38:56 -04009174static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9175 int err, struct nfs4_exception *exception)
9176{
9177 exception->retry = 0;
9178 switch(err) {
9179 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009180 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009181 nfs4_handle_exception(server, err, exception);
9182 break;
9183 case -NFS4ERR_BADSESSION:
9184 case -NFS4ERR_BADSLOT:
9185 case -NFS4ERR_BAD_HIGH_SLOT:
9186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9187 case -NFS4ERR_DEADSESSION:
9188 nfs4_do_handle_exception(server, err, exception);
9189 }
9190}
9191
Chuck Lever38527b12012-07-11 16:30:23 -04009192/**
9193 * nfs41_test_stateid - perform a TEST_STATEID operation
9194 *
9195 * @server: server / transport on which to perform the operation
9196 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009197 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009198 *
9199 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9200 * Otherwise a negative NFS4ERR value is returned if the operation
9201 * failed or the state ID is not currently valid.
9202 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009203static int nfs41_test_stateid(struct nfs_server *server,
9204 nfs4_stateid *stateid,
9205 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009206{
9207 struct nfs4_exception exception = { };
9208 int err;
9209 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009210 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009211 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009212 } while (exception.retry);
9213 return err;
9214}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009215
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009216struct nfs_free_stateid_data {
9217 struct nfs_server *server;
9218 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009219 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009220};
9221
9222static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9223{
9224 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009225 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009226 &data->args.seq_args,
9227 &data->res.seq_res,
9228 task);
9229}
9230
9231static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9232{
9233 struct nfs_free_stateid_data *data = calldata;
9234
9235 nfs41_sequence_done(task, &data->res.seq_res);
9236
9237 switch (task->tk_status) {
9238 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009239 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009240 rpc_restart_call_prepare(task);
9241 }
9242}
9243
9244static void nfs41_free_stateid_release(void *calldata)
9245{
9246 kfree(calldata);
9247}
9248
Trond Myklebust17f26b12013-08-21 15:48:42 -04009249static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009250 .rpc_call_prepare = nfs41_free_stateid_prepare,
9251 .rpc_call_done = nfs41_free_stateid_done,
9252 .rpc_release = nfs41_free_stateid_release,
9253};
9254
9255static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009256 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009257 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009258 bool privileged)
9259{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009260 struct rpc_message msg = {
9261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009262 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009263 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009264 struct rpc_task_setup task_setup = {
9265 .rpc_client = server->client,
9266 .rpc_message = &msg,
9267 .callback_ops = &nfs41_free_stateid_ops,
9268 .flags = RPC_TASK_ASYNC,
9269 };
9270 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009271
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009272 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9273 &task_setup.rpc_client, &msg);
9274
Chuck Lever38527b12012-07-11 16:30:23 -04009275 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009276 data = kmalloc(sizeof(*data), GFP_NOFS);
9277 if (!data)
9278 return ERR_PTR(-ENOMEM);
9279 data->server = server;
9280 nfs4_stateid_copy(&data->args.stateid, stateid);
9281
9282 task_setup.callback_data = data;
9283
9284 msg.rpc_argp = &data->args;
9285 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009286 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009287 if (privileged)
9288 nfs4_set_sequence_privileged(&data->args.seq_args);
9289
9290 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009291}
9292
Chuck Lever38527b12012-07-11 16:30:23 -04009293/**
9294 * nfs41_free_stateid - perform a FREE_STATEID operation
9295 *
9296 * @server: server / transport on which to perform the operation
9297 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009298 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009299 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009300 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009301 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009302 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009303static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009304 const nfs4_stateid *stateid,
9305 struct rpc_cred *cred,
9306 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009307{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009308 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009309
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009310 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009311 if (IS_ERR(task))
9312 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009313 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009314 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009315}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009316
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009317static void
9318nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009319{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009320 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009321
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009322 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009323 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009324}
9325
Trond Myklebust36281ca2012-03-04 18:13:56 -05009326static bool nfs41_match_stateid(const nfs4_stateid *s1,
9327 const nfs4_stateid *s2)
9328{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009329 if (s1->type != s2->type)
9330 return false;
9331
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009332 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009333 return false;
9334
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009335 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009336 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009337
Anna Schumaker045c5512017-01-11 16:59:48 -05009338 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009339}
9340
Andy Adamson557134a2009-04-01 09:21:53 -04009341#endif /* CONFIG_NFS_V4_1 */
9342
Trond Myklebust36281ca2012-03-04 18:13:56 -05009343static bool nfs4_match_stateid(const nfs4_stateid *s1,
9344 const nfs4_stateid *s2)
9345{
Trond Myklebustf597c532012-03-04 18:13:56 -05009346 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009347}
9348
9349
Trond Myklebust17280172012-03-11 13:11:00 -04009350static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009351 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009352 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353 .recover_open = nfs4_open_reclaim,
9354 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009355 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009356 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357};
9358
Andy Adamson591d71c2009-04-01 09:22:47 -04009359#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009360static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009361 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9362 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9363 .recover_open = nfs4_open_reclaim,
9364 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009365 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009366 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009367 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009368};
9369#endif /* CONFIG_NFS_V4_1 */
9370
Trond Myklebust17280172012-03-11 13:11:00 -04009371static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009372 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009373 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009374 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009376 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377};
9378
Andy Adamson591d71c2009-04-01 09:22:47 -04009379#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009380static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009381 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9382 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009383 .recover_open = nfs41_open_expired,
9384 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009385 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009386};
9387#endif /* CONFIG_NFS_V4_1 */
9388
Trond Myklebust17280172012-03-11 13:11:00 -04009389static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009390 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009391 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009392 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009393};
9394
9395#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009396static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009397 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009398 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009399 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009400};
9401#endif
9402
Chuck Leverec011fe2013-10-17 14:12:39 -04009403static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009404 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009405 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009406};
9407
9408#if defined(CONFIG_NFS_V4_1)
9409static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009410 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009411 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009412};
9413#endif /* CONFIG_NFS_V4_1 */
9414
Trond Myklebust97dc1352010-06-16 09:52:26 -04009415static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9416 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009417 .init_caps = NFS_CAP_READDIRPLUS
9418 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009419 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009420 .init_client = nfs40_init_client,
9421 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009422 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009423 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009424 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009425 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009426 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009427 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009428 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9429 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9430 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009431 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009432};
9433
9434#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009435static struct nfs_seqid *
9436nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9437{
9438 return NULL;
9439}
9440
Trond Myklebust97dc1352010-06-16 09:52:26 -04009441static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9442 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009443 .init_caps = NFS_CAP_READDIRPLUS
9444 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009445 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009446 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009447 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009448 .init_client = nfs41_init_client,
9449 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009450 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009451 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009452 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009453 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009454 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009455 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009456 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009457 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9458 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9459 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009460 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009461};
9462#endif
9463
Steve Dickson42c2c422013-05-22 12:50:38 -04009464#if defined(CONFIG_NFS_V4_2)
9465static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9466 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009467 .init_caps = NFS_CAP_READDIRPLUS
9468 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009469 | NFS_CAP_POSIX_LOCK
9470 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009471 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009472 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009473 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009474 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009475 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009476 | NFS_CAP_LAYOUTSTATS
9477 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009478 .init_client = nfs41_init_client,
9479 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009480 .match_stateid = nfs41_match_stateid,
9481 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009482 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009483 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009484 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009485 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009486 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009487 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9488 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9489 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009490 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009491};
9492#endif
9493
Trond Myklebust97dc1352010-06-16 09:52:26 -04009494const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9495 [0] = &nfs_v4_0_minor_ops,
9496#if defined(CONFIG_NFS_V4_1)
9497 [1] = &nfs_v4_1_minor_ops,
9498#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009499#if defined(CONFIG_NFS_V4_2)
9500 [2] = &nfs_v4_2_minor_ops,
9501#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009502};
9503
Trond Myklebust13997822016-07-24 17:10:52 -04009504static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009505{
9506 ssize_t error, error2;
9507
9508 error = generic_listxattr(dentry, list, size);
9509 if (error < 0)
9510 return error;
9511 if (list) {
9512 list += error;
9513 size -= error;
9514 }
9515
9516 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9517 if (error2 < 0)
9518 return error2;
9519 return error + error2;
9520}
9521
Trond Myklebust17f26b12013-08-21 15:48:42 -04009522static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009523 .create = nfs_create,
9524 .lookup = nfs_lookup,
9525 .atomic_open = nfs_atomic_open,
9526 .link = nfs_link,
9527 .unlink = nfs_unlink,
9528 .symlink = nfs_symlink,
9529 .mkdir = nfs_mkdir,
9530 .rmdir = nfs_rmdir,
9531 .mknod = nfs_mknod,
9532 .rename = nfs_rename,
9533 .permission = nfs_permission,
9534 .getattr = nfs_getattr,
9535 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009536 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009537};
9538
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009539static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009540 .permission = nfs_permission,
9541 .getattr = nfs_getattr,
9542 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009543 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009544};
9545
David Howells509de812006-08-22 20:06:11 -04009546const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547 .version = 4, /* protocol version */
9548 .dentry_ops = &nfs4_dentry_operations,
9549 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009550 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009551 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009553 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009554 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 .getattr = nfs4_proc_getattr,
9556 .setattr = nfs4_proc_setattr,
9557 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009558 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 .access = nfs4_proc_access,
9560 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 .create = nfs4_proc_create,
9562 .remove = nfs4_proc_remove,
9563 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009564 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009566 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009567 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009568 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 .link = nfs4_proc_link,
9570 .symlink = nfs4_proc_symlink,
9571 .mkdir = nfs4_proc_mkdir,
9572 .rmdir = nfs4_proc_remove,
9573 .readdir = nfs4_proc_readdir,
9574 .mknod = nfs4_proc_mknod,
9575 .statfs = nfs4_proc_statfs,
9576 .fsinfo = nfs4_proc_fsinfo,
9577 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009578 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009580 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009581 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009582 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009584 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009586 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009587 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009589 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009590 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009591 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009592 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009593 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009594 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009595 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009596 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009597 .create_server = nfs4_create_server,
9598 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599};
9600
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009601static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009602 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009603 .list = nfs4_xattr_list_nfs4_acl,
9604 .get = nfs4_xattr_get_nfs4_acl,
9605 .set = nfs4_xattr_set_nfs4_acl,
9606};
9607
9608const struct xattr_handler *nfs4_xattr_handlers[] = {
9609 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009610#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9611 &nfs4_xattr_nfs4_label_handler,
9612#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009613 NULL
9614};
9615
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616/*
9617 * Local variables:
9618 * c-basic-offset: 8
9619 * End:
9620 */