blob: a0f16c8c5ebd80c688207baefb09405ae636a33f [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>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static 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 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
101 struct rpc_cred *cred,
102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
105 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
107 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400280 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700281 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000284 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
286 return;
287 }
288
289 readdir->cookie = 0;
290 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
291 if (cookie == 2)
292 return;
293
294 /*
295 * NFSv4 servers do not return entries for '.' and '..'
296 * Therefore, we fake these entries here. We let '.'
297 * have cookie 0 and '..' have cookie 1. Note that
298 * when talking to the server, we always send cookie 0
299 * instead of 1 or 2.
300 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800301 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (cookie == 0) {
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_one; /* cookie, second word */
307 *p++ = xdr_one; /* entry len */
308 memcpy(p, ".\0\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400311 *p++ = htonl(attrs); /* bitmap */
312 *p++ = htonl(12); /* attribute buffer length */
313 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000314 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 *p++ = xdr_one; /* next */
318 *p++ = xdr_zero; /* cookie, first word */
319 *p++ = xdr_two; /* cookie, second word */
320 *p++ = xdr_two; /* entry len */
321 memcpy(p, "..\0\0", 4); /* entry */
322 p++;
323 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400324 *p++ = htonl(attrs); /* bitmap */
325 *p++ = htonl(12); /* attribute buffer length */
326 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000327 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 readdir->pgbase = (char *)p - (char *)start;
330 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800331 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Trond Myklebust26d36302016-09-22 13:39:05 -0400334static void nfs4_test_and_free_stateid(struct nfs_server *server,
335 nfs4_stateid *stateid,
336 struct rpc_cred *cred)
337{
338 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
339
340 ops->test_and_free_expired(server, stateid, cred);
341}
342
343static void __nfs4_free_revoked_stateid(struct nfs_server *server,
344 nfs4_stateid *stateid,
345 struct rpc_cred *cred)
346{
347 stateid->type = NFS4_REVOKED_STATEID_TYPE;
348 nfs4_test_and_free_stateid(server, stateid, cred);
349}
350
351static void nfs4_free_revoked_stateid(struct nfs_server *server,
352 const nfs4_stateid *stateid,
353 struct rpc_cred *cred)
354{
355 nfs4_stateid tmp;
356
357 nfs4_stateid_copy(&tmp, stateid);
358 __nfs4_free_revoked_stateid(server, &tmp, cred);
359}
360
NeilBrown8478eaa2014-09-18 16:09:27 +1000361static long nfs4_update_delay(long *timeout)
362{
363 long ret;
364 if (!timeout)
365 return NFS4_POLL_RETRY_MAX;
366 if (*timeout <= 0)
367 *timeout = NFS4_POLL_RETRY_MIN;
368 if (*timeout > NFS4_POLL_RETRY_MAX)
369 *timeout = NFS4_POLL_RETRY_MAX;
370 ret = *timeout;
371 *timeout <<= 1;
372 return ret;
373}
374
Trond Myklebust65de8722008-12-23 15:21:44 -0500375static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376{
377 int res = 0;
378
379 might_sleep();
380
NeilBrown8478eaa2014-09-18 16:09:27 +1000381 freezable_schedule_timeout_killable_unsafe(
382 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 if (fatal_signal_pending(current))
384 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 return res;
386}
387
388/* This is the error handling routine for processes that are allowed
389 * to sleep.
390 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400391static int nfs4_do_handle_exception(struct nfs_server *server,
392 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500393{
394 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500395 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400396 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 int ret = errorcode;
399
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400400 exception->delay = 0;
401 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400403
404 if (stateid == NULL && state != NULL)
405 stateid = &state->stateid;
406
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 switch(errorcode) {
408 case 0:
409 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400410 case -NFS4ERR_BADHANDLE:
411 case -ESTALE:
412 if (inode != NULL && S_ISREG(inode->i_mode))
413 pnfs_destroy_layout(NFS_I(inode));
414 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400415 case -NFS4ERR_DELEG_REVOKED:
416 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400417 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400418 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400419 if (inode != NULL && stateid != NULL) {
420 nfs_inode_find_state_and_recover(inode,
421 stateid);
422 goto wait_on_recovery;
423 }
424 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400425 if (inode) {
426 int err;
427
428 err = nfs_async_inode_return_delegation(inode,
429 stateid);
430 if (err == 0)
431 goto wait_on_recovery;
432 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
433 exception->retry = 1;
434 break;
435 }
436 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500437 if (state == NULL)
438 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400439 ret = nfs4_schedule_stateid_recovery(server, state);
440 if (ret < 0)
441 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500442 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500443 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500444 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500445 nfs4_schedule_lease_recovery(clp);
446 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400447 case -NFS4ERR_MOVED:
448 ret = nfs4_schedule_migration_recovery(server);
449 if (ret < 0)
450 break;
451 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400452 case -NFS4ERR_LEASE_MOVED:
453 nfs4_schedule_lease_moved_recovery(clp);
454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400456 case -NFS4ERR_BADSESSION:
457 case -NFS4ERR_BADSLOT:
458 case -NFS4ERR_BAD_HIGH_SLOT:
459 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
460 case -NFS4ERR_DEADSESSION:
461 case -NFS4ERR_SEQ_FALSE_RETRY:
462 case -NFS4ERR_SEQ_MISORDERED:
463 dprintk("%s ERROR: %d Reset session\n", __func__,
464 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400465 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400466 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500467#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500469 if (exception->timeout > HZ) {
470 /* We have retried a decent amount, time to
471 * fail
472 */
473 ret = -EBUSY;
474 break;
475 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500476 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400477 nfs_inc_server_stats(server, NFSIOS_DELAY);
478 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400479 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400480 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400481 exception->delay = 1;
482 return 0;
483
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400484 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500485 case -NFS4ERR_OLD_STATEID:
486 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800487 break;
488 case -NFS4ERR_BADOWNER:
489 /* The following works around a Linux server bug! */
490 case -NFS4ERR_BADNAME:
491 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
492 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
493 exception->retry = 1;
494 printk(KERN_WARNING "NFS: v4 server %s "
495 "does not accept raw "
496 "uid/gids. "
497 "Reenabling the idmapper.\n",
498 server->nfs_client->cl_hostname);
499 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500500 }
501 /* We failed to handle the error */
502 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500503wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400504 exception->recovering = 1;
505 return 0;
506}
507
508/* This is the error handling routine for processes that are allowed
509 * to sleep.
510 */
511int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
512{
513 struct nfs_client *clp = server->nfs_client;
514 int ret;
515
516 ret = nfs4_do_handle_exception(server, errorcode, exception);
517 if (exception->delay) {
518 ret = nfs4_delay(server->client, &exception->timeout);
519 goto out_retry;
520 }
521 if (exception->recovering) {
522 ret = nfs4_wait_clnt_recover(clp);
523 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
524 return -EIO;
525 goto out_retry;
526 }
527 return ret;
528out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500529 if (ret == 0)
530 exception->retry = 1;
531 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500532}
533
Trond Myklebust037fc982015-09-20 15:51:00 -0400534static int
535nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
536 int errorcode, struct nfs4_exception *exception)
537{
538 struct nfs_client *clp = server->nfs_client;
539 int ret;
540
541 ret = nfs4_do_handle_exception(server, errorcode, exception);
542 if (exception->delay) {
543 rpc_delay(task, nfs4_update_delay(&exception->timeout));
544 goto out_retry;
545 }
546 if (exception->recovering) {
547 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
548 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
549 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
550 goto out_retry;
551 }
552 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
553 ret = -EIO;
554 return ret;
555out_retry:
556 if (ret == 0)
557 exception->retry = 1;
558 return ret;
559}
560
561static int
562nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
563 struct nfs4_state *state, long *timeout)
564{
565 struct nfs4_exception exception = {
566 .state = state,
567 };
568
569 if (task->tk_status >= 0)
570 return 0;
571 if (timeout)
572 exception.timeout = *timeout;
573 task->tk_status = nfs4_async_handle_exception(task, server,
574 task->tk_status,
575 &exception);
576 if (exception.delay && timeout)
577 *timeout = exception.timeout;
578 if (exception.retry)
579 return -EAGAIN;
580 return 0;
581}
582
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400583/*
584 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
585 * or 'false' otherwise.
586 */
587static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
588{
589 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500590 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400616 struct nfs4_sequence_res *res, int cache_reply,
617 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400618{
619 args->sa_slot = NULL;
620 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400621 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400622
623 res->sr_slot = NULL;
624}
625
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400626static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400627{
628 struct nfs4_slot *slot = res->sr_slot;
629 struct nfs4_slot_table *tbl;
630
Chuck Lever3bd23842013-08-09 12:49:19 -0400631 tbl = slot->table;
632 spin_lock(&tbl->slot_tbl_lock);
633 if (!nfs41_wake_and_assign_slot(tbl, slot))
634 nfs4_free_slot(tbl, slot);
635 spin_unlock(&tbl->slot_tbl_lock);
636
637 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400638}
639
640static int nfs40_sequence_done(struct rpc_task *task,
641 struct nfs4_sequence_res *res)
642{
643 if (res->sr_slot != NULL)
644 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400645 return 1;
646}
647
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400648#if defined(CONFIG_NFS_V4_1)
649
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400650static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400651{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500652 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400653 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500654 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400655
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400656 if (!slot)
657 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500658 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500659 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500660
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400661 /* Bump the slot sequence number */
662 if (slot->seq_done)
663 slot->seq_nr++;
664 slot->seq_done = 0;
665
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500666 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500667 /* Be nice to the server: try to ensure that the last transmitted
668 * value for highest_user_slotid <= target_highest_slotid
669 */
670 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
671 send_new_highest_used_slotid = true;
672
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500673 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500674 send_new_highest_used_slotid = false;
675 goto out_unlock;
676 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500677 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500678
679 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
680 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500681out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500682 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500683 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400684 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400685 if (waitqueue_active(&tbl->slot_waitq))
686 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400687}
688
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400689static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
690{
691 nfs41_release_slot(res->sr_slot);
692 res->sr_slot = NULL;
693}
694
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400695static int nfs41_sequence_process(struct rpc_task *task,
696 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400700 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500701 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500702 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500704 if (slot == NULL)
705 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400706 /* don't increment the sequence number if the task wasn't sent */
707 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400708 goto out;
709
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500710 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500711
Trond Myklebustac20d162012-12-15 15:36:07 -0500712 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400713 if (res->sr_status != -NFS4ERR_DELAY)
714 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500715 interrupted = true;
716 }
717
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400718 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500719 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400720 switch (res->sr_status) {
721 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400722 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400723 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500724 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500725 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500726 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400727 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
728 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500729 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400730 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500731 case 1:
732 /*
733 * sr_status remains 1 if an RPC level error occurred.
734 * The server may or may not have processed the sequence
735 * operation..
736 * Mark the slot as having hosted an interrupted RPC call.
737 */
738 slot->interrupted = 1;
739 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 case -NFS4ERR_DELAY:
741 /* The server detected a resend of the RPC call and
742 * returned NFS4ERR_DELAY as per Section 2.10.6.2
743 * of RFC5661.
744 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400746 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500747 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500748 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400749 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500750 case -NFS4ERR_BADSLOT:
751 /*
752 * The slot id we used was probably retired. Try again
753 * using a different slot id.
754 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400755 if (slot->seq_nr < slot->table->target_highest_slotid)
756 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500757 goto retry_nowait;
758 case -NFS4ERR_SEQ_MISORDERED:
759 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500760 * Was the last operation on this sequence interrupted?
761 * If so, retry after bumping the sequence number.
762 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400763 if (interrupted)
764 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500765 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500766 * Could this slot have been previously retired?
767 * If so, then the server may be expecting seq_nr = 1!
768 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500769 if (slot->seq_nr != 1) {
770 slot->seq_nr = 1;
771 goto retry_nowait;
772 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400773 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500774 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400775 if (interrupted)
776 goto retry_new_seq;
777 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 default:
779 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400780 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400781 }
782out:
783 /* The session may be reset by one of the error handlers. */
784 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500785out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500786 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400787session_recover:
788 nfs4_schedule_session_recovery(session, res->sr_status);
789 goto retry_nowait;
790retry_new_seq:
791 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500792retry_nowait:
793 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400794 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500795 task->tk_status = 0;
796 ret = 0;
797 }
798 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400799out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400800 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400801 goto out;
802 rpc_delay(task, NFS4_POLL_RETRY_MAX);
803 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400804}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400805
806int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
807{
808 if (!nfs41_sequence_process(task, res))
809 return 0;
810 if (res->sr_slot != NULL)
811 nfs41_sequence_free_slot(res);
812 return 1;
813
814}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500815EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400817static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
818{
819 if (res->sr_slot == NULL)
820 return 1;
821 if (res->sr_slot->table->session != NULL)
822 return nfs41_sequence_process(task, res);
823 return nfs40_sequence_done(task, res);
824}
825
826static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
827{
828 if (res->sr_slot != NULL) {
829 if (res->sr_slot->table->session != NULL)
830 nfs41_sequence_free_slot(res);
831 else
832 nfs40_sequence_free_slot(res);
833 }
834}
835
Peng Tao2c4b1312014-06-11 05:24:16 +0800836int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400837{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500838 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400840 if (!res->sr_slot->table->session)
841 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400842 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400843}
Peng Tao2c4b1312014-06-11 05:24:16 +0800844EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400845
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
847{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400848 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400849
Trond Myklebust035168ab2010-06-16 09:52:26 -0400850 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
851
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500852 nfs4_setup_sequence(data->seq_server->nfs_client,
853 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854}
855
Andy Adamson69ab40c2009-04-01 09:22:19 -0400856static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
857{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400858 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400859
Trond Myklebust14516c32010-07-31 14:29:06 -0400860 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400861}
862
Trond Myklebust17280172012-03-11 13:11:00 -0400863static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400864 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400865 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400866};
867
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400868static void
869nfs4_sequence_process_interrupted(struct nfs_client *client,
870 struct nfs4_slot *slot, struct rpc_cred *cred)
871{
872 struct rpc_task *task;
873
874 task = _nfs41_proc_sequence(client, cred, slot, true);
875 if (!IS_ERR(task))
876 rpc_put_task_async(task);
877}
878
Chuck Lever3bd23842013-08-09 12:49:19 -0400879#else /* !CONFIG_NFS_V4_1 */
880
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400881static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
882{
883 return nfs40_sequence_done(task, res);
884}
885
886static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
887{
888 if (res->sr_slot != NULL)
889 nfs40_sequence_free_slot(res);
890}
891
Peng Tao2c4b1312014-06-11 05:24:16 +0800892int nfs4_sequence_done(struct rpc_task *task,
893 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400894{
Chuck Lever3bd23842013-08-09 12:49:19 -0400895 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400896}
Peng Tao2c4b1312014-06-11 05:24:16 +0800897EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400898
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400899static void
900nfs4_sequence_process_interrupted(struct nfs_client *client,
901 struct nfs4_slot *slot, struct rpc_cred *cred)
902{
903 WARN_ON_ONCE(1);
904 slot->interrupted = 0;
905}
906
Chuck Lever3bd23842013-08-09 12:49:19 -0400907#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400908
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909static
910void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
911 struct nfs4_sequence_res *res,
912 struct nfs4_slot *slot)
913{
914 if (!slot)
915 return;
916 slot->privileged = args->sa_privileged ? 1 : 0;
917 args->sa_slot = slot;
918
919 res->sr_slot = slot;
920 res->sr_timestamp = jiffies;
921 res->sr_status_flags = 0;
922 res->sr_status = 1;
923
924}
925
926int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500927 struct nfs4_sequence_args *args,
928 struct nfs4_sequence_res *res,
929 struct rpc_task *task)
930{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500931 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500932 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500933 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500934
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500935 /* slot already allocated? */
936 if (res->sr_slot != NULL)
937 goto out_start;
938
Anna Schumaker76ee0352017-01-10 16:49:31 -0500939 if (session) {
940 tbl = &session->fc_slot_table;
941 task->tk_timeout = 0;
942 }
943
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400944 for (;;) {
945 spin_lock(&tbl->slot_tbl_lock);
946 /* The state manager will wait until the slot table is empty */
947 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
948 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500949
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400950 slot = nfs4_alloc_slot(tbl);
951 if (IS_ERR(slot)) {
952 /* Try again in 1/4 second */
953 if (slot == ERR_PTR(-ENOMEM))
954 task->tk_timeout = HZ >> 2;
955 goto out_sleep;
956 }
957 spin_unlock(&tbl->slot_tbl_lock);
958
959 if (likely(!slot->interrupted))
960 break;
961 nfs4_sequence_process_interrupted(client,
962 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500963 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500964
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400965 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500966
Anna Schumakerad05cc02017-01-11 13:37:06 -0500967 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500968out_start:
969 rpc_call_start(task);
970 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500971
972out_sleep:
973 if (args->sa_privileged)
974 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
975 NULL, RPC_PRIORITY_PRIVILEGED);
976 else
977 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
978 spin_unlock(&tbl->slot_tbl_lock);
979 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500980}
981EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
982
Chuck Lever9915ea72013-08-09 12:48:27 -0400983static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
984{
985 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500986 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400987 data->seq_args, data->seq_res, task);
988}
989
990static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
991{
992 struct nfs4_call_sync_data *data = calldata;
993 nfs4_sequence_done(task, data->seq_res);
994}
995
996static const struct rpc_call_ops nfs40_call_sync_ops = {
997 .rpc_call_prepare = nfs40_call_sync_prepare,
998 .rpc_call_done = nfs40_call_sync_done,
999};
1000
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001001static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001002 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001004 struct nfs4_sequence_args *args,
1005 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001006{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001007 int ret;
1008 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001009 struct nfs_client *clp = server->nfs_client;
1010 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001011 .seq_server = server,
1012 .seq_args = args,
1013 .seq_res = res,
1014 };
1015 struct rpc_task_setup task_setup = {
1016 .rpc_client = clnt,
1017 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001018 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001019 .callback_data = &data
1020 };
1021
1022 task = rpc_run_task(&task_setup);
1023 if (IS_ERR(task))
1024 ret = PTR_ERR(task);
1025 else {
1026 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001027 rpc_put_task(task);
1028 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001029 return ret;
1030}
1031
Bryan Schumaker7c513052011-03-24 17:12:24 +00001032int nfs4_call_sync(struct rpc_clnt *clnt,
1033 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001034 struct rpc_message *msg,
1035 struct nfs4_sequence_args *args,
1036 struct nfs4_sequence_res *res,
1037 int cache_reply)
1038{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001039 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001040 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001043static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1044 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct nfs_inode *nfsi = NFS_I(dir);
1047
1048 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001049 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1050 | NFS_INO_INVALID_MTIME
1051 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001052 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001053 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1054 nfsi->attrtimeo_timestamp = jiffies;
1055 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001057 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001058 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1059 NFS_INO_INVALID_ACL;
1060 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001061 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001062 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001063 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001064 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_unlock(&dir->i_lock);
1066}
1067
1068struct nfs4_opendata {
1069 struct kref kref;
1070 struct nfs_openargs o_arg;
1071 struct nfs_openres o_res;
1072 struct nfs_open_confirmargs c_arg;
1073 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001074 struct nfs4_string owner_name;
1075 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001076 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001078 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001080 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct nfs4_state_owner *owner;
1082 struct nfs4_state *state;
1083 struct iattr attrs;
1084 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001085 bool rpc_done;
1086 bool file_created;
1087 bool is_recover;
1088 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001089 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001091
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001092struct nfs4_open_createattrs {
1093 struct nfs4_label *label;
1094 struct iattr *sattr;
1095 const __u32 verf[2];
1096};
1097
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001098static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1099 int err, struct nfs4_exception *exception)
1100{
1101 if (err != -EINVAL)
1102 return false;
1103 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1104 return false;
1105 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1106 exception->retry = 1;
1107 return true;
1108}
1109
Trond Myklebust6ae37332015-01-30 14:21:14 -05001110static u32
1111nfs4_map_atomic_open_share(struct nfs_server *server,
1112 fmode_t fmode, int openflags)
1113{
1114 u32 res = 0;
1115
1116 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1117 case FMODE_READ:
1118 res = NFS4_SHARE_ACCESS_READ;
1119 break;
1120 case FMODE_WRITE:
1121 res = NFS4_SHARE_ACCESS_WRITE;
1122 break;
1123 case FMODE_READ|FMODE_WRITE:
1124 res = NFS4_SHARE_ACCESS_BOTH;
1125 }
1126 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1127 goto out;
1128 /* Want no delegation if we're using O_DIRECT */
1129 if (openflags & O_DIRECT)
1130 res |= NFS4_SHARE_WANT_NO_DELEG;
1131out:
1132 return res;
1133}
1134
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001135static enum open_claim_type4
1136nfs4_map_atomic_open_claim(struct nfs_server *server,
1137 enum open_claim_type4 claim)
1138{
1139 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1140 return claim;
1141 switch (claim) {
1142 default:
1143 return claim;
1144 case NFS4_OPEN_CLAIM_FH:
1145 return NFS4_OPEN_CLAIM_NULL;
1146 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1147 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1148 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1149 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1150 }
1151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001154{
1155 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001156 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001157 p->o_res.seqid = p->o_arg.seqid;
1158 p->c_res.seqid = p->c_arg.seqid;
1159 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001160 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001162 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001163}
1164
Al Viro82a2c1b2011-06-22 18:30:55 -04001165static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001167 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001168 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001169 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170{
Al Viro82a2c1b2011-06-22 18:30:55 -04001171 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001172 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001174 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001175 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 struct nfs4_opendata *p;
1177
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001178 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179 if (p == NULL)
1180 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001181
1182 p->f_label = nfs4_label_alloc(server, gfp_mask);
1183 if (IS_ERR(p->f_label))
1184 goto err_free_p;
1185
Kinglong Meea49c2692015-07-27 15:31:38 +08001186 p->a_label = nfs4_label_alloc(server, gfp_mask);
1187 if (IS_ERR(p->a_label))
1188 goto err_free_f;
1189
Trond Myklebust63f5f792015-01-23 19:19:25 -05001190 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1191 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001192 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001193 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001194 nfs_sb_active(dentry->d_sb);
1195 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 p->dir = parent;
1197 p->owner = sp;
1198 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001199 p->o_arg.open_flags = flags;
1200 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001201 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001202 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001203 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1204 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001205 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1206 * will return permission denied for all bits until close */
1207 if (!(flags & O_EXCL)) {
1208 /* ask server to check for all possible rights as results
1209 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001210 switch (p->o_arg.claim) {
1211 default:
1212 break;
1213 case NFS4_OPEN_CLAIM_NULL:
1214 case NFS4_OPEN_CLAIM_FH:
1215 p->o_arg.access = NFS4_ACCESS_READ |
1216 NFS4_ACCESS_MODIFY |
1217 NFS4_ACCESS_EXTEND |
1218 NFS4_ACCESS_EXECUTE;
1219 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001220 }
David Howells7539bba2006-08-22 20:06:09 -04001221 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001222 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1223 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001224 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001225 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001226 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001227 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001228 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001229 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001230 case NFS4_OPEN_CLAIM_NULL:
1231 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1232 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1233 p->o_arg.fh = NFS_FH(dir);
1234 break;
1235 case NFS4_OPEN_CLAIM_PREVIOUS:
1236 case NFS4_OPEN_CLAIM_FH:
1237 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1238 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001239 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001240 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001241 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001242 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001243 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001244
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001245 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001246 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001248 p->c_arg.fh = &p->o_res.fh;
1249 p->c_arg.stateid = &p->o_res.stateid;
1250 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001251 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001252 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001253 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001254
1255err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001256 nfs4_label_free(p->a_label);
1257err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001258 nfs4_label_free(p->f_label);
1259err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001260 kfree(p);
1261err:
1262 dput(parent);
1263 return NULL;
1264}
1265
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001266static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001268 struct nfs4_opendata *p = container_of(kref,
1269 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001270 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001271
1272 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001273 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274 if (p->state != NULL)
1275 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001276 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001277
Kinglong Meea49c2692015-07-27 15:31:38 +08001278 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001279 nfs4_label_free(p->f_label);
1280
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001282 dput(p->dentry);
1283 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001284 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001285 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001286 kfree(p);
1287}
1288
1289static void nfs4_opendata_put(struct nfs4_opendata *p)
1290{
1291 if (p != NULL)
1292 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293}
1294
Trond Myklebust24311f82015-09-20 10:50:17 -04001295static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1296 fmode_t fmode)
1297{
1298 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1299 case FMODE_READ|FMODE_WRITE:
1300 return state->n_rdwr != 0;
1301 case FMODE_WRITE:
1302 return state->n_wronly != 0;
1303 case FMODE_READ:
1304 return state->n_rdonly != 0;
1305 }
1306 WARN_ON_ONCE(1);
1307 return false;
1308}
1309
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001311{
1312 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001313
Trond Myklebust536e43d2012-01-17 22:04:26 -05001314 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 goto out;
1316 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001317 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001318 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1319 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001320 break;
1321 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001322 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1323 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 break;
1325 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001326 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1327 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001328 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330 return ret;
1331}
1332
Trond Myklebust2a606182015-08-19 22:30:00 -05001333static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1334 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001336 if (delegation == NULL)
1337 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001338 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001339 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001340 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1341 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001342 switch (claim) {
1343 case NFS4_OPEN_CLAIM_NULL:
1344 case NFS4_OPEN_CLAIM_FH:
1345 break;
1346 case NFS4_OPEN_CLAIM_PREVIOUS:
1347 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1348 break;
1349 default:
1350 return 0;
1351 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001352 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001353 return 1;
1354}
1355
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001357{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001359 case FMODE_WRITE:
1360 state->n_wronly++;
1361 break;
1362 case FMODE_READ:
1363 state->n_rdonly++;
1364 break;
1365 case FMODE_READ|FMODE_WRITE:
1366 state->n_rdwr++;
1367 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001368 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001369}
1370
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001371#ifdef CONFIG_NFS_V4_1
1372static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1373{
1374 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1375 return true;
1376 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1377 return true;
1378 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1379 return true;
1380 return false;
1381}
1382#endif /* CONFIG_NFS_V4_1 */
1383
Trond Myklebustc9399f22017-11-06 15:28:01 -05001384static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1385{
1386 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1387 wake_up_all(&state->waitq);
1388}
1389
1390static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1391 const nfs4_stateid *stateid)
1392{
1393 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1394 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1395
1396 if (stateid_seqid == state_seqid + 1U ||
1397 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1398 nfs_state_log_update_open_stateid(state);
1399 else
1400 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1401}
1402
Trond Myklebust4f14c192014-02-12 19:15:06 -05001403static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001404{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001405 struct nfs_client *clp = state->owner->so_server->nfs_client;
1406 bool need_recover = false;
1407
1408 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1409 need_recover = true;
1410 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1411 need_recover = true;
1412 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1413 need_recover = true;
1414 if (need_recover)
1415 nfs4_state_mark_reclaim_nograce(clp, state);
1416}
1417
Trond Myklebustc9399f22017-11-06 15:28:01 -05001418/*
1419 * Check for whether or not the caller may update the open stateid
1420 * to the value passed in by stateid.
1421 *
1422 * Note: This function relies heavily on the server implementing
1423 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1424 * correctly.
1425 * i.e. The stateid seqids have to be initialised to 1, and
1426 * are then incremented on every state transition.
1427 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001428static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001429 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001430{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001431 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1432 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1433 if (stateid->seqid == cpu_to_be32(1))
1434 nfs_state_log_update_open_stateid(state);
1435 else
1436 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001438 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001439
1440 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1441 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001442 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001443 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001444 return false;
1445}
1446
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1448{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001449 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1450 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451 if (state->n_wronly)
1452 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1453 if (state->n_rdonly)
1454 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1455 if (state->n_rdwr)
1456 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001457 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001458}
1459
Trond Myklebust226056c2014-02-11 10:41:07 -05001460static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1461 nfs4_stateid *stateid, fmode_t fmode)
1462{
1463 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1464 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1465 case FMODE_WRITE:
1466 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 break;
1468 case FMODE_READ:
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 break;
1471 case 0:
1472 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1473 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1474 clear_bit(NFS_OPEN_STATE, &state->flags);
1475 }
1476 if (stateid == NULL)
1477 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001478 /* Handle OPEN+OPEN_DOWNGRADE races */
1479 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1480 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001481 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001482 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001483 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001484 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001485 nfs4_stateid_copy(&state->stateid, stateid);
1486 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001487 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001488out:
1489 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001490}
1491
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001492static void nfs_clear_open_stateid(struct nfs4_state *state,
1493 nfs4_stateid *arg_stateid,
1494 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001495{
1496 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001497 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1498 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1499 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001500 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001501 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1502 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001503}
1504
Trond Myklebust1393d962016-09-22 13:39:13 -04001505static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001506 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001507{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001508 DEFINE_WAIT(wait);
1509 int status = 0;
1510 for (;;) {
1511
1512 if (!nfs_need_update_open_stateid(state, stateid))
1513 return;
1514 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001515 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001516 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001517 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001518 /* Rely on seqids for serialisation with NFSv4.0 */
1519 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1520 break;
1521
1522 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1523 /*
1524 * Ensure we process the state changes in the same order
1525 * in which the server processed them by delaying the
1526 * update of the stateid until we are in sequence.
1527 */
1528 write_sequnlock(&state->seqlock);
1529 spin_unlock(&state->owner->so_lock);
1530 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001531 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001532 if (!signal_pending(current)) {
1533 if (schedule_timeout(5*HZ) == 0)
1534 status = -EAGAIN;
1535 else
1536 status = 0;
1537 } else
1538 status = -EINTR;
1539 finish_wait(&state->waitq, &wait);
1540 rcu_read_lock();
1541 spin_lock(&state->owner->so_lock);
1542 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001543 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001544
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001545 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1546 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001547 nfs4_stateid_copy(freeme, &state->open_stateid);
1548 nfs_test_and_clear_all_open_stateid(state);
1549 }
1550
Trond Myklebuste999e802014-02-10 18:20:47 -05001551 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1552 nfs4_stateid_copy(&state->stateid, stateid);
1553 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001554 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001556}
1557
Trond Myklebustc9399f22017-11-06 15:28:01 -05001558static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001560 fmode_t fmode,
1561 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001563 /*
1564 * Protect the call to nfs4_state_set_mode_locked and
1565 * serialise the stateid update
1566 */
1567 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001568 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1569 switch (fmode) {
1570 case FMODE_READ:
1571 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1572 break;
1573 case FMODE_WRITE:
1574 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1575 break;
1576 case FMODE_READ|FMODE_WRITE:
1577 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001578 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001579 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001580 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001581}
1582
1583static void nfs_state_set_delegation(struct nfs4_state *state,
1584 const nfs4_stateid *deleg_stateid,
1585 fmode_t fmode)
1586{
1587 /*
1588 * Protect the call to nfs4_state_set_mode_locked and
1589 * serialise the stateid update
1590 */
1591 write_seqlock(&state->seqlock);
1592 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1593 set_bit(NFS_DELEGATED_STATE, &state->flags);
1594 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Trond Myklebust1393d962016-09-22 13:39:13 -04001597static int update_open_stateid(struct nfs4_state *state,
1598 const nfs4_stateid *open_stateid,
1599 const nfs4_stateid *delegation,
1600 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001601{
Trond Myklebust1393d962016-09-22 13:39:13 -04001602 struct nfs_server *server = NFS_SERVER(state->inode);
1603 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001604 struct nfs_inode *nfsi = NFS_I(state->inode);
1605 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001606 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001607 int ret = 0;
1608
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001610
1611 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001612 spin_lock(&state->owner->so_lock);
1613 if (open_stateid != NULL) {
1614 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1615 ret = 1;
1616 }
1617
Trond Myklebust34310432008-12-23 15:21:38 -05001618 deleg_cur = rcu_dereference(nfsi->delegation);
1619 if (deleg_cur == NULL)
1620 goto no_delegation;
1621
1622 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001623 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001624 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001626 goto no_delegation_unlock;
1627
1628 if (delegation == NULL)
1629 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001630 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001631 goto no_delegation_unlock;
1632
Trond Myklebustb7391f42008-12-23 15:21:52 -05001633 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001634 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001635 ret = 1;
1636no_delegation_unlock:
1637 spin_unlock(&deleg_cur->lock);
1638no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001639 if (ret)
1640 update_open_stateflags(state, fmode);
1641 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001642 rcu_read_unlock();
1643
Trond Myklebust4f14c192014-02-12 19:15:06 -05001644 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001645 nfs4_schedule_state_manager(clp);
1646 if (freeme.type != 0)
1647 nfs4_test_and_free_stateid(server, &freeme,
1648 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001649
1650 return ret;
1651}
1652
Trond Myklebust39071e62015-01-24 15:07:56 -05001653static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1654 const nfs4_stateid *stateid)
1655{
1656 struct nfs4_state *state = lsp->ls_state;
1657 bool ret = false;
1658
1659 spin_lock(&state->state_lock);
1660 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1661 goto out_noupdate;
1662 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1663 goto out_noupdate;
1664 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1665 ret = true;
1666out_noupdate:
1667 spin_unlock(&state->state_lock);
1668 return ret;
1669}
Trond Myklebust34310432008-12-23 15:21:38 -05001670
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001671static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672{
1673 struct nfs_delegation *delegation;
1674
Trond Myklebustf5086242018-03-20 16:43:13 -04001675 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 rcu_read_lock();
1677 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001678 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 rcu_read_unlock();
1680 return;
1681 }
1682 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001683 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684}
1685
Trond Myklebust6ee41262007-07-08 14:11:36 -04001686static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001687{
1688 struct nfs4_state *state = opendata->state;
1689 struct nfs_inode *nfsi = NFS_I(state->inode);
1690 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001691 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001692 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001693 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001694 nfs4_stateid stateid;
1695 int ret = -EAGAIN;
1696
Trond Myklebustaac00a82007-07-05 19:02:21 -04001697 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001698 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001699 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001700 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001701 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001702 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001703 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001704 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001705 rcu_read_lock();
1706 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001707 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001708 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001709 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001710 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001711 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001712 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001713 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001714 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001715 if (!opendata->is_recover) {
1716 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1717 if (ret != 0)
1718 goto out;
1719 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001721
1722 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001723 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001724 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001725 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726out:
1727 return ERR_PTR(ret);
1728out_return_state:
1729 atomic_inc(&state->count);
1730 return state;
1731}
1732
Andy Adamsone23008e2012-10-02 21:07:32 -04001733static void
1734nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1735{
1736 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1737 struct nfs_delegation *delegation;
1738 int delegation_flags = 0;
1739
1740 rcu_read_lock();
1741 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1742 if (delegation)
1743 delegation_flags = delegation->flags;
1744 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001745 switch (data->o_arg.claim) {
1746 default:
1747 break;
1748 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1749 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001750 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1751 "returning a delegation for "
1752 "OPEN(CLAIM_DELEGATE_CUR)\n",
1753 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001754 return;
1755 }
1756 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001757 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001758 data->owner->so_cred,
1759 data->o_res.delegation_type,
1760 &data->o_res.delegation,
1761 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001762 else
1763 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001764 data->owner->so_cred,
1765 data->o_res.delegation_type,
1766 &data->o_res.delegation,
1767 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001768}
1769
1770/*
1771 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1772 * and update the nfs4_state.
1773 */
1774static struct nfs4_state *
1775_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1776{
1777 struct inode *inode = data->state->inode;
1778 struct nfs4_state *state = data->state;
1779 int ret;
1780
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001781 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001782 if (data->rpc_status)
1783 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001784 /* cached opens have already been processed */
1785 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001786 }
1787
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 ret = nfs_refresh_inode(inode, &data->f_attr);
1789 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001790 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001791
1792 if (data->o_res.delegation_type != 0)
1793 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001794update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001795 update_open_stateid(state, &data->o_res.stateid, NULL,
1796 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001797 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001798
1799 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001800}
1801
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001802static struct inode *
1803nfs4_opendata_get_inode(struct nfs4_opendata *data)
1804{
1805 struct inode *inode;
1806
1807 switch (data->o_arg.claim) {
1808 case NFS4_OPEN_CLAIM_NULL:
1809 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1810 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1811 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1812 return ERR_PTR(-EAGAIN);
1813 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1814 &data->f_attr, data->f_label);
1815 break;
1816 default:
1817 inode = d_inode(data->dentry);
1818 ihold(inode);
1819 nfs_refresh_inode(inode, &data->f_attr);
1820 }
1821 return inode;
1822}
1823
Andy Adamsone23008e2012-10-02 21:07:32 -04001824static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001825nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1826{
1827 struct nfs4_state *state;
1828 struct inode *inode;
1829
1830 inode = nfs4_opendata_get_inode(data);
1831 if (IS_ERR(inode))
1832 return ERR_CAST(inode);
1833 if (data->state != NULL && data->state->inode == inode) {
1834 state = data->state;
1835 atomic_inc(&state->count);
1836 } else
1837 state = nfs4_get_open_state(inode, data->owner);
1838 iput(inode);
1839 if (state == NULL)
1840 state = ERR_PTR(-ENOMEM);
1841 return state;
1842}
1843
Andy Adamsone23008e2012-10-02 21:07:32 -04001844static struct nfs4_state *
1845_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001846{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001847 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001848
Trond Myklebustaac00a82007-07-05 19:02:21 -04001849 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001850 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001851 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001852 goto out;
1853 }
1854
Trond Myklebust75e8c482017-08-08 10:38:07 -04001855 state = nfs4_opendata_find_nfs4_state(data);
1856 if (IS_ERR(state))
1857 goto out;
1858
Andy Adamsone23008e2012-10-02 21:07:32 -04001859 if (data->o_res.delegation_type != 0)
1860 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001861 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001862 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001863out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001864 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001865 return state;
1866}
1867
Andy Adamsone23008e2012-10-02 21:07:32 -04001868static struct nfs4_state *
1869nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1870{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001871 struct nfs4_state *ret;
1872
Andy Adamsone23008e2012-10-02 21:07:32 -04001873 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001874 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1875 else
1876 ret = _nfs4_opendata_to_nfs4_state(data);
1877 nfs4_sequence_free_slot(&data->o_res.seq_res);
1878 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001879}
1880
Trond Myklebust864472e2006-01-03 09:55:15 +01001881static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1882{
1883 struct nfs_inode *nfsi = NFS_I(state->inode);
1884 struct nfs_open_context *ctx;
1885
1886 spin_lock(&state->inode->i_lock);
1887 list_for_each_entry(ctx, &nfsi->open_files, list) {
1888 if (ctx->state != state)
1889 continue;
1890 get_nfs_open_context(ctx);
1891 spin_unlock(&state->inode->i_lock);
1892 return ctx;
1893 }
1894 spin_unlock(&state->inode->i_lock);
1895 return ERR_PTR(-ENOENT);
1896}
1897
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001898static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1899 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001900{
1901 struct nfs4_opendata *opendata;
1902
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001903 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001904 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001905 if (opendata == NULL)
1906 return ERR_PTR(-ENOMEM);
1907 opendata->state = state;
1908 atomic_inc(&state->count);
1909 return opendata;
1910}
1911
Trond Myklebust24311f82015-09-20 10:50:17 -04001912static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1913 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001914{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001915 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001916 int ret;
1917
Trond Myklebust24311f82015-09-20 10:50:17 -04001918 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001919 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001920 opendata->o_arg.open_flags = 0;
1921 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001922 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1923 NFS_SB(opendata->dentry->d_sb),
1924 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001925 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1926 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1927 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001928 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001929 if (ret != 0)
1930 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001931 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001932 if (IS_ERR(newstate))
1933 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001934 if (newstate != opendata->state)
1935 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001936 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001937 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001938}
1939
1940static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1941{
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 int ret;
1943
Trond Myklebust4f14c192014-02-12 19:15:06 -05001944 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1945 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1946 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1947 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001948 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001949 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001950 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001951 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001952 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1953 if (ret != 0)
1954 return ret;
1955 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1956 if (ret != 0)
1957 return ret;
1958 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1959 if (ret != 0)
1960 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001961 /*
1962 * We may have performed cached opens for all three recoveries.
1963 * Check if we need to update the current stateid.
1964 */
1965 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001966 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001967 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001968 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001969 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001970 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001971 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001972 return 0;
1973}
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/*
1976 * OPEN_RECLAIM:
1977 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001979static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001981 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001982 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001983 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 int status;
1985
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001986 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1987 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001988 if (IS_ERR(opendata))
1989 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001990 rcu_read_lock();
1991 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001992 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001993 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001994 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001995 opendata->o_arg.u.delegation_type = delegation_type;
1996 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001997 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return status;
1999}
2000
Trond Myklebust539cd032007-06-05 11:46:42 -04002001static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct nfs_server *server = NFS_SERVER(state->inode);
2004 struct nfs4_exception exception = { };
2005 int err;
2006 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002007 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002008 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002009 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2010 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002011 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002012 break;
2013 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 } while (exception.retry);
2015 return err;
2016}
2017
Trond Myklebust864472e2006-01-03 09:55:15 +01002018static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2019{
2020 struct nfs_open_context *ctx;
2021 int ret;
2022
2023 ctx = nfs4_state_find_open_context(state);
2024 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002025 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002026 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002027 put_nfs_open_context(ctx);
2028 return ret;
2029}
2030
NeilBrowndce26302017-12-13 09:57:09 +11002031static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002033 switch (err) {
2034 default:
2035 printk(KERN_ERR "NFS: %s: unhandled error "
2036 "%d.\n", __func__, err);
2037 case 0:
2038 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002039 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002040 case -ESTALE:
2041 break;
2042 case -NFS4ERR_BADSESSION:
2043 case -NFS4ERR_BADSLOT:
2044 case -NFS4ERR_BAD_HIGH_SLOT:
2045 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2046 case -NFS4ERR_DEADSESSION:
2047 set_bit(NFS_DELEGATED_STATE, &state->flags);
2048 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2049 return -EAGAIN;
2050 case -NFS4ERR_STALE_CLIENTID:
2051 case -NFS4ERR_STALE_STATEID:
2052 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002053 /* Don't recall a delegation if it was lost */
2054 nfs4_schedule_lease_recovery(server->nfs_client);
2055 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002056 case -NFS4ERR_MOVED:
2057 nfs4_schedule_migration_recovery(server);
2058 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002059 case -NFS4ERR_LEASE_MOVED:
2060 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2061 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002062 case -NFS4ERR_DELEG_REVOKED:
2063 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002064 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002065 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002066 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002067 nfs_inode_find_state_and_recover(state->inode,
2068 stateid);
2069 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002070 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002071 case -NFS4ERR_DELAY:
2072 case -NFS4ERR_GRACE:
2073 set_bit(NFS_DELEGATED_STATE, &state->flags);
2074 ssleep(1);
2075 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002076 case -ENOMEM:
2077 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002078 if (fl) {
2079 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2080 if (lsp)
2081 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2082 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002083 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return err;
2086}
2087
Trond Myklebust24311f82015-09-20 10:50:17 -04002088int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2089 struct nfs4_state *state, const nfs4_stateid *stateid,
2090 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002091{
2092 struct nfs_server *server = NFS_SERVER(state->inode);
2093 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002094 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002095
2096 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2097 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2098 if (IS_ERR(opendata))
2099 return PTR_ERR(opendata);
2100 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002101 write_seqlock(&state->seqlock);
2102 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2103 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002104 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2105 switch (type & (FMODE_READ|FMODE_WRITE)) {
2106 case FMODE_READ|FMODE_WRITE:
2107 case FMODE_WRITE:
2108 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2109 if (err)
2110 break;
2111 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2112 if (err)
2113 break;
2114 case FMODE_READ:
2115 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2116 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002117 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002118 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002119}
2120
Chuck Leverbe05c862013-08-09 12:49:47 -04002121static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2122{
2123 struct nfs4_opendata *data = calldata;
2124
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002125 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2126 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002127}
2128
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002129static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2130{
2131 struct nfs4_opendata *data = calldata;
2132
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002133 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002134
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002135 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002136 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002137 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002138 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002139 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002140 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002141 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002142}
2143
2144static void nfs4_open_confirm_release(void *calldata)
2145{
2146 struct nfs4_opendata *data = calldata;
2147 struct nfs4_state *state = NULL;
2148
2149 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002150 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002151 goto out_free;
2152 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002153 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002154 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002155 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002156 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002157 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002158out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002159 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002160}
2161
2162static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002163 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002164 .rpc_call_done = nfs4_open_confirm_done,
2165 .rpc_release = nfs4_open_confirm_release,
2166};
2167
2168/*
2169 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2170 */
2171static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2172{
David Howells2b0143b2015-03-17 22:25:59 +00002173 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002174 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 struct rpc_message msg = {
2176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2177 .rpc_argp = &data->c_arg,
2178 .rpc_resp = &data->c_res,
2179 .rpc_cred = data->owner->so_cred,
2180 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002181 struct rpc_task_setup task_setup_data = {
2182 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002183 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002184 .callback_ops = &nfs4_open_confirm_ops,
2185 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002186 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002187 .flags = RPC_TASK_ASYNC,
2188 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 int status;
2190
Anna Schumakerfba83f32018-05-04 16:22:50 -04002191 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2192 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002193 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002194 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002195 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002196 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002197 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002198 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002199 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002200 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002201 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002202 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002203 smp_wmb();
2204 } else
2205 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002206 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return status;
2208}
2209
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002212 struct nfs4_opendata *data = calldata;
2213 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002214 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002215 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002216
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002218 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002219 /*
2220 * Check if we still need to send an OPEN call, or if we can use
2221 * a delegation instead.
2222 */
2223 if (data->state != NULL) {
2224 struct nfs_delegation *delegation;
2225
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002226 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002227 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002228 rcu_read_lock();
2229 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002230 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002231 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002232 rcu_read_unlock();
2233 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002234 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002235 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002236 switch (claim) {
2237 default:
2238 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002239 case NFS4_OPEN_CLAIM_PREVIOUS:
2240 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2241 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002242 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002243 case NFS4_OPEN_CLAIM_FH:
2244 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002245 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002246 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002247 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002248 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002249 &data->o_res.seq_res,
2250 task) != 0)
2251 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002252
2253 /* Set the create mode (note dependency on the session type) */
2254 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2255 if (data->o_arg.open_flags & O_EXCL) {
2256 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2257 if (nfs4_has_persistent_session(clp))
2258 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2259 else if (clp->cl_mvops->minor_version > 0)
2260 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2261 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002262 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002263unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002264 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002265 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002266out_no_action:
2267 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002268out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002269 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002270}
2271
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002272static void nfs4_open_done(struct rpc_task *task, void *calldata)
2273{
2274 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002276 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002277
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002278 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002279 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002280
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002282 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2283 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002284 case S_IFREG:
2285 break;
2286 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002287 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002288 break;
2289 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002290 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002291 break;
2292 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002294 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002295 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002296 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002297 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2298 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002299 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002300 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002302
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303static void nfs4_open_release(void *calldata)
2304{
2305 struct nfs4_opendata *data = calldata;
2306 struct nfs4_state *state = NULL;
2307
2308 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002309 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310 goto out_free;
2311 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002312 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002313 goto out_free;
2314 /* In case we need an open_confirm, no cleanup! */
2315 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2316 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002318 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002319 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002321 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322}
2323
2324static const struct rpc_call_ops nfs4_open_ops = {
2325 .rpc_call_prepare = nfs4_open_prepare,
2326 .rpc_call_done = nfs4_open_done,
2327 .rpc_release = nfs4_open_release,
2328};
2329
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002330static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002331{
David Howells2b0143b2015-03-17 22:25:59 +00002332 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002333 struct nfs_server *server = NFS_SERVER(dir);
2334 struct nfs_openargs *o_arg = &data->o_arg;
2335 struct nfs_openres *o_res = &data->o_res;
2336 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2339 .rpc_argp = o_arg,
2340 .rpc_resp = o_res,
2341 .rpc_cred = data->owner->so_cred,
2342 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002345 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002346 .callback_ops = &nfs4_open_ops,
2347 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002349 .flags = RPC_TASK_ASYNC,
2350 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002351 int status;
2352
Anna Schumakerfba83f32018-05-04 16:22:50 -04002353 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, isrecover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002354 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002355 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002356 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002357 data->cancelled = false;
2358 data->is_recover = false;
Anna Schumakerfba83f32018-05-04 16:22:50 -04002359 if (isrecover)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002360 data->is_recover = true;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002361 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002362 if (IS_ERR(task))
2363 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002364 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002365 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002366 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002367 smp_wmb();
2368 } else
2369 status = data->rpc_status;
2370 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002371
2372 return status;
2373}
2374
2375static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2376{
David Howells2b0143b2015-03-17 22:25:59 +00002377 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002378 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002379 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002380
2381 status = nfs4_run_open_task(data, 1);
2382 if (status != 0 || !data->rpc_done)
2383 return status;
2384
Trond Myklebust6926afd12012-01-07 13:22:46 -05002385 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2386
Anna Schumakerd7e98252017-01-11 16:13:29 -05002387 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002388 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002389
2390 return status;
2391}
2392
Trond Myklebustf3792d62014-07-10 08:54:32 -04002393/*
2394 * Additional permission checks in order to distinguish between an
2395 * open for read, and an open for execute. This works around the
2396 * fact that NFSv4 OPEN treats read and execute permissions as being
2397 * the same.
2398 * Note that in the non-execute case, we want to turn off permission
2399 * checking if we just created a new file (POSIX open() semantics).
2400 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002401static int nfs4_opendata_access(struct rpc_cred *cred,
2402 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002403 struct nfs4_state *state, fmode_t fmode,
2404 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002405{
2406 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002407 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002408
2409 /* access call failed or for some reason the server doesn't
2410 * support any access modes -- defer access call until later */
2411 if (opendata->o_res.access_supported == 0)
2412 return 0;
2413
2414 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002415 /*
2416 * Use openflags to check for exec, because fmode won't
2417 * always have FMODE_EXEC set when file open for exec.
2418 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002419 if (openflags & __FMODE_EXEC) {
2420 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002421 if (S_ISDIR(state->inode->i_mode))
2422 mask = NFS4_ACCESS_LOOKUP;
2423 else
2424 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002425 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002426 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427
2428 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002429 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2430 nfs_access_add_cache(state->inode, &cache);
2431
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002432 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2433 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002434 return 0;
2435
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002436 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002437}
2438
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002439/*
2440 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2441 */
2442static int _nfs4_proc_open(struct nfs4_opendata *data)
2443{
David Howells2b0143b2015-03-17 22:25:59 +00002444 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002445 struct nfs_server *server = NFS_SERVER(dir);
2446 struct nfs_openargs *o_arg = &data->o_arg;
2447 struct nfs_openres *o_res = &data->o_res;
2448 int status;
2449
2450 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002451 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002452 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002453 if (status != 0) {
2454 if (status == -NFS4ERR_BADNAME &&
2455 !(o_arg->open_flags & O_CREAT))
2456 return -ENOENT;
2457 return status;
2458 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002459
Trond Myklebust6926afd12012-01-07 13:22:46 -05002460 nfs_fattr_map_and_free_names(server, &data->f_attr);
2461
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002462 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002463 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002464 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002465 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002466 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002467 if (data->file_created ||
2468 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002469 update_changeattr(dir, &o_res->cinfo,
2470 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002471 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002472 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2473 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002475 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002479 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2480 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002481 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002482 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486/*
2487 * OPEN_EXPIRED:
2488 * reclaim state on the server after a network partition.
2489 * Assumes caller holds the appropriate lock
2490 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002491static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002493 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002494 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002496 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002497 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002498 if (IS_ERR(opendata))
2499 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002500 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002501 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002502 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002503 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002507static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002508{
Trond Myklebust539cd032007-06-05 11:46:42 -04002509 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002510 struct nfs4_exception exception = { };
2511 int err;
2512
2513 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002514 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002515 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002516 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2517 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002518 switch (err) {
2519 default:
2520 goto out;
2521 case -NFS4ERR_GRACE:
2522 case -NFS4ERR_DELAY:
2523 nfs4_handle_exception(server, err, &exception);
2524 err = 0;
2525 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002526 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002527out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002528 return err;
2529}
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2532{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002534 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Trond Myklebust864472e2006-01-03 09:55:15 +01002536 ctx = nfs4_state_find_open_context(state);
2537 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002538 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002539 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002540 put_nfs_open_context(ctx);
2541 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
Trond Myklebust41020b62016-09-22 13:38:58 -04002544static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2545 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002546{
Trond Myklebust41020b62016-09-22 13:38:58 -04002547 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002548 write_seqlock(&state->seqlock);
2549 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2550 write_sequnlock(&state->seqlock);
2551 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2552}
2553
2554static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2555{
2556 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002557 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002558}
2559
2560static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2561{
2562 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2563 nfs40_clear_delegation_stateid(state);
2564 return nfs4_open_expired(sp, state);
2565}
2566
Trond Myklebust45870d62016-09-22 13:38:59 -04002567static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2568 nfs4_stateid *stateid,
2569 struct rpc_cred *cred)
2570{
2571 return -NFS4ERR_BAD_STATEID;
2572}
2573
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002574#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002575static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2576 nfs4_stateid *stateid,
2577 struct rpc_cred *cred)
2578{
2579 int status;
2580
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002581 switch (stateid->type) {
2582 default:
2583 break;
2584 case NFS4_INVALID_STATEID_TYPE:
2585 case NFS4_SPECIAL_STATEID_TYPE:
2586 return -NFS4ERR_BAD_STATEID;
2587 case NFS4_REVOKED_STATEID_TYPE:
2588 goto out_free;
2589 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002590
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002591 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002592 switch (status) {
2593 case -NFS4ERR_EXPIRED:
2594 case -NFS4ERR_ADMIN_REVOKED:
2595 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002596 break;
2597 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002598 return status;
2599 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002600out_free:
2601 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002602 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002603 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002604}
2605
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002606static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002607{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002608 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002609 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002610 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002611 struct rpc_cred *cred;
2612 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002613
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002614 /* Get the delegation credential for use by test/free_stateid */
2615 rcu_read_lock();
2616 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002617 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002618 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002619 return;
2620 }
2621
2622 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002623 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2624 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2625 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002626 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002627 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002628 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002629 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002630
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002631 cred = get_rpccred(delegation->cred);
2632 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002633 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002634 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002635 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002636 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002637
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002638 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002639}
2640
2641/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002642 * nfs41_check_expired_locks - possibly free a lock stateid
2643 *
2644 * @state: NFSv4 state for an inode
2645 *
2646 * Returns NFS_OK if recovery for this stateid is now finished.
2647 * Otherwise a negative NFS4ERR value is returned.
2648 */
2649static int nfs41_check_expired_locks(struct nfs4_state *state)
2650{
2651 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002652 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002653 struct nfs_server *server = NFS_SERVER(state->inode);
2654
2655 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2656 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002657
2658 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002659 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2660 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2661 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2662
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002663 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002664 spin_unlock(&state->state_lock);
2665
2666 nfs4_put_lock_state(prev);
2667 prev = lsp;
2668
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002669 status = nfs41_test_and_free_expired_stateid(server,
2670 &lsp->ls_stateid,
2671 cred);
2672 trace_nfs4_test_lock_stateid(state, lsp, status);
2673 if (status == -NFS4ERR_EXPIRED ||
2674 status == -NFS4ERR_BAD_STATEID) {
2675 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002676 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002677 if (!recover_lost_locks)
2678 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2679 } else if (status != NFS_OK) {
2680 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002681 nfs4_put_lock_state(prev);
2682 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002683 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002684 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002685 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002686 }
2687 spin_unlock(&state->state_lock);
2688 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002689out:
2690 return ret;
2691}
2692
2693/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002694 * nfs41_check_open_stateid - possibly free an open stateid
2695 *
2696 * @state: NFSv4 state for an inode
2697 *
2698 * Returns NFS_OK if recovery for this stateid is now finished.
2699 * Otherwise a negative NFS4ERR value is returned.
2700 */
2701static int nfs41_check_open_stateid(struct nfs4_state *state)
2702{
2703 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002704 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002705 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002706 int status;
2707
Trond Myklebustb134fc42016-09-22 13:39:16 -04002708 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002709 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2710 if (nfs4_have_delegation(state->inode, state->state))
2711 return NFS_OK;
2712 return -NFS4ERR_OPENMODE;
2713 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002714 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002715 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002716 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002717 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002718 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002719 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2720 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2721 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002722 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002723 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002724 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002725 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002726 if (nfs_open_stateid_recover_openmode(state))
2727 return -NFS4ERR_OPENMODE;
2728 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002729}
2730
2731static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2732{
Chuck Levereb64cf92012-07-11 16:30:05 -04002733 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002734
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002735 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002736 status = nfs41_check_expired_locks(state);
2737 if (status != NFS_OK)
2738 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002739 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002740 if (status != NFS_OK)
2741 status = nfs4_open_expired(sp, state);
2742 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002743}
2744#endif
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002747 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2748 * fields corresponding to attributes that were used to store the verifier.
2749 * Make sure we clobber those fields in the later setattr call
2750 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002751static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002753{
Trond Myklebust609339c2018-03-28 16:18:17 -04002754 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2755 __u32 attrset[3];
2756 unsigned ret;
2757 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002758
Trond Myklebust609339c2018-03-28 16:18:17 -04002759 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2760 attrset[i] = opendata->o_res.attrset[i];
2761 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2762 attrset[i] &= ~bitmask[i];
2763 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002764
Trond Myklebust609339c2018-03-28 16:18:17 -04002765 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2766 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002767
Trond Myklebust609339c2018-03-28 16:18:17 -04002768 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2769 if (sattr->ia_valid & ATTR_ATIME_SET)
2770 ret |= ATTR_ATIME_SET;
2771 else
2772 ret |= ATTR_ATIME;
2773 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002774
Trond Myklebust609339c2018-03-28 16:18:17 -04002775 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2776 if (sattr->ia_valid & ATTR_MTIME_SET)
2777 ret |= ATTR_MTIME_SET;
2778 else
2779 ret |= ATTR_MTIME;
2780 }
2781
2782 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002783 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002784 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002785}
2786
Trond Myklebustc21443c2013-02-07 14:26:21 -05002787static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2788 fmode_t fmode,
2789 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002790 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002791{
2792 struct nfs4_state_owner *sp = opendata->owner;
2793 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002794 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002795 struct nfs4_state *state;
2796 unsigned int seq;
2797 int ret;
2798
2799 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2800
2801 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002802 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002803 goto out;
2804
2805 state = nfs4_opendata_to_nfs4_state(opendata);
2806 ret = PTR_ERR(state);
2807 if (IS_ERR(state))
2808 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002809 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002810 if (server->caps & NFS_CAP_POSIX_LOCK)
2811 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002812 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2813 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002814
Trond Myklebust275bb302013-05-29 13:11:28 -04002815 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002816 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002817 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002818 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002819 alias = d_exact_alias(dentry, state->inode);
2820 if (!alias)
2821 alias = d_splice_alias(igrab(state->inode), dentry);
2822 /* d_splice_alias() can't fail here - it's a non-directory */
2823 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002824 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002825 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002826 }
2827 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002828 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002829 }
2830
Trond Myklebustc21443c2013-02-07 14:26:21 -05002831 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2832 if (ret != 0)
2833 goto out;
2834
David Howells2b0143b2015-03-17 22:25:59 +00002835 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002836 nfs_inode_attach_open_context(ctx);
2837 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2838 nfs4_schedule_stateid_recovery(server, state);
2839 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002840out:
2841 return ret;
2842}
2843
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002844/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002845 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 */
Andy Adamson82be4172012-05-23 05:02:35 -04002847static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002848 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002849 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002850 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002851 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 struct nfs4_state_owner *sp;
2854 struct nfs4_state *state = NULL;
2855 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002856 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002857 struct dentry *dentry = ctx->dentry;
2858 struct rpc_cred *cred = ctx->cred;
2859 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2860 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002861 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002862 struct iattr *sattr = c->sattr;
2863 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002864 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002865 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002869 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2870 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2872 goto out_err;
2873 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002874 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002875 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002876 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002877 if (d_really_is_positive(dentry))
2878 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002879 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002880 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002881 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002882 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2883 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002884 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002885 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
David Quigley14c43f72013-05-22 12:50:43 -04002887 if (label) {
2888 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2889 if (IS_ERR(olabel)) {
2890 status = PTR_ERR(olabel);
2891 goto err_opendata_put;
2892 }
2893 }
2894
Trond Myklebuste911b812014-03-26 13:24:37 -07002895 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2896 if (!opendata->f_attr.mdsthreshold) {
2897 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2898 if (!opendata->f_attr.mdsthreshold)
2899 goto err_free_label;
2900 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002901 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002902 }
David Howells2b0143b2015-03-17 22:25:59 +00002903 if (d_really_is_positive(dentry))
2904 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002905
Trond Myklebust3efb9722013-05-29 13:17:04 -04002906 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002907 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002908 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002909 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002910
NeilBrownefcbc042015-07-30 13:00:56 +10002911 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002912 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002913 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002914 /*
2915 * send create attributes which was not set by open
2916 * with an extra setattr.
2917 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002918 if (attrs || label) {
2919 unsigned ia_old = sattr->ia_valid;
2920
2921 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002922 nfs_fattr_init(opendata->o_res.f_attr);
2923 status = nfs4_do_setattr(state->inode, cred,
2924 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002925 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002926 if (status == 0) {
2927 nfs_setattr_update_inode(state->inode, sattr,
2928 opendata->o_res.f_attr);
2929 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2930 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002931 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002932 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002933 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002934 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002935 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002936
Trond Myklebuste911b812014-03-26 13:24:37 -07002937 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002938 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002939 opendata->f_attr.mdsthreshold = NULL;
2940 }
Andy Adamson82be4172012-05-23 05:02:35 -04002941
David Quigley14c43f72013-05-22 12:50:43 -04002942 nfs4_label_free(olabel);
2943
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002944 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002947err_free_label:
2948 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002949err_opendata_put:
2950 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002951err_put_state_owner:
2952 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return status;
2955}
2956
2957
Andy Adamson82be4172012-05-23 05:02:35 -04002958static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002959 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002960 int flags,
2961 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002962 struct nfs4_label *label,
2963 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002965 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct nfs4_exception exception = { };
2967 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002968 struct nfs4_open_createattrs c = {
2969 .label = label,
2970 .sattr = sattr,
2971 .verf = {
2972 [0] = (__u32)jiffies,
2973 [1] = (__u32)current->pid,
2974 },
2975 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 int status;
2977
2978 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002979 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002980 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002981 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (status == 0)
2983 break;
2984 /* NOTE: BAD_SEQID means the server and client disagree about the
2985 * book-keeping w.r.t. state-changing operations
2986 * (OPEN/CLOSE/LOCK/LOCKU...)
2987 * It is actually a sign of a bug on the client or on the server.
2988 *
2989 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002990 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 * have unhashed the old state_owner for us, and that we can
2992 * therefore safely retry using a new one. We should still warn
2993 * the user though...
2994 */
2995 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002996 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002997 " returned a bad sequence-id error!\n",
2998 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 exception.retry = 1;
3000 continue;
3001 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003002 /*
3003 * BAD_STATEID on OPEN means that the server cancelled our
3004 * state before it received the OPEN_CONFIRM.
3005 * Recover by retrying the request as per the discussion
3006 * on Page 181 of RFC3530.
3007 */
3008 if (status == -NFS4ERR_BAD_STATEID) {
3009 exception.retry = 1;
3010 continue;
3011 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003012 if (status == -EAGAIN) {
3013 /* We must have found a delegation */
3014 exception.retry = 1;
3015 continue;
3016 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003017 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3018 continue;
3019 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 status, &exception));
3021 } while (exception.retry);
3022 return res;
3023}
3024
Trond Myklebust8487c472016-06-26 08:44:35 -04003025static int _nfs4_do_setattr(struct inode *inode,
3026 struct nfs_setattrargs *arg,
3027 struct nfs_setattrres *res,
3028 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003029 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003030{
3031 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003032 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3034 .rpc_argp = arg,
3035 .rpc_resp = res,
3036 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003037 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003038 struct rpc_cred *delegation_cred = NULL;
3039 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003040 bool truncate;
3041 int status;
3042
3043 nfs_fattr_init(res->fattr);
3044
3045 /* Servers should only apply open mode checks for file size changes */
3046 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003047 if (!truncate)
3048 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003049
Trond Myklebust991eedb2018-04-09 11:15:30 -04003050 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003051 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003052 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003053 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003054 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003055 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003056 l_ctx = nfs_get_lock_context(ctx);
3057 if (IS_ERR(l_ctx))
3058 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003059 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3060 &arg->stateid, &delegation_cred);
3061 nfs_put_lock_context(l_ctx);
3062 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003063 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003064 } else {
3065zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003066 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003067 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003068 if (delegation_cred)
3069 msg.rpc_cred = delegation_cred;
3070
3071 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3072
3073 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003074 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003075 renew_lease(server, timestamp);
3076 trace_nfs4_setattr(inode, &arg->stateid, status);
3077 return status;
3078}
3079
3080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3081 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003082 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003083 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003085 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003086 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003087 struct nfs_setattrargs arg = {
3088 .fh = NFS_FH(inode),
3089 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 .server = server,
3091 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003092 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003093 };
3094 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003096 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003098 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003099 struct nfs4_exception exception = {
3100 .state = state,
3101 .inode = inode,
3102 .stateid = &arg.stateid,
3103 };
3104 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
David Quigleyaa9c2662013-05-22 12:50:44 -04003106 arg.bitmask = nfs4_bitmask(server, ilabel);
3107 if (ilabel)
3108 arg.bitmask = nfs4_bitmask(server, olabel);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003111 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003112 switch (err) {
3113 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003114 if (!(sattr->ia_valid & ATTR_SIZE)) {
3115 pr_warn_once("NFSv4: server %s is incorrectly "
3116 "applying open mode checks to "
3117 "a SETATTR that is not "
3118 "changing file size.\n",
3119 server->nfs_client->cl_hostname);
3120 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003121 if (state && !(state->state & FMODE_WRITE)) {
3122 err = -EBADF;
3123 if (sattr->ia_valid & ATTR_OPEN)
3124 err = -EACCES;
3125 goto out;
3126 }
3127 }
3128 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003130out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return err;
3132}
3133
Peng Tao500d7012015-09-22 11:35:22 +08003134static bool
3135nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3136{
3137 if (inode == NULL || !nfs_have_layout(inode))
3138 return false;
3139
3140 return pnfs_wait_on_layoutreturn(inode, task);
3141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143struct nfs4_closedata {
3144 struct inode *inode;
3145 struct nfs4_state *state;
3146 struct nfs_closeargs arg;
3147 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003148 struct {
3149 struct nfs4_layoutreturn_args arg;
3150 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003151 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003152 u32 roc_barrier;
3153 bool roc;
3154 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003155 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003156 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157};
3158
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003159static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003160{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003161 struct nfs4_closedata *calldata = data;
3162 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003163 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003164
Trond Myklebustcf805162016-11-15 14:56:07 -05003165 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003166 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3167 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003168 nfs4_put_open_state(calldata->state);
3169 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003170 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003171 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003172 kfree(calldata);
3173}
3174
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003175static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003180 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003181 struct nfs4_exception exception = {
3182 .state = state,
3183 .inode = calldata->inode,
3184 .stateid = &calldata->arg.stateid,
3185 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Chuck Levera3ca5652012-03-01 17:00:40 -05003187 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003188 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3189 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003190 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003191
3192 /* Handle Layoutreturn errors */
3193 if (calldata->arg.lr_args && task->tk_status != 0) {
3194 switch (calldata->res.lr_ret) {
3195 default:
3196 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3197 break;
3198 case 0:
3199 calldata->arg.lr_args = NULL;
3200 calldata->res.lr_res = NULL;
3201 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003202 case -NFS4ERR_OLD_STATEID:
3203 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003204 calldata->inode))
3205 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003206 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003207 case -NFS4ERR_ADMIN_REVOKED:
3208 case -NFS4ERR_DELEG_REVOKED:
3209 case -NFS4ERR_EXPIRED:
3210 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003211 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3212 case -NFS4ERR_WRONG_CRED:
3213 calldata->arg.lr_args = NULL;
3214 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003215 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003216 }
3217 }
3218
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003219 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 * the state_owner. we keep this around to process errors
3221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 switch (task->tk_status) {
3223 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003224 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003225 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003226 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003227 case -NFS4ERR_ACCESS:
3228 if (calldata->arg.bitmask != NULL) {
3229 calldata->arg.bitmask = NULL;
3230 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003231 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003232
3233 }
3234 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003235 case -NFS4ERR_OLD_STATEID:
3236 /* Did we race with OPEN? */
3237 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003238 state))
3239 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003240 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003241 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003243 case -NFS4ERR_EXPIRED:
3244 nfs4_free_revoked_stateid(server,
3245 &calldata->arg.stateid,
3246 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003247 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003248 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003251 task->tk_status = nfs4_async_handle_exception(task,
3252 server, task->tk_status, &exception);
3253 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003254 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003256 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3257 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003258out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003259 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003260 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003261 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003262 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003263 return;
3264lr_restart:
3265 calldata->res.lr_ret = 0;
3266out_restart:
3267 task->tk_status = 0;
3268 rpc_restart_call_prepare(task);
3269 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003272static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003274 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003275 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003276 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003277 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003278 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003279
Chuck Levera3ca5652012-03-01 17:00:40 -05003280 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003281 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003282 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003283
Trond Myklebust88069f72009-12-08 08:33:16 -05003284 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003285 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003286 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3287 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3288 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003289 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003290 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003291 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003292 if (state->n_rdonly == 0)
3293 call_close |= is_rdonly;
3294 else if (is_rdonly)
3295 calldata->arg.fmode |= FMODE_READ;
3296 if (state->n_wronly == 0)
3297 call_close |= is_wronly;
3298 else if (is_wronly)
3299 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003300 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3301 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003302 } else if (is_rdwr)
3303 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3304
Trond Myklebust5cc78612016-11-14 11:19:56 -05003305 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003306 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003307 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003308 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003309
3310 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003311 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003312 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003313 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003314
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003315 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003316 nfs_release_seqid(calldata->arg.seqid);
3317 goto out_wait;
3318 }
3319
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003320 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003321 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003322
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003323 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003324 /* Close-to-open cache consistency revalidation */
3325 if (!nfs4_have_delegation(inode, FMODE_READ))
3326 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3327 else
3328 calldata->arg.bitmask = NULL;
3329 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003330
Trond Myklebust6ae37332015-01-30 14:21:14 -05003331 calldata->arg.share_access =
3332 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3333 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003334
Trond Myklebustd8d84982016-12-19 12:14:44 -05003335 if (calldata->res.fattr == NULL)
3336 calldata->arg.bitmask = NULL;
3337 else if (calldata->arg.bitmask == NULL)
3338 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003339 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003340 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003341 &calldata->arg.seq_args,
3342 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003343 task) != 0)
3344 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003345 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003346 return;
3347out_no_action:
3348 task->tk_action = NULL;
3349out_wait:
3350 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003353static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003354 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003355 .rpc_call_done = nfs4_close_done,
3356 .rpc_release = nfs4_free_closedata,
3357};
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359/*
3360 * It is possible for data to be read/written from a mem-mapped file
3361 * after the sys_close call (which hits the vfs layer as a flush).
3362 * This means that we can't safely call nfsv4 close on a file until
3363 * the inode is cleared. This in turn means that we are not good
3364 * NFSv4 citizens - we do not indicate to the server to update the file's
3365 * share state even when we are done with one of the three share
3366 * stateid's in the inode.
3367 *
3368 * NOTE: Caller must be holding the sp->so_owner semaphore!
3369 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003370int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003372 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003373 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003375 struct nfs4_state_owner *sp = state->owner;
3376 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3379 .rpc_cred = state->owner->so_cred,
3380 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003381 struct rpc_task_setup task_setup_data = {
3382 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003383 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003384 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003385 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003386 .flags = RPC_TASK_ASYNC,
3387 };
Trond Myklebust95121352005-10-18 14:20:12 -07003388 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003390 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3391 &task_setup_data.rpc_client, &msg);
3392
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003393 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003395 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003396 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003397 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003399 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003400 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3401 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003403 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3404 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003405 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003406 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003407 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003408 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003409 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003410 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003411 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003412 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003413 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003414 calldata->lr.roc = pnfs_roc(state->inode,
3415 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3416 if (calldata->lr.roc) {
3417 calldata->arg.lr_args = &calldata->lr.arg;
3418 calldata->res.lr_res = &calldata->lr.res;
3419 }
Al Viro643168c2011-06-22 18:20:23 -04003420 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003421
Trond Myklebust1174dd12010-12-21 10:52:24 -05003422 msg.rpc_argp = &calldata->arg;
3423 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003424 task_setup_data.callback_data = calldata;
3425 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003426 if (IS_ERR(task))
3427 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003428 status = 0;
3429 if (wait)
3430 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003431 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003432 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003433out_free_calldata:
3434 kfree(calldata);
3435out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003436 nfs4_put_open_state(state);
3437 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
Trond Myklebust2b484292010-09-17 10:56:51 -04003441static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003442nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3443 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003446 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3447
3448 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Trond Myklebust565277f2007-10-15 18:17:53 -04003450 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003451 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003452
3453 nfs4_label_release_security(label);
3454
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003455 if (IS_ERR(state))
3456 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003457 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Trond Myklebust1185a552009-12-03 15:54:02 -05003460static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003461{
3462 if (ctx->state == NULL)
3463 return;
3464 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003465 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003466 else
Al Viro643168c2011-06-22 18:20:23 -04003467 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Trond Myklebustb944dba2013-11-04 15:20:20 -05003470#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3471#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003472#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3475{
Kinglong Mee8c612822015-08-26 21:12:58 +08003476 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003477 struct nfs4_server_caps_arg args = {
3478 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003479 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 struct nfs4_server_caps_res res = {};
3482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003484 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .rpc_resp = &res,
3486 };
3487 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003488 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Kinglong Mee8c612822015-08-26 21:12:58 +08003490 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3491 FATTR4_WORD0_FH_EXPIRE_TYPE |
3492 FATTR4_WORD0_LINK_SUPPORT |
3493 FATTR4_WORD0_SYMLINK_SUPPORT |
3494 FATTR4_WORD0_ACLSUPPORT;
3495 if (minorversion)
3496 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3497
Bryan Schumaker7c513052011-03-24 17:12:24 +00003498 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003500 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003501 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003502 case 0:
3503 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3504 res.attr_bitmask[2] = 0;
3505 break;
3506 case 1:
3507 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3508 break;
3509 case 2:
3510 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003513 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3514 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3515 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3516 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003517 NFS_CAP_CTIME|NFS_CAP_MTIME|
3518 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003519 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3520 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 server->caps |= NFS_CAP_ACLS;
3522 if (res.has_links != 0)
3523 server->caps |= NFS_CAP_HARDLINKS;
3524 if (res.has_symlinks != 0)
3525 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003526 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3527 server->caps |= NFS_CAP_FILEID;
3528 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3529 server->caps |= NFS_CAP_MODE;
3530 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3531 server->caps |= NFS_CAP_NLINK;
3532 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3533 server->caps |= NFS_CAP_OWNER;
3534 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3535 server->caps |= NFS_CAP_OWNER_GROUP;
3536 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3537 server->caps |= NFS_CAP_ATIME;
3538 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3539 server->caps |= NFS_CAP_CTIME;
3540 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3541 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003542#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3543 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3544 server->caps |= NFS_CAP_SECURITY_LABEL;
3545#endif
3546 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3547 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003548 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003549
Trond Myklebusta65318b2009-03-11 14:10:28 -04003550 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3551 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3552 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003553 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003554
3555 /* Avoid a regression due to buggy server */
3556 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3557 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003558 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3559 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003562 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return status;
3566}
3567
Trond Myklebust55a97592006-06-09 09:34:19 -04003568int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
3570 struct nfs4_exception exception = { };
3571 int err;
3572 do {
3573 err = nfs4_handle_exception(server,
3574 _nfs4_server_capabilities(server, fhandle),
3575 &exception);
3576 } while (exception.retry);
3577 return err;
3578}
3579
3580static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3581 struct nfs_fsinfo *info)
3582{
David Quigleyaa9c2662013-05-22 12:50:44 -04003583 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003585 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 };
3587 struct nfs4_lookup_res res = {
3588 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003589 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 .fh = fhandle,
3591 };
3592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3594 .rpc_argp = &args,
3595 .rpc_resp = &res,
3596 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003597
David Quigleyaa9c2662013-05-22 12:50:44 -04003598 bitmask[0] = nfs4_fattr_bitmap[0];
3599 bitmask[1] = nfs4_fattr_bitmap[1];
3600 /*
3601 * Process the label in the upcoming getfattr
3602 */
3603 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3604
Trond Myklebust0e574af2005-10-27 22:12:38 -04003605 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003606 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
3609static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3610 struct nfs_fsinfo *info)
3611{
3612 struct nfs4_exception exception = { };
3613 int err;
3614 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003615 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003616 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003617 switch (err) {
3618 case 0:
3619 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003620 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003621 default:
3622 err = nfs4_handle_exception(server, err, &exception);
3623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003625out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 return err;
3627}
3628
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003629static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3630 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3631{
Trond Myklebustc2190662013-08-26 19:23:04 -04003632 struct rpc_auth_create_args auth_args = {
3633 .pseudoflavor = flavor,
3634 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003635 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003636
Trond Myklebustc2190662013-08-26 19:23:04 -04003637 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003638 if (IS_ERR(auth))
3639 return -EACCES;
3640 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003641}
3642
Chuck Lever9a744ba2013-03-16 15:56:02 -04003643/*
3644 * Retry pseudoroot lookup with various security flavors. We do this when:
3645 *
3646 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3647 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3648 *
3649 * Returns zero on success, or a negative NFS4ERR value, or a
3650 * negative errno value.
3651 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003652static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003653 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003655 /* Per 3530bis 15.33.5 */
3656 static const rpc_authflavor_t flav_array[] = {
3657 RPC_AUTH_GSS_KRB5P,
3658 RPC_AUTH_GSS_KRB5I,
3659 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003660 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003661 RPC_AUTH_NULL,
3662 };
3663 int status = -EPERM;
3664 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003666 if (server->auth_info.flavor_len > 0) {
3667 /* try each flavor specified by user */
3668 for (i = 0; i < server->auth_info.flavor_len; i++) {
3669 status = nfs4_lookup_root_sec(server, fhandle, info,
3670 server->auth_info.flavors[i]);
3671 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3672 continue;
3673 break;
3674 }
3675 } else {
3676 /* no flavors specified by user, try default list */
3677 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3678 status = nfs4_lookup_root_sec(server, fhandle, info,
3679 flav_array[i]);
3680 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3681 continue;
3682 break;
3683 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003684 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003685
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003686 /*
3687 * -EACCESS could mean that the user doesn't have correct permissions
3688 * to access the mount. It could also mean that we tried to mount
3689 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3690 * existing mount programs don't handle -EACCES very well so it should
3691 * be mapped to -EPERM instead.
3692 */
3693 if (status == -EACCES)
3694 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003695 return status;
3696}
3697
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003698/**
3699 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3700 * @server: initialized nfs_server handle
3701 * @fhandle: we fill in the pseudo-fs root file handle
3702 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003703 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003704 *
3705 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003706 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003707int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003708 struct nfs_fsinfo *info,
3709 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003710{
Andre Przywarac7757072015-04-23 17:17:40 +01003711 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003712
Andre Przywarac7757072015-04-23 17:17:40 +01003713 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003714 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003715
3716 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003717 status = server->nfs_client->cl_mvops->find_root_sec(server,
3718 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 if (status == 0)
3721 status = nfs4_server_capabilities(server, fhandle);
3722 if (status == 0)
3723 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003724
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003725 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Bryan Schumakerbae36242012-05-10 15:07:31 -04003728static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3729 struct nfs_fsinfo *info)
3730{
3731 int error;
3732 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003733 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003734
3735 error = nfs4_server_capabilities(server, mntfh);
3736 if (error < 0) {
3737 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3738 return error;
3739 }
3740
David Quigley14c43f72013-05-22 12:50:43 -04003741 label = nfs4_label_alloc(server, GFP_KERNEL);
3742 if (IS_ERR(label))
3743 return PTR_ERR(label);
3744
David Quigley1775fd32013-05-22 12:50:42 -04003745 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003746 if (error < 0) {
3747 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003748 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003749 }
3750
3751 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3752 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3753 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3754
David Quigley14c43f72013-05-22 12:50:43 -04003755err_free_label:
3756 nfs4_label_free(label);
3757
Bryan Schumakerbae36242012-05-10 15:07:31 -04003758 return error;
3759}
3760
Manoj Naik6b97fd32006-06-09 09:34:29 -04003761/*
3762 * Get locations and (maybe) other attributes of a referral.
3763 * Note that we'll actually follow the referral later when
3764 * we detect fsid mismatch in inode revalidation
3765 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003766static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3767 const struct qstr *name, struct nfs_fattr *fattr,
3768 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003769{
3770 int status = -ENOMEM;
3771 struct page *page = NULL;
3772 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003773
3774 page = alloc_page(GFP_KERNEL);
3775 if (page == NULL)
3776 goto out;
3777 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3778 if (locations == NULL)
3779 goto out;
3780
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003781 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003782 if (status != 0)
3783 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003784
3785 /*
3786 * If the fsid didn't change, this is a migration event, not a
3787 * referral. Cause us to drop into the exception handler, which
3788 * will kick off migration recovery.
3789 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003790 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003791 dprintk("%s: server did not return a different fsid for"
3792 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003793 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003794 goto out;
3795 }
Andy Adamson533eb462011-06-13 18:25:56 -04003796 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3797 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003798
Andy Adamson533eb462011-06-13 18:25:56 -04003799 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003800 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003801 memset(fhandle, 0, sizeof(struct nfs_fh));
3802out:
3803 if (page)
3804 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003805 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003806 return status;
3807}
3808
David Quigley1775fd32013-05-22 12:50:42 -04003809static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3810 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 struct nfs4_getattr_arg args = {
3813 .fh = fhandle,
3814 .bitmask = server->attr_bitmask,
3815 };
3816 struct nfs4_getattr_res res = {
3817 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003818 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 .server = server,
3820 };
3821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3823 .rpc_argp = &args,
3824 .rpc_resp = &res,
3825 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003826
3827 args.bitmask = nfs4_bitmask(server, label);
3828
Trond Myklebust0e574af2005-10-27 22:12:38 -04003829 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003830 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
David Quigley1775fd32013-05-22 12:50:42 -04003833static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3834 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
3836 struct nfs4_exception exception = { };
3837 int err;
3838 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003839 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3840 trace_nfs4_getattr(server, fhandle, fattr, err);
3841 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 &exception);
3843 } while (exception.retry);
3844 return err;
3845}
3846
3847/*
3848 * The file is not closed if it is opened due to the a request to change
3849 * the size of the file. The open call will not be needed once the
3850 * VFS layer lookup-intents are implemented.
3851 *
3852 * Close is called when the inode is destroyed.
3853 * If we haven't opened the file for O_WRONLY, we
3854 * need to in the size_change case to obtain a stateid.
3855 *
3856 * Got race?
3857 * Because OPEN is always done by name in nfsv4, it is
3858 * possible that we opened a different file by the same
3859 * name. We can recognize this race condition, but we
3860 * can't do anything about it besides returning an error.
3861 *
3862 * This will be fixed with VFS changes (lookup-intent).
3863 */
3864static int
3865nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3866 struct iattr *sattr)
3867{
David Howells2b0143b2015-03-17 22:25:59 +00003868 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003869 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003870 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003871 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 int status;
3873
Peng Tao88ac8152014-09-12 11:04:10 +08003874 if (pnfs_ld_layoutret_on_setattr(inode) &&
3875 sattr->ia_valid & ATTR_SIZE &&
3876 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003877 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003878
Trond Myklebust0e574af2005-10-27 22:12:38 -04003879 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Andy Adamson26699402012-05-30 16:12:24 -04003881 /* Deal with open(O_TRUNC) */
3882 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003883 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003884
3885 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003886 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003887 return 0;
3888
Trond Myklebustd5308382005-11-04 15:33:38 -05003889 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003890 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003891
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003892 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003893 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003894 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003895 }
3896
David Quigley14c43f72013-05-22 12:50:43 -04003897 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3898 if (IS_ERR(label))
3899 return PTR_ERR(label);
3900
Trond Myklebust199366f2018-03-20 16:43:18 -04003901 /* Return any delegations if we're going to change ACLs */
3902 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003903 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003904
NeilBrown29b59f92016-10-13 15:26:47 +11003905 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003906 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003907 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003908 nfs_setsecurity(inode, fattr, label);
3909 }
David Quigley14c43f72013-05-22 12:50:43 -04003910 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return status;
3912}
3913
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003914static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3915 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003916 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003917{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003918 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003919 int status;
3920 struct nfs4_lookup_arg args = {
3921 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003922 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003923 .name = name,
3924 };
3925 struct nfs4_lookup_res res = {
3926 .server = server,
3927 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003929 .fh = fhandle,
3930 };
3931 struct rpc_message msg = {
3932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3933 .rpc_argp = &args,
3934 .rpc_resp = &res,
3935 };
3936
David Quigleyaa9c2662013-05-22 12:50:44 -04003937 args.bitmask = nfs4_bitmask(server, label);
3938
David Howells2b3de442006-08-22 20:06:09 -04003939 nfs_fattr_init(fattr);
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003942 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 dprintk("NFS reply lookup: %d\n", status);
3944 return status;
3945}
3946
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003947static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003948{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003949 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003950 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003951 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3952 fattr->nlink = 2;
3953}
3954
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003955static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003956 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003957 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003958{
3959 struct nfs4_exception exception = { };
3960 struct rpc_clnt *client = *clnt;
3961 int err;
3962 do {
David Quigley1775fd32013-05-22 12:50:42 -04003963 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003964 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003965 switch (err) {
3966 case -NFS4ERR_BADNAME:
3967 err = -ENOENT;
3968 goto out;
3969 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003970 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003971 if (err == -NFS4ERR_MOVED)
3972 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003973 goto out;
3974 case -NFS4ERR_WRONGSEC:
3975 err = -EPERM;
3976 if (client != *clnt)
3977 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003978 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003979 if (IS_ERR(client))
3980 return PTR_ERR(client);
3981
3982 exception.retry = 1;
3983 break;
3984 default:
3985 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3986 }
3987 } while (exception.retry);
3988
3989out:
3990 if (err == 0)
3991 *clnt = client;
3992 else if (client != *clnt)
3993 rpc_shutdown_client(client);
3994
3995 return err;
3996}
3997
Al Virobeffb8f2016-07-20 16:34:42 -04003998static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003999 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4000 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004002 int status;
4003 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004004
David Quigley1775fd32013-05-22 12:50:42 -04004005 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004006 if (client != NFS_CLIENT(dir)) {
4007 rpc_shutdown_client(client);
4008 nfs_fixup_secinfo_attributes(fattr);
4009 }
4010 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004013struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004014nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004015 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4016{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004017 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004018 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004019
David Quigley1775fd32013-05-22 12:50:42 -04004020 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004021 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004022 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004023 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004024}
4025
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004026static int _nfs4_proc_lookupp(struct inode *inode,
4027 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4028 struct nfs4_label *label)
4029{
4030 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4031 struct nfs_server *server = NFS_SERVER(inode);
4032 int status;
4033 struct nfs4_lookupp_arg args = {
4034 .bitmask = server->attr_bitmask,
4035 .fh = NFS_FH(inode),
4036 };
4037 struct nfs4_lookupp_res res = {
4038 .server = server,
4039 .fattr = fattr,
4040 .label = label,
4041 .fh = fhandle,
4042 };
4043 struct rpc_message msg = {
4044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4045 .rpc_argp = &args,
4046 .rpc_resp = &res,
4047 };
4048
4049 args.bitmask = nfs4_bitmask(server, label);
4050
4051 nfs_fattr_init(fattr);
4052
4053 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4054 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4055 &res.seq_res, 0);
4056 dprintk("NFS reply lookupp: %d\n", status);
4057 return status;
4058}
4059
4060static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4061 struct nfs_fattr *fattr, struct nfs4_label *label)
4062{
4063 struct nfs4_exception exception = { };
4064 int err;
4065 do {
4066 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4067 trace_nfs4_lookupp(inode, err);
4068 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4069 &exception);
4070 } while (exception.retry);
4071 return err;
4072}
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4075{
Trond Myklebust76b32992007-08-10 17:45:11 -04004076 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 struct nfs4_accessargs args = {
4078 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004079 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004081 struct nfs4_accessres res = {
4082 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 struct rpc_message msg = {
4085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4086 .rpc_argp = &args,
4087 .rpc_resp = &res,
4088 .rpc_cred = entry->cred,
4089 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004090 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004092 if (!nfs_have_delegated_attributes(inode)) {
4093 res.fattr = nfs_alloc_fattr();
4094 if (res.fattr == NULL)
4095 return -ENOMEM;
4096 args.bitmask = server->cache_consistency_bitmask;
4097 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004098
Bryan Schumaker7c513052011-03-24 17:12:24 +00004099 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004101 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004102 if (res.fattr)
4103 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004105 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return status;
4107}
4108
4109static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4110{
4111 struct nfs4_exception exception = { };
4112 int err;
4113 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004114 err = _nfs4_proc_access(inode, entry);
4115 trace_nfs4_access(inode, err);
4116 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 &exception);
4118 } while (exception.retry);
4119 return err;
4120}
4121
4122/*
4123 * TODO: For the time being, we don't try to get any attributes
4124 * along with any of the zero-copy operations READ, READDIR,
4125 * READLINK, WRITE.
4126 *
4127 * In the case of the first three, we want to put the GETATTR
4128 * after the read-type operation -- this is because it is hard
4129 * to predict the length of a GETATTR response in v4, and thus
4130 * align the READ data correctly. This means that the GETATTR
4131 * may end up partially falling into the page cache, and we should
4132 * shift it into the 'tail' of the xdr_buf before processing.
4133 * To do this efficiently, we need to know the total length
4134 * of data received, which doesn't seem to be available outside
4135 * of the RPC layer.
4136 *
4137 * In the case of WRITE, we also want to put the GETATTR after
4138 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004139 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 *
4141 * Both of these changes to the XDR layer would in fact be quite
4142 * minor, but I decided to leave them for a subsequent patch.
4143 */
4144static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4145 unsigned int pgbase, unsigned int pglen)
4146{
4147 struct nfs4_readlink args = {
4148 .fh = NFS_FH(inode),
4149 .pgbase = pgbase,
4150 .pglen = pglen,
4151 .pages = &page,
4152 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004153 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 struct rpc_message msg = {
4155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4156 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 };
4159
Bryan Schumaker7c513052011-03-24 17:12:24 +00004160 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 -07004161}
4162
4163static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4164 unsigned int pgbase, unsigned int pglen)
4165{
4166 struct nfs4_exception exception = { };
4167 int err;
4168 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004169 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4170 trace_nfs4_readlink(inode, err);
4171 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 &exception);
4173 } while (exception.retry);
4174 return err;
4175}
4176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004178 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180static int
4181nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004182 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004184 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004185 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004186 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 int status = 0;
4189
NeilBrown532d4de2016-10-13 15:26:47 +11004190 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004191 if (IS_ERR(ctx))
4192 return PTR_ERR(ctx);
4193
David Quigleyaa9c2662013-05-22 12:50:44 -04004194 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4195
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004196 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4197 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004198 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (IS_ERR(state)) {
4200 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004204 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004205 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return status;
4207}
4208
Al Virobeffb8f2016-07-20 16:34:42 -04004209static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
Trond Myklebust16e42952005-10-27 22:12:44 -04004211 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004212 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004214 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004216 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004217 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004218 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4221 .rpc_argp = &args,
4222 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004224 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004225 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004226
Bryan Schumaker7c513052011-03-24 17:12:24 +00004227 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004228 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004229 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return status;
4231}
4232
Trond Myklebust912678d2018-03-20 16:43:15 -04004233static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4234{
4235 struct nfs4_exception exception = { };
4236 struct inode *inode = d_inode(dentry);
4237 int err;
4238
Trond Myklebustc01d3642018-03-20 16:43:20 -04004239 if (inode) {
4240 if (inode->i_nlink == 1)
4241 nfs4_inode_return_delegation(inode);
4242 else
4243 nfs4_inode_make_writeable(inode);
4244 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004245 do {
4246 err = _nfs4_proc_remove(dir, &dentry->d_name);
4247 trace_nfs4_remove(dir, &dentry->d_name, err);
4248 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4249 &exception);
4250 } while (exception.retry);
4251 return err;
4252}
4253
4254static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
4256 struct nfs4_exception exception = { };
4257 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004260 err = _nfs4_proc_remove(dir, name);
4261 trace_nfs4_remove(dir, name, err);
4262 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 &exception);
4264 } while (exception.retry);
4265 return err;
4266}
4267
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004268static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4269 struct dentry *dentry,
4270 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004272 struct nfs_removeargs *args = msg->rpc_argp;
4273 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Trond Myklebust977fcc22018-03-20 16:43:17 -04004275 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004277 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004278
4279 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004280
4281 if (inode)
4282 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004285static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4286{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004287 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004288 &data->args.seq_args,
4289 &data->res.seq_res,
4290 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004293static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004295 struct nfs_unlinkdata *data = task->tk_calldata;
4296 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004297
Trond Myklebust14516c32010-07-31 14:29:06 -04004298 if (!nfs4_sequence_done(task, &res->seq_res))
4299 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004300 if (nfs4_async_handle_error(task, res->server, NULL,
4301 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004302 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004303 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004304 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004305 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004308static void nfs4_proc_rename_setup(struct rpc_message *msg,
4309 struct dentry *old_dentry,
4310 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004311{
Jeff Laytond3d41522010-09-17 17:31:57 -04004312 struct nfs_renameargs *arg = msg->rpc_argp;
4313 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004314 struct inode *old_inode = d_inode(old_dentry);
4315 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004316
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004317 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004318 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004319 if (new_inode)
4320 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004321 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004322 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004323 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004324}
4325
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004326static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4327{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004328 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004329 &data->args.seq_args,
4330 &data->res.seq_res,
4331 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004332}
4333
4334static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4335 struct inode *new_dir)
4336{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004337 struct nfs_renamedata *data = task->tk_calldata;
4338 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004339
4340 if (!nfs4_sequence_done(task, &res->seq_res))
4341 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004342 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004343 return 0;
4344
Trond Myklebustc733c492017-01-11 12:32:26 -05004345 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004346 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004347 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004348 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004349 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004350 return 1;
4351}
4352
Al Virobeffb8f2016-07-20 16:34:42 -04004353static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004355 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 struct nfs4_link_arg arg = {
4357 .fh = NFS_FH(inode),
4358 .dir_fh = NFS_FH(dir),
4359 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004360 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004362 struct nfs4_link_res res = {
4363 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004364 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004365 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4368 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004369 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004371 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Trond Myklebust136f2622010-04-16 16:22:49 -04004373 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004374 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004375 goto out;
4376
David Quigley14c43f72013-05-22 12:50:43 -04004377 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4378 if (IS_ERR(res.label)) {
4379 status = PTR_ERR(res.label);
4380 goto out;
4381 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004382 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004383
Trond Myklebustc01d3642018-03-20 16:43:20 -04004384 nfs4_inode_make_writeable(inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004385
Bryan Schumaker7c513052011-03-24 17:12:24 +00004386 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004387 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004388 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004389 status = nfs_post_op_update_inode(inode, res.fattr);
4390 if (!status)
4391 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004392 }
David Quigley14c43f72013-05-22 12:50:43 -04004393
4394
4395 nfs4_label_free(res.label);
4396
Trond Myklebust136f2622010-04-16 16:22:49 -04004397out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004398 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return status;
4400}
4401
Al Virobeffb8f2016-07-20 16:34:42 -04004402static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
4404 struct nfs4_exception exception = { };
4405 int err;
4406 do {
4407 err = nfs4_handle_exception(NFS_SERVER(inode),
4408 _nfs4_proc_link(inode, dir, name),
4409 &exception);
4410 } while (exception.retry);
4411 return err;
4412}
4413
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414struct nfs4_createdata {
4415 struct rpc_message msg;
4416 struct nfs4_create_arg arg;
4417 struct nfs4_create_res res;
4418 struct nfs_fh fh;
4419 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004420 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004421};
4422
4423static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004424 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004425{
4426 struct nfs4_createdata *data;
4427
4428 data = kzalloc(sizeof(*data), GFP_KERNEL);
4429 if (data != NULL) {
4430 struct nfs_server *server = NFS_SERVER(dir);
4431
David Quigley14c43f72013-05-22 12:50:43 -04004432 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4433 if (IS_ERR(data->label))
4434 goto out_free;
4435
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004436 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4437 data->msg.rpc_argp = &data->arg;
4438 data->msg.rpc_resp = &data->res;
4439 data->arg.dir_fh = NFS_FH(dir);
4440 data->arg.server = server;
4441 data->arg.name = name;
4442 data->arg.attrs = sattr;
4443 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004444 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004445 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004446 data->res.server = server;
4447 data->res.fh = &data->fh;
4448 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004449 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004451 }
4452 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004453out_free:
4454 kfree(data);
4455 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004456}
4457
4458static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4459{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004460 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004461 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004462 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004463 update_changeattr(dir, &data->res.dir_cinfo,
4464 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004465 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004466 }
4467 return status;
4468}
4469
4470static void nfs4_free_createdata(struct nfs4_createdata *data)
4471{
David Quigley14c43f72013-05-22 12:50:43 -04004472 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004473 kfree(data);
4474}
4475
Chuck Lever4f390c12006-08-22 20:06:22 -04004476static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004477 struct page *page, unsigned int len, struct iattr *sattr,
4478 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004480 struct nfs4_createdata *data;
4481 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Chuck Lever94a6d752006-08-22 20:06:23 -04004483 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004484 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004485
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004486 status = -ENOMEM;
4487 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4488 if (data == NULL)
4489 goto out;
4490
4491 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4492 data->arg.u.symlink.pages = &page;
4493 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004494 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004496 status = nfs4_do_create(dir, dentry, data);
4497
4498 nfs4_free_createdata(data);
4499out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return status;
4501}
4502
Chuck Lever4f390c12006-08-22 20:06:22 -04004503static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004504 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
4506 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004507 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004509
4510 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004513 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4514 trace_nfs4_symlink(dir, &dentry->d_name, err);
4515 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 &exception);
4517 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004518
4519 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return err;
4521}
4522
4523static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004524 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004526 struct nfs4_createdata *data;
4527 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004529 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4530 if (data == NULL)
4531 goto out;
4532
David Quigley1775fd32013-05-22 12:50:42 -04004533 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004534 status = nfs4_do_create(dir, dentry, data);
4535
4536 nfs4_free_createdata(data);
4537out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return status;
4539}
4540
4541static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4542 struct iattr *sattr)
4543{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004544 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004546 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004548
David Quigleyaa9c2662013-05-22 12:50:44 -04004549 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4550
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004551 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4552 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004554 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4555 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4556 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 &exception);
4558 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004559 nfs4_label_release_security(label);
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return err;
4562}
4563
4564static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004565 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
David Howells2b0143b2015-03-17 22:25:59 +00004567 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 struct nfs4_readdir_arg args = {
4569 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004570 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 .pgbase = 0,
4572 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004573 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004574 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 };
4576 struct nfs4_readdir_res res;
4577 struct rpc_message msg = {
4578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4579 .rpc_argp = &args,
4580 .rpc_resp = &res,
4581 .rpc_cred = cred,
4582 };
4583 int status;
4584
Al Viro6de14722013-09-16 10:53:17 -04004585 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4586 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004587 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004588 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004590 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 -05004591 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004592 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004593 status += args.pgbase;
4594 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004595
4596 nfs_invalidate_atime(dir);
4597
Harvey Harrison3110ff82008-05-02 13:42:44 -07004598 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 return status;
4600}
4601
4602static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004603 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
4605 struct nfs4_exception exception = { };
4606 int err;
4607 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004608 err = _nfs4_proc_readdir(dentry, cred, cookie,
4609 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004610 trace_nfs4_readdir(d_inode(dentry), err);
4611 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 &exception);
4613 } while (exception.retry);
4614 return err;
4615}
4616
4617static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004618 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004620 struct nfs4_createdata *data;
4621 int mode = sattr->ia_mode;
4622 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004624 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4625 if (data == NULL)
4626 goto out;
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004629 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004631 data->arg.ftype = NF4BLK;
4632 data->arg.u.device.specdata1 = MAJOR(rdev);
4633 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 }
4635 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004636 data->arg.ftype = NF4CHR;
4637 data->arg.u.device.specdata1 = MAJOR(rdev);
4638 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004639 } else if (!S_ISSOCK(mode)) {
4640 status = -EINVAL;
4641 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
David Quigley1775fd32013-05-22 12:50:42 -04004643
David Quigleyaa9c2662013-05-22 12:50:44 -04004644 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004645 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004646out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004647 nfs4_free_createdata(data);
4648out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return status;
4650}
4651
4652static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4653 struct iattr *sattr, dev_t rdev)
4654{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004655 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004657 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004659
David Quigleyaa9c2662013-05-22 12:50:44 -04004660 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4661
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004662 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4663 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004665 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4666 trace_nfs4_mknod(dir, &dentry->d_name, err);
4667 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 &exception);
4669 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004670
4671 nfs4_label_release_security(label);
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return err;
4674}
4675
4676static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4677 struct nfs_fsstat *fsstat)
4678{
4679 struct nfs4_statfs_arg args = {
4680 .fh = fhandle,
4681 .bitmask = server->attr_bitmask,
4682 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004683 struct nfs4_statfs_res res = {
4684 .fsstat = fsstat,
4685 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 struct rpc_message msg = {
4687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4688 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004689 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 };
4691
Trond Myklebust0e574af2005-10-27 22:12:38 -04004692 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004693 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
4696static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4697{
4698 struct nfs4_exception exception = { };
4699 int err;
4700 do {
4701 err = nfs4_handle_exception(server,
4702 _nfs4_proc_statfs(server, fhandle, fsstat),
4703 &exception);
4704 } while (exception.retry);
4705 return err;
4706}
4707
4708static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4709 struct nfs_fsinfo *fsinfo)
4710{
4711 struct nfs4_fsinfo_arg args = {
4712 .fh = fhandle,
4713 .bitmask = server->attr_bitmask,
4714 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004715 struct nfs4_fsinfo_res res = {
4716 .fsinfo = fsinfo,
4717 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 struct rpc_message msg = {
4719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4720 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004721 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 };
4723
Bryan Schumaker7c513052011-03-24 17:12:24 +00004724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
4727static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4728{
4729 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004730 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 int err;
4732
4733 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004734 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004735 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004736 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004737 nfs4_set_lease_period(server->nfs_client,
4738 fsinfo->lease_time * HZ,
4739 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004740 break;
4741 }
4742 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 } while (exception.retry);
4744 return err;
4745}
4746
4747static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4748{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004749 int error;
4750
Trond Myklebust0e574af2005-10-27 22:12:38 -04004751 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004752 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004753 if (error == 0) {
4754 /* block layout checks this! */
4755 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004756 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004757 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004758
4759 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760}
4761
4762static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4763 struct nfs_pathconf *pathconf)
4764{
4765 struct nfs4_pathconf_arg args = {
4766 .fh = fhandle,
4767 .bitmask = server->attr_bitmask,
4768 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004769 struct nfs4_pathconf_res res = {
4770 .pathconf = pathconf,
4771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 struct rpc_message msg = {
4773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4774 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004775 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 };
4777
4778 /* None of the pathconf attributes are mandatory to implement */
4779 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4780 memset(pathconf, 0, sizeof(*pathconf));
4781 return 0;
4782 }
4783
Trond Myklebust0e574af2005-10-27 22:12:38 -04004784 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004785 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
4788static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4789 struct nfs_pathconf *pathconf)
4790{
4791 struct nfs4_exception exception = { };
4792 int err;
4793
4794 do {
4795 err = nfs4_handle_exception(server,
4796 _nfs4_proc_pathconf(server, fhandle, pathconf),
4797 &exception);
4798 } while (exception.retry);
4799 return err;
4800}
4801
Trond Myklebust5521abf2013-03-16 20:54:34 -04004802int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004803 const struct nfs_open_context *ctx,
4804 const struct nfs_lock_context *l_ctx,
4805 fmode_t fmode)
4806{
NeilBrown17393472016-10-13 15:26:47 +11004807 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004808}
4809EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4810
Trond Myklebust5521abf2013-03-16 20:54:34 -04004811static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4812 const struct nfs_open_context *ctx,
4813 const struct nfs_lock_context *l_ctx,
4814 fmode_t fmode)
4815{
4816 nfs4_stateid current_stateid;
4817
Trond Myklebuste1253be2014-03-05 08:44:23 -05004818 /* If the current stateid represents a lost lock, then exit */
4819 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4820 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004821 return nfs4_stateid_match(stateid, &current_stateid);
4822}
4823
4824static bool nfs4_error_stateid_expired(int err)
4825{
4826 switch (err) {
4827 case -NFS4ERR_DELEG_REVOKED:
4828 case -NFS4ERR_ADMIN_REVOKED:
4829 case -NFS4ERR_BAD_STATEID:
4830 case -NFS4ERR_STALE_STATEID:
4831 case -NFS4ERR_OLD_STATEID:
4832 case -NFS4ERR_OPENMODE:
4833 case -NFS4ERR_EXPIRED:
4834 return true;
4835 }
4836 return false;
4837}
4838
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004839static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004841 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004843 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004844 if (task->tk_status < 0) {
4845 struct nfs4_exception exception = {
4846 .inode = hdr->inode,
4847 .state = hdr->args.context->state,
4848 .stateid = &hdr->args.stateid,
4849 };
4850 task->tk_status = nfs4_async_handle_exception(task,
4851 server, task->tk_status, &exception);
4852 if (exception.retry) {
4853 rpc_restart_call_prepare(task);
4854 return -EAGAIN;
4855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004859 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861}
4862
Trond Myklebust5521abf2013-03-16 20:54:34 -04004863static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004864 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004865{
4866
4867 if (!nfs4_error_stateid_expired(task->tk_status) ||
4868 nfs4_stateid_is_current(&args->stateid,
4869 args->context,
4870 args->lock_context,
4871 FMODE_READ))
4872 return false;
4873 rpc_restart_call_prepare(task);
4874 return true;
4875}
4876
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004877static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004878{
4879
4880 dprintk("--> %s\n", __func__);
4881
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004882 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004883 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004884 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004885 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004886 if (task->tk_status > 0)
4887 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004888 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4889 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004890}
4891
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004892static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4893 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004895 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004896 if (!hdr->pgio_done_cb)
4897 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004898 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004899 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004902static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4903 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004904{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004905 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906 &hdr->args.seq_args,
4907 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004908 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004909 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004910 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4911 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004912 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004913 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004914 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004915 return -EIO;
4916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004919static int nfs4_write_done_cb(struct rpc_task *task,
4920 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004922 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004923
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004924 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004925 if (task->tk_status < 0) {
4926 struct nfs4_exception exception = {
4927 .inode = hdr->inode,
4928 .state = hdr->args.context->state,
4929 .stateid = &hdr->args.stateid,
4930 };
4931 task->tk_status = nfs4_async_handle_exception(task,
4932 NFS_SERVER(inode), task->tk_status,
4933 &exception);
4934 if (exception.retry) {
4935 rpc_restart_call_prepare(task);
4936 return -EAGAIN;
4937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004939 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004940 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004941 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004942 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
Trond Myklebust5521abf2013-03-16 20:54:34 -04004946static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004947 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004948{
4949
4950 if (!nfs4_error_stateid_expired(task->tk_status) ||
4951 nfs4_stateid_is_current(&args->stateid,
4952 args->context,
4953 args->lock_context,
4954 FMODE_WRITE))
4955 return false;
4956 rpc_restart_call_prepare(task);
4957 return true;
4958}
4959
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004960static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004961{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004962 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004963 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004964 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004965 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004966 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4967 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004968}
4969
Trond Myklebust5a37f852012-04-28 14:55:16 -04004970static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004971bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004972{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004973 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004974 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004975 return false;
4976 /* Otherwise, request attributes if and only if we don't hold
4977 * a delegation
4978 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004979 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004980}
Fred Isamana69aef12011-03-03 15:13:47 +00004981
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004982static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004983 struct rpc_message *msg,
4984 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004986 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004987
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004988 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4989 hdr->args.bitmask = NULL;
4990 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004991 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004992 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004993
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004994 if (!hdr->pgio_done_cb)
4995 hdr->pgio_done_cb = nfs4_write_done_cb;
4996 hdr->res.server = server;
4997 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004999 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005000 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005001 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
Fred Isaman0b7c0152012-04-20 14:47:39 -04005004static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5005{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005006 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005007 &data->args.seq_args,
5008 &data->res.seq_res,
5009 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010}
5011
Fred Isaman0b7c0152012-04-20 14:47:39 -04005012static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005015
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005016 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005017 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5018 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005019 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005020 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
Fred Isaman0b7c0152012-04-20 14:47:39 -04005025static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005026{
5027 if (!nfs4_sequence_done(task, &data->res.seq_res))
5028 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005029 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005030}
5031
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005032static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5033 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005035 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005036
Fred Isaman0b7c0152012-04-20 14:47:39 -04005037 if (data->commit_done_cb == NULL)
5038 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005039 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005040 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005041 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005042 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005045struct nfs4_renewdata {
5046 struct nfs_client *client;
5047 unsigned long timestamp;
5048};
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050/*
5051 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5052 * standalone procedure for queueing an asynchronous RENEW.
5053 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005054static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005055{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005056 struct nfs4_renewdata *data = calldata;
5057 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005058
Elena Reshetova212bf412017-10-20 12:53:38 +03005059 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005060 nfs4_schedule_state_renewal(clp);
5061 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005062 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005063}
5064
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005065static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005067 struct nfs4_renewdata *data = calldata;
5068 struct nfs_client *clp = data->client;
5069 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005071 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005072 switch (task->tk_status) {
5073 case 0:
5074 break;
5075 case -NFS4ERR_LEASE_MOVED:
5076 nfs4_schedule_lease_moved_recovery(clp);
5077 break;
5078 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005079 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005080 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5081 return;
5082 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005083 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005084 return;
5085 }
5086 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005088 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
5090
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005091static const struct rpc_call_ops nfs4_renew_ops = {
5092 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005093 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005094};
5095
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005096static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097{
5098 struct rpc_message msg = {
5099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5100 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005101 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005103 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005105 if (renew_flags == 0)
5106 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005107 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005108 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005109 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005110 if (data == NULL) {
5111 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005112 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005113 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005114 data->client = clp;
5115 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005116 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005117 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118}
5119
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005120static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121{
5122 struct rpc_message msg = {
5123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5124 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005125 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 };
5127 unsigned long now = jiffies;
5128 int status;
5129
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005130 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 if (status < 0)
5132 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005133 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 return 0;
5135}
5136
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005137static inline int nfs4_server_supports_acls(struct nfs_server *server)
5138{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005139 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005140}
5141
Trond Myklebust21f498c2012-08-24 10:59:25 -04005142/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5143 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005144 * the stack.
5145 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005146#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005147
Neil Hormane9e3d722011-03-04 19:26:03 -05005148static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005149 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005150{
5151 struct page *newpage, **spages;
5152 int rc = 0;
5153 size_t len;
5154 spages = pages;
5155
5156 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005157 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005158 newpage = alloc_page(GFP_KERNEL);
5159
5160 if (newpage == NULL)
5161 goto unwind;
5162 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005163 buf += len;
5164 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005165 *pages++ = newpage;
5166 rc++;
5167 } while (buflen != 0);
5168
5169 return rc;
5170
5171unwind:
5172 for(; rc > 0; rc--)
5173 __free_page(spages[rc-1]);
5174 return -ENOMEM;
5175}
5176
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005177struct nfs4_cached_acl {
5178 int cached;
5179 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005180 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005181};
5182
5183static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005184{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005185 struct nfs_inode *nfsi = NFS_I(inode);
5186
5187 spin_lock(&inode->i_lock);
5188 kfree(nfsi->nfs4_acl);
5189 nfsi->nfs4_acl = acl;
5190 spin_unlock(&inode->i_lock);
5191}
5192
5193static void nfs4_zap_acl_attr(struct inode *inode)
5194{
5195 nfs4_set_cached_acl(inode, NULL);
5196}
5197
5198static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5199{
5200 struct nfs_inode *nfsi = NFS_I(inode);
5201 struct nfs4_cached_acl *acl;
5202 int ret = -ENOENT;
5203
5204 spin_lock(&inode->i_lock);
5205 acl = nfsi->nfs4_acl;
5206 if (acl == NULL)
5207 goto out;
5208 if (buf == NULL) /* user is just asking for length */
5209 goto out_len;
5210 if (acl->cached == 0)
5211 goto out;
5212 ret = -ERANGE; /* see getxattr(2) man page */
5213 if (acl->len > buflen)
5214 goto out;
5215 memcpy(buf, acl->data, acl->len);
5216out_len:
5217 ret = acl->len;
5218out:
5219 spin_unlock(&inode->i_lock);
5220 return ret;
5221}
5222
Sachin Prabhu5794d212012-04-17 14:36:40 +01005223static 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 +00005224{
5225 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005226 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005227
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005228 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005229 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005230 if (acl == NULL)
5231 goto out;
5232 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005233 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005234 } else {
5235 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5236 if (acl == NULL)
5237 goto out;
5238 acl->cached = 0;
5239 }
5240 acl->len = acl_len;
5241out:
5242 nfs4_set_cached_acl(inode, acl);
5243}
5244
Andy Adamsonbf118a32011-12-07 11:55:27 -05005245/*
5246 * The getxattr API returns the required buffer length when called with a
5247 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5248 * the required buf. On a NULL buf, we send a page of data to the server
5249 * guessing that the ACL request can be serviced by a page. If so, we cache
5250 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5251 * the cache. If not so, we throw away the page, and cache the required
5252 * length. The next getxattr call will then produce another round trip to
5253 * the server, this time with the input buf of the required size.
5254 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005255static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005256{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005257 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005258 struct nfs_getaclargs args = {
5259 .fh = NFS_FH(inode),
5260 .acl_pages = pages,
5261 .acl_len = buflen,
5262 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005263 struct nfs_getaclres res = {
5264 .acl_len = buflen,
5265 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005266 struct rpc_message msg = {
5267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5268 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005269 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005270 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005271 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005272 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005273
Trond Myklebust21f498c2012-08-24 10:59:25 -04005274 if (npages > ARRAY_SIZE(pages))
5275 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005276
Andy Adamsonbf118a32011-12-07 11:55:27 -05005277 for (i = 0; i < npages; i++) {
5278 pages[i] = alloc_page(GFP_KERNEL);
5279 if (!pages[i])
5280 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005281 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005282
5283 /* for decoding across pages */
5284 res.acl_scratch = alloc_page(GFP_KERNEL);
5285 if (!res.acl_scratch)
5286 goto out_free;
5287
Andy Adamsonbf118a32011-12-07 11:55:27 -05005288 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005289
Peng Taode040be2012-01-10 22:42:47 +08005290 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005291 __func__, buf, buflen, npages, args.acl_len);
5292 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5293 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005294 if (ret)
5295 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005296
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005297 /* Handle the case where the passed-in buffer is too short */
5298 if (res.acl_flags & NFS4_ACL_TRUNC) {
5299 /* Did the user only issue a request for the acl length? */
5300 if (buf == NULL)
5301 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005302 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005303 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005304 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005305 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005306 if (buf) {
5307 if (res.acl_len > buflen) {
5308 ret = -ERANGE;
5309 goto out_free;
5310 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005311 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005312 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005313out_ok:
5314 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005315out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005316 for (i = 0; i < npages; i++)
5317 if (pages[i])
5318 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005319 if (res.acl_scratch)
5320 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005321 return ret;
5322}
5323
Trond Myklebust16b42892006-08-24 12:27:15 -04005324static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5325{
5326 struct nfs4_exception exception = { };
5327 ssize_t ret;
5328 do {
5329 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005330 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005331 if (ret >= 0)
5332 break;
5333 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5334 } while (exception.retry);
5335 return ret;
5336}
5337
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005338static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5339{
5340 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005341 int ret;
5342
5343 if (!nfs4_server_supports_acls(server))
5344 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005345 ret = nfs_revalidate_inode(server, inode);
5346 if (ret < 0)
5347 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005348 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5349 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005350 ret = nfs4_read_cached_acl(inode, buf, buflen);
5351 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005352 /* -ENOENT is returned if there is no ACL or if there is an ACL
5353 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005354 return ret;
5355 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005356}
5357
Trond Myklebust16b42892006-08-24 12:27:15 -04005358static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005359{
5360 struct nfs_server *server = NFS_SERVER(inode);
5361 struct page *pages[NFS4ACL_MAXPAGES];
5362 struct nfs_setaclargs arg = {
5363 .fh = NFS_FH(inode),
5364 .acl_pages = pages,
5365 .acl_len = buflen,
5366 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005367 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005368 struct rpc_message msg = {
5369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5370 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005371 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005372 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005373 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005374 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005375
5376 if (!nfs4_server_supports_acls(server))
5377 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005378 if (npages > ARRAY_SIZE(pages))
5379 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005380 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005381 if (i < 0)
5382 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005383 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005384 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005385
5386 /*
5387 * Free each page after tx, so the only ref left is
5388 * held by the network stack
5389 */
5390 for (; i > 0; i--)
5391 put_page(pages[i-1]);
5392
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005393 /*
5394 * Acl update can result in inode attribute update.
5395 * so mark the attribute cache invalid.
5396 */
5397 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005398 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005399 | NFS_INO_INVALID_CTIME
5400 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005401 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005402 nfs_access_zap_cache(inode);
5403 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005404 return ret;
5405}
5406
Trond Myklebust16b42892006-08-24 12:27:15 -04005407static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5408{
5409 struct nfs4_exception exception = { };
5410 int err;
5411 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005412 err = __nfs4_proc_set_acl(inode, buf, buflen);
5413 trace_nfs4_set_acl(inode, err);
5414 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005415 &exception);
5416 } while (exception.retry);
5417 return err;
5418}
5419
David Quigleyaa9c2662013-05-22 12:50:44 -04005420#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5421static int _nfs4_get_security_label(struct inode *inode, void *buf,
5422 size_t buflen)
5423{
5424 struct nfs_server *server = NFS_SERVER(inode);
5425 struct nfs_fattr fattr;
5426 struct nfs4_label label = {0, 0, buflen, buf};
5427
5428 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005429 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005430 .fh = NFS_FH(inode),
5431 .bitmask = bitmask,
5432 };
5433 struct nfs4_getattr_res res = {
5434 .fattr = &fattr,
5435 .label = &label,
5436 .server = server,
5437 };
5438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005440 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005441 .rpc_resp = &res,
5442 };
5443 int ret;
5444
5445 nfs_fattr_init(&fattr);
5446
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005447 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005448 if (ret)
5449 return ret;
5450 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5451 return -ENOENT;
5452 if (buflen < label.len)
5453 return -ERANGE;
5454 return 0;
5455}
5456
5457static int nfs4_get_security_label(struct inode *inode, void *buf,
5458 size_t buflen)
5459{
5460 struct nfs4_exception exception = { };
5461 int err;
5462
5463 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5464 return -EOPNOTSUPP;
5465
5466 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005467 err = _nfs4_get_security_label(inode, buf, buflen);
5468 trace_nfs4_get_security_label(inode, err);
5469 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005470 &exception);
5471 } while (exception.retry);
5472 return err;
5473}
5474
5475static int _nfs4_do_set_security_label(struct inode *inode,
5476 struct nfs4_label *ilabel,
5477 struct nfs_fattr *fattr,
5478 struct nfs4_label *olabel)
5479{
5480
5481 struct iattr sattr = {0};
5482 struct nfs_server *server = NFS_SERVER(inode);
5483 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005484 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005485 .fh = NFS_FH(inode),
5486 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005487 .server = server,
5488 .bitmask = bitmask,
5489 .label = ilabel,
5490 };
5491 struct nfs_setattrres res = {
5492 .fattr = fattr,
5493 .label = olabel,
5494 .server = server,
5495 };
5496 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5498 .rpc_argp = &arg,
5499 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005500 };
5501 int status;
5502
Jeff Layton12207f62013-11-01 10:49:32 -04005503 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005504
Jeff Layton12207f62013-11-01 10:49:32 -04005505 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005506 if (status)
5507 dprintk("%s failed: %d\n", __func__, status);
5508
5509 return status;
5510}
5511
5512static int nfs4_do_set_security_label(struct inode *inode,
5513 struct nfs4_label *ilabel,
5514 struct nfs_fattr *fattr,
5515 struct nfs4_label *olabel)
5516{
5517 struct nfs4_exception exception = { };
5518 int err;
5519
5520 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005521 err = _nfs4_do_set_security_label(inode, ilabel,
5522 fattr, olabel);
5523 trace_nfs4_set_security_label(inode, err);
5524 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005525 &exception);
5526 } while (exception.retry);
5527 return err;
5528}
5529
5530static int
Al Viro59301222016-05-27 10:19:30 -04005531nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005532{
5533 struct nfs4_label ilabel, *olabel = NULL;
5534 struct nfs_fattr fattr;
5535 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005536 int status;
5537
5538 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5539 return -EOPNOTSUPP;
5540
5541 nfs_fattr_init(&fattr);
5542
5543 ilabel.pi = 0;
5544 ilabel.lfs = 0;
5545 ilabel.label = (char *)buf;
5546 ilabel.len = buflen;
5547
5548 cred = rpc_lookup_cred();
5549 if (IS_ERR(cred))
5550 return PTR_ERR(cred);
5551
5552 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5553 if (IS_ERR(olabel)) {
5554 status = -PTR_ERR(olabel);
5555 goto out;
5556 }
5557
5558 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5559 if (status == 0)
5560 nfs_setsecurity(inode, &fattr, olabel);
5561
5562 nfs4_label_free(olabel);
5563out:
5564 put_rpccred(cred);
5565 return status;
5566}
5567#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5568
5569
Chuck Leverf0920752012-05-21 22:45:41 -04005570static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5571 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005572{
5573 __be32 verf[2];
5574
Chuck Lever2c820d92012-05-21 22:45:33 -04005575 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5576 /* An impossible timestamp guarantees this value
5577 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005578 verf[0] = cpu_to_be32(U32_MAX);
5579 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005580 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005581 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005582 u64 ns = ktime_to_ns(nn->boot_time);
5583
5584 verf[0] = cpu_to_be32(ns >> 32);
5585 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005586 }
Chuck Levercd937102012-03-02 17:14:31 -05005587 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5588}
5589
Jeff Laytona3192682015-06-09 19:43:59 -04005590static int
5591nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005592{
Jeff Laytona3192682015-06-09 19:43:59 -04005593 size_t len;
5594 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005595
Trond Myklebustceb3a162015-01-03 15:16:04 -05005596 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005597 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005598
Jeff Laytona3192682015-06-09 19:43:59 -04005599 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005600 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005601 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5602 1 +
5603 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5604 1;
5605 rcu_read_unlock();
5606
5607 if (len > NFS4_OPAQUE_LIMIT + 1)
5608 return -EINVAL;
5609
5610 /*
5611 * Since this string is allocated at mount time, and held until the
5612 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5613 * about a memory-reclaim deadlock.
5614 */
5615 str = kmalloc(len, GFP_KERNEL);
5616 if (!str)
5617 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005618
Chuck Levere984a552012-09-14 17:24:21 -04005619 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005620 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005621 clp->cl_ipaddr,
5622 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5623 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005624 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005625
Jeff Laytona3192682015-06-09 19:43:59 -04005626 clp->cl_owner_id = str;
5627 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005628}
5629
Jeff Layton873e3852015-06-09 19:44:00 -04005630static int
5631nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005632{
Jeff Layton873e3852015-06-09 19:44:00 -04005633 size_t len;
5634 char *str;
5635
5636 len = 10 + 10 + 1 + 10 + 1 +
5637 strlen(nfs4_client_id_uniquifier) + 1 +
5638 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5639
5640 if (len > NFS4_OPAQUE_LIMIT + 1)
5641 return -EINVAL;
5642
5643 /*
5644 * Since this string is allocated at mount time, and held until the
5645 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5646 * about a memory-reclaim deadlock.
5647 */
5648 str = kmalloc(len, GFP_KERNEL);
5649 if (!str)
5650 return -ENOMEM;
5651
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005652 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005653 clp->rpc_ops->version, clp->cl_minorversion,
5654 nfs4_client_id_uniquifier,
5655 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005656 clp->cl_owner_id = str;
5657 return 0;
5658}
5659
5660static int
5661nfs4_init_uniform_client_string(struct nfs_client *clp)
5662{
Jeff Layton873e3852015-06-09 19:44:00 -04005663 size_t len;
5664 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005665
5666 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005667 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005668
5669 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005670 return nfs4_init_uniquifier_client_string(clp);
5671
5672 len = 10 + 10 + 1 + 10 + 1 +
5673 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5674
5675 if (len > NFS4_OPAQUE_LIMIT + 1)
5676 return -EINVAL;
5677
5678 /*
5679 * Since this string is allocated at mount time, and held until the
5680 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5681 * about a memory-reclaim deadlock.
5682 */
5683 str = kmalloc(len, GFP_KERNEL);
5684 if (!str)
5685 return -ENOMEM;
5686
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005687 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005688 clp->rpc_ops->version, clp->cl_minorversion,
5689 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005690 clp->cl_owner_id = str;
5691 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005692}
5693
Chuck Lever706cb8d2014-03-12 12:51:47 -04005694/*
5695 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5696 * services. Advertise one based on the address family of the
5697 * clientaddr.
5698 */
5699static unsigned int
5700nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5701{
5702 if (strchr(clp->cl_ipaddr, ':') != NULL)
5703 return scnprintf(buf, len, "tcp6");
5704 else
5705 return scnprintf(buf, len, "tcp");
5706}
5707
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005708static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5709{
5710 struct nfs4_setclientid *sc = calldata;
5711
5712 if (task->tk_status == 0)
5713 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5714}
5715
5716static const struct rpc_call_ops nfs4_setclientid_ops = {
5717 .rpc_call_done = nfs4_setclientid_done,
5718};
5719
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005720/**
5721 * nfs4_proc_setclientid - Negotiate client ID
5722 * @clp: state data structure
5723 * @program: RPC program for NFSv4 callback service
5724 * @port: IP port number for NFS4 callback service
5725 * @cred: RPC credential to use for this call
5726 * @res: where to place the result
5727 *
5728 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5729 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005730int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5731 unsigned short port, struct rpc_cred *cred,
5732 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733{
5734 nfs4_verifier sc_verifier;
5735 struct nfs4_setclientid setclientid = {
5736 .sc_verifier = &sc_verifier,
5737 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005738 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 };
5740 struct rpc_message msg = {
5741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5742 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005743 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005744 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005746 struct rpc_task *task;
5747 struct rpc_task_setup task_setup_data = {
5748 .rpc_client = clp->cl_rpcclient,
5749 .rpc_message = &msg,
5750 .callback_ops = &nfs4_setclientid_ops,
5751 .callback_data = &setclientid,
5752 .flags = RPC_TASK_TIMEOUT,
5753 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005754 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
Chuck Leverde734832012-07-11 16:30:50 -04005756 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005757 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005758
5759 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5760 status = nfs4_init_uniform_client_string(clp);
5761 else
Jeff Laytona3192682015-06-09 19:43:59 -04005762 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005763
5764 if (status)
5765 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005766
Chuck Leverde734832012-07-11 16:30:50 -04005767 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005768 setclientid.sc_netid_len =
5769 nfs4_init_callback_netid(clp,
5770 setclientid.sc_netid,
5771 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005772 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005773 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 clp->cl_ipaddr, port >> 8, port & 255);
5775
Jeff Layton3a6bb732015-06-09 19:43:57 -04005776 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005777 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005778 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005779 task = rpc_run_task(&task_setup_data);
5780 if (IS_ERR(task)) {
5781 status = PTR_ERR(task);
5782 goto out;
5783 }
5784 status = task->tk_status;
5785 if (setclientid.sc_cred) {
5786 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5787 put_rpccred(setclientid.sc_cred);
5788 }
5789 rpc_put_task(task);
5790out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005791 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005792 dprintk("NFS reply setclientid: %d\n", status);
5793 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794}
5795
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005796/**
5797 * nfs4_proc_setclientid_confirm - Confirm client ID
5798 * @clp: state data structure
5799 * @res: result of a previous SETCLIENTID
5800 * @cred: RPC credential to use for this call
5801 *
5802 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5803 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005804int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005805 struct nfs4_setclientid_res *arg,
5806 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 struct rpc_message msg = {
5809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005810 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005811 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 int status;
5814
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005815 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5816 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5817 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005818 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005819 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005820 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 return status;
5822}
5823
Trond Myklebustfe650402006-01-03 09:55:18 +01005824struct nfs4_delegreturndata {
5825 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005826 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005827 struct nfs_fh fh;
5828 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005829 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005830 struct {
5831 struct nfs4_layoutreturn_args arg;
5832 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005833 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005834 u32 roc_barrier;
5835 bool roc;
5836 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005837 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005838 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005839 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005840};
5841
Trond Myklebustfe650402006-01-03 09:55:18 +01005842static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5843{
5844 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005845 struct nfs4_exception exception = {
5846 .inode = data->inode,
5847 .stateid = &data->stateid,
5848 };
Andy Adamson938e1012009-04-01 09:22:28 -04005849
Trond Myklebust14516c32010-07-31 14:29:06 -04005850 if (!nfs4_sequence_done(task, &data->res.seq_res))
5851 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005852
Trond Myklebustca8acf82013-08-13 10:36:56 -04005853 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005854
5855 /* Handle Layoutreturn errors */
5856 if (data->args.lr_args && task->tk_status != 0) {
5857 switch(data->res.lr_ret) {
5858 default:
5859 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5860 break;
5861 case 0:
5862 data->args.lr_args = NULL;
5863 data->res.lr_res = NULL;
5864 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005865 case -NFS4ERR_OLD_STATEID:
5866 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005867 data->inode))
5868 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005869 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005870 case -NFS4ERR_ADMIN_REVOKED:
5871 case -NFS4ERR_DELEG_REVOKED:
5872 case -NFS4ERR_EXPIRED:
5873 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005874 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5875 case -NFS4ERR_WRONG_CRED:
5876 data->args.lr_args = NULL;
5877 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005878 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005879 }
5880 }
5881
Ricardo Labiaga79708862009-12-07 09:23:21 -05005882 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005883 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005884 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005885 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005886 case -NFS4ERR_ADMIN_REVOKED:
5887 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005888 case -NFS4ERR_EXPIRED:
5889 nfs4_free_revoked_stateid(data->res.server,
5890 data->args.stateid,
5891 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005892 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005893 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005894 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005895 task->tk_status = 0;
5896 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005897 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005898 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5899 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005900 task->tk_status = 0;
5901 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005902 case -NFS4ERR_ACCESS:
5903 if (data->args.bitmask) {
5904 data->args.bitmask = NULL;
5905 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005906 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005907 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005908 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005909 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005910 task->tk_status = nfs4_async_handle_exception(task,
5911 data->res.server, task->tk_status,
5912 &exception);
5913 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005914 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005915 }
5916 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005917 return;
5918lr_restart:
5919 data->res.lr_ret = 0;
5920out_restart:
5921 task->tk_status = 0;
5922 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005923}
5924
5925static void nfs4_delegreturn_release(void *calldata)
5926{
Peng Tao039b7562014-07-03 13:05:02 +08005927 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005928 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005929
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005930 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005931 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005932 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5933 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005934 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005935 nfs_iput_and_deactive(inode);
5936 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005937 kfree(calldata);
5938}
5939
Andy Adamson938e1012009-04-01 09:22:28 -04005940static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5941{
5942 struct nfs4_delegreturndata *d_data;
5943
5944 d_data = (struct nfs4_delegreturndata *)data;
5945
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005946 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005947 return;
5948
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005949 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005950 &d_data->args.seq_args,
5951 &d_data->res.seq_res,
5952 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005953}
Andy Adamson938e1012009-04-01 09:22:28 -04005954
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005955static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005956 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005957 .rpc_call_done = nfs4_delegreturn_done,
5958 .rpc_release = nfs4_delegreturn_release,
5959};
5960
Trond Myklebuste6f81072008-01-24 18:14:34 -05005961static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005962{
5963 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005964 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005965 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005966 struct rpc_message msg = {
5967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5968 .rpc_cred = cred,
5969 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005970 struct rpc_task_setup task_setup_data = {
5971 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005972 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005973 .callback_ops = &nfs4_delegreturn_ops,
5974 .flags = RPC_TASK_ASYNC,
5975 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005976 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005977
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005978 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005979 if (data == NULL)
5980 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04005981 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005982
5983 nfs4_state_protect(server->nfs_client,
5984 NFS_SP4_MACH_CRED_CLEANUP,
5985 &task_setup_data.rpc_client, &msg);
5986
Trond Myklebustfe650402006-01-03 09:55:18 +01005987 data->args.fhandle = &data->fh;
5988 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005989 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005990 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005991 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005992 data->res.fattr = &data->fattr;
5993 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005994 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005995 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005996 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005997 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005998 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005999 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006000 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006001 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006002 if (data->lr.roc) {
6003 data->args.lr_args = &data->lr.arg;
6004 data->res.lr_res = &data->lr.res;
6005 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006006 } else if (data->lr.roc) {
6007 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6008 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006009 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006010
Trond Myklebustc970aa82007-07-14 15:39:59 -04006011 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006012 msg.rpc_argp = &data->args;
6013 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006014 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006015 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006016 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006017 if (!issync)
6018 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006019 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006020 if (status != 0)
6021 goto out;
6022 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006023out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006024 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006025 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026}
6027
Trond Myklebuste6f81072008-01-24 18:14:34 -05006028int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029{
6030 struct nfs_server *server = NFS_SERVER(inode);
6031 struct nfs4_exception exception = { };
6032 int err;
6033 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006034 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006035 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 switch (err) {
6037 case -NFS4ERR_STALE_STATEID:
6038 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 case 0:
6040 return 0;
6041 }
6042 err = nfs4_handle_exception(server, err, &exception);
6043 } while (exception.retry);
6044 return err;
6045}
6046
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6048{
6049 struct inode *inode = state->inode;
6050 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006051 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006052 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006054 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006056 struct nfs_lockt_res res = {
6057 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 };
6059 struct rpc_message msg = {
6060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006061 .rpc_argp = &arg,
6062 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 .rpc_cred = state->owner->so_cred,
6064 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 struct nfs4_lock_state *lsp;
6066 int status;
6067
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006068 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006069 status = nfs4_set_lock_state(state, request);
6070 if (status != 0)
6071 goto out;
6072 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006073 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006074 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006075 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006076 switch (status) {
6077 case 0:
6078 request->fl_type = F_UNLCK;
6079 break;
6080 case -NFS4ERR_DENIED:
6081 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006083 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006084 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006085out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return status;
6087}
6088
6089static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6090{
6091 struct nfs4_exception exception = { };
6092 int err;
6093
6094 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006095 err = _nfs4_proc_getlk(state, cmd, request);
6096 trace_nfs4_get_lock(request, state, cmd, err);
6097 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 &exception);
6099 } while (exception.retry);
6100 return err;
6101}
6102
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006103struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006104 struct nfs_locku_args arg;
6105 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006106 struct nfs4_lock_state *lsp;
6107 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006108 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006109 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006110 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006111 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006112};
6113
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006114static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6115 struct nfs_open_context *ctx,
6116 struct nfs4_lock_state *lsp,
6117 struct nfs_seqid *seqid)
6118{
6119 struct nfs4_unlockdata *p;
6120 struct inode *inode = lsp->ls_state->inode;
6121
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006122 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006123 if (p == NULL)
6124 return NULL;
6125 p->arg.fh = NFS_FH(inode);
6126 p->arg.fl = &p->fl;
6127 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006128 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006129 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006130 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006131 /* Ensure we don't close file until we're done freeing locks! */
6132 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006133 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006134 memcpy(&p->fl, fl, sizeof(p->fl));
6135 p->server = NFS_SERVER(inode);
6136 return p;
6137}
6138
Trond Myklebust06f814a2006-01-03 09:55:07 +01006139static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006140{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006141 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006142 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006143 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006144 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006145 put_nfs_open_context(calldata->ctx);
6146 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006147}
6148
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006149static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006150{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006151 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006152 struct nfs4_exception exception = {
6153 .inode = calldata->lsp->ls_state->inode,
6154 .stateid = &calldata->arg.stateid,
6155 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006156
Trond Myklebust14516c32010-07-31 14:29:06 -04006157 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6158 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006159 switch (task->tk_status) {
6160 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006161 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006162 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006163 if (nfs4_update_lock_stateid(calldata->lsp,
6164 &calldata->res.stateid))
6165 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006166 case -NFS4ERR_ADMIN_REVOKED:
6167 case -NFS4ERR_EXPIRED:
6168 nfs4_free_revoked_stateid(calldata->server,
6169 &calldata->arg.stateid,
6170 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006171 case -NFS4ERR_BAD_STATEID:
6172 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006173 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006174 if (!nfs4_stateid_match(&calldata->arg.stateid,
6175 &calldata->lsp->ls_stateid))
6176 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006177 break;
6178 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006179 task->tk_status = nfs4_async_handle_exception(task,
6180 calldata->server, task->tk_status,
6181 &exception);
6182 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006183 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006184 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006185 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006186}
6187
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006188static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006189{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006190 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006192 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6193 nfs_async_iocounter_wait(task, calldata->l_ctx))
6194 return;
6195
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006196 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006197 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006198 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006199 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006200 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006201 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006202 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006203 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006204 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006205 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006206 &calldata->res.seq_res,
6207 task) != 0)
6208 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006209 return;
6210out_no_action:
6211 task->tk_action = NULL;
6212out_wait:
6213 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214}
6215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006216static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006217 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006218 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006219 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006220};
6221
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6223 struct nfs_open_context *ctx,
6224 struct nfs4_lock_state *lsp,
6225 struct nfs_seqid *seqid)
6226{
6227 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006228 struct rpc_message msg = {
6229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6230 .rpc_cred = ctx->cred,
6231 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006232 struct rpc_task_setup task_setup_data = {
6233 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006234 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006235 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006236 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006237 .flags = RPC_TASK_ASYNC,
6238 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006239
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006240 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6241 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6242
Frank Filz137d6ac2007-07-09 15:32:29 -07006243 /* Ensure this is an unlock - when canceling a lock, the
6244 * canceled lock is passed in, and it won't be an unlock.
6245 */
6246 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006247 if (fl->fl_flags & FL_CLOSE)
6248 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006249
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006250 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6251 if (data == NULL) {
6252 nfs_free_seqid(seqid);
6253 return ERR_PTR(-ENOMEM);
6254 }
6255
Anna Schumakerfba83f32018-05-04 16:22:50 -04006256 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006257 msg.rpc_argp = &data->arg;
6258 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006259 task_setup_data.callback_data = data;
6260 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006261}
6262
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6264{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006265 struct inode *inode = state->inode;
6266 struct nfs4_state_owner *sp = state->owner;
6267 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006268 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006269 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006270 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006271 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006272 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006273 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
Trond Myklebust9b073572006-06-29 16:38:34 -04006275 status = nfs4_set_lock_state(state, request);
6276 /* Unlock _before_ we do the RPC call */
6277 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006278 /* Exclude nfs_delegation_claim_locks() */
6279 mutex_lock(&sp->so_delegreturn_mutex);
6280 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006281 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006282 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006283 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006284 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006285 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006286 }
6287 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006288 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006289 if (status != 0)
6290 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006291 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006292 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006293 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6294 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006295 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6296 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006297 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006298 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006299 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006300 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006301 status = PTR_ERR(task);
6302 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006303 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006304 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006305 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006306out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006307 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006308 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310}
6311
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006312struct nfs4_lockdata {
6313 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006314 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315 struct nfs4_lock_state *lsp;
6316 struct nfs_open_context *ctx;
6317 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006318 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006319 int rpc_status;
6320 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006321 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006322};
6323
6324static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006325 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6326 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006327{
6328 struct nfs4_lockdata *p;
6329 struct inode *inode = lsp->ls_state->inode;
6330 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006331 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006332
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006333 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006334 if (p == NULL)
6335 return NULL;
6336
6337 p->arg.fh = NFS_FH(inode);
6338 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006339 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006340 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006341 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006342 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6343 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006344 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006345 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006346 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006347 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006348 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006349 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006350 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006351 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006352 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006353 p->ctx = get_nfs_open_context(ctx);
6354 memcpy(&p->fl, fl, sizeof(p->fl));
6355 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006356out_free_seqid:
6357 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006358out_free:
6359 kfree(p);
6360 return NULL;
6361}
6362
6363static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6364{
6365 struct nfs4_lockdata *data = calldata;
6366 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Harvey Harrison3110ff82008-05-02 13:42:44 -07006368 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006369 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006370 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006371 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006372 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006373 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006374 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006375 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006376 nfs4_stateid_copy(&data->arg.open_stateid,
6377 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006378 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006379 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006380 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006381 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006382 nfs4_stateid_copy(&data->arg.lock_stateid,
6383 &data->lsp->ls_stateid);
6384 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006385 if (!nfs4_valid_open_stateid(state)) {
6386 data->rpc_status = -EBADF;
6387 task->tk_action = NULL;
6388 goto out_release_open_seqid;
6389 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006390 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006391 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006392 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006393 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006394 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006395 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006396out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006397 nfs_release_seqid(data->arg.open_seqid);
6398out_release_lock_seqid:
6399 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006400out_wait:
6401 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006402 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006403}
6404
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006405static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6406{
6407 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006408 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006409
Harvey Harrison3110ff82008-05-02 13:42:44 -07006410 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006411
Trond Myklebust14516c32010-07-31 14:29:06 -04006412 if (!nfs4_sequence_done(task, &data->res.seq_res))
6413 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006414
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006415 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006416 switch (task->tk_status) {
6417 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006418 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006419 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006420 if (data->arg.new_lock) {
6421 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006422 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006423 rpc_restart_call_prepare(task);
6424 break;
6425 }
6426 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006427 if (data->arg.new_lock_owner != 0) {
6428 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6429 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6430 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6431 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6432 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006433 break;
6434 case -NFS4ERR_BAD_STATEID:
6435 case -NFS4ERR_OLD_STATEID:
6436 case -NFS4ERR_STALE_STATEID:
6437 case -NFS4ERR_EXPIRED:
6438 if (data->arg.new_lock_owner != 0) {
6439 if (!nfs4_stateid_match(&data->arg.open_stateid,
6440 &lsp->ls_state->open_stateid))
6441 rpc_restart_call_prepare(task);
6442 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6443 &lsp->ls_stateid))
6444 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006445 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006446 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006447}
6448
6449static void nfs4_lock_release(void *calldata)
6450{
6451 struct nfs4_lockdata *data = calldata;
6452
Harvey Harrison3110ff82008-05-02 13:42:44 -07006453 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006454 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006455 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006456 struct rpc_task *task;
6457 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6458 data->arg.lock_seqid);
6459 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006460 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006461 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006462 } else
6463 nfs_free_seqid(data->arg.lock_seqid);
6464 nfs4_put_lock_state(data->lsp);
6465 put_nfs_open_context(data->ctx);
6466 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006467 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006468}
6469
6470static const struct rpc_call_ops nfs4_lock_ops = {
6471 .rpc_call_prepare = nfs4_lock_prepare,
6472 .rpc_call_done = nfs4_lock_done,
6473 .rpc_release = nfs4_lock_release,
6474};
6475
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006476static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6477{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006478 switch (error) {
6479 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006480 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006481 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006482 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006483 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006484 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006485 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006486 break;
6487 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006488 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006489 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006490 };
6491}
6492
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006493static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006494{
6495 struct nfs4_lockdata *data;
6496 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006497 struct rpc_message msg = {
6498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6499 .rpc_cred = state->owner->so_cred,
6500 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006501 struct rpc_task_setup task_setup_data = {
6502 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006503 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006504 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006505 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006506 .flags = RPC_TASK_ASYNC,
6507 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006508 int ret;
6509
Harvey Harrison3110ff82008-05-02 13:42:44 -07006510 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006511 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006512 fl->fl_u.nfs4_fl.owner,
6513 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006514 if (data == NULL)
6515 return -ENOMEM;
6516 if (IS_SETLKW(cmd))
6517 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006518 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6519 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006520 msg.rpc_argp = &data->arg;
6521 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006522 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006523 if (recovery_type > NFS_LOCK_NEW) {
6524 if (recovery_type == NFS_LOCK_RECLAIM)
6525 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006526 } else
6527 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006528 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006529 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006530 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006531 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006532 if (ret == 0) {
6533 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006534 if (ret)
6535 nfs4_handle_setlk_error(data->server, data->lsp,
6536 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006537 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006538 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006539 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006540 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006541 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006542 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543}
6544
6545static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6546{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006547 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006548 struct nfs4_exception exception = {
6549 .inode = state->inode,
6550 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006551 int err;
6552
6553 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006554 /* Cache the lock if possible... */
6555 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6556 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006557 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006558 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006559 break;
6560 nfs4_handle_exception(server, err, &exception);
6561 } while (exception.retry);
6562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563}
6564
6565static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6566{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006567 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006568 struct nfs4_exception exception = {
6569 .inode = state->inode,
6570 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006571 int err;
6572
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006573 err = nfs4_set_lock_state(state, request);
6574 if (err != 0)
6575 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006576 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006577 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6578 return 0;
6579 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006580 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006581 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6582 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006583 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006584 switch (err) {
6585 default:
6586 goto out;
6587 case -NFS4ERR_GRACE:
6588 case -NFS4ERR_DELAY:
6589 nfs4_handle_exception(server, err, &exception);
6590 err = 0;
6591 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006592 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006593out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595}
6596
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006597#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006598static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6599{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006600 struct nfs4_lock_state *lsp;
6601 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006602
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006603 status = nfs4_set_lock_state(state, request);
6604 if (status != 0)
6605 return status;
6606 lsp = request->fl_u.nfs4_fl.owner;
6607 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6608 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6609 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006610 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006611}
6612#endif
6613
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6615{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006616 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006617 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006618 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006619 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620
Trond Myklebust01c3b862006-06-29 16:38:39 -04006621 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006622 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006623 if (status < 0)
6624 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006625 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006626 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006627 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006628 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006629 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006630 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006631 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006632 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006633 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006634 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006635 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006636 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006637 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006638 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006639out:
6640 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 return status;
6642}
6643
6644static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6645{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006646 struct nfs4_exception exception = {
6647 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006648 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006649 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 int err;
6651
6652 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006653 err = _nfs4_proc_setlk(state, cmd, request);
6654 if (err == -NFS4ERR_DENIED)
6655 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006657 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 } while (exception.retry);
6659 return err;
6660}
6661
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006662#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6663#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6664
6665static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006666nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6667 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006668{
6669 int status = -ERESTARTSYS;
6670 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6671
6672 while(!signalled()) {
6673 status = nfs4_proc_setlk(state, cmd, request);
6674 if ((status != -EAGAIN) || IS_SETLK(cmd))
6675 break;
6676 freezable_schedule_timeout_interruptible(timeout);
6677 timeout *= 2;
6678 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6679 status = -ERESTARTSYS;
6680 }
6681 return status;
6682}
6683
Jeff Laytona1d617d82016-09-17 18:17:39 -04006684#ifdef CONFIG_NFS_V4_1
6685struct nfs4_lock_waiter {
6686 struct task_struct *task;
6687 struct inode *inode;
6688 struct nfs_lowner *owner;
6689 bool notified;
6690};
6691
6692static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006693nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006694{
6695 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006696 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006697
Jeff Layton57174592018-03-18 08:37:03 -04006698 /* NULL key means to wake up everyone */
6699 if (key) {
6700 struct cb_notify_lock_args *cbnl = key;
6701 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6702 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006703
Jeff Layton57174592018-03-18 08:37:03 -04006704 /* Only wake if the callback was for the same owner. */
6705 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6706 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006707
Jeff Layton57174592018-03-18 08:37:03 -04006708 /* Make sure it's for the right inode */
6709 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6710 return 0;
6711
6712 waiter->notified = true;
6713 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006714
6715 /* override "private" so we can use default_wake_function */
6716 wait->private = waiter->task;
6717 ret = autoremove_wake_function(wait, mode, flags, key);
6718 wait->private = waiter;
6719 return ret;
6720}
6721
6722static int
6723nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6724{
6725 int status = -ERESTARTSYS;
6726 unsigned long flags;
6727 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6728 struct nfs_server *server = NFS_SERVER(state->inode);
6729 struct nfs_client *clp = server->nfs_client;
6730 wait_queue_head_t *q = &clp->cl_lock_waitq;
6731 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6732 .id = lsp->ls_seqid.owner_id,
6733 .s_dev = server->s_dev };
6734 struct nfs4_lock_waiter waiter = { .task = current,
6735 .inode = state->inode,
6736 .owner = &owner,
6737 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006738 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006739
6740 /* Don't bother with waitqueue if we don't expect a callback */
6741 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6742 return nfs4_retry_setlk_simple(state, cmd, request);
6743
6744 init_wait(&wait);
6745 wait.private = &waiter;
6746 wait.func = nfs4_wake_lock_waiter;
6747 add_wait_queue(q, &wait);
6748
6749 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006750 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006751 status = nfs4_proc_setlk(state, cmd, request);
6752 if ((status != -EAGAIN) || IS_SETLK(cmd))
6753 break;
6754
6755 status = -ERESTARTSYS;
6756 spin_lock_irqsave(&q->lock, flags);
6757 if (waiter.notified) {
6758 spin_unlock_irqrestore(&q->lock, flags);
6759 continue;
6760 }
6761 set_current_state(TASK_INTERRUPTIBLE);
6762 spin_unlock_irqrestore(&q->lock, flags);
6763
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006764 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006765 }
6766
6767 finish_wait(q, &wait);
6768 return status;
6769}
6770#else /* !CONFIG_NFS_V4_1 */
6771static inline int
6772nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6773{
6774 return nfs4_retry_setlk_simple(state, cmd, request);
6775}
6776#endif
6777
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778static int
6779nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6780{
6781 struct nfs_open_context *ctx;
6782 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 int status;
6784
6785 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006786 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 state = ctx->state;
6788
Trond Myklebustd9531262009-07-21 19:22:38 -04006789 if (IS_GETLK(cmd)) {
6790 if (state != NULL)
6791 return nfs4_proc_getlk(state, F_GETLK, request);
6792 return 0;
6793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
6795 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6796 return -EINVAL;
6797
Trond Myklebustd9531262009-07-21 19:22:38 -04006798 if (request->fl_type == F_UNLCK) {
6799 if (state != NULL)
6800 return nfs4_proc_unlck(state, cmd, request);
6801 return 0;
6802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Trond Myklebustd9531262009-07-21 19:22:38 -04006804 if (state == NULL)
6805 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006806
6807 if ((request->fl_flags & FL_POSIX) &&
6808 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6809 return -ENOLCK;
6810
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006811 /*
6812 * Don't rely on the VFS having checked the file open mode,
6813 * since it won't do this for flock() locks.
6814 */
6815 switch (request->fl_type) {
6816 case F_RDLCK:
6817 if (!(filp->f_mode & FMODE_READ))
6818 return -EBADF;
6819 break;
6820 case F_WRLCK:
6821 if (!(filp->f_mode & FMODE_WRITE))
6822 return -EBADF;
6823 }
6824
Jeff Layton1ea67db2016-09-17 18:17:37 -04006825 status = nfs4_set_lock_state(state, request);
6826 if (status != 0)
6827 return status;
6828
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006829 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830}
6831
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006832int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006833{
6834 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006835 int err;
6836
6837 err = nfs4_set_lock_state(state, fl);
6838 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006839 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006840 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006841 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006842}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006843
Trond Myklebustcf470c32012-03-07 13:49:12 -05006844struct nfs_release_lockowner_data {
6845 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006846 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006847 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006848 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006849 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006850};
6851
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006852static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6853{
6854 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006855 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006856 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6857 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006858 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006859 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006860}
6861
6862static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6863{
6864 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006865 struct nfs_server *server = data->server;
6866
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006867 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006868
6869 switch (task->tk_status) {
6870 case 0:
6871 renew_lease(server, data->timestamp);
6872 break;
6873 case -NFS4ERR_STALE_CLIENTID:
6874 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006875 nfs4_schedule_lease_recovery(server->nfs_client);
6876 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006877 case -NFS4ERR_LEASE_MOVED:
6878 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006879 if (nfs4_async_handle_error(task, server,
6880 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006881 rpc_restart_call_prepare(task);
6882 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006883}
6884
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006885static void nfs4_release_lockowner_release(void *calldata)
6886{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006887 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006888 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006889 kfree(calldata);
6890}
6891
Trond Myklebust17280172012-03-11 13:11:00 -04006892static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006893 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6894 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006895 .rpc_release = nfs4_release_lockowner_release,
6896};
6897
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006898static void
6899nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006900{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006901 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006902 struct rpc_message msg = {
6903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6904 };
6905
6906 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006907 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006908
Trond Myklebustcf470c32012-03-07 13:49:12 -05006909 data = kmalloc(sizeof(*data), GFP_NOFS);
6910 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006911 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006912 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006913 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006914 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6915 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6916 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006917
Trond Myklebustcf470c32012-03-07 13:49:12 -05006918 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006919 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006920 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006921 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006922}
6923
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006924#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6925
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006926static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006927 struct dentry *unused, struct inode *inode,
6928 const char *key, const void *buf,
6929 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006930{
Al Viro59301222016-05-27 10:19:30 -04006931 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006932}
6933
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006934static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006935 struct dentry *unused, struct inode *inode,
6936 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006937{
Al Virob2968212016-04-10 20:48:24 -04006938 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006939}
6940
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006941static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006942{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006943 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006944}
6945
David Quigleyc9bccef2013-05-22 12:50:45 -04006946#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006947
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006948static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006949 struct dentry *unused, struct inode *inode,
6950 const char *key, const void *buf,
6951 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006952{
6953 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006954 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006955
6956 return -EOPNOTSUPP;
6957}
6958
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006959static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006960 struct dentry *unused, struct inode *inode,
6961 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006962{
6963 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006964 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006965 return -EOPNOTSUPP;
6966}
6967
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006968static ssize_t
6969nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006970{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006971 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006972
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006973 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6974 len = security_inode_listsecurity(inode, list, list_len);
6975 if (list_len && len > list_len)
6976 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006977 }
6978 return len;
6979}
6980
6981static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6982 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006983 .get = nfs4_xattr_get_nfs4_label,
6984 .set = nfs4_xattr_set_nfs4_label,
6985};
David Quigleyc9bccef2013-05-22 12:50:45 -04006986
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006987#else
6988
6989static ssize_t
6990nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6991{
6992 return 0;
6993}
6994
6995#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006996
Andy Adamson533eb462011-06-13 18:25:56 -04006997/*
6998 * nfs_fhget will use either the mounted_on_fileid or the fileid
6999 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007000static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7001{
Andy Adamson533eb462011-06-13 18:25:56 -04007002 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7003 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7004 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007005 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007006 return;
7007
7008 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007009 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007010 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7011 fattr->nlink = 2;
7012}
7013
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007014static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7015 const struct qstr *name,
7016 struct nfs4_fs_locations *fs_locations,
7017 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007018{
7019 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007020 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007021 struct nfs4_fs_locations_arg args = {
7022 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007023 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007024 .page = page,
7025 .bitmask = bitmask,
7026 };
Benny Halevy22958462009-04-01 09:22:02 -04007027 struct nfs4_fs_locations_res res = {
7028 .fs_locations = fs_locations,
7029 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007030 struct rpc_message msg = {
7031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7032 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007033 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007034 };
7035 int status;
7036
Harvey Harrison3110ff82008-05-02 13:42:44 -07007037 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007038
Chuck Leverc05cefc2017-11-05 15:45:22 -05007039 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7040 bitmask[1] = nfs4_fattr_bitmap[1];
7041
Andy Adamson533eb462011-06-13 18:25:56 -04007042 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7043 * is not supported */
7044 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007045 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007046 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007047 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007048
Trond Myklebustc228fd32007-01-13 02:28:11 -05007049 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007050 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007051 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007052 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007053 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007054 return status;
7055}
7056
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007057int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7058 const struct qstr *name,
7059 struct nfs4_fs_locations *fs_locations,
7060 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007061{
7062 struct nfs4_exception exception = { };
7063 int err;
7064 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007065 err = _nfs4_proc_fs_locations(client, dir, name,
7066 fs_locations, page);
7067 trace_nfs4_get_fs_locations(dir, name, err);
7068 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007069 &exception);
7070 } while (exception.retry);
7071 return err;
7072}
7073
Chuck Leverb03d7352013-10-17 14:12:50 -04007074/*
7075 * This operation also signals the server that this client is
7076 * performing migration recovery. The server can stop returning
7077 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7078 * appended to this compound to identify the client ID which is
7079 * performing recovery.
7080 */
7081static int _nfs40_proc_get_locations(struct inode *inode,
7082 struct nfs4_fs_locations *locations,
7083 struct page *page, struct rpc_cred *cred)
7084{
7085 struct nfs_server *server = NFS_SERVER(inode);
7086 struct rpc_clnt *clnt = server->client;
7087 u32 bitmask[2] = {
7088 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7089 };
7090 struct nfs4_fs_locations_arg args = {
7091 .clientid = server->nfs_client->cl_clientid,
7092 .fh = NFS_FH(inode),
7093 .page = page,
7094 .bitmask = bitmask,
7095 .migration = 1, /* skip LOOKUP */
7096 .renew = 1, /* append RENEW */
7097 };
7098 struct nfs4_fs_locations_res res = {
7099 .fs_locations = locations,
7100 .migration = 1,
7101 .renew = 1,
7102 };
7103 struct rpc_message msg = {
7104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7105 .rpc_argp = &args,
7106 .rpc_resp = &res,
7107 .rpc_cred = cred,
7108 };
7109 unsigned long now = jiffies;
7110 int status;
7111
7112 nfs_fattr_init(&locations->fattr);
7113 locations->server = server;
7114 locations->nlocations = 0;
7115
Anna Schumakerfba83f32018-05-04 16:22:50 -04007116 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007117 status = nfs4_call_sync_sequence(clnt, server, &msg,
7118 &args.seq_args, &res.seq_res);
7119 if (status)
7120 return status;
7121
7122 renew_lease(server, now);
7123 return 0;
7124}
7125
7126#ifdef CONFIG_NFS_V4_1
7127
7128/*
7129 * This operation also signals the server that this client is
7130 * performing migration recovery. The server can stop asserting
7131 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7132 * performing this operation is identified in the SEQUENCE
7133 * operation in this compound.
7134 *
7135 * When the client supports GETATTR(fs_locations_info), it can
7136 * be plumbed in here.
7137 */
7138static int _nfs41_proc_get_locations(struct inode *inode,
7139 struct nfs4_fs_locations *locations,
7140 struct page *page, struct rpc_cred *cred)
7141{
7142 struct nfs_server *server = NFS_SERVER(inode);
7143 struct rpc_clnt *clnt = server->client;
7144 u32 bitmask[2] = {
7145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7146 };
7147 struct nfs4_fs_locations_arg args = {
7148 .fh = NFS_FH(inode),
7149 .page = page,
7150 .bitmask = bitmask,
7151 .migration = 1, /* skip LOOKUP */
7152 };
7153 struct nfs4_fs_locations_res res = {
7154 .fs_locations = locations,
7155 .migration = 1,
7156 };
7157 struct rpc_message msg = {
7158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7159 .rpc_argp = &args,
7160 .rpc_resp = &res,
7161 .rpc_cred = cred,
7162 };
7163 int status;
7164
7165 nfs_fattr_init(&locations->fattr);
7166 locations->server = server;
7167 locations->nlocations = 0;
7168
Anna Schumakerfba83f32018-05-04 16:22:50 -04007169 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007170 status = nfs4_call_sync_sequence(clnt, server, &msg,
7171 &args.seq_args, &res.seq_res);
7172 if (status == NFS4_OK &&
7173 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7174 status = -NFS4ERR_LEASE_MOVED;
7175 return status;
7176}
7177
7178#endif /* CONFIG_NFS_V4_1 */
7179
7180/**
7181 * nfs4_proc_get_locations - discover locations for a migrated FSID
7182 * @inode: inode on FSID that is migrating
7183 * @locations: result of query
7184 * @page: buffer
7185 * @cred: credential to use for this operation
7186 *
7187 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7188 * operation failed, or a negative errno if a local error occurred.
7189 *
7190 * On success, "locations" is filled in, but if the server has
7191 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7192 * asserted.
7193 *
7194 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7195 * from this client that require migration recovery.
7196 */
7197int nfs4_proc_get_locations(struct inode *inode,
7198 struct nfs4_fs_locations *locations,
7199 struct page *page, struct rpc_cred *cred)
7200{
7201 struct nfs_server *server = NFS_SERVER(inode);
7202 struct nfs_client *clp = server->nfs_client;
7203 const struct nfs4_mig_recovery_ops *ops =
7204 clp->cl_mvops->mig_recovery_ops;
7205 struct nfs4_exception exception = { };
7206 int status;
7207
7208 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7209 (unsigned long long)server->fsid.major,
7210 (unsigned long long)server->fsid.minor,
7211 clp->cl_hostname);
7212 nfs_display_fhandle(NFS_FH(inode), __func__);
7213
7214 do {
7215 status = ops->get_locations(inode, locations, page, cred);
7216 if (status != -NFS4ERR_DELAY)
7217 break;
7218 nfs4_handle_exception(server, status, &exception);
7219 } while (exception.retry);
7220 return status;
7221}
7222
Chuck Lever44c99932013-10-17 14:13:30 -04007223/*
7224 * This operation also signals the server that this client is
7225 * performing "lease moved" recovery. The server can stop
7226 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7227 * is appended to this compound to identify the client ID which is
7228 * performing recovery.
7229 */
7230static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7231{
7232 struct nfs_server *server = NFS_SERVER(inode);
7233 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7234 struct rpc_clnt *clnt = server->client;
7235 struct nfs4_fsid_present_arg args = {
7236 .fh = NFS_FH(inode),
7237 .clientid = clp->cl_clientid,
7238 .renew = 1, /* append RENEW */
7239 };
7240 struct nfs4_fsid_present_res res = {
7241 .renew = 1,
7242 };
7243 struct rpc_message msg = {
7244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7245 .rpc_argp = &args,
7246 .rpc_resp = &res,
7247 .rpc_cred = cred,
7248 };
7249 unsigned long now = jiffies;
7250 int status;
7251
7252 res.fh = nfs_alloc_fhandle();
7253 if (res.fh == NULL)
7254 return -ENOMEM;
7255
Anna Schumakerfba83f32018-05-04 16:22:50 -04007256 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007257 status = nfs4_call_sync_sequence(clnt, server, &msg,
7258 &args.seq_args, &res.seq_res);
7259 nfs_free_fhandle(res.fh);
7260 if (status)
7261 return status;
7262
7263 do_renew_lease(clp, now);
7264 return 0;
7265}
7266
7267#ifdef CONFIG_NFS_V4_1
7268
7269/*
7270 * This operation also signals the server that this client is
7271 * performing "lease moved" recovery. The server can stop asserting
7272 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7273 * this operation is identified in the SEQUENCE operation in this
7274 * compound.
7275 */
7276static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7277{
7278 struct nfs_server *server = NFS_SERVER(inode);
7279 struct rpc_clnt *clnt = server->client;
7280 struct nfs4_fsid_present_arg args = {
7281 .fh = NFS_FH(inode),
7282 };
7283 struct nfs4_fsid_present_res res = {
7284 };
7285 struct rpc_message msg = {
7286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7287 .rpc_argp = &args,
7288 .rpc_resp = &res,
7289 .rpc_cred = cred,
7290 };
7291 int status;
7292
7293 res.fh = nfs_alloc_fhandle();
7294 if (res.fh == NULL)
7295 return -ENOMEM;
7296
Anna Schumakerfba83f32018-05-04 16:22:50 -04007297 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007298 status = nfs4_call_sync_sequence(clnt, server, &msg,
7299 &args.seq_args, &res.seq_res);
7300 nfs_free_fhandle(res.fh);
7301 if (status == NFS4_OK &&
7302 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7303 status = -NFS4ERR_LEASE_MOVED;
7304 return status;
7305}
7306
7307#endif /* CONFIG_NFS_V4_1 */
7308
7309/**
7310 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7311 * @inode: inode on FSID to check
7312 * @cred: credential to use for this operation
7313 *
7314 * Server indicates whether the FSID is present, moved, or not
7315 * recognized. This operation is necessary to clear a LEASE_MOVED
7316 * condition for this client ID.
7317 *
7318 * Returns NFS4_OK if the FSID is present on this server,
7319 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7320 * NFS4ERR code if some error occurred on the server, or a
7321 * negative errno if a local failure occurred.
7322 */
7323int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7324{
7325 struct nfs_server *server = NFS_SERVER(inode);
7326 struct nfs_client *clp = server->nfs_client;
7327 const struct nfs4_mig_recovery_ops *ops =
7328 clp->cl_mvops->mig_recovery_ops;
7329 struct nfs4_exception exception = { };
7330 int status;
7331
7332 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7333 (unsigned long long)server->fsid.major,
7334 (unsigned long long)server->fsid.minor,
7335 clp->cl_hostname);
7336 nfs_display_fhandle(NFS_FH(inode), __func__);
7337
7338 do {
7339 status = ops->fsid_present(inode, cred);
7340 if (status != -NFS4ERR_DELAY)
7341 break;
7342 nfs4_handle_exception(server, status, &exception);
7343 } while (exception.retry);
7344 return status;
7345}
7346
Andy Adamson5ec16a82013-08-08 10:57:55 -04007347/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007348 * If 'use_integrity' is true and the state managment nfs_client
7349 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7350 * and the machine credential as per RFC3530bis and RFC5661 Security
7351 * Considerations sections. Otherwise, just use the user cred with the
7352 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007353 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007354static 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 +00007355{
7356 int status;
7357 struct nfs4_secinfo_arg args = {
7358 .dir_fh = NFS_FH(dir),
7359 .name = name,
7360 };
7361 struct nfs4_secinfo_res res = {
7362 .flavors = flavors,
7363 };
7364 struct rpc_message msg = {
7365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7366 .rpc_argp = &args,
7367 .rpc_resp = &res,
7368 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007369 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007370 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007371
7372 if (use_integrity) {
7373 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007374 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7375 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007376 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007377
7378 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007379
7380 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7381 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7382
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007383 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7384 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007385 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007386
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007387 if (cred)
7388 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007389
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007390 return status;
7391}
7392
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007393int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7394 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007395{
7396 struct nfs4_exception exception = { };
7397 int err;
7398 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007399 err = -NFS4ERR_WRONGSEC;
7400
7401 /* try to use integrity protection with machine cred */
7402 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7403 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7404
7405 /*
7406 * if unable to use integrity protection, or SECINFO with
7407 * integrity protection returns NFS4ERR_WRONGSEC (which is
7408 * disallowed by spec, but exists in deployed servers) use
7409 * the current filesystem's rpc_client and the user cred.
7410 */
7411 if (err == -NFS4ERR_WRONGSEC)
7412 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7413
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007414 trace_nfs4_secinfo(dir, name, err);
7415 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007416 &exception);
7417 } while (exception.retry);
7418 return err;
7419}
7420
Andy Adamson557134a2009-04-01 09:21:53 -04007421#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007422/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007423 * Check the exchange flags returned by the server for invalid flags, having
7424 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7425 * DS flags set.
7426 */
7427static int nfs4_check_cl_exchange_flags(u32 flags)
7428{
7429 if (flags & ~EXCHGID4_FLAG_MASK_R)
7430 goto out_inval;
7431 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7432 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7433 goto out_inval;
7434 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7435 goto out_inval;
7436 return NFS_OK;
7437out_inval:
7438 return -NFS4ERR_INVAL;
7439}
7440
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007441static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007442nfs41_same_server_scope(struct nfs41_server_scope *a,
7443 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007444{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007445 if (a->server_scope_sz != b->server_scope_sz)
7446 return false;
7447 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007448}
7449
Andy Adamson02a95de2016-02-05 16:08:37 -05007450static void
7451nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7452{
7453}
7454
7455static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7456 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7457};
7458
Andy Adamson357f54d2010-12-14 10:11:57 -05007459/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007460 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007461 *
7462 * The 4.1 client currently uses the same TCP connection for the
7463 * fore and backchannel.
7464 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007465static
7466int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7467 struct rpc_xprt *xprt,
7468 struct nfs_client *clp,
7469 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007470{
7471 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007472 struct nfs41_bind_conn_to_session_args args = {
7473 .client = clp,
7474 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7475 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007476 struct nfs41_bind_conn_to_session_res res;
7477 struct rpc_message msg = {
7478 .rpc_proc =
7479 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007480 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007481 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007482 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007483 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007484 struct rpc_task_setup task_setup_data = {
7485 .rpc_client = clnt,
7486 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007487 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007488 .rpc_message = &msg,
7489 .flags = RPC_TASK_TIMEOUT,
7490 };
7491 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007492
Trond Myklebust71a097c2015-02-18 09:27:18 -08007493 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7494 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7495 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007496
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007497 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7498 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7499 args.dir = NFS4_CDFC4_FORE;
7500
7501 task = rpc_run_task(&task_setup_data);
7502 if (!IS_ERR(task)) {
7503 status = task->tk_status;
7504 rpc_put_task(task);
7505 } else
7506 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007507 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007508 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007509 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007510 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7511 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007512 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007513 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007514 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007515 dprintk("NFS: %s: Unexpected direction from server\n",
7516 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007517 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007518 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007519 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007520 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7521 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007522 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007523 }
7524 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007525
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007526 return status;
7527}
7528
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007529struct rpc_bind_conn_calldata {
7530 struct nfs_client *clp;
7531 struct rpc_cred *cred;
7532};
7533
7534static int
7535nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7536 struct rpc_xprt *xprt,
7537 void *calldata)
7538{
7539 struct rpc_bind_conn_calldata *p = calldata;
7540
7541 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7542}
7543
7544int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7545{
7546 struct rpc_bind_conn_calldata data = {
7547 .clp = clp,
7548 .cred = cred,
7549 };
7550 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7551 nfs4_proc_bind_conn_to_session_callback, &data);
7552}
7553
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007554/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007555 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7556 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007557 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007558static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7559 .how = SP4_MACH_CRED,
7560 .enforce.u.words = {
7561 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7562 1 << (OP_EXCHANGE_ID - 32) |
7563 1 << (OP_CREATE_SESSION - 32) |
7564 1 << (OP_DESTROY_SESSION - 32) |
7565 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007566 },
7567 .allow.u.words = {
7568 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007569 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007570 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007571 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007572 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007573 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007574 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007575 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007576 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007577 1 << (OP_FREE_STATEID - 32) |
7578 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007579 }
7580};
7581
7582/*
7583 * Select the state protection mode for client `clp' given the server results
7584 * from exchange_id in `sp'.
7585 *
7586 * Returns 0 on success, negative errno otherwise.
7587 */
7588static int nfs4_sp4_select_mode(struct nfs_client *clp,
7589 struct nfs41_state_protection *sp)
7590{
7591 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7592 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7593 1 << (OP_EXCHANGE_ID - 32) |
7594 1 << (OP_CREATE_SESSION - 32) |
7595 1 << (OP_DESTROY_SESSION - 32) |
7596 1 << (OP_DESTROY_CLIENTID - 32)
7597 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007598 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007599 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007600 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007601
7602 if (sp->how == SP4_MACH_CRED) {
7603 /* Print state protect result */
7604 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7605 for (i = 0; i <= LAST_NFS4_OP; i++) {
7606 if (test_bit(i, sp->enforce.u.longs))
7607 dfprintk(MOUNT, " enforce op %d\n", i);
7608 if (test_bit(i, sp->allow.u.longs))
7609 dfprintk(MOUNT, " allow op %d\n", i);
7610 }
7611
7612 /* make sure nothing is on enforce list that isn't supported */
7613 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7614 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7615 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007616 ret = -EINVAL;
7617 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007618 }
7619 }
7620
7621 /*
7622 * Minimal mode - state operations are allowed to use machine
7623 * credential. Note this already happens by default, so the
7624 * client doesn't have to do anything more than the negotiation.
7625 *
7626 * NOTE: we don't care if EXCHANGE_ID is in the list -
7627 * we're already using the machine cred for exchange_id
7628 * and will never use a different cred.
7629 */
7630 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7631 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7632 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7633 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7634 dfprintk(MOUNT, "sp4_mach_cred:\n");
7635 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007636 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007637 } else {
7638 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007639 ret = -EINVAL;
7640 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007641 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007642
7643 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007644 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7645 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007646 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7647 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007648 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007649 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007650
Andrew Elble99ade3c2015-12-02 09:39:51 -05007651 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7652 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007653 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007654 }
7655
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007656 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7657 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7658 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007659 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007660 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007661
7662 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7663 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7664 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007665 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007666 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007667
7668 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7669 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007670 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007671 }
7672
7673 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7674 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007675 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007676 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007677 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007678out:
7679 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007680 return 0;
7681}
7682
Andy Adamson8d89bd72016-09-09 09:22:18 -04007683struct nfs41_exchange_id_data {
7684 struct nfs41_exchange_id_res res;
7685 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007686};
7687
Andy Adamson8d89bd72016-09-09 09:22:18 -04007688static void nfs4_exchange_id_release(void *data)
7689{
7690 struct nfs41_exchange_id_data *cdata =
7691 (struct nfs41_exchange_id_data *)data;
7692
Olga Kornievskaia63513232017-03-13 10:36:19 -04007693 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007694 kfree(cdata->res.impl_id);
7695 kfree(cdata->res.server_scope);
7696 kfree(cdata->res.server_owner);
7697 kfree(cdata);
7698}
7699
7700static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007701 .rpc_release = nfs4_exchange_id_release,
7702};
7703
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007704/*
7705 * _nfs4_proc_exchange_id()
7706 *
7707 * Wrapper for EXCHANGE_ID operation.
7708 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007709static struct rpc_task *
7710nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007711 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007712{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007713 struct rpc_message msg = {
7714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007715 .rpc_cred = cred,
7716 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007717 struct rpc_task_setup task_setup_data = {
7718 .rpc_client = clp->cl_rpcclient,
7719 .callback_ops = &nfs4_exchange_id_call_ops,
7720 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007721 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007722 };
7723 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007724 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007725
Elena Reshetova212bf412017-10-20 12:53:38 +03007726 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007727 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007728
Trond Myklebust9c760d12017-07-31 18:38:50 -04007729 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007730 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007731 if (!calldata)
7732 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007733
Trond Myklebustfd405592017-08-01 16:02:47 -04007734 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007735
7736 status = nfs4_init_uniform_client_string(clp);
7737 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007738 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007739
Andy Adamson8d89bd72016-09-09 09:22:18 -04007740 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7741 GFP_NOFS);
7742 status = -ENOMEM;
7743 if (unlikely(calldata->res.server_owner == NULL))
7744 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007745
Andy Adamson8d89bd72016-09-09 09:22:18 -04007746 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007747 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007748 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007749 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007750
Andy Adamson8d89bd72016-09-09 09:22:18 -04007751 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7752 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007753 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007754
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007755 switch (sp4_how) {
7756 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007757 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007758 break;
7759
7760 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007761 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007762 break;
7763
7764 default:
7765 /* unsupported! */
7766 WARN_ON_ONCE(1);
7767 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007768 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007769 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007770 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007771 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007772 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007773 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7774 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007775 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007776 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007777 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7778 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007779#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007780 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007781#endif
7782 msg.rpc_argp = &calldata->args;
7783 msg.rpc_resp = &calldata->res;
7784 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007785
Trond Myklebust9c760d12017-07-31 18:38:50 -04007786 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007787
7788out_impl_id:
7789 kfree(calldata->res.impl_id);
7790out_server_scope:
7791 kfree(calldata->res.server_scope);
7792out_server_owner:
7793 kfree(calldata->res.server_owner);
7794out_calldata:
7795 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007796out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007797 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007798 return ERR_PTR(status);
7799}
7800
7801/*
7802 * _nfs4_proc_exchange_id()
7803 *
7804 * Wrapper for EXCHANGE_ID operation.
7805 */
7806static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7807 u32 sp4_how)
7808{
7809 struct rpc_task *task;
7810 struct nfs41_exchange_id_args *argp;
7811 struct nfs41_exchange_id_res *resp;
7812 int status;
7813
7814 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7815 if (IS_ERR(task))
7816 return PTR_ERR(task);
7817
7818 argp = task->tk_msg.rpc_argp;
7819 resp = task->tk_msg.rpc_resp;
7820 status = task->tk_status;
7821 if (status != 0)
7822 goto out;
7823
7824 status = nfs4_check_cl_exchange_flags(resp->flags);
7825 if (status != 0)
7826 goto out;
7827
7828 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7829 if (status != 0)
7830 goto out;
7831
7832 clp->cl_clientid = resp->clientid;
7833 clp->cl_exchange_flags = resp->flags;
7834 clp->cl_seqid = resp->seqid;
7835 /* Client ID is not confirmed */
7836 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7837 clear_bit(NFS4_SESSION_ESTABLISHED,
7838 &clp->cl_session->session_state);
7839
7840 if (clp->cl_serverscope != NULL &&
7841 !nfs41_same_server_scope(clp->cl_serverscope,
7842 resp->server_scope)) {
7843 dprintk("%s: server_scope mismatch detected\n",
7844 __func__);
7845 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7846 }
7847
7848 swap(clp->cl_serverowner, resp->server_owner);
7849 swap(clp->cl_serverscope, resp->server_scope);
7850 swap(clp->cl_implid, resp->impl_id);
7851
7852 /* Save the EXCHANGE_ID verifier session trunk tests */
7853 memcpy(clp->cl_confirm.data, argp->verifier.data,
7854 sizeof(clp->cl_confirm.data));
7855out:
7856 trace_nfs4_exchange_id(clp, status);
7857 rpc_put_task(task);
7858 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007859}
7860
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007861/*
7862 * nfs4_proc_exchange_id()
7863 *
7864 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7865 *
7866 * Since the clientid has expired, all compounds using sessions
7867 * associated with the stale clientid will be returning
7868 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7869 * be in some phase of session reset.
7870 *
7871 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7872 */
7873int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7874{
7875 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7876 int status;
7877
7878 /* try SP4_MACH_CRED if krb5i/p */
7879 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7880 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007881 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007882 if (!status)
7883 return 0;
7884 }
7885
7886 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007887 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007888}
7889
Andy Adamson04fa2c62016-09-09 09:22:29 -04007890/**
7891 * nfs4_test_session_trunk
7892 *
7893 * This is an add_xprt_test() test function called from
7894 * rpc_clnt_setup_test_and_add_xprt.
7895 *
7896 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7897 * and is dereferrenced in nfs4_exchange_id_release
7898 *
7899 * Upon success, add the new transport to the rpc_clnt
7900 *
7901 * @clnt: struct rpc_clnt to get new transport
7902 * @xprt: the rpc_xprt to test
7903 * @data: call data for _nfs4_proc_exchange_id.
7904 */
7905int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7906 void *data)
7907{
7908 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007909 struct rpc_task *task;
7910 int status;
7911
Andy Adamson04fa2c62016-09-09 09:22:29 -04007912 u32 sp4_how;
7913
7914 dprintk("--> %s try %s\n", __func__,
7915 xprt->address_strings[RPC_DISPLAY_ADDR]);
7916
7917 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7918
7919 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007920 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7921 if (IS_ERR(task))
7922 return PTR_ERR(task);
7923
7924 status = task->tk_status;
7925 if (status == 0)
7926 status = nfs4_detect_session_trunking(adata->clp,
7927 task->tk_msg.rpc_resp, xprt);
7928
7929 rpc_put_task(task);
7930 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007931}
7932EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7933
Trond Myklebust66245532012-05-25 17:18:09 -04007934static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7935 struct rpc_cred *cred)
7936{
7937 struct rpc_message msg = {
7938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7939 .rpc_argp = clp,
7940 .rpc_cred = cred,
7941 };
7942 int status;
7943
7944 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007945 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007946 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007947 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007948 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7949 return status;
7950}
7951
7952static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7953 struct rpc_cred *cred)
7954{
7955 unsigned int loop;
7956 int ret;
7957
7958 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7959 ret = _nfs4_proc_destroy_clientid(clp, cred);
7960 switch (ret) {
7961 case -NFS4ERR_DELAY:
7962 case -NFS4ERR_CLIENTID_BUSY:
7963 ssleep(1);
7964 break;
7965 default:
7966 return ret;
7967 }
7968 }
7969 return 0;
7970}
7971
7972int nfs4_destroy_clientid(struct nfs_client *clp)
7973{
7974 struct rpc_cred *cred;
7975 int ret = 0;
7976
7977 if (clp->cl_mvops->minor_version < 1)
7978 goto out;
7979 if (clp->cl_exchange_flags == 0)
7980 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007981 if (clp->cl_preserve_clid)
7982 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007983 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007984 ret = nfs4_proc_destroy_clientid(clp, cred);
7985 if (cred)
7986 put_rpccred(cred);
7987 switch (ret) {
7988 case 0:
7989 case -NFS4ERR_STALE_CLIENTID:
7990 clp->cl_exchange_flags = 0;
7991 }
7992out:
7993 return ret;
7994}
7995
Andy Adamson2050f0c2009-04-01 09:22:30 -04007996struct nfs4_get_lease_time_data {
7997 struct nfs4_get_lease_time_args *args;
7998 struct nfs4_get_lease_time_res *res;
7999 struct nfs_client *clp;
8000};
8001
8002static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8003 void *calldata)
8004{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008005 struct nfs4_get_lease_time_data *data =
8006 (struct nfs4_get_lease_time_data *)calldata;
8007
8008 dprintk("--> %s\n", __func__);
8009 /* just setup sequence, do not trigger session recovery
8010 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008011 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008012 &data->args->la_seq_args,
8013 &data->res->lr_seq_res,
8014 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008015 dprintk("<-- %s\n", __func__);
8016}
8017
8018/*
8019 * Called from nfs4_state_manager thread for session setup, so don't recover
8020 * from sequence operation or clientid errors.
8021 */
8022static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8023{
8024 struct nfs4_get_lease_time_data *data =
8025 (struct nfs4_get_lease_time_data *)calldata;
8026
8027 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008028 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8029 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008030 switch (task->tk_status) {
8031 case -NFS4ERR_DELAY:
8032 case -NFS4ERR_GRACE:
8033 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8034 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8035 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008036 /* fall through */
8037 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008038 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008039 return;
8040 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008041 dprintk("<-- %s\n", __func__);
8042}
8043
Trond Myklebust17280172012-03-11 13:11:00 -04008044static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008045 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8046 .rpc_call_done = nfs4_get_lease_time_done,
8047};
8048
8049int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8050{
8051 struct rpc_task *task;
8052 struct nfs4_get_lease_time_args args;
8053 struct nfs4_get_lease_time_res res = {
8054 .lr_fsinfo = fsinfo,
8055 };
8056 struct nfs4_get_lease_time_data data = {
8057 .args = &args,
8058 .res = &res,
8059 .clp = clp,
8060 };
8061 struct rpc_message msg = {
8062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8063 .rpc_argp = &args,
8064 .rpc_resp = &res,
8065 };
8066 struct rpc_task_setup task_setup = {
8067 .rpc_client = clp->cl_rpcclient,
8068 .rpc_message = &msg,
8069 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008070 .callback_data = &data,
8071 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008072 };
8073 int status;
8074
Anna Schumakerfba83f32018-05-04 16:22:50 -04008075 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008076 task = rpc_run_task(&task_setup);
8077
8078 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008079 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008080
Anna Schumakerf6148712017-04-07 14:15:23 -04008081 status = task->tk_status;
8082 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008083 return status;
8084}
8085
Andy Adamsonfc931582009-04-01 09:22:31 -04008086/*
8087 * Initialize the values to be used by the client in CREATE_SESSION
8088 * If nfs4_init_session set the fore channel request and response sizes,
8089 * use them.
8090 *
8091 * Set the back channel max_resp_sz_cached to zero to force the client to
8092 * always set csa_cachethis to FALSE because the current implementation
8093 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8094 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008095static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8096 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008097{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008098 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008099 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008100
Andy Adamson18aad3d2013-06-26 12:21:49 -04008101 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8102 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8103
Andy Adamsonfc931582009-04-01 09:22:31 -04008104 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008105 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8106 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008107 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008108 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008109
8110 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008111 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008112 __func__,
8113 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008114 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008115
8116 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008117 args->bc_attrs.max_rqst_sz = max_bc_payload;
8118 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008119 args->bc_attrs.max_resp_sz_cached = 0;
8120 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008121 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008122
8123 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8124 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8125 __func__,
8126 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8127 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8128 args->bc_attrs.max_reqs);
8129}
8130
Trond Myklebust79969dd12015-02-18 11:30:18 -08008131static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8132 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008133{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008134 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008135 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008136
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008137 if (rcvd->max_resp_sz > sent->max_resp_sz)
8138 return -EINVAL;
8139 /*
8140 * Our requested max_ops is the minimum we need; we're not
8141 * prepared to break up compounds into smaller pieces than that.
8142 * So, no point even trying to continue if the server won't
8143 * cooperate:
8144 */
8145 if (rcvd->max_ops < sent->max_ops)
8146 return -EINVAL;
8147 if (rcvd->max_reqs == 0)
8148 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008149 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8150 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008151 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008152}
8153
Trond Myklebust79969dd12015-02-18 11:30:18 -08008154static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8155 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008156{
8157 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008158 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008159
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008160 if (!(res->flags & SESSION4_BACK_CHAN))
8161 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008162 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8163 return -EINVAL;
8164 if (rcvd->max_resp_sz < sent->max_resp_sz)
8165 return -EINVAL;
8166 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8167 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008168 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008169 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008170 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008171 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008172out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008173 return 0;
8174}
Andy Adamson8d353012009-04-01 09:22:32 -04008175
Andy Adamson8d353012009-04-01 09:22:32 -04008176static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008177 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008178{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008179 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008180
Trond Myklebust79969dd12015-02-18 11:30:18 -08008181 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008182 if (ret)
8183 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008184 return nfs4_verify_back_channel_attrs(args, res);
8185}
8186
8187static void nfs4_update_session(struct nfs4_session *session,
8188 struct nfs41_create_session_res *res)
8189{
8190 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008191 /* Mark client id and session as being confirmed */
8192 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8193 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008194 session->flags = res->flags;
8195 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008196 if (res->flags & SESSION4_BACK_CHAN)
8197 memcpy(&session->bc_attrs, &res->bc_attrs,
8198 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008199}
8200
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008201static int _nfs4_proc_create_session(struct nfs_client *clp,
8202 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008203{
8204 struct nfs4_session *session = clp->cl_session;
8205 struct nfs41_create_session_args args = {
8206 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008207 .clientid = clp->cl_clientid,
8208 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008209 .cb_program = NFS4_CALLBACK,
8210 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008211 struct nfs41_create_session_res res;
8212
Andy Adamsonfc931582009-04-01 09:22:31 -04008213 struct rpc_message msg = {
8214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8215 .rpc_argp = &args,
8216 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008217 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008218 };
8219 int status;
8220
Chuck Lever6b26cc82016-05-02 14:40:40 -04008221 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008222 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008223
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008224 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008225 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008226
Trond Myklebustb519d402016-09-11 14:50:01 -04008227 switch (status) {
8228 case -NFS4ERR_STALE_CLIENTID:
8229 case -NFS4ERR_DELAY:
8230 case -ETIMEDOUT:
8231 case -EACCES:
8232 case -EAGAIN:
8233 goto out;
8234 };
8235
8236 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008237 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008238 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008239 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008240 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008241 if (status)
8242 goto out;
8243 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008244 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008245out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008246 return status;
8247}
8248
8249/*
8250 * Issues a CREATE_SESSION operation to the server.
8251 * It is the responsibility of the caller to verify the session is
8252 * expired before calling this routine.
8253 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008254int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008255{
8256 int status;
8257 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008258 struct nfs4_session *session = clp->cl_session;
8259
8260 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8261
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008262 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008263 if (status)
8264 goto out;
8265
Andy Adamsonaacd5532011-11-09 13:58:21 -05008266 /* Init or reset the session slot tables */
8267 status = nfs4_setup_session_slot_tables(session);
8268 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008269 if (status)
8270 goto out;
8271
8272 ptr = (unsigned *)&session->sess_id.data[0];
8273 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8274 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008275out:
8276 dprintk("<-- %s\n", __func__);
8277 return status;
8278}
8279
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008280/*
8281 * Issue the over-the-wire RPC DESTROY_SESSION.
8282 * The caller must serialize access to this routine.
8283 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008284int nfs4_proc_destroy_session(struct nfs4_session *session,
8285 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008286{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008287 struct rpc_message msg = {
8288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8289 .rpc_argp = session,
8290 .rpc_cred = cred,
8291 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008292 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008293
8294 dprintk("--> nfs4_proc_destroy_session\n");
8295
8296 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008297 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8298 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008299
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008300 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008301 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008302
8303 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008304 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008305 "Session has been destroyed regardless...\n", status);
8306
8307 dprintk("<-- nfs4_proc_destroy_session\n");
8308 return status;
8309}
8310
Trond Myklebust7b38c362012-05-23 13:23:31 -04008311/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008312 * Renew the cl_session lease.
8313 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008314struct nfs4_sequence_data {
8315 struct nfs_client *clp;
8316 struct nfs4_sequence_args args;
8317 struct nfs4_sequence_res res;
8318};
8319
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008320static void nfs41_sequence_release(void *data)
8321{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008322 struct nfs4_sequence_data *calldata = data;
8323 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008324
Elena Reshetova212bf412017-10-20 12:53:38 +03008325 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008326 nfs4_schedule_state_renewal(clp);
8327 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008328 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008329}
8330
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008331static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8332{
8333 switch(task->tk_status) {
8334 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008335 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8336 return -EAGAIN;
8337 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008338 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008339 }
8340 return 0;
8341}
8342
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008343static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008344{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008345 struct nfs4_sequence_data *calldata = data;
8346 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008347
Trond Myklebust14516c32010-07-31 14:29:06 -04008348 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8349 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008350
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008351 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008352 if (task->tk_status < 0) {
8353 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008354 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008355 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008356
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008357 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8358 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008359 return;
8360 }
8361 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008362 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008363out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008364 dprintk("<-- %s\n", __func__);
8365}
8366
8367static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8368{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008369 struct nfs4_sequence_data *calldata = data;
8370 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008371 struct nfs4_sequence_args *args;
8372 struct nfs4_sequence_res *res;
8373
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008374 args = task->tk_msg.rpc_argp;
8375 res = task->tk_msg.rpc_resp;
8376
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008377 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008378}
8379
8380static const struct rpc_call_ops nfs41_sequence_ops = {
8381 .rpc_call_done = nfs41_sequence_call_done,
8382 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008383 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008384};
8385
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008386static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8387 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008388 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008389 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008390{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008391 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008392 struct rpc_message msg = {
8393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8394 .rpc_cred = cred,
8395 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008396 struct rpc_task_setup task_setup_data = {
8397 .rpc_client = clp->cl_rpcclient,
8398 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008399 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008400 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008401 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008402 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008403
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008404 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008405 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008406 goto out_err;
8407
8408 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008409 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008410 if (calldata == NULL)
8411 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008412 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008413 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008414 msg.rpc_argp = &calldata->args;
8415 msg.rpc_resp = &calldata->res;
8416 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008417 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008418
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008419 ret = rpc_run_task(&task_setup_data);
8420 if (IS_ERR(ret))
8421 goto out_err;
8422 return ret;
8423out_put_clp:
8424 nfs_put_client(clp);
8425out_err:
8426 nfs41_release_slot(slot);
8427 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008428}
8429
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008430static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008431{
8432 struct rpc_task *task;
8433 int ret = 0;
8434
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008435 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008436 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008437 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008438 if (IS_ERR(task))
8439 ret = PTR_ERR(task);
8440 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008441 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008442 dprintk("<-- %s status=%d\n", __func__, ret);
8443 return ret;
8444}
8445
8446static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8447{
8448 struct rpc_task *task;
8449 int ret;
8450
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008451 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008452 if (IS_ERR(task)) {
8453 ret = PTR_ERR(task);
8454 goto out;
8455 }
8456 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008457 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008458 ret = task->tk_status;
8459 rpc_put_task(task);
8460out:
8461 dprintk("<-- %s status=%d\n", __func__, ret);
8462 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008463}
8464
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008465struct nfs4_reclaim_complete_data {
8466 struct nfs_client *clp;
8467 struct nfs41_reclaim_complete_args arg;
8468 struct nfs41_reclaim_complete_res res;
8469};
8470
8471static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8472{
8473 struct nfs4_reclaim_complete_data *calldata = data;
8474
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008475 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008476 &calldata->arg.seq_args,
8477 &calldata->res.seq_res,
8478 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008479}
8480
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008481static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8482{
8483 switch(task->tk_status) {
8484 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008485 wake_up_all(&clp->cl_lock_waitq);
8486 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008487 case -NFS4ERR_COMPLETE_ALREADY:
8488 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8489 break;
8490 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008491 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008492 /* fall through */
8493 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008494 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008495 case -NFS4ERR_BADSESSION:
8496 case -NFS4ERR_DEADSESSION:
8497 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8498 nfs4_schedule_session_recovery(clp->cl_session,
8499 task->tk_status);
8500 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008501 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008502 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008503 }
8504 return 0;
8505}
8506
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008507static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8508{
8509 struct nfs4_reclaim_complete_data *calldata = data;
8510 struct nfs_client *clp = calldata->clp;
8511 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8512
8513 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008514 if (!nfs41_sequence_done(task, res))
8515 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008516
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008517 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008518 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8519 rpc_restart_call_prepare(task);
8520 return;
8521 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008522 dprintk("<-- %s\n", __func__);
8523}
8524
8525static void nfs4_free_reclaim_complete_data(void *data)
8526{
8527 struct nfs4_reclaim_complete_data *calldata = data;
8528
8529 kfree(calldata);
8530}
8531
8532static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8533 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8534 .rpc_call_done = nfs4_reclaim_complete_done,
8535 .rpc_release = nfs4_free_reclaim_complete_data,
8536};
8537
8538/*
8539 * Issue a global reclaim complete.
8540 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008541static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8542 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008543{
8544 struct nfs4_reclaim_complete_data *calldata;
8545 struct rpc_task *task;
8546 struct rpc_message msg = {
8547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008548 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008549 };
8550 struct rpc_task_setup task_setup_data = {
8551 .rpc_client = clp->cl_rpcclient,
8552 .rpc_message = &msg,
8553 .callback_ops = &nfs4_reclaim_complete_call_ops,
8554 .flags = RPC_TASK_ASYNC,
8555 };
8556 int status = -ENOMEM;
8557
8558 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008559 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008560 if (calldata == NULL)
8561 goto out;
8562 calldata->clp = clp;
8563 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008564
Anna Schumakerfba83f32018-05-04 16:22:50 -04008565 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008566 msg.rpc_argp = &calldata->arg;
8567 msg.rpc_resp = &calldata->res;
8568 task_setup_data.callback_data = calldata;
8569 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008570 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008571 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008572 goto out;
8573 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008574 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008575 if (status == 0)
8576 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008577 rpc_put_task(task);
8578out:
8579 dprintk("<-- %s status=%d\n", __func__, status);
8580 return status;
8581}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008582
8583static void
8584nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8585{
8586 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008587 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008588
8589 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008590 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008591 &lgp->res.seq_res, task);
8592 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008593}
8594
8595static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8596{
8597 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008598
8599 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008600 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008601 dprintk("<-- %s\n", __func__);
8602}
8603
8604static int
8605nfs4_layoutget_handle_exception(struct rpc_task *task,
8606 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8607{
Trond Myklebustee314c22012-10-01 17:25:48 -07008608 struct inode *inode = lgp->args.inode;
8609 struct nfs_server *server = NFS_SERVER(inode);
8610 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008611 int nfs4err = task->tk_status;
8612 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008613 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008614
Boaz Harroshed7e5422014-01-22 20:34:54 +02008615 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008616
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008617 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008618 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008619 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008620
8621 /*
8622 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8623 * on the file. set tk_status to -ENODATA to tell upper layer to
8624 * retry go inband.
8625 */
8626 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008627 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008628 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008629 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008630 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8631 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8632 */
8633 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008634 status = -EOVERFLOW;
8635 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008636 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008637 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008638 * (or clients) writing to the same RAID stripe except when
8639 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008640 *
8641 * Treat it like we would RECALLCONFLICT -- we retry for a little
8642 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008643 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008644 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008645 if (lgp->args.minlength == 0) {
8646 status = -EOVERFLOW;
8647 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008648 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008649 status = -EBUSY;
8650 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008651 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008652 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008653 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008654 case -NFS4ERR_DELEG_REVOKED:
8655 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008656 case -NFS4ERR_EXPIRED:
8657 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008658 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008659 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008660 lo = NFS_I(inode)->layout;
8661 /* If the open stateid was bad, then recover it. */
8662 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008663 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008664 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008665 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008666 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008667 break;
8668 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008669
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008670 /*
8671 * Mark the bad layout state as invalid, then retry
8672 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008673 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008674 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008675 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008676 pnfs_free_lseg_list(&head);
8677 status = -EAGAIN;
8678 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008679 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008680
Trond Myklebust8ac09252017-01-23 22:44:12 -05008681 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008682 err = nfs4_handle_exception(server, nfs4err, exception);
8683 if (!status) {
8684 if (exception->retry)
8685 status = -EAGAIN;
8686 else
8687 status = err;
8688 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008689out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008690 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008691 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008692}
8693
Idan Kedar85541162012-08-02 11:47:10 +03008694static size_t max_response_pages(struct nfs_server *server)
8695{
8696 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8697 return nfs_page_array_len(0, max_resp_sz);
8698}
8699
8700static void nfs4_free_pages(struct page **pages, size_t size)
8701{
8702 int i;
8703
8704 if (!pages)
8705 return;
8706
8707 for (i = 0; i < size; i++) {
8708 if (!pages[i])
8709 break;
8710 __free_page(pages[i]);
8711 }
8712 kfree(pages);
8713}
8714
8715static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8716{
8717 struct page **pages;
8718 int i;
8719
8720 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8721 if (!pages) {
8722 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8723 return NULL;
8724 }
8725
8726 for (i = 0; i < size; i++) {
8727 pages[i] = alloc_page(gfp_flags);
8728 if (!pages[i]) {
8729 dprintk("%s: failed to allocate page\n", __func__);
8730 nfs4_free_pages(pages, size);
8731 return NULL;
8732 }
8733 }
8734
8735 return pages;
8736}
8737
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008738static void nfs4_layoutget_release(void *calldata)
8739{
8740 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008741 struct inode *inode = lgp->args.inode;
8742 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008743 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008744
8745 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008746 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008747 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008748 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008749 put_nfs_open_context(lgp->args.ctx);
8750 kfree(calldata);
8751 dprintk("<-- %s\n", __func__);
8752}
8753
8754static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8755 .rpc_call_prepare = nfs4_layoutget_prepare,
8756 .rpc_call_done = nfs4_layoutget_done,
8757 .rpc_release = nfs4_layoutget_release,
8758};
8759
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008760struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008761nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008762{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008763 struct inode *inode = lgp->args.inode;
8764 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008765 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008766 struct rpc_task *task;
8767 struct rpc_message msg = {
8768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8769 .rpc_argp = &lgp->args,
8770 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008771 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008772 };
8773 struct rpc_task_setup task_setup_data = {
8774 .rpc_client = server->client,
8775 .rpc_message = &msg,
8776 .callback_ops = &nfs4_layoutget_call_ops,
8777 .callback_data = lgp,
8778 .flags = RPC_TASK_ASYNC,
8779 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008780 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008781 struct nfs4_exception exception = {
8782 .inode = inode,
8783 .timeout = *timeout,
8784 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008785 int status = 0;
8786
8787 dprintk("--> %s\n", __func__);
8788
Peng Tao4bd5a982014-11-17 11:05:17 +08008789 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8790 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8791
Idan Kedar85541162012-08-02 11:47:10 +03008792 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8793 if (!lgp->args.layout.pages) {
8794 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008795 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008796 }
8797 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8798
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008799 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008800 lgp->res.seq_res.sr_slot = NULL;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008801 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008802
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008803 task = rpc_run_task(&task_setup_data);
8804 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008805 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008806 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008807 if (status == 0) {
8808 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8809 *timeout = exception.timeout;
8810 }
8811
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008812 trace_nfs4_layoutget(lgp->args.ctx,
8813 &lgp->args.range,
8814 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008815 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008816 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008817
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008818 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8819 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008820 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008821 rpc_put_task(task);
8822 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008823 if (status)
8824 return ERR_PTR(status);
8825 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008826}
8827
Benny Halevycbe82602011-05-22 19:52:37 +03008828static void
8829nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8830{
8831 struct nfs4_layoutreturn *lrp = calldata;
8832
8833 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008834 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008835 &lrp->args.seq_args,
8836 &lrp->res.seq_res,
8837 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008838}
8839
8840static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8841{
8842 struct nfs4_layoutreturn *lrp = calldata;
8843 struct nfs_server *server;
8844
8845 dprintk("--> %s\n", __func__);
8846
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008847 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008848 return;
8849
8850 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008851 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008852 case -NFS4ERR_OLD_STATEID:
8853 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8854 lrp->args.inode))
8855 goto out_restart;
8856 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008857 default:
8858 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008859 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008860 case 0:
8861 break;
8862 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008863 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008864 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008865 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008866 }
Benny Halevycbe82602011-05-22 19:52:37 +03008867 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008868 return;
8869out_restart:
8870 task->tk_status = 0;
8871 nfs4_sequence_free_slot(&lrp->res.seq_res);
8872 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008873}
8874
8875static void nfs4_layoutreturn_release(void *calldata)
8876{
8877 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008878 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008879
8880 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008881 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008882 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008883 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008884 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8885 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008886 pnfs_put_layout_hdr(lrp->args.layout);
8887 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008888 kfree(calldata);
8889 dprintk("<-- %s\n", __func__);
8890}
8891
8892static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8893 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8894 .rpc_call_done = nfs4_layoutreturn_done,
8895 .rpc_release = nfs4_layoutreturn_release,
8896};
8897
Peng Tao6c166052014-11-17 09:30:40 +08008898int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008899{
8900 struct rpc_task *task;
8901 struct rpc_message msg = {
8902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8903 .rpc_argp = &lrp->args,
8904 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008905 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008906 };
8907 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04008908 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008909 .rpc_message = &msg,
8910 .callback_ops = &nfs4_layoutreturn_call_ops,
8911 .callback_data = lrp,
8912 };
Peng Tao6c166052014-11-17 09:30:40 +08008913 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008914
Andrew Elble99ade3c2015-12-02 09:39:51 -05008915 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8916 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8917 &task_setup_data.rpc_client, &msg);
8918
Benny Halevycbe82602011-05-22 19:52:37 +03008919 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008920 if (!sync) {
8921 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8922 if (!lrp->inode) {
8923 nfs4_layoutreturn_release(lrp);
8924 return -EAGAIN;
8925 }
8926 task_setup_data.flags |= RPC_TASK_ASYNC;
8927 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008928 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008929 task = rpc_run_task(&task_setup_data);
8930 if (IS_ERR(task))
8931 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008932 if (sync)
8933 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008934 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008935 dprintk("<-- %s status=%d\n", __func__, status);
8936 rpc_put_task(task);
8937 return status;
8938}
8939
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008940static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008941_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8942 struct pnfs_device *pdev,
8943 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008944{
8945 struct nfs4_getdeviceinfo_args args = {
8946 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008947 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8948 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008949 };
8950 struct nfs4_getdeviceinfo_res res = {
8951 .pdev = pdev,
8952 };
8953 struct rpc_message msg = {
8954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8955 .rpc_argp = &args,
8956 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008957 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008958 };
8959 int status;
8960
8961 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008962 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008963 if (res.notification & ~args.notify_types)
8964 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008965 if (res.notification != args.notify_types)
8966 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008967
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008968 dprintk("<-- %s status=%d\n", __func__, status);
8969
8970 return status;
8971}
8972
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008973int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8974 struct pnfs_device *pdev,
8975 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008976{
8977 struct nfs4_exception exception = { };
8978 int err;
8979
8980 do {
8981 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008982 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008983 &exception);
8984 } while (exception.retry);
8985 return err;
8986}
8987EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8988
Andy Adamson863a3c62011-03-23 13:27:54 +00008989static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8990{
8991 struct nfs4_layoutcommit_data *data = calldata;
8992 struct nfs_server *server = NFS_SERVER(data->args.inode);
8993
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008994 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008995 &data->args.seq_args,
8996 &data->res.seq_res,
8997 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008998}
8999
9000static void
9001nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9002{
9003 struct nfs4_layoutcommit_data *data = calldata;
9004 struct nfs_server *server = NFS_SERVER(data->args.inode);
9005
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009006 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009007 return;
9008
9009 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009010 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9011 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9012 case -NFS4ERR_BADLAYOUT: /* no layout */
9013 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009014 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009015 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009016 break;
9017 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009018 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009019 rpc_restart_call_prepare(task);
9020 return;
9021 }
9022 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009023}
9024
9025static void nfs4_layoutcommit_release(void *calldata)
9026{
9027 struct nfs4_layoutcommit_data *data = calldata;
9028
Andy Adamsondb29c082011-07-30 20:52:38 -04009029 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009030 nfs_post_op_update_inode_force_wcc(data->args.inode,
9031 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009032 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009033 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009034 kfree(data);
9035}
9036
9037static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9038 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9039 .rpc_call_done = nfs4_layoutcommit_done,
9040 .rpc_release = nfs4_layoutcommit_release,
9041};
9042
9043int
Andy Adamsonef311532011-03-12 02:58:10 -05009044nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009045{
9046 struct rpc_message msg = {
9047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9048 .rpc_argp = &data->args,
9049 .rpc_resp = &data->res,
9050 .rpc_cred = data->cred,
9051 };
9052 struct rpc_task_setup task_setup_data = {
9053 .task = &data->task,
9054 .rpc_client = NFS_CLIENT(data->args.inode),
9055 .rpc_message = &msg,
9056 .callback_ops = &nfs4_layoutcommit_ops,
9057 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009058 };
9059 struct rpc_task *task;
9060 int status = 0;
9061
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009062 dprintk("NFS: initiating layoutcommit call. sync %d "
9063 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009064 data->args.lastbytewritten,
9065 data->args.inode->i_ino);
9066
Trond Myklebust472e2592015-02-05 16:50:30 -05009067 if (!sync) {
9068 data->inode = nfs_igrab_and_active(data->args.inode);
9069 if (data->inode == NULL) {
9070 nfs4_layoutcommit_release(data);
9071 return -EAGAIN;
9072 }
9073 task_setup_data.flags = RPC_TASK_ASYNC;
9074 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009075 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009076 task = rpc_run_task(&task_setup_data);
9077 if (IS_ERR(task))
9078 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009079 if (sync)
9080 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009081 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009082 dprintk("%s: status %d\n", __func__, status);
9083 rpc_put_task(task);
9084 return status;
9085}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009086
Andy Adamson97431202013-08-08 10:57:56 -04009087/**
9088 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9089 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9090 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009091static int
9092_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009093 struct nfs_fsinfo *info,
9094 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009095{
9096 struct nfs41_secinfo_no_name_args args = {
9097 .style = SECINFO_STYLE_CURRENT_FH,
9098 };
9099 struct nfs4_secinfo_res res = {
9100 .flavors = flavors,
9101 };
9102 struct rpc_message msg = {
9103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9104 .rpc_argp = &args,
9105 .rpc_resp = &res,
9106 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009107 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009108 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009109 int status;
9110
9111 if (use_integrity) {
9112 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009113 cred = nfs4_get_clid_cred(server->nfs_client);
9114 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009115 }
9116
9117 dprintk("--> %s\n", __func__);
9118 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9119 &res.seq_res, 0);
9120 dprintk("<-- %s status=%d\n", __func__, status);
9121
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009122 if (cred)
9123 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009124
9125 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009126}
9127
9128static int
9129nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9130 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9131{
9132 struct nfs4_exception exception = { };
9133 int err;
9134 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009135 /* first try using integrity protection */
9136 err = -NFS4ERR_WRONGSEC;
9137
9138 /* try to use integrity protection with machine cred */
9139 if (_nfs4_is_integrity_protected(server->nfs_client))
9140 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9141 flavors, true);
9142
9143 /*
9144 * if unable to use integrity protection, or SECINFO with
9145 * integrity protection returns NFS4ERR_WRONGSEC (which is
9146 * disallowed by spec, but exists in deployed servers) use
9147 * the current filesystem's rpc_client and the user cred.
9148 */
9149 if (err == -NFS4ERR_WRONGSEC)
9150 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9151 flavors, false);
9152
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009153 switch (err) {
9154 case 0:
9155 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009156 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009157 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009158 default:
9159 err = nfs4_handle_exception(server, err, &exception);
9160 }
9161 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009162out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009163 return err;
9164}
9165
9166static int
9167nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9168 struct nfs_fsinfo *info)
9169{
9170 int err;
9171 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009172 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009173 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009174 struct nfs4_secinfo4 *secinfo;
9175 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009176
9177 page = alloc_page(GFP_KERNEL);
9178 if (!page) {
9179 err = -ENOMEM;
9180 goto out;
9181 }
9182
9183 flavors = page_address(page);
9184 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9185
9186 /*
9187 * Fall back on "guess and check" method if
9188 * the server doesn't support SECINFO_NO_NAME
9189 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009190 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009191 err = nfs4_find_root_sec(server, fhandle, info);
9192 goto out_freepage;
9193 }
9194 if (err)
9195 goto out_freepage;
9196
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009197 for (i = 0; i < flavors->num_flavors; i++) {
9198 secinfo = &flavors->flavors[i];
9199
9200 switch (secinfo->flavor) {
9201 case RPC_AUTH_NULL:
9202 case RPC_AUTH_UNIX:
9203 case RPC_AUTH_GSS:
9204 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9205 &secinfo->flavor_info);
9206 break;
9207 default:
9208 flavor = RPC_AUTH_MAXFLAVOR;
9209 break;
9210 }
9211
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009212 if (!nfs_auth_info_match(&server->auth_info, flavor))
9213 flavor = RPC_AUTH_MAXFLAVOR;
9214
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009215 if (flavor != RPC_AUTH_MAXFLAVOR) {
9216 err = nfs4_lookup_root_sec(server, fhandle,
9217 info, flavor);
9218 if (!err)
9219 break;
9220 }
9221 }
9222
9223 if (flavor == RPC_AUTH_MAXFLAVOR)
9224 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009225
9226out_freepage:
9227 put_page(page);
9228 if (err == -EACCES)
9229 return -EPERM;
9230out:
9231 return err;
9232}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009233
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009234static int _nfs41_test_stateid(struct nfs_server *server,
9235 nfs4_stateid *stateid,
9236 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009237{
9238 int status;
9239 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009240 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009241 };
9242 struct nfs41_test_stateid_res res;
9243 struct rpc_message msg = {
9244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9245 .rpc_argp = &args,
9246 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009247 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009248 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009249 struct rpc_clnt *rpc_client = server->client;
9250
9251 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9252 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009253
Chuck Lever38527b12012-07-11 16:30:23 -04009254 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009255 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009256 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009257 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009258 if (status != NFS_OK) {
9259 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009260 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009261 }
9262 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009263 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009264}
9265
Trond Myklebust43912bb2016-09-22 13:38:56 -04009266static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9267 int err, struct nfs4_exception *exception)
9268{
9269 exception->retry = 0;
9270 switch(err) {
9271 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009272 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009273 nfs4_handle_exception(server, err, exception);
9274 break;
9275 case -NFS4ERR_BADSESSION:
9276 case -NFS4ERR_BADSLOT:
9277 case -NFS4ERR_BAD_HIGH_SLOT:
9278 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9279 case -NFS4ERR_DEADSESSION:
9280 nfs4_do_handle_exception(server, err, exception);
9281 }
9282}
9283
Chuck Lever38527b12012-07-11 16:30:23 -04009284/**
9285 * nfs41_test_stateid - perform a TEST_STATEID operation
9286 *
9287 * @server: server / transport on which to perform the operation
9288 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009289 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009290 *
9291 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9292 * Otherwise a negative NFS4ERR value is returned if the operation
9293 * failed or the state ID is not currently valid.
9294 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009295static int nfs41_test_stateid(struct nfs_server *server,
9296 nfs4_stateid *stateid,
9297 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009298{
9299 struct nfs4_exception exception = { };
9300 int err;
9301 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009302 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009303 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009304 } while (exception.retry);
9305 return err;
9306}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009307
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009308struct nfs_free_stateid_data {
9309 struct nfs_server *server;
9310 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009311 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009312};
9313
9314static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9315{
9316 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009317 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009318 &data->args.seq_args,
9319 &data->res.seq_res,
9320 task);
9321}
9322
9323static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9324{
9325 struct nfs_free_stateid_data *data = calldata;
9326
9327 nfs41_sequence_done(task, &data->res.seq_res);
9328
9329 switch (task->tk_status) {
9330 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009331 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009332 rpc_restart_call_prepare(task);
9333 }
9334}
9335
9336static void nfs41_free_stateid_release(void *calldata)
9337{
9338 kfree(calldata);
9339}
9340
Trond Myklebust17f26b12013-08-21 15:48:42 -04009341static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009342 .rpc_call_prepare = nfs41_free_stateid_prepare,
9343 .rpc_call_done = nfs41_free_stateid_done,
9344 .rpc_release = nfs41_free_stateid_release,
9345};
9346
Anna Schumaker2f261022018-05-15 13:03:39 -04009347/**
9348 * nfs41_free_stateid - perform a FREE_STATEID operation
9349 *
9350 * @server: server / transport on which to perform the operation
9351 * @stateid: state ID to release
9352 * @cred: credential
9353 * @is_recovery: set to true if this call needs to be privileged
9354 *
9355 * Note: this function is always asynchronous.
9356 */
9357static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009358 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009359 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009360 bool privileged)
9361{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009362 struct rpc_message msg = {
9363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009364 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009365 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009366 struct rpc_task_setup task_setup = {
9367 .rpc_client = server->client,
9368 .rpc_message = &msg,
9369 .callback_ops = &nfs41_free_stateid_ops,
9370 .flags = RPC_TASK_ASYNC,
9371 };
9372 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009373 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009374
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009375 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9376 &task_setup.rpc_client, &msg);
9377
Chuck Lever38527b12012-07-11 16:30:23 -04009378 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009379 data = kmalloc(sizeof(*data), GFP_NOFS);
9380 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009381 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009382 data->server = server;
9383 nfs4_stateid_copy(&data->args.stateid, stateid);
9384
9385 task_setup.callback_data = data;
9386
9387 msg.rpc_argp = &data->args;
9388 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009389 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009390 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009391 if (IS_ERR(task))
9392 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009393 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009394 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009395}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009396
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009397static void
9398nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009399{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009400 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009401
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009402 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009403 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009404}
9405
Trond Myklebust36281ca2012-03-04 18:13:56 -05009406static bool nfs41_match_stateid(const nfs4_stateid *s1,
9407 const nfs4_stateid *s2)
9408{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009409 if (s1->type != s2->type)
9410 return false;
9411
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009412 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009413 return false;
9414
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009415 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009416 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009417
Anna Schumaker045c5512017-01-11 16:59:48 -05009418 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009419}
9420
Andy Adamson557134a2009-04-01 09:21:53 -04009421#endif /* CONFIG_NFS_V4_1 */
9422
Trond Myklebust36281ca2012-03-04 18:13:56 -05009423static bool nfs4_match_stateid(const nfs4_stateid *s1,
9424 const nfs4_stateid *s2)
9425{
Trond Myklebustf597c532012-03-04 18:13:56 -05009426 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009427}
9428
9429
Trond Myklebust17280172012-03-11 13:11:00 -04009430static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009431 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009432 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 .recover_open = nfs4_open_reclaim,
9434 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009435 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009436 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437};
9438
Andy Adamson591d71c2009-04-01 09:22:47 -04009439#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009440static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009441 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9442 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9443 .recover_open = nfs4_open_reclaim,
9444 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009445 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009446 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009447 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009448};
9449#endif /* CONFIG_NFS_V4_1 */
9450
Trond Myklebust17280172012-03-11 13:11:00 -04009451static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009452 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009453 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009454 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009456 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457};
9458
Andy Adamson591d71c2009-04-01 09:22:47 -04009459#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009460static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009461 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9462 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009463 .recover_open = nfs41_open_expired,
9464 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009465 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009466};
9467#endif /* CONFIG_NFS_V4_1 */
9468
Trond Myklebust17280172012-03-11 13:11:00 -04009469static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009470 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009471 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009472 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009473};
9474
9475#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009476static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009477 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009478 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009479 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009480};
9481#endif
9482
Chuck Leverec011fe2013-10-17 14:12:39 -04009483static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009484 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009485 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009486};
9487
9488#if defined(CONFIG_NFS_V4_1)
9489static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009490 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009491 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009492};
9493#endif /* CONFIG_NFS_V4_1 */
9494
Trond Myklebust97dc1352010-06-16 09:52:26 -04009495static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9496 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009497 .init_caps = NFS_CAP_READDIRPLUS
9498 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009499 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009500 .init_client = nfs40_init_client,
9501 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009502 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009503 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009504 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009505 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009506 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009507 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009508 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9509 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9510 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009511 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009512};
9513
9514#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009515static struct nfs_seqid *
9516nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9517{
9518 return NULL;
9519}
9520
Trond Myklebust97dc1352010-06-16 09:52:26 -04009521static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9522 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009523 .init_caps = NFS_CAP_READDIRPLUS
9524 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009525 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009526 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009527 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009528 .init_client = nfs41_init_client,
9529 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009530 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009531 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009532 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009533 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009534 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009535 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009536 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009537 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9538 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9539 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009540 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009541};
9542#endif
9543
Steve Dickson42c2c422013-05-22 12:50:38 -04009544#if defined(CONFIG_NFS_V4_2)
9545static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9546 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009547 .init_caps = NFS_CAP_READDIRPLUS
9548 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009549 | NFS_CAP_POSIX_LOCK
9550 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009551 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009552 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009553 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009554 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009555 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009556 | NFS_CAP_LAYOUTSTATS
9557 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009558 .init_client = nfs41_init_client,
9559 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009560 .match_stateid = nfs41_match_stateid,
9561 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009562 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009563 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009564 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009565 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009566 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009567 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9568 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9569 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009570 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009571};
9572#endif
9573
Trond Myklebust97dc1352010-06-16 09:52:26 -04009574const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9575 [0] = &nfs_v4_0_minor_ops,
9576#if defined(CONFIG_NFS_V4_1)
9577 [1] = &nfs_v4_1_minor_ops,
9578#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009579#if defined(CONFIG_NFS_V4_2)
9580 [2] = &nfs_v4_2_minor_ops,
9581#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009582};
9583
Trond Myklebust13997822016-07-24 17:10:52 -04009584static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009585{
9586 ssize_t error, error2;
9587
9588 error = generic_listxattr(dentry, list, size);
9589 if (error < 0)
9590 return error;
9591 if (list) {
9592 list += error;
9593 size -= error;
9594 }
9595
9596 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9597 if (error2 < 0)
9598 return error2;
9599 return error + error2;
9600}
9601
Trond Myklebust17f26b12013-08-21 15:48:42 -04009602static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009603 .create = nfs_create,
9604 .lookup = nfs_lookup,
9605 .atomic_open = nfs_atomic_open,
9606 .link = nfs_link,
9607 .unlink = nfs_unlink,
9608 .symlink = nfs_symlink,
9609 .mkdir = nfs_mkdir,
9610 .rmdir = nfs_rmdir,
9611 .mknod = nfs_mknod,
9612 .rename = nfs_rename,
9613 .permission = nfs_permission,
9614 .getattr = nfs_getattr,
9615 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009616 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009617};
9618
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009619static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009620 .permission = nfs_permission,
9621 .getattr = nfs_getattr,
9622 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009623 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009624};
9625
David Howells509de812006-08-22 20:06:11 -04009626const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009627 .version = 4, /* protocol version */
9628 .dentry_ops = &nfs4_dentry_operations,
9629 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009630 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009631 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009633 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009634 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635 .getattr = nfs4_proc_getattr,
9636 .setattr = nfs4_proc_setattr,
9637 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009638 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009639 .access = nfs4_proc_access,
9640 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009641 .create = nfs4_proc_create,
9642 .remove = nfs4_proc_remove,
9643 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009644 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009645 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009646 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009647 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009648 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649 .link = nfs4_proc_link,
9650 .symlink = nfs4_proc_symlink,
9651 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009652 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653 .readdir = nfs4_proc_readdir,
9654 .mknod = nfs4_proc_mknod,
9655 .statfs = nfs4_proc_statfs,
9656 .fsinfo = nfs4_proc_fsinfo,
9657 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009658 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009660 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009661 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009662 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009664 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009666 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009667 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009669 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009670 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009671 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009672 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009673 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009674 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009675 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009676 .create_server = nfs4_create_server,
9677 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678};
9679
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009680static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009681 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009682 .list = nfs4_xattr_list_nfs4_acl,
9683 .get = nfs4_xattr_get_nfs4_acl,
9684 .set = nfs4_xattr_set_nfs4_acl,
9685};
9686
9687const struct xattr_handler *nfs4_xattr_handlers[] = {
9688 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009689#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9690 &nfs4_xattr_nfs4_label_handler,
9691#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009692 NULL
9693};
9694
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695/*
9696 * Local variables:
9697 * c-basic-offset: 8
9698 * End:
9699 */