blob: 6b23a032ee1e4a43b166931910869c95e3a578f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -040099static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
100 struct rpc_cred *cred,
101 struct nfs4_slot *slot,
102 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400103static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
104 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400105static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
106 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400107#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400108
109#ifdef CONFIG_NFS_V4_SECURITY_LABEL
110static inline struct nfs4_label *
111nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
112 struct iattr *sattr, struct nfs4_label *label)
113{
114 int err;
115
116 if (label == NULL)
117 return NULL;
118
119 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
120 return NULL;
121
David Quigleyaa9c2662013-05-22 12:50:44 -0400122 err = security_dentry_init_security(dentry, sattr->ia_mode,
123 &dentry->d_name, (void **)&label->label, &label->len);
124 if (err == 0)
125 return label;
126
127 return NULL;
128}
129static inline void
130nfs4_label_release_security(struct nfs4_label *label)
131{
132 if (label)
133 security_release_secctx(label->label, label->len);
134}
135static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
136{
137 if (label)
138 return server->attr_bitmask;
139
140 return server->attr_bitmask_nl;
141}
142#else
143static inline struct nfs4_label *
144nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
145 struct iattr *sattr, struct nfs4_label *l)
146{ return NULL; }
147static inline void
148nfs4_label_release_security(struct nfs4_label *label)
149{ return; }
150static inline u32 *
151nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
152{ return server->attr_bitmask; }
153#endif
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500156static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Trond Myklebust52567b02009-10-23 14:46:42 -0400158 if (err >= -1000)
159 return err;
160 switch (err) {
161 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500162 case -NFS4ERR_LAYOUTTRYLATER:
163 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000165 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400166 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800168 case -NFS4ERR_BADOWNER:
169 case -NFS4ERR_BADNAME:
170 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400171 case -NFS4ERR_SHARE_DENIED:
172 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400173 case -NFS4ERR_MINOR_VERS_MISMATCH:
174 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400175 case -NFS4ERR_FILE_OPEN:
176 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700179 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * This is our standard bitmap for GETATTR requests.
187 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400188const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID,
194 FATTR4_WORD1_MODE
195 | FATTR4_WORD1_NUMLINKS
196 | FATTR4_WORD1_OWNER
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400204#ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Trond Myklebust1549210f2012-06-05 09:16:47 -0400209static const u32 nfs4_pnfs_open_bitmap[3] = {
210 FATTR4_WORD0_TYPE
211 | FATTR4_WORD0_CHANGE
212 | FATTR4_WORD0_SIZE
213 | FATTR4_WORD0_FSID
214 | FATTR4_WORD0_FILEID,
215 FATTR4_WORD1_MODE
216 | FATTR4_WORD1_NUMLINKS
217 | FATTR4_WORD1_OWNER
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY,
224 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500225#ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
227#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400228};
229
Andy Adamsone23008e2012-10-02 21:07:32 -0400230static const u32 nfs4_open_noattr_bitmap[3] = {
231 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400232 | FATTR4_WORD0_FILEID,
233};
234
David Quigleya09df2c2013-05-22 12:50:41 -0400235const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_MAXLINK
246 | FATTR4_WORD0_MAXNAME,
247 0
248};
249
Fred Isamandae100c2011-07-30 20:52:37 -0400250const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700254 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400255 | FATTR4_WORD1_FS_LAYOUT_TYPES,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800257 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
David Quigleya09df2c2013-05-22 12:50:41 -0400260const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500261 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400262 | FATTR4_WORD0_SIZE
263 | FATTR4_WORD0_FSID
264 | FATTR4_WORD0_FILEID
265 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500266 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400274};
275
Al Virobc4785c2006-10-19 23:28:51 -0700276static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nfs4_readdir_arg *readdir)
278{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400279 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400310 *p++ = htonl(attrs); /* bitmap */
311 *p++ = htonl(12); /* attribute buffer length */
312 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000313 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 *p++ = xdr_one; /* next */
317 *p++ = xdr_zero; /* cookie, first word */
318 *p++ = xdr_two; /* cookie, second word */
319 *p++ = xdr_two; /* entry len */
320 memcpy(p, "..\0\0", 4); /* entry */
321 p++;
322 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400323 *p++ = htonl(attrs); /* bitmap */
324 *p++ = htonl(12); /* attribute buffer length */
325 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000326 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 readdir->pgbase = (char *)p - (char *)start;
329 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800330 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Trond Myklebust26d36302016-09-22 13:39:05 -0400333static void nfs4_test_and_free_stateid(struct nfs_server *server,
334 nfs4_stateid *stateid,
335 struct rpc_cred *cred)
336{
337 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
338
339 ops->test_and_free_expired(server, stateid, cred);
340}
341
342static void __nfs4_free_revoked_stateid(struct nfs_server *server,
343 nfs4_stateid *stateid,
344 struct rpc_cred *cred)
345{
346 stateid->type = NFS4_REVOKED_STATEID_TYPE;
347 nfs4_test_and_free_stateid(server, stateid, cred);
348}
349
350static void nfs4_free_revoked_stateid(struct nfs_server *server,
351 const nfs4_stateid *stateid,
352 struct rpc_cred *cred)
353{
354 nfs4_stateid tmp;
355
356 nfs4_stateid_copy(&tmp, stateid);
357 __nfs4_free_revoked_stateid(server, &tmp, cred);
358}
359
NeilBrown8478eaa2014-09-18 16:09:27 +1000360static long nfs4_update_delay(long *timeout)
361{
362 long ret;
363 if (!timeout)
364 return NFS4_POLL_RETRY_MAX;
365 if (*timeout <= 0)
366 *timeout = NFS4_POLL_RETRY_MIN;
367 if (*timeout > NFS4_POLL_RETRY_MAX)
368 *timeout = NFS4_POLL_RETRY_MAX;
369 ret = *timeout;
370 *timeout <<= 1;
371 return ret;
372}
373
Trond Myklebust65de8722008-12-23 15:21:44 -0500374static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
375{
376 int res = 0;
377
378 might_sleep();
379
NeilBrown8478eaa2014-09-18 16:09:27 +1000380 freezable_schedule_timeout_killable_unsafe(
381 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 if (fatal_signal_pending(current))
383 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500384 return res;
385}
386
387/* This is the error handling routine for processes that are allowed
388 * to sleep.
389 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400390static int nfs4_do_handle_exception(struct nfs_server *server,
391 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500392{
393 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500394 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400395 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500396 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 int ret = errorcode;
398
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400399 exception->delay = 0;
400 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500401 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400402
403 if (stateid == NULL && state != NULL)
404 stateid = &state->stateid;
405
Trond Myklebust65de8722008-12-23 15:21:44 -0500406 switch(errorcode) {
407 case 0:
408 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400409 case -NFS4ERR_DELEG_REVOKED:
410 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400412 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400413 if (inode != NULL && stateid != NULL) {
414 nfs_inode_find_state_and_recover(inode,
415 stateid);
416 goto wait_on_recovery;
417 }
418 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400419 if (inode) {
420 int err;
421
422 err = nfs_async_inode_return_delegation(inode,
423 stateid);
424 if (err == 0)
425 goto wait_on_recovery;
426 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
427 exception->retry = 1;
428 break;
429 }
430 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500431 if (state == NULL)
432 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400433 ret = nfs4_schedule_stateid_recovery(server, state);
434 if (ret < 0)
435 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500436 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500439 nfs4_schedule_lease_recovery(clp);
440 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400441 case -NFS4ERR_MOVED:
442 ret = nfs4_schedule_migration_recovery(server);
443 if (ret < 0)
444 break;
445 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400446 case -NFS4ERR_LEASE_MOVED:
447 nfs4_schedule_lease_moved_recovery(clp);
448 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500449#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400450 case -NFS4ERR_BADSESSION:
451 case -NFS4ERR_BADSLOT:
452 case -NFS4ERR_BAD_HIGH_SLOT:
453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
454 case -NFS4ERR_DEADSESSION:
455 case -NFS4ERR_SEQ_FALSE_RETRY:
456 case -NFS4ERR_SEQ_MISORDERED:
457 dprintk("%s ERROR: %d Reset session\n", __func__,
458 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400459 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400460 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500461#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500462 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500463 if (exception->timeout > HZ) {
464 /* We have retried a decent amount, time to
465 * fail
466 */
467 ret = -EBUSY;
468 break;
469 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500470 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400471 nfs_inc_server_stats(server, NFSIOS_DELAY);
472 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400473 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400474 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400475 exception->delay = 1;
476 return 0;
477
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400478 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500479 case -NFS4ERR_OLD_STATEID:
480 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800481 break;
482 case -NFS4ERR_BADOWNER:
483 /* The following works around a Linux server bug! */
484 case -NFS4ERR_BADNAME:
485 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
486 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
487 exception->retry = 1;
488 printk(KERN_WARNING "NFS: v4 server %s "
489 "does not accept raw "
490 "uid/gids. "
491 "Reenabling the idmapper.\n",
492 server->nfs_client->cl_hostname);
493 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500494 }
495 /* We failed to handle the error */
496 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500497wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400498 exception->recovering = 1;
499 return 0;
500}
501
502/* This is the error handling routine for processes that are allowed
503 * to sleep.
504 */
505int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
506{
507 struct nfs_client *clp = server->nfs_client;
508 int ret;
509
510 ret = nfs4_do_handle_exception(server, errorcode, exception);
511 if (exception->delay) {
512 ret = nfs4_delay(server->client, &exception->timeout);
513 goto out_retry;
514 }
515 if (exception->recovering) {
516 ret = nfs4_wait_clnt_recover(clp);
517 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
518 return -EIO;
519 goto out_retry;
520 }
521 return ret;
522out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500523 if (ret == 0)
524 exception->retry = 1;
525 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500526}
527
Trond Myklebust037fc982015-09-20 15:51:00 -0400528static int
529nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
530 int errorcode, struct nfs4_exception *exception)
531{
532 struct nfs_client *clp = server->nfs_client;
533 int ret;
534
535 ret = nfs4_do_handle_exception(server, errorcode, exception);
536 if (exception->delay) {
537 rpc_delay(task, nfs4_update_delay(&exception->timeout));
538 goto out_retry;
539 }
540 if (exception->recovering) {
541 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
542 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
543 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
544 goto out_retry;
545 }
546 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
547 ret = -EIO;
548 return ret;
549out_retry:
550 if (ret == 0)
551 exception->retry = 1;
552 return ret;
553}
554
555static int
556nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
557 struct nfs4_state *state, long *timeout)
558{
559 struct nfs4_exception exception = {
560 .state = state,
561 };
562
563 if (task->tk_status >= 0)
564 return 0;
565 if (timeout)
566 exception.timeout = *timeout;
567 task->tk_status = nfs4_async_handle_exception(task, server,
568 task->tk_status,
569 &exception);
570 if (exception.delay && timeout)
571 *timeout = exception.timeout;
572 if (exception.retry)
573 return -EAGAIN;
574 return 0;
575}
576
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400577/*
578 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
579 * or 'false' otherwise.
580 */
581static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
582{
583 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500584 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400585}
Trond Myklebust65de8722008-12-23 15:21:44 -0500586
Trond Myklebust452e9352010-07-31 14:29:06 -0400587static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 spin_lock(&clp->cl_lock);
590 if (time_before(clp->cl_last_renewal,timestamp))
591 clp->cl_last_renewal = timestamp;
592 spin_unlock(&clp->cl_lock);
593}
594
Trond Myklebust452e9352010-07-31 14:29:06 -0400595static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
596{
Trond Myklebustbe824162015-07-05 14:50:46 -0400597 struct nfs_client *clp = server->nfs_client;
598
599 if (!nfs4_has_session(clp))
600 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400601}
602
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400603struct nfs4_call_sync_data {
604 const struct nfs_server *seq_server;
605 struct nfs4_sequence_args *seq_args;
606 struct nfs4_sequence_res *seq_res;
607};
608
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800609void nfs4_init_sequence(struct nfs4_sequence_args *args,
610 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400611{
612 args->sa_slot = NULL;
613 args->sa_cache_this = cache_reply;
614 args->sa_privileged = 0;
615
616 res->sr_slot = NULL;
617}
618
619static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
620{
621 args->sa_privileged = 1;
622}
623
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400624static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400625{
626 struct nfs4_slot *slot = res->sr_slot;
627 struct nfs4_slot_table *tbl;
628
Chuck Lever3bd23842013-08-09 12:49:19 -0400629 tbl = slot->table;
630 spin_lock(&tbl->slot_tbl_lock);
631 if (!nfs41_wake_and_assign_slot(tbl, slot))
632 nfs4_free_slot(tbl, slot);
633 spin_unlock(&tbl->slot_tbl_lock);
634
635 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400636}
637
638static int nfs40_sequence_done(struct rpc_task *task,
639 struct nfs4_sequence_res *res)
640{
641 if (res->sr_slot != NULL)
642 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 return 1;
644}
645
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400646#if defined(CONFIG_NFS_V4_1)
647
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400648static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400649{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500650 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400651 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500652 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400653
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400654 if (!slot)
655 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500656 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500657 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500658
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400659 /* Bump the slot sequence number */
660 if (slot->seq_done)
661 slot->seq_nr++;
662 slot->seq_done = 0;
663
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500664 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500665 /* Be nice to the server: try to ensure that the last transmitted
666 * value for highest_user_slotid <= target_highest_slotid
667 */
668 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
669 send_new_highest_used_slotid = true;
670
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500671 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500672 send_new_highest_used_slotid = false;
673 goto out_unlock;
674 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500675 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500676
677 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
678 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500679out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500680 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500681 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400682 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400683 if (waitqueue_active(&tbl->slot_waitq))
684 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400685}
686
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400687static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
688{
689 nfs41_release_slot(res->sr_slot);
690 res->sr_slot = NULL;
691}
692
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400693static int nfs41_sequence_process(struct rpc_task *task,
694 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400695{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500696 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500699 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500700 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400701
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500702 if (slot == NULL)
703 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400704 /* don't increment the sequence number if the task wasn't sent */
705 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400706 goto out;
707
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500708 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500709
Trond Myklebustac20d162012-12-15 15:36:07 -0500710 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400711 if (res->sr_status != -NFS4ERR_DELAY)
712 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500713 interrupted = true;
714 }
715
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400716 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500717 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400718 switch (res->sr_status) {
719 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400720 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400721 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500723 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500724 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
726 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500727 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 case 1:
730 /*
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
733 * operation..
734 * Mark the slot as having hosted an interrupted RPC call.
735 */
736 slot->interrupted = 1;
737 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 case -NFS4ERR_DELAY:
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
741 * of RFC5661.
742 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500746 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400747 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500748 case -NFS4ERR_BADSLOT:
749 /*
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
752 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400753 if (slot->seq_nr < slot->table->target_highest_slotid)
754 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500755 goto retry_nowait;
756 case -NFS4ERR_SEQ_MISORDERED:
757 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500758 * Was the last operation on this sequence interrupted?
759 * If so, retry after bumping the sequence number.
760 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400761 if (interrupted)
762 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500763 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
766 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 if (slot->seq_nr != 1) {
768 slot->seq_nr = 1;
769 goto retry_nowait;
770 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400771 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500772 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400773 if (interrupted)
774 goto retry_new_seq;
775 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 default:
777 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400778 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779 }
780out:
781 /* The session may be reset by one of the error handlers. */
782 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500783out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500784 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400785session_recover:
786 nfs4_schedule_session_recovery(session, res->sr_status);
787 goto retry_nowait;
788retry_new_seq:
789 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500790retry_nowait:
791 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400792 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500793 task->tk_status = 0;
794 ret = 0;
795 }
796 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400797out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400798 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400799 goto out;
800 rpc_delay(task, NFS4_POLL_RETRY_MAX);
801 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400802}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400803
804int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
805{
806 if (!nfs41_sequence_process(task, res))
807 return 0;
808 if (res->sr_slot != NULL)
809 nfs41_sequence_free_slot(res);
810 return 1;
811
812}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500813EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400814
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400815static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
816{
817 if (res->sr_slot == NULL)
818 return 1;
819 if (res->sr_slot->table->session != NULL)
820 return nfs41_sequence_process(task, res);
821 return nfs40_sequence_done(task, res);
822}
823
824static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
825{
826 if (res->sr_slot != NULL) {
827 if (res->sr_slot->table->session != NULL)
828 nfs41_sequence_free_slot(res);
829 else
830 nfs40_sequence_free_slot(res);
831 }
832}
833
Peng Tao2c4b1312014-06-11 05:24:16 +0800834int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400835{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500836 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400837 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400838 if (!res->sr_slot->table->session)
839 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400841}
Peng Tao2c4b1312014-06-11 05:24:16 +0800842EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400843
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
845{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400846 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400847
Trond Myklebust035168ab2010-06-16 09:52:26 -0400848 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
849
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500850 nfs4_setup_sequence(data->seq_server->nfs_client,
851 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852}
853
Andy Adamson69ab40c2009-04-01 09:22:19 -0400854static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
855{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400856 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857
Trond Myklebust14516c32010-07-31 14:29:06 -0400858 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400859}
860
Trond Myklebust17280172012-03-11 13:11:00 -0400861static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400862 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400863 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400864};
865
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400866static void
867nfs4_sequence_process_interrupted(struct nfs_client *client,
868 struct nfs4_slot *slot, struct rpc_cred *cred)
869{
870 struct rpc_task *task;
871
872 task = _nfs41_proc_sequence(client, cred, slot, true);
873 if (!IS_ERR(task))
874 rpc_put_task_async(task);
875}
876
Chuck Lever3bd23842013-08-09 12:49:19 -0400877#else /* !CONFIG_NFS_V4_1 */
878
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400879static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
880{
881 return nfs40_sequence_done(task, res);
882}
883
884static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
885{
886 if (res->sr_slot != NULL)
887 nfs40_sequence_free_slot(res);
888}
889
Peng Tao2c4b1312014-06-11 05:24:16 +0800890int nfs4_sequence_done(struct rpc_task *task,
891 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400892{
Chuck Lever3bd23842013-08-09 12:49:19 -0400893 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400894}
Peng Tao2c4b1312014-06-11 05:24:16 +0800895EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400896
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400897static void
898nfs4_sequence_process_interrupted(struct nfs_client *client,
899 struct nfs4_slot *slot, struct rpc_cred *cred)
900{
901 WARN_ON_ONCE(1);
902 slot->interrupted = 0;
903}
904
Chuck Lever3bd23842013-08-09 12:49:19 -0400905#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400906
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400907static
908void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
909 struct nfs4_sequence_res *res,
910 struct nfs4_slot *slot)
911{
912 if (!slot)
913 return;
914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 res->sr_timestamp = jiffies;
919 res->sr_status_flags = 0;
920 res->sr_status = 1;
921
922}
923
924int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500925 struct nfs4_sequence_args *args,
926 struct nfs4_sequence_res *res,
927 struct rpc_task *task)
928{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500929 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500930 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500931 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500932
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500933 /* slot already allocated? */
934 if (res->sr_slot != NULL)
935 goto out_start;
936
Anna Schumaker76ee0352017-01-10 16:49:31 -0500937 if (session) {
938 tbl = &session->fc_slot_table;
939 task->tk_timeout = 0;
940 }
941
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400942 for (;;) {
943 spin_lock(&tbl->slot_tbl_lock);
944 /* The state manager will wait until the slot table is empty */
945 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
946 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500947
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400948 slot = nfs4_alloc_slot(tbl);
949 if (IS_ERR(slot)) {
950 /* Try again in 1/4 second */
951 if (slot == ERR_PTR(-ENOMEM))
952 task->tk_timeout = HZ >> 2;
953 goto out_sleep;
954 }
955 spin_unlock(&tbl->slot_tbl_lock);
956
957 if (likely(!slot->interrupted))
958 break;
959 nfs4_sequence_process_interrupted(client,
960 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500961 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500962
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400963 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500964
Anna Schumakerad05cc02017-01-11 13:37:06 -0500965 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500966out_start:
967 rpc_call_start(task);
968 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500969
970out_sleep:
971 if (args->sa_privileged)
972 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
973 NULL, RPC_PRIORITY_PRIVILEGED);
974 else
975 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
976 spin_unlock(&tbl->slot_tbl_lock);
977 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978}
979EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
980
Chuck Lever9915ea72013-08-09 12:48:27 -0400981static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
982{
983 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500984 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400985 data->seq_args, data->seq_res, task);
986}
987
988static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
989{
990 struct nfs4_call_sync_data *data = calldata;
991 nfs4_sequence_done(task, data->seq_res);
992}
993
994static const struct rpc_call_ops nfs40_call_sync_ops = {
995 .rpc_call_prepare = nfs40_call_sync_prepare,
996 .rpc_call_done = nfs40_call_sync_done,
997};
998
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400999static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001000 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001004{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 int ret;
1006 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001007 struct nfs_client *clp = server->nfs_client;
1008 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009 .seq_server = server,
1010 .seq_args = args,
1011 .seq_res = res,
1012 };
1013 struct rpc_task_setup task_setup = {
1014 .rpc_client = clnt,
1015 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001016 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001017 .callback_data = &data
1018 };
1019
1020 task = rpc_run_task(&task_setup);
1021 if (IS_ERR(task))
1022 ret = PTR_ERR(task);
1023 else {
1024 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001025 rpc_put_task(task);
1026 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001027 return ret;
1028}
1029
Bryan Schumaker7c513052011-03-24 17:12:24 +00001030int nfs4_call_sync(struct rpc_clnt *clnt,
1031 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001032 struct rpc_message *msg,
1033 struct nfs4_sequence_args *args,
1034 struct nfs4_sequence_res *res,
1035 int cache_reply)
1036{
Chuck Levera9c92d62013-08-09 12:48:18 -04001037 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001038 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001041static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1042 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct nfs_inode *nfsi = NFS_I(dir);
1045
1046 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001047 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001048 if (cinfo->atomic && cinfo->before == dir->i_version) {
1049 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1050 nfsi->attrtimeo_timestamp = jiffies;
1051 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001053 if (cinfo->before != dir->i_version)
1054 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1055 NFS_INO_INVALID_ACL;
1056 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001057 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001058 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001059 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001060 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_unlock(&dir->i_lock);
1062}
1063
1064struct nfs4_opendata {
1065 struct kref kref;
1066 struct nfs_openargs o_arg;
1067 struct nfs_openres o_res;
1068 struct nfs_open_confirmargs c_arg;
1069 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001070 struct nfs4_string owner_name;
1071 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001072 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001074 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001076 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct nfs4_state_owner *owner;
1078 struct nfs4_state *state;
1079 struct iattr attrs;
1080 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001081 bool rpc_done;
1082 bool file_created;
1083 bool is_recover;
1084 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001085 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001087
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001088struct nfs4_open_createattrs {
1089 struct nfs4_label *label;
1090 struct iattr *sattr;
1091 const __u32 verf[2];
1092};
1093
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001094static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1095 int err, struct nfs4_exception *exception)
1096{
1097 if (err != -EINVAL)
1098 return false;
1099 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1100 return false;
1101 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1102 exception->retry = 1;
1103 return true;
1104}
1105
Trond Myklebust6ae37332015-01-30 14:21:14 -05001106static u32
1107nfs4_map_atomic_open_share(struct nfs_server *server,
1108 fmode_t fmode, int openflags)
1109{
1110 u32 res = 0;
1111
1112 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1113 case FMODE_READ:
1114 res = NFS4_SHARE_ACCESS_READ;
1115 break;
1116 case FMODE_WRITE:
1117 res = NFS4_SHARE_ACCESS_WRITE;
1118 break;
1119 case FMODE_READ|FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_BOTH;
1121 }
1122 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1123 goto out;
1124 /* Want no delegation if we're using O_DIRECT */
1125 if (openflags & O_DIRECT)
1126 res |= NFS4_SHARE_WANT_NO_DELEG;
1127out:
1128 return res;
1129}
1130
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001131static enum open_claim_type4
1132nfs4_map_atomic_open_claim(struct nfs_server *server,
1133 enum open_claim_type4 claim)
1134{
1135 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1136 return claim;
1137 switch (claim) {
1138 default:
1139 return claim;
1140 case NFS4_OPEN_CLAIM_FH:
1141 return NFS4_OPEN_CLAIM_NULL;
1142 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1143 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1144 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1145 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1146 }
1147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150{
1151 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001152 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153 p->o_res.seqid = p->o_arg.seqid;
1154 p->c_res.seqid = p->c_arg.seqid;
1155 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001156 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001157 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001158 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159}
1160
Al Viro82a2c1b2011-06-22 18:30:55 -04001161static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001163 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001164 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001165 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166{
Al Viro82a2c1b2011-06-22 18:30:55 -04001167 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001168 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001170 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001171 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs4_opendata *p;
1173
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001174 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 if (p == NULL)
1176 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001177
1178 p->f_label = nfs4_label_alloc(server, gfp_mask);
1179 if (IS_ERR(p->f_label))
1180 goto err_free_p;
1181
Kinglong Meea49c2692015-07-27 15:31:38 +08001182 p->a_label = nfs4_label_alloc(server, gfp_mask);
1183 if (IS_ERR(p->a_label))
1184 goto err_free_f;
1185
Trond Myklebust63f5f792015-01-23 19:19:25 -05001186 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1187 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001188 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001189 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001190 nfs_sb_active(dentry->d_sb);
1191 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 p->dir = parent;
1193 p->owner = sp;
1194 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 p->o_arg.open_flags = flags;
1196 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001197 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001198 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001199 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1200 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001201 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1202 * will return permission denied for all bits until close */
1203 if (!(flags & O_EXCL)) {
1204 /* ask server to check for all possible rights as results
1205 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001206 switch (p->o_arg.claim) {
1207 default:
1208 break;
1209 case NFS4_OPEN_CLAIM_NULL:
1210 case NFS4_OPEN_CLAIM_FH:
1211 p->o_arg.access = NFS4_ACCESS_READ |
1212 NFS4_ACCESS_MODIFY |
1213 NFS4_ACCESS_EXTEND |
1214 NFS4_ACCESS_EXECUTE;
1215 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001216 }
David Howells7539bba2006-08-22 20:06:09 -04001217 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001218 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1219 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001222 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001223 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001224 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001225 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001226 case NFS4_OPEN_CLAIM_NULL:
1227 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1228 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1229 p->o_arg.fh = NFS_FH(dir);
1230 break;
1231 case NFS4_OPEN_CLAIM_PREVIOUS:
1232 case NFS4_OPEN_CLAIM_FH:
1233 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001235 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001236 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001237 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001238 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001239 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001240
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001241 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001242 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001243 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001244 p->c_arg.fh = &p->o_res.fh;
1245 p->c_arg.stateid = &p->o_res.stateid;
1246 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001247 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001250
1251err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001252 nfs4_label_free(p->a_label);
1253err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001254 nfs4_label_free(p->f_label);
1255err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001256 kfree(p);
1257err:
1258 dput(parent);
1259 return NULL;
1260}
1261
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001263{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001264 struct nfs4_opendata *p = container_of(kref,
1265 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001266 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267
1268 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001269 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001270 if (p->state != NULL)
1271 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001273
Kinglong Meea49c2692015-07-27 15:31:38 +08001274 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001275 nfs4_label_free(p->f_label);
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001278 dput(p->dentry);
1279 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001280 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001281 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282 kfree(p);
1283}
1284
1285static void nfs4_opendata_put(struct nfs4_opendata *p)
1286{
1287 if (p != NULL)
1288 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289}
1290
Trond Myklebust24311f82015-09-20 10:50:17 -04001291static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1292 fmode_t fmode)
1293{
1294 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1295 case FMODE_READ|FMODE_WRITE:
1296 return state->n_rdwr != 0;
1297 case FMODE_WRITE:
1298 return state->n_wronly != 0;
1299 case FMODE_READ:
1300 return state->n_rdonly != 0;
1301 }
1302 WARN_ON_ONCE(1);
1303 return false;
1304}
1305
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001307{
1308 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309
Trond Myklebust536e43d2012-01-17 22:04:26 -05001310 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 goto out;
1312 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001314 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1315 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 break;
1317 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001318 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1319 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001320 break;
1321 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001322 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1323 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001325out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001326 return ret;
1327}
1328
Trond Myklebust2a606182015-08-19 22:30:00 -05001329static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1330 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001331{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001332 if (delegation == NULL)
1333 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001334 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001336 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1337 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001338 switch (claim) {
1339 case NFS4_OPEN_CLAIM_NULL:
1340 case NFS4_OPEN_CLAIM_FH:
1341 break;
1342 case NFS4_OPEN_CLAIM_PREVIOUS:
1343 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1344 break;
1345 default:
1346 return 0;
1347 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001348 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001349 return 1;
1350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001353{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001355 case FMODE_WRITE:
1356 state->n_wronly++;
1357 break;
1358 case FMODE_READ:
1359 state->n_rdonly++;
1360 break;
1361 case FMODE_READ|FMODE_WRITE:
1362 state->n_rdwr++;
1363 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001365}
1366
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001367#ifdef CONFIG_NFS_V4_1
1368static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1369{
1370 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1371 return true;
1372 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1373 return true;
1374 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1375 return true;
1376 return false;
1377}
1378#endif /* CONFIG_NFS_V4_1 */
1379
Trond Myklebustc9399f22017-11-06 15:28:01 -05001380static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1381{
1382 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1383 wake_up_all(&state->waitq);
1384}
1385
1386static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1387 const nfs4_stateid *stateid)
1388{
1389 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1390 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1391
1392 if (stateid_seqid == state_seqid + 1U ||
1393 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1394 nfs_state_log_update_open_stateid(state);
1395 else
1396 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1397}
1398
Trond Myklebust4f14c192014-02-12 19:15:06 -05001399static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001400{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001401 struct nfs_client *clp = state->owner->so_server->nfs_client;
1402 bool need_recover = false;
1403
1404 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1405 need_recover = true;
1406 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1407 need_recover = true;
1408 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1409 need_recover = true;
1410 if (need_recover)
1411 nfs4_state_mark_reclaim_nograce(clp, state);
1412}
1413
Trond Myklebustc9399f22017-11-06 15:28:01 -05001414/*
1415 * Check for whether or not the caller may update the open stateid
1416 * to the value passed in by stateid.
1417 *
1418 * Note: This function relies heavily on the server implementing
1419 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1420 * correctly.
1421 * i.e. The stateid seqids have to be initialised to 1, and
1422 * are then incremented on every state transition.
1423 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001424static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001425 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001426{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001427 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1428 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1429 if (stateid->seqid == cpu_to_be32(1))
1430 nfs_state_log_update_open_stateid(state);
1431 else
1432 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001434 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001435
1436 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1437 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001439 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001440 return false;
1441}
1442
Trond Myklebustf95549c2015-01-23 18:06:09 -05001443static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1444{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001445 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1446 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447 if (state->n_wronly)
1448 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1449 if (state->n_rdonly)
1450 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1451 if (state->n_rdwr)
1452 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001454}
1455
Trond Myklebust226056c2014-02-11 10:41:07 -05001456static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1457 nfs4_stateid *stateid, fmode_t fmode)
1458{
1459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1460 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1461 case FMODE_WRITE:
1462 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 break;
1464 case FMODE_READ:
1465 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 break;
1467 case 0:
1468 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 clear_bit(NFS_OPEN_STATE, &state->flags);
1471 }
1472 if (stateid == NULL)
1473 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001474 /* Handle OPEN+OPEN_DOWNGRADE races */
1475 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1476 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001477 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001478 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 nfs4_stateid_copy(&state->stateid, stateid);
1482 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001483 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001484out:
1485 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486}
1487
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001488static void nfs_clear_open_stateid(struct nfs4_state *state,
1489 nfs4_stateid *arg_stateid,
1490 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001491{
1492 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001493 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1494 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1495 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001496 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001497 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1498 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499}
1500
Trond Myklebust1393d962016-09-22 13:39:13 -04001501static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001502 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001503{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001504 DEFINE_WAIT(wait);
1505 int status = 0;
1506 for (;;) {
1507
1508 if (!nfs_need_update_open_stateid(state, stateid))
1509 return;
1510 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001511 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001512 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001513 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001514 /* Rely on seqids for serialisation with NFSv4.0 */
1515 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1516 break;
1517
1518 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1519 /*
1520 * Ensure we process the state changes in the same order
1521 * in which the server processed them by delaying the
1522 * update of the stateid until we are in sequence.
1523 */
1524 write_sequnlock(&state->seqlock);
1525 spin_unlock(&state->owner->so_lock);
1526 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001527 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001528 if (!signal_pending(current)) {
1529 if (schedule_timeout(5*HZ) == 0)
1530 status = -EAGAIN;
1531 else
1532 status = 0;
1533 } else
1534 status = -EINTR;
1535 finish_wait(&state->waitq, &wait);
1536 rcu_read_lock();
1537 spin_lock(&state->owner->so_lock);
1538 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001539 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001540
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001541 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1542 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543 nfs4_stateid_copy(freeme, &state->open_stateid);
1544 nfs_test_and_clear_all_open_stateid(state);
1545 }
1546
Trond Myklebuste999e802014-02-10 18:20:47 -05001547 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1548 nfs4_stateid_copy(&state->stateid, stateid);
1549 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001550 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001552}
1553
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001556 fmode_t fmode,
1557 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001559 /*
1560 * Protect the call to nfs4_state_set_mode_locked and
1561 * serialise the stateid update
1562 */
1563 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001564 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1565 switch (fmode) {
1566 case FMODE_READ:
1567 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1568 break;
1569 case FMODE_WRITE:
1570 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_READ|FMODE_WRITE:
1573 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001574 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001576 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001577}
1578
1579static void nfs_state_set_delegation(struct nfs4_state *state,
1580 const nfs4_stateid *deleg_stateid,
1581 fmode_t fmode)
1582{
1583 /*
1584 * Protect the call to nfs4_state_set_mode_locked and
1585 * serialise the stateid update
1586 */
1587 write_seqlock(&state->seqlock);
1588 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1589 set_bit(NFS_DELEGATED_STATE, &state->flags);
1590 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Trond Myklebust1393d962016-09-22 13:39:13 -04001593static int update_open_stateid(struct nfs4_state *state,
1594 const nfs4_stateid *open_stateid,
1595 const nfs4_stateid *delegation,
1596 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001597{
Trond Myklebust1393d962016-09-22 13:39:13 -04001598 struct nfs_server *server = NFS_SERVER(state->inode);
1599 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001600 struct nfs_inode *nfsi = NFS_I(state->inode);
1601 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001602 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001603 int ret = 0;
1604
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001605 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001606
1607 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001608 spin_lock(&state->owner->so_lock);
1609 if (open_stateid != NULL) {
1610 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1611 ret = 1;
1612 }
1613
Trond Myklebust34310432008-12-23 15:21:38 -05001614 deleg_cur = rcu_dereference(nfsi->delegation);
1615 if (deleg_cur == NULL)
1616 goto no_delegation;
1617
1618 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001619 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001620 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001621 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001622 goto no_delegation_unlock;
1623
1624 if (delegation == NULL)
1625 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001626 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001627 goto no_delegation_unlock;
1628
Trond Myklebustb7391f42008-12-23 15:21:52 -05001629 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001631 ret = 1;
1632no_delegation_unlock:
1633 spin_unlock(&deleg_cur->lock);
1634no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001635 if (ret)
1636 update_open_stateflags(state, fmode);
1637 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001638 rcu_read_unlock();
1639
Trond Myklebust4f14c192014-02-12 19:15:06 -05001640 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001641 nfs4_schedule_state_manager(clp);
1642 if (freeme.type != 0)
1643 nfs4_test_and_free_stateid(server, &freeme,
1644 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001645
1646 return ret;
1647}
1648
Trond Myklebust39071e62015-01-24 15:07:56 -05001649static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1650 const nfs4_stateid *stateid)
1651{
1652 struct nfs4_state *state = lsp->ls_state;
1653 bool ret = false;
1654
1655 spin_lock(&state->state_lock);
1656 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1657 goto out_noupdate;
1658 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1659 goto out_noupdate;
1660 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1661 ret = true;
1662out_noupdate:
1663 spin_unlock(&state->state_lock);
1664 return ret;
1665}
Trond Myklebust34310432008-12-23 15:21:38 -05001666
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668{
1669 struct nfs_delegation *delegation;
1670
1671 rcu_read_lock();
1672 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 rcu_read_unlock();
1675 return;
1676 }
1677 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001678 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679}
1680
Trond Myklebust6ee41262007-07-08 14:11:36 -04001681static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682{
1683 struct nfs4_state *state = opendata->state;
1684 struct nfs_inode *nfsi = NFS_I(state->inode);
1685 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001686 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001687 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001688 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 nfs4_stateid stateid;
1690 int ret = -EAGAIN;
1691
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001693 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001696 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001700 rcu_read_lock();
1701 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001702 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001703 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001704 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001705 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001707 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001709 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001710 if (!opendata->is_recover) {
1711 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1712 if (ret != 0)
1713 goto out;
1714 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001716
1717 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001719 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721out:
1722 return ERR_PTR(ret);
1723out_return_state:
1724 atomic_inc(&state->count);
1725 return state;
1726}
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728static void
1729nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1730{
1731 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1732 struct nfs_delegation *delegation;
1733 int delegation_flags = 0;
1734
1735 rcu_read_lock();
1736 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1737 if (delegation)
1738 delegation_flags = delegation->flags;
1739 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001740 switch (data->o_arg.claim) {
1741 default:
1742 break;
1743 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1744 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001745 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1746 "returning a delegation for "
1747 "OPEN(CLAIM_DELEGATE_CUR)\n",
1748 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001749 return;
1750 }
1751 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001752 nfs_inode_set_delegation(state->inode,
1753 data->owner->so_cred,
1754 &data->o_res);
1755 else
1756 nfs_inode_reclaim_delegation(state->inode,
1757 data->owner->so_cred,
1758 &data->o_res);
1759}
1760
1761/*
1762 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1763 * and update the nfs4_state.
1764 */
1765static struct nfs4_state *
1766_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1767{
1768 struct inode *inode = data->state->inode;
1769 struct nfs4_state *state = data->state;
1770 int ret;
1771
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001772 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001773 if (data->rpc_status)
1774 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001775 /* cached opens have already been processed */
1776 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001777 }
1778
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 ret = nfs_refresh_inode(inode, &data->f_attr);
1780 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001781 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001782
1783 if (data->o_res.delegation_type != 0)
1784 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001785update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001786 update_open_stateid(state, &data->o_res.stateid, NULL,
1787 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001788 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001789
1790 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001791}
1792
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001793static struct inode *
1794nfs4_opendata_get_inode(struct nfs4_opendata *data)
1795{
1796 struct inode *inode;
1797
1798 switch (data->o_arg.claim) {
1799 case NFS4_OPEN_CLAIM_NULL:
1800 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1801 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1802 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1803 return ERR_PTR(-EAGAIN);
1804 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1805 &data->f_attr, data->f_label);
1806 break;
1807 default:
1808 inode = d_inode(data->dentry);
1809 ihold(inode);
1810 nfs_refresh_inode(inode, &data->f_attr);
1811 }
1812 return inode;
1813}
1814
Andy Adamsone23008e2012-10-02 21:07:32 -04001815static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001816nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1817{
1818 struct nfs4_state *state;
1819 struct inode *inode;
1820
1821 inode = nfs4_opendata_get_inode(data);
1822 if (IS_ERR(inode))
1823 return ERR_CAST(inode);
1824 if (data->state != NULL && data->state->inode == inode) {
1825 state = data->state;
1826 atomic_inc(&state->count);
1827 } else
1828 state = nfs4_get_open_state(inode, data->owner);
1829 iput(inode);
1830 if (state == NULL)
1831 state = ERR_PTR(-ENOMEM);
1832 return state;
1833}
1834
Andy Adamsone23008e2012-10-02 21:07:32 -04001835static struct nfs4_state *
1836_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001838 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001841 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001842 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001843 goto out;
1844 }
1845
Trond Myklebust75e8c482017-08-08 10:38:07 -04001846 state = nfs4_opendata_find_nfs4_state(data);
1847 if (IS_ERR(state))
1848 goto out;
1849
Andy Adamsone23008e2012-10-02 21:07:32 -04001850 if (data->o_res.delegation_type != 0)
1851 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001852 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001853 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001855 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 return state;
1857}
1858
Andy Adamsone23008e2012-10-02 21:07:32 -04001859static struct nfs4_state *
1860nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1861{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001862 struct nfs4_state *ret;
1863
Andy Adamsone23008e2012-10-02 21:07:32 -04001864 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001865 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1866 else
1867 ret = _nfs4_opendata_to_nfs4_state(data);
1868 nfs4_sequence_free_slot(&data->o_res.seq_res);
1869 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001870}
1871
Trond Myklebust864472e2006-01-03 09:55:15 +01001872static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1873{
1874 struct nfs_inode *nfsi = NFS_I(state->inode);
1875 struct nfs_open_context *ctx;
1876
1877 spin_lock(&state->inode->i_lock);
1878 list_for_each_entry(ctx, &nfsi->open_files, list) {
1879 if (ctx->state != state)
1880 continue;
1881 get_nfs_open_context(ctx);
1882 spin_unlock(&state->inode->i_lock);
1883 return ctx;
1884 }
1885 spin_unlock(&state->inode->i_lock);
1886 return ERR_PTR(-ENOENT);
1887}
1888
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001889static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1890 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001891{
1892 struct nfs4_opendata *opendata;
1893
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001894 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001895 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001896 if (opendata == NULL)
1897 return ERR_PTR(-ENOMEM);
1898 opendata->state = state;
1899 atomic_inc(&state->count);
1900 return opendata;
1901}
1902
Trond Myklebust24311f82015-09-20 10:50:17 -04001903static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1904 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001905{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001906 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001907 int ret;
1908
Trond Myklebust24311f82015-09-20 10:50:17 -04001909 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001910 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001911 opendata->o_arg.open_flags = 0;
1912 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001913 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1914 NFS_SB(opendata->dentry->d_sb),
1915 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001916 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1917 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1918 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001919 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001920 if (ret != 0)
1921 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001922 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001923 if (IS_ERR(newstate))
1924 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001925 if (newstate != opendata->state)
1926 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001927 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001928 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001929}
1930
1931static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1932{
Trond Myklebust864472e2006-01-03 09:55:15 +01001933 int ret;
1934
Trond Myklebust4f14c192014-02-12 19:15:06 -05001935 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1936 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1937 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1938 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001939 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001940 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001941 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001943 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1944 if (ret != 0)
1945 return ret;
1946 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1947 if (ret != 0)
1948 return ret;
1949 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1950 if (ret != 0)
1951 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001952 /*
1953 * We may have performed cached opens for all three recoveries.
1954 * Check if we need to update the current stateid.
1955 */
1956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001957 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001958 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001959 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001960 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001961 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001962 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001963 return 0;
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * OPEN_RECLAIM:
1968 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001970static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001972 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001973 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001974 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int status;
1976
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1978 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001981 rcu_read_lock();
1982 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001983 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001984 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001985 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001986 opendata->o_arg.u.delegation_type = delegation_type;
1987 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001988 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return status;
1990}
1991
Trond Myklebust539cd032007-06-05 11:46:42 -04001992static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct nfs_server *server = NFS_SERVER(state->inode);
1995 struct nfs4_exception exception = { };
1996 int err;
1997 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001998 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001999 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002000 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2001 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002002 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002003 break;
2004 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } while (exception.retry);
2006 return err;
2007}
2008
Trond Myklebust864472e2006-01-03 09:55:15 +01002009static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2010{
2011 struct nfs_open_context *ctx;
2012 int ret;
2013
2014 ctx = nfs4_state_find_open_context(state);
2015 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002016 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002017 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002018 put_nfs_open_context(ctx);
2019 return ret;
2020}
2021
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002022static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002024 switch (err) {
2025 default:
2026 printk(KERN_ERR "NFS: %s: unhandled error "
2027 "%d.\n", __func__, err);
2028 case 0:
2029 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002030 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002031 case -ESTALE:
2032 break;
2033 case -NFS4ERR_BADSESSION:
2034 case -NFS4ERR_BADSLOT:
2035 case -NFS4ERR_BAD_HIGH_SLOT:
2036 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2037 case -NFS4ERR_DEADSESSION:
2038 set_bit(NFS_DELEGATED_STATE, &state->flags);
2039 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2040 return -EAGAIN;
2041 case -NFS4ERR_STALE_CLIENTID:
2042 case -NFS4ERR_STALE_STATEID:
2043 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002044 /* Don't recall a delegation if it was lost */
2045 nfs4_schedule_lease_recovery(server->nfs_client);
2046 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002047 case -NFS4ERR_MOVED:
2048 nfs4_schedule_migration_recovery(server);
2049 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002050 case -NFS4ERR_LEASE_MOVED:
2051 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2052 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002053 case -NFS4ERR_DELEG_REVOKED:
2054 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002055 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002056 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002057 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 nfs_inode_find_state_and_recover(state->inode,
2059 stateid);
2060 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002061 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002062 case -NFS4ERR_DELAY:
2063 case -NFS4ERR_GRACE:
2064 set_bit(NFS_DELEGATED_STATE, &state->flags);
2065 ssleep(1);
2066 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002067 case -ENOMEM:
2068 case -NFS4ERR_DENIED:
2069 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2070 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return err;
2073}
2074
Trond Myklebust24311f82015-09-20 10:50:17 -04002075int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2076 struct nfs4_state *state, const nfs4_stateid *stateid,
2077 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002078{
2079 struct nfs_server *server = NFS_SERVER(state->inode);
2080 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002081 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002082
2083 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2084 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2085 if (IS_ERR(opendata))
2086 return PTR_ERR(opendata);
2087 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002088 write_seqlock(&state->seqlock);
2089 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2090 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002091 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2092 switch (type & (FMODE_READ|FMODE_WRITE)) {
2093 case FMODE_READ|FMODE_WRITE:
2094 case FMODE_WRITE:
2095 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2096 if (err)
2097 break;
2098 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2099 if (err)
2100 break;
2101 case FMODE_READ:
2102 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2103 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002104 nfs4_opendata_put(opendata);
2105 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2106}
2107
Chuck Leverbe05c862013-08-09 12:49:47 -04002108static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2109{
2110 struct nfs4_opendata *data = calldata;
2111
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002112 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2113 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002114}
2115
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002116static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2117{
2118 struct nfs4_opendata *data = calldata;
2119
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002120 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002121
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002122 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002124 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002125 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002127 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002129}
2130
2131static void nfs4_open_confirm_release(void *calldata)
2132{
2133 struct nfs4_opendata *data = calldata;
2134 struct nfs4_state *state = NULL;
2135
2136 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002137 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002138 goto out_free;
2139 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002140 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002141 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002142 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002143 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002144 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002145out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002146 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147}
2148
2149static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002150 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002151 .rpc_call_done = nfs4_open_confirm_done,
2152 .rpc_release = nfs4_open_confirm_release,
2153};
2154
2155/*
2156 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2157 */
2158static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2159{
David Howells2b0143b2015-03-17 22:25:59 +00002160 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002161 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2164 .rpc_argp = &data->c_arg,
2165 .rpc_resp = &data->c_res,
2166 .rpc_cred = data->owner->so_cred,
2167 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002168 struct rpc_task_setup task_setup_data = {
2169 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002170 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002171 .callback_ops = &nfs4_open_confirm_ops,
2172 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002173 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002174 .flags = RPC_TASK_ASYNC,
2175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int status;
2177
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002178 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002179 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002180 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002181 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002183 if (data->is_recover)
2184 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002185 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002186 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002187 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002188 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002189 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002190 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002191 smp_wmb();
2192 } else
2193 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002194 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return status;
2196}
2197
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200 struct nfs4_opendata *data = calldata;
2201 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002202 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002203 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002204
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002206 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002207 /*
2208 * Check if we still need to send an OPEN call, or if we can use
2209 * a delegation instead.
2210 */
2211 if (data->state != NULL) {
2212 struct nfs_delegation *delegation;
2213
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002214 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002215 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002216 rcu_read_lock();
2217 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002218 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002219 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002220 rcu_read_unlock();
2221 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002222 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002223 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002224 switch (claim) {
2225 default:
2226 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002227 case NFS4_OPEN_CLAIM_PREVIOUS:
2228 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2229 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002230 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002231 case NFS4_OPEN_CLAIM_FH:
2232 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002233 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002234 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002235 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002236 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002237 &data->o_res.seq_res,
2238 task) != 0)
2239 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002240
2241 /* Set the create mode (note dependency on the session type) */
2242 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2243 if (data->o_arg.open_flags & O_EXCL) {
2244 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2245 if (nfs4_has_persistent_session(clp))
2246 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2247 else if (clp->cl_mvops->minor_version > 0)
2248 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2249 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002250 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002251unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002252 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002253 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002254out_no_action:
2255 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002256out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002257 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002258}
2259
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260static void nfs4_open_done(struct rpc_task *task, void *calldata)
2261{
2262 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002264 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002265
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002266 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002267 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002268
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002269 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002270 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2271 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002272 case S_IFREG:
2273 break;
2274 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002276 break;
2277 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002279 break;
2280 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002282 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002283 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002284 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002285 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2286 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002287 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002288 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002289}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002290
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291static void nfs4_open_release(void *calldata)
2292{
2293 struct nfs4_opendata *data = calldata;
2294 struct nfs4_state *state = NULL;
2295
2296 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002297 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298 goto out_free;
2299 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002300 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 goto out_free;
2302 /* In case we need an open_confirm, no cleanup! */
2303 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2304 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002306 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002307 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002309 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310}
2311
2312static const struct rpc_call_ops nfs4_open_ops = {
2313 .rpc_call_prepare = nfs4_open_prepare,
2314 .rpc_call_done = nfs4_open_done,
2315 .rpc_release = nfs4_open_release,
2316};
2317
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002318static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002319{
David Howells2b0143b2015-03-17 22:25:59 +00002320 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321 struct nfs_server *server = NFS_SERVER(dir);
2322 struct nfs_openargs *o_arg = &data->o_arg;
2323 struct nfs_openres *o_res = &data->o_res;
2324 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2327 .rpc_argp = o_arg,
2328 .rpc_resp = o_res,
2329 .rpc_cred = data->owner->so_cred,
2330 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002333 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 .callback_ops = &nfs4_open_ops,
2335 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002336 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 .flags = RPC_TASK_ASYNC,
2338 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002339 int status;
2340
Chuck Levera9c92d62013-08-09 12:48:18 -04002341 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002342 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002343 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002344 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002345 data->cancelled = false;
2346 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002347 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002348 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002350 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002351 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002352 if (IS_ERR(task))
2353 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002354 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002355 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 smp_wmb();
2358 } else
2359 status = data->rpc_status;
2360 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002361
2362 return status;
2363}
2364
2365static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2366{
David Howells2b0143b2015-03-17 22:25:59 +00002367 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002368 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002369 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370
2371 status = nfs4_run_open_task(data, 1);
2372 if (status != 0 || !data->rpc_done)
2373 return status;
2374
Trond Myklebust6926afd12012-01-07 13:22:46 -05002375 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2376
Anna Schumakerd7e98252017-01-11 16:13:29 -05002377 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002378 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002379
2380 return status;
2381}
2382
Trond Myklebustf3792d62014-07-10 08:54:32 -04002383/*
2384 * Additional permission checks in order to distinguish between an
2385 * open for read, and an open for execute. This works around the
2386 * fact that NFSv4 OPEN treats read and execute permissions as being
2387 * the same.
2388 * Note that in the non-execute case, we want to turn off permission
2389 * checking if we just created a new file (POSIX open() semantics).
2390 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002391static int nfs4_opendata_access(struct rpc_cred *cred,
2392 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002393 struct nfs4_state *state, fmode_t fmode,
2394 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002395{
2396 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002397 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002398
2399 /* access call failed or for some reason the server doesn't
2400 * support any access modes -- defer access call until later */
2401 if (opendata->o_res.access_supported == 0)
2402 return 0;
2403
2404 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002405 /*
2406 * Use openflags to check for exec, because fmode won't
2407 * always have FMODE_EXEC set when file open for exec.
2408 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002409 if (openflags & __FMODE_EXEC) {
2410 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002411 if (S_ISDIR(state->inode->i_mode))
2412 mask = NFS4_ACCESS_LOOKUP;
2413 else
2414 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002415 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002416 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002417
2418 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002419 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2420 nfs_access_add_cache(state->inode, &cache);
2421
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002422 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2423 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424 return 0;
2425
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002426 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427}
2428
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002429/*
2430 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2431 */
2432static int _nfs4_proc_open(struct nfs4_opendata *data)
2433{
David Howells2b0143b2015-03-17 22:25:59 +00002434 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002435 struct nfs_server *server = NFS_SERVER(dir);
2436 struct nfs_openargs *o_arg = &data->o_arg;
2437 struct nfs_openres *o_res = &data->o_res;
2438 int status;
2439
2440 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002441 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002442 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002443 if (status != 0) {
2444 if (status == -NFS4ERR_BADNAME &&
2445 !(o_arg->open_flags & O_CREAT))
2446 return -ENOENT;
2447 return status;
2448 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002449
Trond Myklebust6926afd12012-01-07 13:22:46 -05002450 nfs_fattr_map_and_free_names(server, &data->f_attr);
2451
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002452 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002453 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002454 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002455 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002456 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002457 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002458 update_changeattr(dir, &o_res->cinfo,
2459 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002460 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002461 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2462 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002464 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002466 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002468 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2469 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002470 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002471 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475/*
2476 * OPEN_EXPIRED:
2477 * reclaim state on the server after a network partition.
2478 * Assumes caller holds the appropriate lock
2479 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002480static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002482 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002483 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002485 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002486 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002487 if (IS_ERR(opendata))
2488 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002489 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002490 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002491 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002492 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002496static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002497{
Trond Myklebust539cd032007-06-05 11:46:42 -04002498 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002499 struct nfs4_exception exception = { };
2500 int err;
2501
2502 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002503 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002504 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002505 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2506 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002507 switch (err) {
2508 default:
2509 goto out;
2510 case -NFS4ERR_GRACE:
2511 case -NFS4ERR_DELAY:
2512 nfs4_handle_exception(server, err, &exception);
2513 err = 0;
2514 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002515 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002516out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002517 return err;
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002523 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Trond Myklebust864472e2006-01-03 09:55:15 +01002525 ctx = nfs4_state_find_open_context(state);
2526 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002527 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002528 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002529 put_nfs_open_context(ctx);
2530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Trond Myklebust41020b62016-09-22 13:38:58 -04002533static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2534 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002535{
Trond Myklebust41020b62016-09-22 13:38:58 -04002536 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002537 write_seqlock(&state->seqlock);
2538 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2539 write_sequnlock(&state->seqlock);
2540 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2541}
2542
2543static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2544{
2545 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002546 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002547}
2548
2549static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2550{
2551 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2552 nfs40_clear_delegation_stateid(state);
2553 return nfs4_open_expired(sp, state);
2554}
2555
Trond Myklebust45870d62016-09-22 13:38:59 -04002556static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2557 nfs4_stateid *stateid,
2558 struct rpc_cred *cred)
2559{
2560 return -NFS4ERR_BAD_STATEID;
2561}
2562
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002563#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002564static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2565 nfs4_stateid *stateid,
2566 struct rpc_cred *cred)
2567{
2568 int status;
2569
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002570 switch (stateid->type) {
2571 default:
2572 break;
2573 case NFS4_INVALID_STATEID_TYPE:
2574 case NFS4_SPECIAL_STATEID_TYPE:
2575 return -NFS4ERR_BAD_STATEID;
2576 case NFS4_REVOKED_STATEID_TYPE:
2577 goto out_free;
2578 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002579
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002580 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002581 switch (status) {
2582 case -NFS4ERR_EXPIRED:
2583 case -NFS4ERR_ADMIN_REVOKED:
2584 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002585 break;
2586 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002587 return status;
2588 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002589out_free:
2590 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002591 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002592 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002593}
2594
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002595static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002596{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002597 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002598 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002599 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002600 struct rpc_cred *cred;
2601 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002602
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002603 /* Get the delegation credential for use by test/free_stateid */
2604 rcu_read_lock();
2605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002606 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002607 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002608 return;
2609 }
2610
2611 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002612 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2613 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2614 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002615 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002616 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002617 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002618 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002619
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 cred = get_rpccred(delegation->cred);
2621 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002622 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002623 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002624 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002625 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002626
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002627 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628}
2629
2630/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002631 * nfs41_check_expired_locks - possibly free a lock stateid
2632 *
2633 * @state: NFSv4 state for an inode
2634 *
2635 * Returns NFS_OK if recovery for this stateid is now finished.
2636 * Otherwise a negative NFS4ERR value is returned.
2637 */
2638static int nfs41_check_expired_locks(struct nfs4_state *state)
2639{
2640 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002641 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002642 struct nfs_server *server = NFS_SERVER(state->inode);
2643
2644 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2645 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002646
2647 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002648 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2649 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2650 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2651
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002652 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002653 spin_unlock(&state->state_lock);
2654
2655 nfs4_put_lock_state(prev);
2656 prev = lsp;
2657
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002658 status = nfs41_test_and_free_expired_stateid(server,
2659 &lsp->ls_stateid,
2660 cred);
2661 trace_nfs4_test_lock_stateid(state, lsp, status);
2662 if (status == -NFS4ERR_EXPIRED ||
2663 status == -NFS4ERR_BAD_STATEID) {
2664 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002665 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002666 if (!recover_lost_locks)
2667 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2668 } else if (status != NFS_OK) {
2669 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002670 nfs4_put_lock_state(prev);
2671 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002672 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002673 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002675 }
2676 spin_unlock(&state->state_lock);
2677 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002678out:
2679 return ret;
2680}
2681
2682/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002683 * nfs41_check_open_stateid - possibly free an open stateid
2684 *
2685 * @state: NFSv4 state for an inode
2686 *
2687 * Returns NFS_OK if recovery for this stateid is now finished.
2688 * Otherwise a negative NFS4ERR value is returned.
2689 */
2690static int nfs41_check_open_stateid(struct nfs4_state *state)
2691{
2692 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002693 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002694 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002695 int status;
2696
Trond Myklebustb134fc42016-09-22 13:39:16 -04002697 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002698 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2699 if (nfs4_have_delegation(state->inode, state->state))
2700 return NFS_OK;
2701 return -NFS4ERR_OPENMODE;
2702 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002703 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002704 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002705 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002706 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002707 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002708 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2709 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2710 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002711 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002712 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002713 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002714 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002715 if (nfs_open_stateid_recover_openmode(state))
2716 return -NFS4ERR_OPENMODE;
2717 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002718}
2719
2720static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2721{
Chuck Levereb64cf92012-07-11 16:30:05 -04002722 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002723
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002724 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002725 status = nfs41_check_expired_locks(state);
2726 if (status != NFS_OK)
2727 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002728 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002729 if (status != NFS_OK)
2730 status = nfs4_open_expired(sp, state);
2731 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002732}
2733#endif
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002736 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2737 * fields corresponding to attributes that were used to store the verifier.
2738 * Make sure we clobber those fields in the later setattr call
2739 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002740static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2741 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002742{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002743 const u32 *attrset = opendata->o_res.attrset;
2744
2745 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002746 !(sattr->ia_valid & ATTR_ATIME_SET))
2747 sattr->ia_valid |= ATTR_ATIME;
2748
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002749 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002750 !(sattr->ia_valid & ATTR_MTIME_SET))
2751 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752
2753 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002754 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002755 (attrset[1] & FATTR4_WORD1_MODE ||
2756 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002757 sattr->ia_valid &= ~ATTR_MODE;
2758
2759 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2760 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002761}
2762
Trond Myklebustc21443c2013-02-07 14:26:21 -05002763static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2764 fmode_t fmode,
2765 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002766 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002767{
2768 struct nfs4_state_owner *sp = opendata->owner;
2769 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002770 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002771 struct nfs4_state *state;
2772 unsigned int seq;
2773 int ret;
2774
2775 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2776
2777 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002778 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002779 goto out;
2780
2781 state = nfs4_opendata_to_nfs4_state(opendata);
2782 ret = PTR_ERR(state);
2783 if (IS_ERR(state))
2784 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002785 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002786 if (server->caps & NFS_CAP_POSIX_LOCK)
2787 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002788 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2789 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002790
Trond Myklebust275bb302013-05-29 13:11:28 -04002791 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002792 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002793 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002794 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002795 alias = d_exact_alias(dentry, state->inode);
2796 if (!alias)
2797 alias = d_splice_alias(igrab(state->inode), dentry);
2798 /* d_splice_alias() can't fail here - it's a non-directory */
2799 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002800 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002801 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002802 }
2803 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002804 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002805 }
2806
Trond Myklebustc21443c2013-02-07 14:26:21 -05002807 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2808 if (ret != 0)
2809 goto out;
2810
David Howells2b0143b2015-03-17 22:25:59 +00002811 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002812 nfs_inode_attach_open_context(ctx);
2813 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2814 nfs4_schedule_stateid_recovery(server, state);
2815 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002816out:
2817 return ret;
2818}
2819
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002820/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002821 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Andy Adamson82be4172012-05-23 05:02:35 -04002823static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002824 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002825 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002826 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002827 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct nfs4_state_owner *sp;
2830 struct nfs4_state *state = NULL;
2831 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002832 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002833 struct dentry *dentry = ctx->dentry;
2834 struct rpc_cred *cred = ctx->cred;
2835 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2836 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002837 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002838 struct iattr *sattr = c->sattr;
2839 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002840 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002841 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002845 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2846 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2848 goto out_err;
2849 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002850 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002851 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002852 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002853 if (d_really_is_positive(dentry))
2854 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002855 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002856 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002857 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002858 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2859 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002860 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002861 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
David Quigley14c43f72013-05-22 12:50:43 -04002863 if (label) {
2864 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2865 if (IS_ERR(olabel)) {
2866 status = PTR_ERR(olabel);
2867 goto err_opendata_put;
2868 }
2869 }
2870
Trond Myklebuste911b812014-03-26 13:24:37 -07002871 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2872 if (!opendata->f_attr.mdsthreshold) {
2873 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2874 if (!opendata->f_attr.mdsthreshold)
2875 goto err_free_label;
2876 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002877 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002878 }
David Howells2b0143b2015-03-17 22:25:59 +00002879 if (d_really_is_positive(dentry))
2880 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002881
Trond Myklebust3efb9722013-05-29 13:17:04 -04002882 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002883 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002884 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002885 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002886
NeilBrownefcbc042015-07-30 13:00:56 +10002887 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002888 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002889 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002890 /*
2891 * send create attributes which was not set by open
2892 * with an extra setattr.
2893 */
2894 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2895 nfs_fattr_init(opendata->o_res.f_attr);
2896 status = nfs4_do_setattr(state->inode, cred,
2897 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002898 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002899 if (status == 0) {
2900 nfs_setattr_update_inode(state->inode, sattr,
2901 opendata->o_res.f_attr);
2902 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2903 }
David Quigley1775fd32013-05-22 12:50:42 -04002904 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002905 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002906 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002907 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002908
Trond Myklebuste911b812014-03-26 13:24:37 -07002909 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002910 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002911 opendata->f_attr.mdsthreshold = NULL;
2912 }
Andy Adamson82be4172012-05-23 05:02:35 -04002913
David Quigley14c43f72013-05-22 12:50:43 -04002914 nfs4_label_free(olabel);
2915
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002916 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002919err_free_label:
2920 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002921err_opendata_put:
2922 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002923err_put_state_owner:
2924 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
2929
Andy Adamson82be4172012-05-23 05:02:35 -04002930static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002931 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002932 int flags,
2933 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002934 struct nfs4_label *label,
2935 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002937 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct nfs4_exception exception = { };
2939 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002940 struct nfs4_open_createattrs c = {
2941 .label = label,
2942 .sattr = sattr,
2943 .verf = {
2944 [0] = (__u32)jiffies,
2945 [1] = (__u32)current->pid,
2946 },
2947 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 int status;
2949
2950 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002951 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002952 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002953 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (status == 0)
2955 break;
2956 /* NOTE: BAD_SEQID means the server and client disagree about the
2957 * book-keeping w.r.t. state-changing operations
2958 * (OPEN/CLOSE/LOCK/LOCKU...)
2959 * It is actually a sign of a bug on the client or on the server.
2960 *
2961 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002962 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 * have unhashed the old state_owner for us, and that we can
2964 * therefore safely retry using a new one. We should still warn
2965 * the user though...
2966 */
2967 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002968 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002969 " returned a bad sequence-id error!\n",
2970 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 exception.retry = 1;
2972 continue;
2973 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002974 /*
2975 * BAD_STATEID on OPEN means that the server cancelled our
2976 * state before it received the OPEN_CONFIRM.
2977 * Recover by retrying the request as per the discussion
2978 * on Page 181 of RFC3530.
2979 */
2980 if (status == -NFS4ERR_BAD_STATEID) {
2981 exception.retry = 1;
2982 continue;
2983 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002984 if (status == -EAGAIN) {
2985 /* We must have found a delegation */
2986 exception.retry = 1;
2987 continue;
2988 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002989 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2990 continue;
2991 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 status, &exception));
2993 } while (exception.retry);
2994 return res;
2995}
2996
Trond Myklebust8487c472016-06-26 08:44:35 -04002997static int _nfs4_do_setattr(struct inode *inode,
2998 struct nfs_setattrargs *arg,
2999 struct nfs_setattrres *res,
3000 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003001 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003002{
3003 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003004 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3006 .rpc_argp = arg,
3007 .rpc_resp = res,
3008 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003009 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003010 struct rpc_cred *delegation_cred = NULL;
3011 unsigned long timestamp = jiffies;
3012 fmode_t fmode;
3013 bool truncate;
3014 int status;
3015
3016 nfs_fattr_init(res->fattr);
3017
3018 /* Servers should only apply open mode checks for file size changes */
3019 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3020 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3021
3022 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3023 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003024 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003025 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003026 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003027 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003028 l_ctx = nfs_get_lock_context(ctx);
3029 if (IS_ERR(l_ctx))
3030 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003031 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3032 &arg->stateid, &delegation_cred);
3033 nfs_put_lock_context(l_ctx);
3034 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003035 return -EBADF;
3036 } else
3037 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3038 if (delegation_cred)
3039 msg.rpc_cred = delegation_cred;
3040
3041 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3042
3043 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003044 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003045 renew_lease(server, timestamp);
3046 trace_nfs4_setattr(inode, &arg->stateid, status);
3047 return status;
3048}
3049
3050static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3051 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003052 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003053 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003055 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003056 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003057 struct nfs_setattrargs arg = {
3058 .fh = NFS_FH(inode),
3059 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 .server = server,
3061 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003062 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003063 };
3064 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003066 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003068 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003069 struct nfs4_exception exception = {
3070 .state = state,
3071 .inode = inode,
3072 .stateid = &arg.stateid,
3073 };
3074 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
David Quigleyaa9c2662013-05-22 12:50:44 -04003076 arg.bitmask = nfs4_bitmask(server, ilabel);
3077 if (ilabel)
3078 arg.bitmask = nfs4_bitmask(server, olabel);
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003081 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003082 switch (err) {
3083 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003084 if (!(sattr->ia_valid & ATTR_SIZE)) {
3085 pr_warn_once("NFSv4: server %s is incorrectly "
3086 "applying open mode checks to "
3087 "a SETATTR that is not "
3088 "changing file size.\n",
3089 server->nfs_client->cl_hostname);
3090 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003091 if (state && !(state->state & FMODE_WRITE)) {
3092 err = -EBADF;
3093 if (sattr->ia_valid & ATTR_OPEN)
3094 err = -EACCES;
3095 goto out;
3096 }
3097 }
3098 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return err;
3102}
3103
Peng Tao500d7012015-09-22 11:35:22 +08003104static bool
3105nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3106{
3107 if (inode == NULL || !nfs_have_layout(inode))
3108 return false;
3109
3110 return pnfs_wait_on_layoutreturn(inode, task);
3111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113struct nfs4_closedata {
3114 struct inode *inode;
3115 struct nfs4_state *state;
3116 struct nfs_closeargs arg;
3117 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003118 struct {
3119 struct nfs4_layoutreturn_args arg;
3120 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003121 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003122 u32 roc_barrier;
3123 bool roc;
3124 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003125 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003126 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127};
3128
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003129static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003130{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131 struct nfs4_closedata *calldata = data;
3132 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003133 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003134
Trond Myklebustcf805162016-11-15 14:56:07 -05003135 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003136 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3137 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003138 nfs4_put_open_state(calldata->state);
3139 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003140 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003141 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003142 kfree(calldata);
3143}
3144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003145static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003150 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Chuck Levera3ca5652012-03-01 17:00:40 -05003152 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003153 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3154 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003155 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003156
3157 /* Handle Layoutreturn errors */
3158 if (calldata->arg.lr_args && task->tk_status != 0) {
3159 switch (calldata->res.lr_ret) {
3160 default:
3161 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3162 break;
3163 case 0:
3164 calldata->arg.lr_args = NULL;
3165 calldata->res.lr_res = NULL;
3166 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003167 case -NFS4ERR_OLD_STATEID:
3168 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003169 calldata->inode))
3170 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003171 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003172 case -NFS4ERR_ADMIN_REVOKED:
3173 case -NFS4ERR_DELEG_REVOKED:
3174 case -NFS4ERR_EXPIRED:
3175 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003176 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3177 case -NFS4ERR_WRONG_CRED:
3178 calldata->arg.lr_args = NULL;
3179 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003180 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003181 }
3182 }
3183
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003184 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 * the state_owner. we keep this around to process errors
3186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 switch (task->tk_status) {
3188 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003189 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003190 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003191 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003192 case -NFS4ERR_ACCESS:
3193 if (calldata->arg.bitmask != NULL) {
3194 calldata->arg.bitmask = NULL;
3195 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003196 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003197
3198 }
3199 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003200 case -NFS4ERR_OLD_STATEID:
3201 /* Did we race with OPEN? */
3202 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003203 state))
3204 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003205 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003206 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003208 case -NFS4ERR_EXPIRED:
3209 nfs4_free_revoked_stateid(server,
3210 &calldata->arg.stateid,
3211 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003212 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003213 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 default:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003216 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3217 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003219 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3220 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003221out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003222 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003223 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003224 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003225 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003226 return;
3227lr_restart:
3228 calldata->res.lr_ret = 0;
3229out_restart:
3230 task->tk_status = 0;
3231 rpc_restart_call_prepare(task);
3232 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003235static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003237 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003238 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003239 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003240 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003241 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003242
Chuck Levera3ca5652012-03-01 17:00:40 -05003243 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003244 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003245 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003246
Trond Myklebust88069f72009-12-08 08:33:16 -05003247 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003248 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003249 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3250 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3251 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003252 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003253 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003254 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003255 if (state->n_rdonly == 0)
3256 call_close |= is_rdonly;
3257 else if (is_rdonly)
3258 calldata->arg.fmode |= FMODE_READ;
3259 if (state->n_wronly == 0)
3260 call_close |= is_wronly;
3261 else if (is_wronly)
3262 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003263 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3264 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003265 } else if (is_rdwr)
3266 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3267
Trond Myklebust5cc78612016-11-14 11:19:56 -05003268 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003269 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003270 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003271 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003272
3273 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003274 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003275 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003276 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003277
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003278 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003279 nfs_release_seqid(calldata->arg.seqid);
3280 goto out_wait;
3281 }
3282
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003283 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003284 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003285
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003286 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003287 /* Close-to-open cache consistency revalidation */
3288 if (!nfs4_have_delegation(inode, FMODE_READ))
3289 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3290 else
3291 calldata->arg.bitmask = NULL;
3292 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003293
Trond Myklebust6ae37332015-01-30 14:21:14 -05003294 calldata->arg.share_access =
3295 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3296 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003297
Trond Myklebustd8d84982016-12-19 12:14:44 -05003298 if (calldata->res.fattr == NULL)
3299 calldata->arg.bitmask = NULL;
3300 else if (calldata->arg.bitmask == NULL)
3301 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003302 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003303 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003304 &calldata->arg.seq_args,
3305 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003306 task) != 0)
3307 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003308 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003309 return;
3310out_no_action:
3311 task->tk_action = NULL;
3312out_wait:
3313 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003316static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003317 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003318 .rpc_call_done = nfs4_close_done,
3319 .rpc_release = nfs4_free_closedata,
3320};
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322/*
3323 * It is possible for data to be read/written from a mem-mapped file
3324 * after the sys_close call (which hits the vfs layer as a flush).
3325 * This means that we can't safely call nfsv4 close on a file until
3326 * the inode is cleared. This in turn means that we are not good
3327 * NFSv4 citizens - we do not indicate to the server to update the file's
3328 * share state even when we are done with one of the three share
3329 * stateid's in the inode.
3330 *
3331 * NOTE: Caller must be holding the sp->so_owner semaphore!
3332 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003333int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003335 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003336 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003338 struct nfs4_state_owner *sp = state->owner;
3339 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003340 struct rpc_message msg = {
3341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3342 .rpc_cred = state->owner->so_cred,
3343 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003344 struct rpc_task_setup task_setup_data = {
3345 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003346 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003347 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003349 .flags = RPC_TASK_ASYNC,
3350 };
Trond Myklebust95121352005-10-18 14:20:12 -07003351 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003353 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3354 &task_setup_data.rpc_client, &msg);
3355
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003356 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003358 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003359 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003360 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003362 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003363 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3364 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003366 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3367 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003368 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003369 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003370 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003371 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003372 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003373 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003374 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003375 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003376 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003377 calldata->lr.roc = pnfs_roc(state->inode,
3378 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3379 if (calldata->lr.roc) {
3380 calldata->arg.lr_args = &calldata->lr.arg;
3381 calldata->res.lr_res = &calldata->lr.res;
3382 }
Al Viro643168c2011-06-22 18:20:23 -04003383 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003384
Trond Myklebust1174dd12010-12-21 10:52:24 -05003385 msg.rpc_argp = &calldata->arg;
3386 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003387 task_setup_data.callback_data = calldata;
3388 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003389 if (IS_ERR(task))
3390 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003391 status = 0;
3392 if (wait)
3393 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003394 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003395 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003396out_free_calldata:
3397 kfree(calldata);
3398out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003399 nfs4_put_open_state(state);
3400 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003401 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Trond Myklebust2b484292010-09-17 10:56:51 -04003404static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003405nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3406 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003409 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3410
3411 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Trond Myklebust565277f2007-10-15 18:17:53 -04003413 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003414 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003415
3416 nfs4_label_release_security(label);
3417
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003418 if (IS_ERR(state))
3419 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003420 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
Trond Myklebust1185a552009-12-03 15:54:02 -05003423static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003424{
3425 if (ctx->state == NULL)
3426 return;
3427 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003428 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003429 else
Al Viro643168c2011-06-22 18:20:23 -04003430 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Trond Myklebustb944dba2013-11-04 15:20:20 -05003433#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3434#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003435#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3438{
Kinglong Mee8c612822015-08-26 21:12:58 +08003439 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003440 struct nfs4_server_caps_arg args = {
3441 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003442 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 struct nfs4_server_caps_res res = {};
3445 struct rpc_message msg = {
3446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003447 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 .rpc_resp = &res,
3449 };
3450 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003451 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Kinglong Mee8c612822015-08-26 21:12:58 +08003453 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3454 FATTR4_WORD0_FH_EXPIRE_TYPE |
3455 FATTR4_WORD0_LINK_SUPPORT |
3456 FATTR4_WORD0_SYMLINK_SUPPORT |
3457 FATTR4_WORD0_ACLSUPPORT;
3458 if (minorversion)
3459 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3460
Bryan Schumaker7c513052011-03-24 17:12:24 +00003461 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003463 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003464 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003465 case 0:
3466 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3467 res.attr_bitmask[2] = 0;
3468 break;
3469 case 1:
3470 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3471 break;
3472 case 2:
3473 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003476 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3477 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3478 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3479 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003480 NFS_CAP_CTIME|NFS_CAP_MTIME|
3481 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003482 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3483 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 server->caps |= NFS_CAP_ACLS;
3485 if (res.has_links != 0)
3486 server->caps |= NFS_CAP_HARDLINKS;
3487 if (res.has_symlinks != 0)
3488 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003489 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3490 server->caps |= NFS_CAP_FILEID;
3491 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3492 server->caps |= NFS_CAP_MODE;
3493 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3494 server->caps |= NFS_CAP_NLINK;
3495 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3496 server->caps |= NFS_CAP_OWNER;
3497 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3498 server->caps |= NFS_CAP_OWNER_GROUP;
3499 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3500 server->caps |= NFS_CAP_ATIME;
3501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3502 server->caps |= NFS_CAP_CTIME;
3503 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3504 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003505#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3506 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3507 server->caps |= NFS_CAP_SECURITY_LABEL;
3508#endif
3509 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3510 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003511 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003512
Trond Myklebusta65318b2009-03-11 14:10:28 -04003513 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3514 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3515 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003516 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003517
3518 /* Avoid a regression due to buggy server */
3519 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3520 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003521 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3522 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003525 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return status;
3529}
3530
Trond Myklebust55a97592006-06-09 09:34:19 -04003531int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 struct nfs4_exception exception = { };
3534 int err;
3535 do {
3536 err = nfs4_handle_exception(server,
3537 _nfs4_server_capabilities(server, fhandle),
3538 &exception);
3539 } while (exception.retry);
3540 return err;
3541}
3542
3543static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3544 struct nfs_fsinfo *info)
3545{
David Quigleyaa9c2662013-05-22 12:50:44 -04003546 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003548 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 };
3550 struct nfs4_lookup_res res = {
3551 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003552 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 .fh = fhandle,
3554 };
3555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3557 .rpc_argp = &args,
3558 .rpc_resp = &res,
3559 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003560
David Quigleyaa9c2662013-05-22 12:50:44 -04003561 bitmask[0] = nfs4_fattr_bitmap[0];
3562 bitmask[1] = nfs4_fattr_bitmap[1];
3563 /*
3564 * Process the label in the upcoming getfattr
3565 */
3566 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3567
Trond Myklebust0e574af2005-10-27 22:12:38 -04003568 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003569 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
3572static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3573 struct nfs_fsinfo *info)
3574{
3575 struct nfs4_exception exception = { };
3576 int err;
3577 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003578 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003579 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003580 switch (err) {
3581 case 0:
3582 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003583 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003584 default:
3585 err = nfs4_handle_exception(server, err, &exception);
3586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003588out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return err;
3590}
3591
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003592static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3593 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3594{
Trond Myklebustc2190662013-08-26 19:23:04 -04003595 struct rpc_auth_create_args auth_args = {
3596 .pseudoflavor = flavor,
3597 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003598 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003599
Trond Myklebustc2190662013-08-26 19:23:04 -04003600 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003601 if (IS_ERR(auth))
3602 return -EACCES;
3603 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003604}
3605
Chuck Lever9a744ba2013-03-16 15:56:02 -04003606/*
3607 * Retry pseudoroot lookup with various security flavors. We do this when:
3608 *
3609 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3610 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3611 *
3612 * Returns zero on success, or a negative NFS4ERR value, or a
3613 * negative errno value.
3614 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003615static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003616 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003618 /* Per 3530bis 15.33.5 */
3619 static const rpc_authflavor_t flav_array[] = {
3620 RPC_AUTH_GSS_KRB5P,
3621 RPC_AUTH_GSS_KRB5I,
3622 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003623 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003624 RPC_AUTH_NULL,
3625 };
3626 int status = -EPERM;
3627 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003629 if (server->auth_info.flavor_len > 0) {
3630 /* try each flavor specified by user */
3631 for (i = 0; i < server->auth_info.flavor_len; i++) {
3632 status = nfs4_lookup_root_sec(server, fhandle, info,
3633 server->auth_info.flavors[i]);
3634 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3635 continue;
3636 break;
3637 }
3638 } else {
3639 /* no flavors specified by user, try default list */
3640 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3641 status = nfs4_lookup_root_sec(server, fhandle, info,
3642 flav_array[i]);
3643 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3644 continue;
3645 break;
3646 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003647 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003648
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003649 /*
3650 * -EACCESS could mean that the user doesn't have correct permissions
3651 * to access the mount. It could also mean that we tried to mount
3652 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3653 * existing mount programs don't handle -EACCES very well so it should
3654 * be mapped to -EPERM instead.
3655 */
3656 if (status == -EACCES)
3657 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003658 return status;
3659}
3660
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003661/**
3662 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3663 * @server: initialized nfs_server handle
3664 * @fhandle: we fill in the pseudo-fs root file handle
3665 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003666 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003667 *
3668 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003669 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003670int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003671 struct nfs_fsinfo *info,
3672 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003673{
Andre Przywarac7757072015-04-23 17:17:40 +01003674 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003675
Andre Przywarac7757072015-04-23 17:17:40 +01003676 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003677 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003678
3679 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003680 status = server->nfs_client->cl_mvops->find_root_sec(server,
3681 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 if (status == 0)
3684 status = nfs4_server_capabilities(server, fhandle);
3685 if (status == 0)
3686 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003687
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003688 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
Bryan Schumakerbae36242012-05-10 15:07:31 -04003691static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3692 struct nfs_fsinfo *info)
3693{
3694 int error;
3695 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003696 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003697
3698 error = nfs4_server_capabilities(server, mntfh);
3699 if (error < 0) {
3700 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3701 return error;
3702 }
3703
David Quigley14c43f72013-05-22 12:50:43 -04003704 label = nfs4_label_alloc(server, GFP_KERNEL);
3705 if (IS_ERR(label))
3706 return PTR_ERR(label);
3707
David Quigley1775fd32013-05-22 12:50:42 -04003708 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003709 if (error < 0) {
3710 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003711 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003712 }
3713
3714 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3715 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3716 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3717
David Quigley14c43f72013-05-22 12:50:43 -04003718err_free_label:
3719 nfs4_label_free(label);
3720
Bryan Schumakerbae36242012-05-10 15:07:31 -04003721 return error;
3722}
3723
Manoj Naik6b97fd32006-06-09 09:34:29 -04003724/*
3725 * Get locations and (maybe) other attributes of a referral.
3726 * Note that we'll actually follow the referral later when
3727 * we detect fsid mismatch in inode revalidation
3728 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003729static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3730 const struct qstr *name, struct nfs_fattr *fattr,
3731 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003732{
3733 int status = -ENOMEM;
3734 struct page *page = NULL;
3735 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003736
3737 page = alloc_page(GFP_KERNEL);
3738 if (page == NULL)
3739 goto out;
3740 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3741 if (locations == NULL)
3742 goto out;
3743
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003744 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003745 if (status != 0)
3746 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003747
3748 /*
3749 * If the fsid didn't change, this is a migration event, not a
3750 * referral. Cause us to drop into the exception handler, which
3751 * will kick off migration recovery.
3752 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003753 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003754 dprintk("%s: server did not return a different fsid for"
3755 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003756 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003757 goto out;
3758 }
Andy Adamson533eb462011-06-13 18:25:56 -04003759 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3760 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003761
Andy Adamson533eb462011-06-13 18:25:56 -04003762 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003763 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764 memset(fhandle, 0, sizeof(struct nfs_fh));
3765out:
3766 if (page)
3767 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003768 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003769 return status;
3770}
3771
David Quigley1775fd32013-05-22 12:50:42 -04003772static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3773 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 struct nfs4_getattr_arg args = {
3776 .fh = fhandle,
3777 .bitmask = server->attr_bitmask,
3778 };
3779 struct nfs4_getattr_res res = {
3780 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003781 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 .server = server,
3783 };
3784 struct rpc_message msg = {
3785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3786 .rpc_argp = &args,
3787 .rpc_resp = &res,
3788 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003789
3790 args.bitmask = nfs4_bitmask(server, label);
3791
Trond Myklebust0e574af2005-10-27 22:12:38 -04003792 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003793 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
David Quigley1775fd32013-05-22 12:50:42 -04003796static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3797 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 struct nfs4_exception exception = { };
3800 int err;
3801 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003802 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3803 trace_nfs4_getattr(server, fhandle, fattr, err);
3804 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 &exception);
3806 } while (exception.retry);
3807 return err;
3808}
3809
3810/*
3811 * The file is not closed if it is opened due to the a request to change
3812 * the size of the file. The open call will not be needed once the
3813 * VFS layer lookup-intents are implemented.
3814 *
3815 * Close is called when the inode is destroyed.
3816 * If we haven't opened the file for O_WRONLY, we
3817 * need to in the size_change case to obtain a stateid.
3818 *
3819 * Got race?
3820 * Because OPEN is always done by name in nfsv4, it is
3821 * possible that we opened a different file by the same
3822 * name. We can recognize this race condition, but we
3823 * can't do anything about it besides returning an error.
3824 *
3825 * This will be fixed with VFS changes (lookup-intent).
3826 */
3827static int
3828nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3829 struct iattr *sattr)
3830{
David Howells2b0143b2015-03-17 22:25:59 +00003831 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003832 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003833 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003834 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 int status;
3836
Peng Tao88ac8152014-09-12 11:04:10 +08003837 if (pnfs_ld_layoutret_on_setattr(inode) &&
3838 sattr->ia_valid & ATTR_SIZE &&
3839 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003840 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003841
Trond Myklebust0e574af2005-10-27 22:12:38 -04003842 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Andy Adamson26699402012-05-30 16:12:24 -04003844 /* Deal with open(O_TRUNC) */
3845 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003846 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003847
3848 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003849 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003850 return 0;
3851
Trond Myklebustd5308382005-11-04 15:33:38 -05003852 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003853 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003854
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003855 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003856 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003857 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003858 }
3859
David Quigley14c43f72013-05-22 12:50:43 -04003860 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3861 if (IS_ERR(label))
3862 return PTR_ERR(label);
3863
NeilBrown29b59f92016-10-13 15:26:47 +11003864 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003865 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003866 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003867 nfs_setsecurity(inode, fattr, label);
3868 }
David Quigley14c43f72013-05-22 12:50:43 -04003869 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return status;
3871}
3872
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003873static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3874 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003875 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003876{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003877 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003878 int status;
3879 struct nfs4_lookup_arg args = {
3880 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003881 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003882 .name = name,
3883 };
3884 struct nfs4_lookup_res res = {
3885 .server = server,
3886 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003888 .fh = fhandle,
3889 };
3890 struct rpc_message msg = {
3891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3892 .rpc_argp = &args,
3893 .rpc_resp = &res,
3894 };
3895
David Quigleyaa9c2662013-05-22 12:50:44 -04003896 args.bitmask = nfs4_bitmask(server, label);
3897
David Howells2b3de442006-08-22 20:06:09 -04003898 nfs_fattr_init(fattr);
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003901 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 dprintk("NFS reply lookup: %d\n", status);
3903 return status;
3904}
3905
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003906static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003907{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003908 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003909 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003910 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3911 fattr->nlink = 2;
3912}
3913
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003914static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003915 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003916 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003917{
3918 struct nfs4_exception exception = { };
3919 struct rpc_clnt *client = *clnt;
3920 int err;
3921 do {
David Quigley1775fd32013-05-22 12:50:42 -04003922 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003923 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003924 switch (err) {
3925 case -NFS4ERR_BADNAME:
3926 err = -ENOENT;
3927 goto out;
3928 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003929 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003930 if (err == -NFS4ERR_MOVED)
3931 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003932 goto out;
3933 case -NFS4ERR_WRONGSEC:
3934 err = -EPERM;
3935 if (client != *clnt)
3936 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003937 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003938 if (IS_ERR(client))
3939 return PTR_ERR(client);
3940
3941 exception.retry = 1;
3942 break;
3943 default:
3944 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3945 }
3946 } while (exception.retry);
3947
3948out:
3949 if (err == 0)
3950 *clnt = client;
3951 else if (client != *clnt)
3952 rpc_shutdown_client(client);
3953
3954 return err;
3955}
3956
Al Virobeffb8f2016-07-20 16:34:42 -04003957static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003958 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3959 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003961 int status;
3962 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003963
David Quigley1775fd32013-05-22 12:50:42 -04003964 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003965 if (client != NFS_CLIENT(dir)) {
3966 rpc_shutdown_client(client);
3967 nfs_fixup_secinfo_attributes(fattr);
3968 }
3969 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003972struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003973nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003974 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3975{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003976 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003977 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003978
David Quigley1775fd32013-05-22 12:50:42 -04003979 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003980 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003981 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003982 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003983}
3984
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003985static int _nfs4_proc_lookupp(struct inode *inode,
3986 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3987 struct nfs4_label *label)
3988{
3989 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3990 struct nfs_server *server = NFS_SERVER(inode);
3991 int status;
3992 struct nfs4_lookupp_arg args = {
3993 .bitmask = server->attr_bitmask,
3994 .fh = NFS_FH(inode),
3995 };
3996 struct nfs4_lookupp_res res = {
3997 .server = server,
3998 .fattr = fattr,
3999 .label = label,
4000 .fh = fhandle,
4001 };
4002 struct rpc_message msg = {
4003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4004 .rpc_argp = &args,
4005 .rpc_resp = &res,
4006 };
4007
4008 args.bitmask = nfs4_bitmask(server, label);
4009
4010 nfs_fattr_init(fattr);
4011
4012 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4013 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4014 &res.seq_res, 0);
4015 dprintk("NFS reply lookupp: %d\n", status);
4016 return status;
4017}
4018
4019static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4020 struct nfs_fattr *fattr, struct nfs4_label *label)
4021{
4022 struct nfs4_exception exception = { };
4023 int err;
4024 do {
4025 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4026 trace_nfs4_lookupp(inode, err);
4027 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4028 &exception);
4029 } while (exception.retry);
4030 return err;
4031}
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4034{
Trond Myklebust76b32992007-08-10 17:45:11 -04004035 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 struct nfs4_accessargs args = {
4037 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004038 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004039 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004041 struct nfs4_accessres res = {
4042 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4046 .rpc_argp = &args,
4047 .rpc_resp = &res,
4048 .rpc_cred = entry->cred,
4049 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004050 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Trond Myklebustc407d412010-04-16 16:22:48 -04004052 res.fattr = nfs_alloc_fattr();
4053 if (res.fattr == NULL)
4054 return -ENOMEM;
4055
Bryan Schumaker7c513052011-03-24 17:12:24 +00004056 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004058 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004059 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004061 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return status;
4063}
4064
4065static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4066{
4067 struct nfs4_exception exception = { };
4068 int err;
4069 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004070 err = _nfs4_proc_access(inode, entry);
4071 trace_nfs4_access(inode, err);
4072 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 &exception);
4074 } while (exception.retry);
4075 return err;
4076}
4077
4078/*
4079 * TODO: For the time being, we don't try to get any attributes
4080 * along with any of the zero-copy operations READ, READDIR,
4081 * READLINK, WRITE.
4082 *
4083 * In the case of the first three, we want to put the GETATTR
4084 * after the read-type operation -- this is because it is hard
4085 * to predict the length of a GETATTR response in v4, and thus
4086 * align the READ data correctly. This means that the GETATTR
4087 * may end up partially falling into the page cache, and we should
4088 * shift it into the 'tail' of the xdr_buf before processing.
4089 * To do this efficiently, we need to know the total length
4090 * of data received, which doesn't seem to be available outside
4091 * of the RPC layer.
4092 *
4093 * In the case of WRITE, we also want to put the GETATTR after
4094 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004095 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 *
4097 * Both of these changes to the XDR layer would in fact be quite
4098 * minor, but I decided to leave them for a subsequent patch.
4099 */
4100static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4101 unsigned int pgbase, unsigned int pglen)
4102{
4103 struct nfs4_readlink args = {
4104 .fh = NFS_FH(inode),
4105 .pgbase = pgbase,
4106 .pglen = pglen,
4107 .pages = &page,
4108 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004109 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 struct rpc_message msg = {
4111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4112 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004113 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 };
4115
Bryan Schumaker7c513052011-03-24 17:12:24 +00004116 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
4119static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4120 unsigned int pgbase, unsigned int pglen)
4121{
4122 struct nfs4_exception exception = { };
4123 int err;
4124 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004125 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4126 trace_nfs4_readlink(inode, err);
4127 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 &exception);
4129 } while (exception.retry);
4130 return err;
4131}
4132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004134 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136static int
4137nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004138 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004140 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004141 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004142 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 int status = 0;
4145
NeilBrown532d4de2016-10-13 15:26:47 +11004146 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004147 if (IS_ERR(ctx))
4148 return PTR_ERR(ctx);
4149
David Quigleyaa9c2662013-05-22 12:50:44 -04004150 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4151
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004152 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4153 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004154 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (IS_ERR(state)) {
4156 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004160 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004161 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return status;
4163}
4164
Al Virobeffb8f2016-07-20 16:34:42 -04004165static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Trond Myklebust16e42952005-10-27 22:12:44 -04004167 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004168 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004170 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004172 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004173 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004174 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4177 .rpc_argp = &args,
4178 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004180 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004181 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004182
Bryan Schumaker7c513052011-03-24 17:12:24 +00004183 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004184 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004185 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return status;
4187}
4188
Al Virobeffb8f2016-07-20 16:34:42 -04004189static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
4191 struct nfs4_exception exception = { };
4192 int err;
4193 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004194 err = _nfs4_proc_remove(dir, name);
4195 trace_nfs4_remove(dir, name, err);
4196 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 &exception);
4198 } while (exception.retry);
4199 return err;
4200}
4201
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004202static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004204 struct nfs_server *server = NFS_SERVER(dir);
4205 struct nfs_removeargs *args = msg->rpc_argp;
4206 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004208 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004210 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004211
4212 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004215static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4216{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004217 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004218 &data->args.seq_args,
4219 &data->res.seq_res,
4220 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004223static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004225 struct nfs_unlinkdata *data = task->tk_calldata;
4226 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004227
Trond Myklebust14516c32010-07-31 14:29:06 -04004228 if (!nfs4_sequence_done(task, &res->seq_res))
4229 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004230 if (nfs4_async_handle_error(task, res->server, NULL,
4231 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004232 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004233 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004234 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004235 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236}
4237
Jeff Laytond3d41522010-09-17 17:31:57 -04004238static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4239{
4240 struct nfs_server *server = NFS_SERVER(dir);
4241 struct nfs_renameargs *arg = msg->rpc_argp;
4242 struct nfs_renameres *res = msg->rpc_resp;
4243
4244 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004245 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004246 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004247}
4248
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004249static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4250{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004251 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004252 &data->args.seq_args,
4253 &data->res.seq_res,
4254 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004255}
4256
4257static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4258 struct inode *new_dir)
4259{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004260 struct nfs_renamedata *data = task->tk_calldata;
4261 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004262
4263 if (!nfs4_sequence_done(task, &res->seq_res))
4264 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004265 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004266 return 0;
4267
Trond Myklebustc733c492017-01-11 12:32:26 -05004268 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004269 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004270 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004271 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004272 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004273 return 1;
4274}
4275
Al Virobeffb8f2016-07-20 16:34:42 -04004276static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004278 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 struct nfs4_link_arg arg = {
4280 .fh = NFS_FH(inode),
4281 .dir_fh = NFS_FH(dir),
4282 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004283 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004285 struct nfs4_link_res res = {
4286 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004287 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004288 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4291 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004292 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004294 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Trond Myklebust136f2622010-04-16 16:22:49 -04004296 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004297 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004298 goto out;
4299
David Quigley14c43f72013-05-22 12:50:43 -04004300 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4301 if (IS_ERR(res.label)) {
4302 status = PTR_ERR(res.label);
4303 goto out;
4304 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004305 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004306
Bryan Schumaker7c513052011-03-24 17:12:24 +00004307 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004308 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004309 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004310 status = nfs_post_op_update_inode(inode, res.fattr);
4311 if (!status)
4312 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004313 }
David Quigley14c43f72013-05-22 12:50:43 -04004314
4315
4316 nfs4_label_free(res.label);
4317
Trond Myklebust136f2622010-04-16 16:22:49 -04004318out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004319 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return status;
4321}
4322
Al Virobeffb8f2016-07-20 16:34:42 -04004323static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
4325 struct nfs4_exception exception = { };
4326 int err;
4327 do {
4328 err = nfs4_handle_exception(NFS_SERVER(inode),
4329 _nfs4_proc_link(inode, dir, name),
4330 &exception);
4331 } while (exception.retry);
4332 return err;
4333}
4334
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004335struct nfs4_createdata {
4336 struct rpc_message msg;
4337 struct nfs4_create_arg arg;
4338 struct nfs4_create_res res;
4339 struct nfs_fh fh;
4340 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004341 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004342};
4343
4344static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004345 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004346{
4347 struct nfs4_createdata *data;
4348
4349 data = kzalloc(sizeof(*data), GFP_KERNEL);
4350 if (data != NULL) {
4351 struct nfs_server *server = NFS_SERVER(dir);
4352
David Quigley14c43f72013-05-22 12:50:43 -04004353 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4354 if (IS_ERR(data->label))
4355 goto out_free;
4356
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004357 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4358 data->msg.rpc_argp = &data->arg;
4359 data->msg.rpc_resp = &data->res;
4360 data->arg.dir_fh = NFS_FH(dir);
4361 data->arg.server = server;
4362 data->arg.name = name;
4363 data->arg.attrs = sattr;
4364 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004365 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004366 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004367 data->res.server = server;
4368 data->res.fh = &data->fh;
4369 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004370 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004371 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004372 }
4373 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004374out_free:
4375 kfree(data);
4376 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004377}
4378
4379static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4380{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004381 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004382 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004383 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004384 update_changeattr(dir, &data->res.dir_cinfo,
4385 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004386 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004387 }
4388 return status;
4389}
4390
4391static void nfs4_free_createdata(struct nfs4_createdata *data)
4392{
David Quigley14c43f72013-05-22 12:50:43 -04004393 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004394 kfree(data);
4395}
4396
Chuck Lever4f390c12006-08-22 20:06:22 -04004397static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004398 struct page *page, unsigned int len, struct iattr *sattr,
4399 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 struct nfs4_createdata *data;
4402 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Chuck Lever94a6d752006-08-22 20:06:23 -04004404 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004405 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004406
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 status = -ENOMEM;
4408 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4409 if (data == NULL)
4410 goto out;
4411
4412 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4413 data->arg.u.symlink.pages = &page;
4414 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004415 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004417 status = nfs4_do_create(dir, dentry, data);
4418
4419 nfs4_free_createdata(data);
4420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 return status;
4422}
4423
Chuck Lever4f390c12006-08-22 20:06:22 -04004424static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004425 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
4427 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004428 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004430
4431 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004434 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4435 trace_nfs4_symlink(dir, &dentry->d_name, err);
4436 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 &exception);
4438 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004439
4440 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 return err;
4442}
4443
4444static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004445 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004447 struct nfs4_createdata *data;
4448 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4451 if (data == NULL)
4452 goto out;
4453
David Quigley1775fd32013-05-22 12:50:42 -04004454 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004455 status = nfs4_do_create(dir, dentry, data);
4456
4457 nfs4_free_createdata(data);
4458out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 return status;
4460}
4461
4462static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4463 struct iattr *sattr)
4464{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004465 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004467 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004469
David Quigleyaa9c2662013-05-22 12:50:44 -04004470 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4471
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004472 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4473 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004475 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4476 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4477 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 &exception);
4479 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004480 nfs4_label_release_security(label);
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 return err;
4483}
4484
4485static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004486 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
David Howells2b0143b2015-03-17 22:25:59 +00004488 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct nfs4_readdir_arg args = {
4490 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004491 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 .pgbase = 0,
4493 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004494 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004495 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 };
4497 struct nfs4_readdir_res res;
4498 struct rpc_message msg = {
4499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4500 .rpc_argp = &args,
4501 .rpc_resp = &res,
4502 .rpc_cred = cred,
4503 };
4504 int status;
4505
Al Viro6de14722013-09-16 10:53:17 -04004506 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4507 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004508 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004509 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004511 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004512 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004513 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004514 status += args.pgbase;
4515 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004516
4517 nfs_invalidate_atime(dir);
4518
Harvey Harrison3110ff82008-05-02 13:42:44 -07004519 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return status;
4521}
4522
4523static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004524 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
4526 struct nfs4_exception exception = { };
4527 int err;
4528 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004529 err = _nfs4_proc_readdir(dentry, cred, cookie,
4530 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004531 trace_nfs4_readdir(d_inode(dentry), err);
4532 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 &exception);
4534 } while (exception.retry);
4535 return err;
4536}
4537
4538static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004539 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004541 struct nfs4_createdata *data;
4542 int mode = sattr->ia_mode;
4543 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004545 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4546 if (data == NULL)
4547 goto out;
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004550 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data->arg.ftype = NF4BLK;
4553 data->arg.u.device.specdata1 = MAJOR(rdev);
4554 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004557 data->arg.ftype = NF4CHR;
4558 data->arg.u.device.specdata1 = MAJOR(rdev);
4559 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004560 } else if (!S_ISSOCK(mode)) {
4561 status = -EINVAL;
4562 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
David Quigley1775fd32013-05-22 12:50:42 -04004564
David Quigleyaa9c2662013-05-22 12:50:44 -04004565 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004566 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004567out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004568 nfs4_free_createdata(data);
4569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return status;
4571}
4572
4573static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4574 struct iattr *sattr, dev_t rdev)
4575{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004576 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004578 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004580
David Quigleyaa9c2662013-05-22 12:50:44 -04004581 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4582
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004583 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4584 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004586 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4587 trace_nfs4_mknod(dir, &dentry->d_name, err);
4588 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 &exception);
4590 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004591
4592 nfs4_label_release_security(label);
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return err;
4595}
4596
4597static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4598 struct nfs_fsstat *fsstat)
4599{
4600 struct nfs4_statfs_arg args = {
4601 .fh = fhandle,
4602 .bitmask = server->attr_bitmask,
4603 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004604 struct nfs4_statfs_res res = {
4605 .fsstat = fsstat,
4606 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 struct rpc_message msg = {
4608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4609 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004610 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 };
4612
Trond Myklebust0e574af2005-10-27 22:12:38 -04004613 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004614 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
4617static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4618{
4619 struct nfs4_exception exception = { };
4620 int err;
4621 do {
4622 err = nfs4_handle_exception(server,
4623 _nfs4_proc_statfs(server, fhandle, fsstat),
4624 &exception);
4625 } while (exception.retry);
4626 return err;
4627}
4628
4629static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4630 struct nfs_fsinfo *fsinfo)
4631{
4632 struct nfs4_fsinfo_arg args = {
4633 .fh = fhandle,
4634 .bitmask = server->attr_bitmask,
4635 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004636 struct nfs4_fsinfo_res res = {
4637 .fsinfo = fsinfo,
4638 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4641 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004642 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 };
4644
Bryan Schumaker7c513052011-03-24 17:12:24 +00004645 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
4647
4648static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4649{
4650 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004651 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 int err;
4653
4654 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004655 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004656 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004657 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004658 nfs4_set_lease_period(server->nfs_client,
4659 fsinfo->lease_time * HZ,
4660 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004661 break;
4662 }
4663 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 } while (exception.retry);
4665 return err;
4666}
4667
4668static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4669{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004670 int error;
4671
Trond Myklebust0e574af2005-10-27 22:12:38 -04004672 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004673 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004674 if (error == 0) {
4675 /* block layout checks this! */
4676 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004677 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004678 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004679
4680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
4683static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4684 struct nfs_pathconf *pathconf)
4685{
4686 struct nfs4_pathconf_arg args = {
4687 .fh = fhandle,
4688 .bitmask = server->attr_bitmask,
4689 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004690 struct nfs4_pathconf_res res = {
4691 .pathconf = pathconf,
4692 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4695 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004696 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 };
4698
4699 /* None of the pathconf attributes are mandatory to implement */
4700 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4701 memset(pathconf, 0, sizeof(*pathconf));
4702 return 0;
4703 }
4704
Trond Myklebust0e574af2005-10-27 22:12:38 -04004705 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004706 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
4709static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4710 struct nfs_pathconf *pathconf)
4711{
4712 struct nfs4_exception exception = { };
4713 int err;
4714
4715 do {
4716 err = nfs4_handle_exception(server,
4717 _nfs4_proc_pathconf(server, fhandle, pathconf),
4718 &exception);
4719 } while (exception.retry);
4720 return err;
4721}
4722
Trond Myklebust5521abf2013-03-16 20:54:34 -04004723int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004724 const struct nfs_open_context *ctx,
4725 const struct nfs_lock_context *l_ctx,
4726 fmode_t fmode)
4727{
NeilBrown17393472016-10-13 15:26:47 +11004728 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004729}
4730EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4731
Trond Myklebust5521abf2013-03-16 20:54:34 -04004732static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4733 const struct nfs_open_context *ctx,
4734 const struct nfs_lock_context *l_ctx,
4735 fmode_t fmode)
4736{
4737 nfs4_stateid current_stateid;
4738
Trond Myklebuste1253be2014-03-05 08:44:23 -05004739 /* If the current stateid represents a lost lock, then exit */
4740 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4741 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004742 return nfs4_stateid_match(stateid, &current_stateid);
4743}
4744
4745static bool nfs4_error_stateid_expired(int err)
4746{
4747 switch (err) {
4748 case -NFS4ERR_DELEG_REVOKED:
4749 case -NFS4ERR_ADMIN_REVOKED:
4750 case -NFS4ERR_BAD_STATEID:
4751 case -NFS4ERR_STALE_STATEID:
4752 case -NFS4ERR_OLD_STATEID:
4753 case -NFS4ERR_OPENMODE:
4754 case -NFS4ERR_EXPIRED:
4755 return true;
4756 }
4757 return false;
4758}
4759
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004760static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004765 if (task->tk_status < 0) {
4766 struct nfs4_exception exception = {
4767 .inode = hdr->inode,
4768 .state = hdr->args.context->state,
4769 .stateid = &hdr->args.stateid,
4770 };
4771 task->tk_status = nfs4_async_handle_exception(task,
4772 server, task->tk_status, &exception);
4773 if (exception.retry) {
4774 rpc_restart_call_prepare(task);
4775 return -EAGAIN;
4776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
Trond Myklebust5521abf2013-03-16 20:54:34 -04004784static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004785 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786{
4787
4788 if (!nfs4_error_stateid_expired(task->tk_status) ||
4789 nfs4_stateid_is_current(&args->stateid,
4790 args->context,
4791 args->lock_context,
4792 FMODE_READ))
4793 return false;
4794 rpc_restart_call_prepare(task);
4795 return true;
4796}
4797
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004798static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004799{
4800
4801 dprintk("--> %s\n", __func__);
4802
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004803 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004804 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004806 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004807 if (task->tk_status > 0)
4808 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004809 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4810 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004811}
4812
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004813static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4814 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004817 if (!hdr->pgio_done_cb)
4818 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004819 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004820 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004823static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4824 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004825{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004826 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004827 &hdr->args.seq_args,
4828 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004829 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004830 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004831 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4832 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004833 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004834 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004835 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004836 return -EIO;
4837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838}
4839
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004840static int nfs4_write_done_cb(struct rpc_task *task,
4841 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004843 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004844
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004845 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004846 if (task->tk_status < 0) {
4847 struct nfs4_exception exception = {
4848 .inode = hdr->inode,
4849 .state = hdr->args.context->state,
4850 .stateid = &hdr->args.stateid,
4851 };
4852 task->tk_status = nfs4_async_handle_exception(task,
4853 NFS_SERVER(inode), task->tk_status,
4854 &exception);
4855 if (exception.retry) {
4856 rpc_restart_call_prepare(task);
4857 return -EAGAIN;
4858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004860 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004861 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004862 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004863 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Trond Myklebust5521abf2013-03-16 20:54:34 -04004867static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004868 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004869{
4870
4871 if (!nfs4_error_stateid_expired(task->tk_status) ||
4872 nfs4_stateid_is_current(&args->stateid,
4873 args->context,
4874 args->lock_context,
4875 FMODE_WRITE))
4876 return false;
4877 rpc_restart_call_prepare(task);
4878 return true;
4879}
4880
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004881static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004882{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004884 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004885 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004886 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4888 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004889}
4890
Trond Myklebust5a37f852012-04-28 14:55:16 -04004891static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004892bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004893{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004894 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004895 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004896 return false;
4897 /* Otherwise, request attributes if and only if we don't hold
4898 * a delegation
4899 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004900 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004901}
Fred Isamana69aef12011-03-03 15:13:47 +00004902
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004903static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4904 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004907
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4909 hdr->args.bitmask = NULL;
4910 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004911 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004912 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004913
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004914 if (!hdr->pgio_done_cb)
4915 hdr->pgio_done_cb = nfs4_write_done_cb;
4916 hdr->res.server = server;
4917 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004919 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004920 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
Fred Isaman0b7c0152012-04-20 14:47:39 -04004923static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4924{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004925 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004926 &data->args.seq_args,
4927 &data->res.seq_res,
4928 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
Fred Isaman0b7c0152012-04-20 14:47:39 -04004931static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004934
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004935 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004936 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4937 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004938 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004939 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
Fred Isaman0b7c0152012-04-20 14:47:39 -04004944static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004945{
4946 if (!nfs4_sequence_done(task, &data->res.seq_res))
4947 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004948 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004949}
4950
Fred Isaman0b7c0152012-04-20 14:47:39 -04004951static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004953 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004954
Fred Isaman0b7c0152012-04-20 14:47:39 -04004955 if (data->commit_done_cb == NULL)
4956 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004957 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004958 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004959 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004962struct nfs4_renewdata {
4963 struct nfs_client *client;
4964 unsigned long timestamp;
4965};
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967/*
4968 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4969 * standalone procedure for queueing an asynchronous RENEW.
4970 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004971static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004972{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004973 struct nfs4_renewdata *data = calldata;
4974 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004975
Elena Reshetova212bf412017-10-20 12:53:38 +03004976 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004977 nfs4_schedule_state_renewal(clp);
4978 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004979 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004980}
4981
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004982static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004984 struct nfs4_renewdata *data = calldata;
4985 struct nfs_client *clp = data->client;
4986 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004988 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004989 switch (task->tk_status) {
4990 case 0:
4991 break;
4992 case -NFS4ERR_LEASE_MOVED:
4993 nfs4_schedule_lease_moved_recovery(clp);
4994 break;
4995 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004996 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004997 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4998 return;
4999 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005000 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005001 return;
5002 }
5003 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005005 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006}
5007
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005008static const struct rpc_call_ops nfs4_renew_ops = {
5009 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005010 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005011};
5012
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005013static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014{
5015 struct rpc_message msg = {
5016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5017 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005018 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005020 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005022 if (renew_flags == 0)
5023 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005024 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005025 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005026 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005027 if (data == NULL) {
5028 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005029 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005030 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005031 data->client = clp;
5032 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005033 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005034 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005037static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038{
5039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5041 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005042 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 };
5044 unsigned long now = jiffies;
5045 int status;
5046
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005047 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 if (status < 0)
5049 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005050 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 return 0;
5052}
5053
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054static inline int nfs4_server_supports_acls(struct nfs_server *server)
5055{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005056 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005057}
5058
Trond Myklebust21f498c2012-08-24 10:59:25 -04005059/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5060 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005061 * the stack.
5062 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005063#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005064
Neil Hormane9e3d722011-03-04 19:26:03 -05005065static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005066 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005067{
5068 struct page *newpage, **spages;
5069 int rc = 0;
5070 size_t len;
5071 spages = pages;
5072
5073 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005074 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005075 newpage = alloc_page(GFP_KERNEL);
5076
5077 if (newpage == NULL)
5078 goto unwind;
5079 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005080 buf += len;
5081 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005082 *pages++ = newpage;
5083 rc++;
5084 } while (buflen != 0);
5085
5086 return rc;
5087
5088unwind:
5089 for(; rc > 0; rc--)
5090 __free_page(spages[rc-1]);
5091 return -ENOMEM;
5092}
5093
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094struct nfs4_cached_acl {
5095 int cached;
5096 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005097 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005098};
5099
5100static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005101{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102 struct nfs_inode *nfsi = NFS_I(inode);
5103
5104 spin_lock(&inode->i_lock);
5105 kfree(nfsi->nfs4_acl);
5106 nfsi->nfs4_acl = acl;
5107 spin_unlock(&inode->i_lock);
5108}
5109
5110static void nfs4_zap_acl_attr(struct inode *inode)
5111{
5112 nfs4_set_cached_acl(inode, NULL);
5113}
5114
5115static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5116{
5117 struct nfs_inode *nfsi = NFS_I(inode);
5118 struct nfs4_cached_acl *acl;
5119 int ret = -ENOENT;
5120
5121 spin_lock(&inode->i_lock);
5122 acl = nfsi->nfs4_acl;
5123 if (acl == NULL)
5124 goto out;
5125 if (buf == NULL) /* user is just asking for length */
5126 goto out_len;
5127 if (acl->cached == 0)
5128 goto out;
5129 ret = -ERANGE; /* see getxattr(2) man page */
5130 if (acl->len > buflen)
5131 goto out;
5132 memcpy(buf, acl->data, acl->len);
5133out_len:
5134 ret = acl->len;
5135out:
5136 spin_unlock(&inode->i_lock);
5137 return ret;
5138}
5139
Sachin Prabhu5794d212012-04-17 14:36:40 +01005140static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005141{
5142 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005143 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005144
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005145 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005146 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005147 if (acl == NULL)
5148 goto out;
5149 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005150 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005151 } else {
5152 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5153 if (acl == NULL)
5154 goto out;
5155 acl->cached = 0;
5156 }
5157 acl->len = acl_len;
5158out:
5159 nfs4_set_cached_acl(inode, acl);
5160}
5161
Andy Adamsonbf118a32011-12-07 11:55:27 -05005162/*
5163 * The getxattr API returns the required buffer length when called with a
5164 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5165 * the required buf. On a NULL buf, we send a page of data to the server
5166 * guessing that the ACL request can be serviced by a page. If so, we cache
5167 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5168 * the cache. If not so, we throw away the page, and cache the required
5169 * length. The next getxattr call will then produce another round trip to
5170 * the server, this time with the input buf of the required size.
5171 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005172static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005173{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005174 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005175 struct nfs_getaclargs args = {
5176 .fh = NFS_FH(inode),
5177 .acl_pages = pages,
5178 .acl_len = buflen,
5179 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005180 struct nfs_getaclres res = {
5181 .acl_len = buflen,
5182 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005183 struct rpc_message msg = {
5184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5185 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005186 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005187 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005188 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005189 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005190
Trond Myklebust21f498c2012-08-24 10:59:25 -04005191 if (npages > ARRAY_SIZE(pages))
5192 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005193
Andy Adamsonbf118a32011-12-07 11:55:27 -05005194 for (i = 0; i < npages; i++) {
5195 pages[i] = alloc_page(GFP_KERNEL);
5196 if (!pages[i])
5197 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005198 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005199
5200 /* for decoding across pages */
5201 res.acl_scratch = alloc_page(GFP_KERNEL);
5202 if (!res.acl_scratch)
5203 goto out_free;
5204
Andy Adamsonbf118a32011-12-07 11:55:27 -05005205 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005206
Peng Taode040be2012-01-10 22:42:47 +08005207 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005208 __func__, buf, buflen, npages, args.acl_len);
5209 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5210 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005211 if (ret)
5212 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005213
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005214 /* Handle the case where the passed-in buffer is too short */
5215 if (res.acl_flags & NFS4_ACL_TRUNC) {
5216 /* Did the user only issue a request for the acl length? */
5217 if (buf == NULL)
5218 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005219 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005220 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005222 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005223 if (buf) {
5224 if (res.acl_len > buflen) {
5225 ret = -ERANGE;
5226 goto out_free;
5227 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005228 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005229 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005230out_ok:
5231 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005232out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005233 for (i = 0; i < npages; i++)
5234 if (pages[i])
5235 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005236 if (res.acl_scratch)
5237 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005238 return ret;
5239}
5240
Trond Myklebust16b42892006-08-24 12:27:15 -04005241static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5242{
5243 struct nfs4_exception exception = { };
5244 ssize_t ret;
5245 do {
5246 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005247 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005248 if (ret >= 0)
5249 break;
5250 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5251 } while (exception.retry);
5252 return ret;
5253}
5254
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005255static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5256{
5257 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005258 int ret;
5259
5260 if (!nfs4_server_supports_acls(server))
5261 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005262 ret = nfs_revalidate_inode(server, inode);
5263 if (ret < 0)
5264 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005265 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5266 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005267 ret = nfs4_read_cached_acl(inode, buf, buflen);
5268 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005269 /* -ENOENT is returned if there is no ACL or if there is an ACL
5270 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005271 return ret;
5272 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005273}
5274
Trond Myklebust16b42892006-08-24 12:27:15 -04005275static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005276{
5277 struct nfs_server *server = NFS_SERVER(inode);
5278 struct page *pages[NFS4ACL_MAXPAGES];
5279 struct nfs_setaclargs arg = {
5280 .fh = NFS_FH(inode),
5281 .acl_pages = pages,
5282 .acl_len = buflen,
5283 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005284 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005285 struct rpc_message msg = {
5286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5287 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005288 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005289 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005290 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005291 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005292
5293 if (!nfs4_server_supports_acls(server))
5294 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005295 if (npages > ARRAY_SIZE(pages))
5296 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005297 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005298 if (i < 0)
5299 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005300 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005301 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005302
5303 /*
5304 * Free each page after tx, so the only ref left is
5305 * held by the network stack
5306 */
5307 for (; i > 0; i--)
5308 put_page(pages[i-1]);
5309
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005310 /*
5311 * Acl update can result in inode attribute update.
5312 * so mark the attribute cache invalid.
5313 */
5314 spin_lock(&inode->i_lock);
5315 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5316 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005317 nfs_access_zap_cache(inode);
5318 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005319 return ret;
5320}
5321
Trond Myklebust16b42892006-08-24 12:27:15 -04005322static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5323{
5324 struct nfs4_exception exception = { };
5325 int err;
5326 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005327 err = __nfs4_proc_set_acl(inode, buf, buflen);
5328 trace_nfs4_set_acl(inode, err);
5329 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005330 &exception);
5331 } while (exception.retry);
5332 return err;
5333}
5334
David Quigleyaa9c2662013-05-22 12:50:44 -04005335#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5336static int _nfs4_get_security_label(struct inode *inode, void *buf,
5337 size_t buflen)
5338{
5339 struct nfs_server *server = NFS_SERVER(inode);
5340 struct nfs_fattr fattr;
5341 struct nfs4_label label = {0, 0, buflen, buf};
5342
5343 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005344 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005345 .fh = NFS_FH(inode),
5346 .bitmask = bitmask,
5347 };
5348 struct nfs4_getattr_res res = {
5349 .fattr = &fattr,
5350 .label = &label,
5351 .server = server,
5352 };
5353 struct rpc_message msg = {
5354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005355 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005356 .rpc_resp = &res,
5357 };
5358 int ret;
5359
5360 nfs_fattr_init(&fattr);
5361
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005362 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005363 if (ret)
5364 return ret;
5365 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5366 return -ENOENT;
5367 if (buflen < label.len)
5368 return -ERANGE;
5369 return 0;
5370}
5371
5372static int nfs4_get_security_label(struct inode *inode, void *buf,
5373 size_t buflen)
5374{
5375 struct nfs4_exception exception = { };
5376 int err;
5377
5378 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5379 return -EOPNOTSUPP;
5380
5381 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005382 err = _nfs4_get_security_label(inode, buf, buflen);
5383 trace_nfs4_get_security_label(inode, err);
5384 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005385 &exception);
5386 } while (exception.retry);
5387 return err;
5388}
5389
5390static int _nfs4_do_set_security_label(struct inode *inode,
5391 struct nfs4_label *ilabel,
5392 struct nfs_fattr *fattr,
5393 struct nfs4_label *olabel)
5394{
5395
5396 struct iattr sattr = {0};
5397 struct nfs_server *server = NFS_SERVER(inode);
5398 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005399 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005400 .fh = NFS_FH(inode),
5401 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005402 .server = server,
5403 .bitmask = bitmask,
5404 .label = ilabel,
5405 };
5406 struct nfs_setattrres res = {
5407 .fattr = fattr,
5408 .label = olabel,
5409 .server = server,
5410 };
5411 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5413 .rpc_argp = &arg,
5414 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005415 };
5416 int status;
5417
Jeff Layton12207f62013-11-01 10:49:32 -04005418 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005419
Jeff Layton12207f62013-11-01 10:49:32 -04005420 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005421 if (status)
5422 dprintk("%s failed: %d\n", __func__, status);
5423
5424 return status;
5425}
5426
5427static int nfs4_do_set_security_label(struct inode *inode,
5428 struct nfs4_label *ilabel,
5429 struct nfs_fattr *fattr,
5430 struct nfs4_label *olabel)
5431{
5432 struct nfs4_exception exception = { };
5433 int err;
5434
5435 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005436 err = _nfs4_do_set_security_label(inode, ilabel,
5437 fattr, olabel);
5438 trace_nfs4_set_security_label(inode, err);
5439 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005440 &exception);
5441 } while (exception.retry);
5442 return err;
5443}
5444
5445static int
Al Viro59301222016-05-27 10:19:30 -04005446nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005447{
5448 struct nfs4_label ilabel, *olabel = NULL;
5449 struct nfs_fattr fattr;
5450 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005451 int status;
5452
5453 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5454 return -EOPNOTSUPP;
5455
5456 nfs_fattr_init(&fattr);
5457
5458 ilabel.pi = 0;
5459 ilabel.lfs = 0;
5460 ilabel.label = (char *)buf;
5461 ilabel.len = buflen;
5462
5463 cred = rpc_lookup_cred();
5464 if (IS_ERR(cred))
5465 return PTR_ERR(cred);
5466
5467 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5468 if (IS_ERR(olabel)) {
5469 status = -PTR_ERR(olabel);
5470 goto out;
5471 }
5472
5473 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5474 if (status == 0)
5475 nfs_setsecurity(inode, &fattr, olabel);
5476
5477 nfs4_label_free(olabel);
5478out:
5479 put_rpccred(cred);
5480 return status;
5481}
5482#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5483
5484
Chuck Leverf0920752012-05-21 22:45:41 -04005485static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5486 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005487{
5488 __be32 verf[2];
5489
Chuck Lever2c820d92012-05-21 22:45:33 -04005490 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5491 /* An impossible timestamp guarantees this value
5492 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005493 verf[0] = cpu_to_be32(U32_MAX);
5494 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005495 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005496 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005497 u64 ns = ktime_to_ns(nn->boot_time);
5498
5499 verf[0] = cpu_to_be32(ns >> 32);
5500 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005501 }
Chuck Levercd937102012-03-02 17:14:31 -05005502 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5503}
5504
Jeff Laytona3192682015-06-09 19:43:59 -04005505static int
5506nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005507{
Jeff Laytona3192682015-06-09 19:43:59 -04005508 size_t len;
5509 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005510
Trond Myklebustceb3a162015-01-03 15:16:04 -05005511 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005512 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005513
Jeff Laytona3192682015-06-09 19:43:59 -04005514 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005515 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005516 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5517 1 +
5518 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5519 1;
5520 rcu_read_unlock();
5521
5522 if (len > NFS4_OPAQUE_LIMIT + 1)
5523 return -EINVAL;
5524
5525 /*
5526 * Since this string is allocated at mount time, and held until the
5527 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5528 * about a memory-reclaim deadlock.
5529 */
5530 str = kmalloc(len, GFP_KERNEL);
5531 if (!str)
5532 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005533
Chuck Levere984a552012-09-14 17:24:21 -04005534 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005535 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005536 clp->cl_ipaddr,
5537 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5538 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005539 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005540
Jeff Laytona3192682015-06-09 19:43:59 -04005541 clp->cl_owner_id = str;
5542 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005543}
5544
Jeff Layton873e3852015-06-09 19:44:00 -04005545static int
5546nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005547{
Jeff Layton873e3852015-06-09 19:44:00 -04005548 size_t len;
5549 char *str;
5550
5551 len = 10 + 10 + 1 + 10 + 1 +
5552 strlen(nfs4_client_id_uniquifier) + 1 +
5553 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5554
5555 if (len > NFS4_OPAQUE_LIMIT + 1)
5556 return -EINVAL;
5557
5558 /*
5559 * Since this string is allocated at mount time, and held until the
5560 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5561 * about a memory-reclaim deadlock.
5562 */
5563 str = kmalloc(len, GFP_KERNEL);
5564 if (!str)
5565 return -ENOMEM;
5566
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005567 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005568 clp->rpc_ops->version, clp->cl_minorversion,
5569 nfs4_client_id_uniquifier,
5570 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005571 clp->cl_owner_id = str;
5572 return 0;
5573}
5574
5575static int
5576nfs4_init_uniform_client_string(struct nfs_client *clp)
5577{
Jeff Layton873e3852015-06-09 19:44:00 -04005578 size_t len;
5579 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005580
5581 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005582 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005583
5584 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005585 return nfs4_init_uniquifier_client_string(clp);
5586
5587 len = 10 + 10 + 1 + 10 + 1 +
5588 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5589
5590 if (len > NFS4_OPAQUE_LIMIT + 1)
5591 return -EINVAL;
5592
5593 /*
5594 * Since this string is allocated at mount time, and held until the
5595 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5596 * about a memory-reclaim deadlock.
5597 */
5598 str = kmalloc(len, GFP_KERNEL);
5599 if (!str)
5600 return -ENOMEM;
5601
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005602 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005603 clp->rpc_ops->version, clp->cl_minorversion,
5604 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005605 clp->cl_owner_id = str;
5606 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005607}
5608
Chuck Lever706cb8d2014-03-12 12:51:47 -04005609/*
5610 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5611 * services. Advertise one based on the address family of the
5612 * clientaddr.
5613 */
5614static unsigned int
5615nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5616{
5617 if (strchr(clp->cl_ipaddr, ':') != NULL)
5618 return scnprintf(buf, len, "tcp6");
5619 else
5620 return scnprintf(buf, len, "tcp");
5621}
5622
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005623static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5624{
5625 struct nfs4_setclientid *sc = calldata;
5626
5627 if (task->tk_status == 0)
5628 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5629}
5630
5631static const struct rpc_call_ops nfs4_setclientid_ops = {
5632 .rpc_call_done = nfs4_setclientid_done,
5633};
5634
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005635/**
5636 * nfs4_proc_setclientid - Negotiate client ID
5637 * @clp: state data structure
5638 * @program: RPC program for NFSv4 callback service
5639 * @port: IP port number for NFS4 callback service
5640 * @cred: RPC credential to use for this call
5641 * @res: where to place the result
5642 *
5643 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5644 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005645int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5646 unsigned short port, struct rpc_cred *cred,
5647 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
5649 nfs4_verifier sc_verifier;
5650 struct nfs4_setclientid setclientid = {
5651 .sc_verifier = &sc_verifier,
5652 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005653 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 };
5655 struct rpc_message msg = {
5656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5657 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005658 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005659 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005661 struct rpc_task *task;
5662 struct rpc_task_setup task_setup_data = {
5663 .rpc_client = clp->cl_rpcclient,
5664 .rpc_message = &msg,
5665 .callback_ops = &nfs4_setclientid_ops,
5666 .callback_data = &setclientid,
5667 .flags = RPC_TASK_TIMEOUT,
5668 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005669 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Chuck Leverde734832012-07-11 16:30:50 -04005671 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005672 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005673
5674 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5675 status = nfs4_init_uniform_client_string(clp);
5676 else
Jeff Laytona3192682015-06-09 19:43:59 -04005677 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005678
5679 if (status)
5680 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005681
Chuck Leverde734832012-07-11 16:30:50 -04005682 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005683 setclientid.sc_netid_len =
5684 nfs4_init_callback_netid(clp,
5685 setclientid.sc_netid,
5686 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005687 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005688 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 clp->cl_ipaddr, port >> 8, port & 255);
5690
Jeff Layton3a6bb732015-06-09 19:43:57 -04005691 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005692 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005693 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005694 task = rpc_run_task(&task_setup_data);
5695 if (IS_ERR(task)) {
5696 status = PTR_ERR(task);
5697 goto out;
5698 }
5699 status = task->tk_status;
5700 if (setclientid.sc_cred) {
5701 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5702 put_rpccred(setclientid.sc_cred);
5703 }
5704 rpc_put_task(task);
5705out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005706 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005707 dprintk("NFS reply setclientid: %d\n", status);
5708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709}
5710
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005711/**
5712 * nfs4_proc_setclientid_confirm - Confirm client ID
5713 * @clp: state data structure
5714 * @res: result of a previous SETCLIENTID
5715 * @cred: RPC credential to use for this call
5716 *
5717 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5718 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005719int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005720 struct nfs4_setclientid_res *arg,
5721 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 struct rpc_message msg = {
5724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005725 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005726 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 int status;
5729
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005730 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5731 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5732 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005733 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005734 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005735 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 return status;
5737}
5738
Trond Myklebustfe650402006-01-03 09:55:18 +01005739struct nfs4_delegreturndata {
5740 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005741 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005742 struct nfs_fh fh;
5743 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005744 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005745 struct {
5746 struct nfs4_layoutreturn_args arg;
5747 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005748 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005749 u32 roc_barrier;
5750 bool roc;
5751 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005752 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005753 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005754 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005755};
5756
Trond Myklebustfe650402006-01-03 09:55:18 +01005757static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5758{
5759 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005760
Trond Myklebust14516c32010-07-31 14:29:06 -04005761 if (!nfs4_sequence_done(task, &data->res.seq_res))
5762 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005763
Trond Myklebustca8acf82013-08-13 10:36:56 -04005764 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005765
5766 /* Handle Layoutreturn errors */
5767 if (data->args.lr_args && task->tk_status != 0) {
5768 switch(data->res.lr_ret) {
5769 default:
5770 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5771 break;
5772 case 0:
5773 data->args.lr_args = NULL;
5774 data->res.lr_res = NULL;
5775 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005776 case -NFS4ERR_OLD_STATEID:
5777 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005778 data->inode))
5779 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005780 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005781 case -NFS4ERR_ADMIN_REVOKED:
5782 case -NFS4ERR_DELEG_REVOKED:
5783 case -NFS4ERR_EXPIRED:
5784 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005785 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5786 case -NFS4ERR_WRONG_CRED:
5787 data->args.lr_args = NULL;
5788 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005789 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005790 }
5791 }
5792
Ricardo Labiaga79708862009-12-07 09:23:21 -05005793 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005794 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005795 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005796 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005797 case -NFS4ERR_ADMIN_REVOKED:
5798 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005799 case -NFS4ERR_EXPIRED:
5800 nfs4_free_revoked_stateid(data->res.server,
5801 data->args.stateid,
5802 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005803 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005804 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005805 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005806 task->tk_status = 0;
5807 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005808 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005809 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5810 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005811 task->tk_status = 0;
5812 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005813 case -NFS4ERR_ACCESS:
5814 if (data->args.bitmask) {
5815 data->args.bitmask = NULL;
5816 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005817 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005818 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005819 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005820 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005821 if (nfs4_async_handle_error(task, data->res.server,
5822 NULL, NULL) == -EAGAIN) {
Trond Myklebust140087fd2017-11-06 15:28:10 -05005823 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005824 }
5825 }
5826 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005827 return;
5828lr_restart:
5829 data->res.lr_ret = 0;
5830out_restart:
5831 task->tk_status = 0;
5832 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005833}
5834
5835static void nfs4_delegreturn_release(void *calldata)
5836{
Peng Tao039b7562014-07-03 13:05:02 +08005837 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005838 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005839
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005840 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005841 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005842 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5843 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005844 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005845 nfs_iput_and_deactive(inode);
5846 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005847 kfree(calldata);
5848}
5849
Andy Adamson938e1012009-04-01 09:22:28 -04005850static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5851{
5852 struct nfs4_delegreturndata *d_data;
5853
5854 d_data = (struct nfs4_delegreturndata *)data;
5855
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005856 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005857 return;
5858
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005859 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005860 &d_data->args.seq_args,
5861 &d_data->res.seq_res,
5862 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005863}
Andy Adamson938e1012009-04-01 09:22:28 -04005864
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005865static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005866 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005867 .rpc_call_done = nfs4_delegreturn_done,
5868 .rpc_release = nfs4_delegreturn_release,
5869};
5870
Trond Myklebuste6f81072008-01-24 18:14:34 -05005871static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005872{
5873 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005874 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005875 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005876 struct rpc_message msg = {
5877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5878 .rpc_cred = cred,
5879 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005880 struct rpc_task_setup task_setup_data = {
5881 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005882 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005883 .callback_ops = &nfs4_delegreturn_ops,
5884 .flags = RPC_TASK_ASYNC,
5885 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005886 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005887
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005888 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005889 if (data == NULL)
5890 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005891 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005892
5893 nfs4_state_protect(server->nfs_client,
5894 NFS_SP4_MACH_CRED_CLEANUP,
5895 &task_setup_data.rpc_client, &msg);
5896
Trond Myklebustfe650402006-01-03 09:55:18 +01005897 data->args.fhandle = &data->fh;
5898 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005899 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005900 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005901 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005902 data->res.fattr = &data->fattr;
5903 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005904 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005905 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005906 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005907 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005908 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005909 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005910 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005911 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005912 if (data->lr.roc) {
5913 data->args.lr_args = &data->lr.arg;
5914 data->res.lr_res = &data->lr.res;
5915 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005916 } else if (data->lr.roc) {
5917 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5918 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005919 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005920
Trond Myklebustc970aa82007-07-14 15:39:59 -04005921 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005922 msg.rpc_argp = &data->args;
5923 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005924 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005925 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005926 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005927 if (!issync)
5928 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005929 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005930 if (status != 0)
5931 goto out;
5932 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005933out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005934 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005935 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936}
5937
Trond Myklebuste6f81072008-01-24 18:14:34 -05005938int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939{
5940 struct nfs_server *server = NFS_SERVER(inode);
5941 struct nfs4_exception exception = { };
5942 int err;
5943 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005944 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005945 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 switch (err) {
5947 case -NFS4ERR_STALE_STATEID:
5948 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 case 0:
5950 return 0;
5951 }
5952 err = nfs4_handle_exception(server, err, &exception);
5953 } while (exception.retry);
5954 return err;
5955}
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5958{
5959 struct inode *inode = state->inode;
5960 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005961 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005962 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005964 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005966 struct nfs_lockt_res res = {
5967 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 };
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005971 .rpc_argp = &arg,
5972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 .rpc_cred = state->owner->so_cred,
5974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 struct nfs4_lock_state *lsp;
5976 int status;
5977
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005978 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005979 status = nfs4_set_lock_state(state, request);
5980 if (status != 0)
5981 goto out;
5982 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005983 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005984 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005986 switch (status) {
5987 case 0:
5988 request->fl_type = F_UNLCK;
5989 break;
5990 case -NFS4ERR_DENIED:
5991 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005993 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005994 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005995out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 return status;
5997}
5998
5999static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6000{
6001 struct nfs4_exception exception = { };
6002 int err;
6003
6004 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006005 err = _nfs4_proc_getlk(state, cmd, request);
6006 trace_nfs4_get_lock(request, state, cmd, err);
6007 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 &exception);
6009 } while (exception.retry);
6010 return err;
6011}
6012
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006013struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006014 struct nfs_locku_args arg;
6015 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006016 struct nfs4_lock_state *lsp;
6017 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006018 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006019 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006020 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006021 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006022};
6023
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006024static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6025 struct nfs_open_context *ctx,
6026 struct nfs4_lock_state *lsp,
6027 struct nfs_seqid *seqid)
6028{
6029 struct nfs4_unlockdata *p;
6030 struct inode *inode = lsp->ls_state->inode;
6031
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006032 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006033 if (p == NULL)
6034 return NULL;
6035 p->arg.fh = NFS_FH(inode);
6036 p->arg.fl = &p->fl;
6037 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006038 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006039 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006040 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006041 /* Ensure we don't close file until we're done freeing locks! */
6042 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006043 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006044 memcpy(&p->fl, fl, sizeof(p->fl));
6045 p->server = NFS_SERVER(inode);
6046 return p;
6047}
6048
Trond Myklebust06f814a2006-01-03 09:55:07 +01006049static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006050{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006051 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006052 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006053 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006054 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006055 put_nfs_open_context(calldata->ctx);
6056 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006057}
6058
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006059static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006060{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006061 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006062
Trond Myklebust14516c32010-07-31 14:29:06 -04006063 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6064 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006065 switch (task->tk_status) {
6066 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006067 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006068 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006069 if (nfs4_update_lock_stateid(calldata->lsp,
6070 &calldata->res.stateid))
6071 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006072 case -NFS4ERR_ADMIN_REVOKED:
6073 case -NFS4ERR_EXPIRED:
6074 nfs4_free_revoked_stateid(calldata->server,
6075 &calldata->arg.stateid,
6076 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006077 case -NFS4ERR_BAD_STATEID:
6078 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006079 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006080 if (!nfs4_stateid_match(&calldata->arg.stateid,
6081 &calldata->lsp->ls_stateid))
6082 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006083 break;
6084 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006085 if (nfs4_async_handle_error(task, calldata->server,
6086 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006087 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006088 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006089 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006090}
6091
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006092static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006093{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006094 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006096 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6097 nfs_async_iocounter_wait(task, calldata->l_ctx))
6098 return;
6099
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006101 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006102 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006103 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006104 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006105 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006106 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006107 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006108 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006109 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006110 &calldata->res.seq_res,
6111 task) != 0)
6112 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006113 return;
6114out_no_action:
6115 task->tk_action = NULL;
6116out_wait:
6117 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118}
6119
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006120static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006121 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006122 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006123 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006124};
6125
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006126static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6127 struct nfs_open_context *ctx,
6128 struct nfs4_lock_state *lsp,
6129 struct nfs_seqid *seqid)
6130{
6131 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006132 struct rpc_message msg = {
6133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6134 .rpc_cred = ctx->cred,
6135 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006136 struct rpc_task_setup task_setup_data = {
6137 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006138 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006139 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006140 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006141 .flags = RPC_TASK_ASYNC,
6142 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006143
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006144 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6145 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6146
Frank Filz137d6ac2007-07-09 15:32:29 -07006147 /* Ensure this is an unlock - when canceling a lock, the
6148 * canceled lock is passed in, and it won't be an unlock.
6149 */
6150 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006151 if (fl->fl_flags & FL_CLOSE)
6152 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006153
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006154 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6155 if (data == NULL) {
6156 nfs_free_seqid(seqid);
6157 return ERR_PTR(-ENOMEM);
6158 }
6159
Chuck Levera9c92d62013-08-09 12:48:18 -04006160 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006161 msg.rpc_argp = &data->arg;
6162 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006163 task_setup_data.callback_data = data;
6164 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006165}
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6168{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006169 struct inode *inode = state->inode;
6170 struct nfs4_state_owner *sp = state->owner;
6171 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006172 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006173 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006174 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006175 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006176 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006177 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Trond Myklebust9b073572006-06-29 16:38:34 -04006179 status = nfs4_set_lock_state(state, request);
6180 /* Unlock _before_ we do the RPC call */
6181 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006182 /* Exclude nfs_delegation_claim_locks() */
6183 mutex_lock(&sp->so_delegreturn_mutex);
6184 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006185 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006186 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006187 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006188 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006189 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006190 }
6191 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006192 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006193 if (status != 0)
6194 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006195 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006196 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006197 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6198 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006199 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6200 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006201 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006202 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006203 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006204 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205 status = PTR_ERR(task);
6206 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006207 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006208 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006209 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006210out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006211 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006212 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006213 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214}
6215
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006216struct nfs4_lockdata {
6217 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006218 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219 struct nfs4_lock_state *lsp;
6220 struct nfs_open_context *ctx;
6221 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006222 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006223 int rpc_status;
6224 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006225 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006226};
6227
6228static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006229 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6230 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231{
6232 struct nfs4_lockdata *p;
6233 struct inode *inode = lsp->ls_state->inode;
6234 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006235 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006236
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006237 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006238 if (p == NULL)
6239 return NULL;
6240
6241 p->arg.fh = NFS_FH(inode);
6242 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006243 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006244 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006245 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006246 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6247 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006248 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006249 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006250 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006251 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006252 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006253 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006255 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006256 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006257 p->ctx = get_nfs_open_context(ctx);
6258 memcpy(&p->fl, fl, sizeof(p->fl));
6259 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006260out_free_seqid:
6261 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006262out_free:
6263 kfree(p);
6264 return NULL;
6265}
6266
6267static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6268{
6269 struct nfs4_lockdata *data = calldata;
6270 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
Harvey Harrison3110ff82008-05-02 13:42:44 -07006272 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006273 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006274 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006275 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006276 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006277 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006278 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006279 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006280 nfs4_stateid_copy(&data->arg.open_stateid,
6281 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006283 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006284 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006285 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006286 nfs4_stateid_copy(&data->arg.lock_stateid,
6287 &data->lsp->ls_stateid);
6288 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006289 if (!nfs4_valid_open_stateid(state)) {
6290 data->rpc_status = -EBADF;
6291 task->tk_action = NULL;
6292 goto out_release_open_seqid;
6293 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006294 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006295 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006296 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006297 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006298 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006299 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006300out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006301 nfs_release_seqid(data->arg.open_seqid);
6302out_release_lock_seqid:
6303 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006304out_wait:
6305 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006306 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006307}
6308
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006309static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6310{
6311 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006312 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006313
Harvey Harrison3110ff82008-05-02 13:42:44 -07006314 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315
Trond Myklebust14516c32010-07-31 14:29:06 -04006316 if (!nfs4_sequence_done(task, &data->res.seq_res))
6317 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006318
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006319 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006320 switch (task->tk_status) {
6321 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006322 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006323 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006324 if (data->arg.new_lock) {
6325 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006326 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006327 rpc_restart_call_prepare(task);
6328 break;
6329 }
6330 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006331 if (data->arg.new_lock_owner != 0) {
6332 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6333 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6334 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6335 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6336 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006337 break;
6338 case -NFS4ERR_BAD_STATEID:
6339 case -NFS4ERR_OLD_STATEID:
6340 case -NFS4ERR_STALE_STATEID:
6341 case -NFS4ERR_EXPIRED:
6342 if (data->arg.new_lock_owner != 0) {
6343 if (!nfs4_stateid_match(&data->arg.open_stateid,
6344 &lsp->ls_state->open_stateid))
6345 rpc_restart_call_prepare(task);
6346 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6347 &lsp->ls_stateid))
6348 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006349 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006350 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351}
6352
6353static void nfs4_lock_release(void *calldata)
6354{
6355 struct nfs4_lockdata *data = calldata;
6356
Harvey Harrison3110ff82008-05-02 13:42:44 -07006357 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006358 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006359 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006360 struct rpc_task *task;
6361 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6362 data->arg.lock_seqid);
6363 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006364 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006365 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006366 } else
6367 nfs_free_seqid(data->arg.lock_seqid);
6368 nfs4_put_lock_state(data->lsp);
6369 put_nfs_open_context(data->ctx);
6370 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006371 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006372}
6373
6374static const struct rpc_call_ops nfs4_lock_ops = {
6375 .rpc_call_prepare = nfs4_lock_prepare,
6376 .rpc_call_done = nfs4_lock_done,
6377 .rpc_release = nfs4_lock_release,
6378};
6379
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006380static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6381{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006382 switch (error) {
6383 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006384 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006385 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006386 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006387 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006388 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006389 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006390 break;
6391 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006392 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006393 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006394 };
6395}
6396
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006397static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006398{
6399 struct nfs4_lockdata *data;
6400 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6403 .rpc_cred = state->owner->so_cred,
6404 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006405 struct rpc_task_setup task_setup_data = {
6406 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006407 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006408 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006409 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006410 .flags = RPC_TASK_ASYNC,
6411 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006412 int ret;
6413
Harvey Harrison3110ff82008-05-02 13:42:44 -07006414 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006415 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006416 fl->fl_u.nfs4_fl.owner,
6417 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006418 if (data == NULL)
6419 return -ENOMEM;
6420 if (IS_SETLKW(cmd))
6421 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006422 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006423 msg.rpc_argp = &data->arg;
6424 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006425 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006426 if (recovery_type > NFS_LOCK_NEW) {
6427 if (recovery_type == NFS_LOCK_RECLAIM)
6428 data->arg.reclaim = NFS_LOCK_RECLAIM;
6429 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006430 } else
6431 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006432 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006433 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006434 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006435 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006436 if (ret == 0) {
6437 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006438 if (ret)
6439 nfs4_handle_setlk_error(data->server, data->lsp,
6440 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006441 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006442 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006443 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006444 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006445 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447}
6448
6449static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6450{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006451 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006452 struct nfs4_exception exception = {
6453 .inode = state->inode,
6454 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006455 int err;
6456
6457 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006458 /* Cache the lock if possible... */
6459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6460 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006461 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006462 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006463 break;
6464 nfs4_handle_exception(server, err, &exception);
6465 } while (exception.retry);
6466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467}
6468
6469static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6470{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006471 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006472 struct nfs4_exception exception = {
6473 .inode = state->inode,
6474 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006475 int err;
6476
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006477 err = nfs4_set_lock_state(state, request);
6478 if (err != 0)
6479 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006480 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006481 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6482 return 0;
6483 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006484 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6486 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006487 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006488 switch (err) {
6489 default:
6490 goto out;
6491 case -NFS4ERR_GRACE:
6492 case -NFS4ERR_DELAY:
6493 nfs4_handle_exception(server, err, &exception);
6494 err = 0;
6495 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006496 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006497out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499}
6500
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006501#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006502static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6503{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006504 struct nfs4_lock_state *lsp;
6505 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006506
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006507 status = nfs4_set_lock_state(state, request);
6508 if (status != 0)
6509 return status;
6510 lsp = request->fl_u.nfs4_fl.owner;
6511 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6512 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6513 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006514 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006515}
6516#endif
6517
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6519{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006520 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006521 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006522 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006523 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
Trond Myklebust01c3b862006-06-29 16:38:39 -04006525 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006526 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006527 if (status < 0)
6528 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006529 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006530 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006531 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006532 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006533 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006534 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006535 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006536 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006537 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006538 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006539 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006540 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006541 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006542 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006543out:
6544 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 return status;
6546}
6547
6548static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6549{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006550 struct nfs4_exception exception = {
6551 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006552 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006553 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 int err;
6555
6556 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006557 err = _nfs4_proc_setlk(state, cmd, request);
6558 if (err == -NFS4ERR_DENIED)
6559 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006561 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 } while (exception.retry);
6563 return err;
6564}
6565
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006566#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6567#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6568
6569static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006570nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6571 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006572{
6573 int status = -ERESTARTSYS;
6574 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6575
6576 while(!signalled()) {
6577 status = nfs4_proc_setlk(state, cmd, request);
6578 if ((status != -EAGAIN) || IS_SETLK(cmd))
6579 break;
6580 freezable_schedule_timeout_interruptible(timeout);
6581 timeout *= 2;
6582 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6583 status = -ERESTARTSYS;
6584 }
6585 return status;
6586}
6587
Jeff Laytona1d617d82016-09-17 18:17:39 -04006588#ifdef CONFIG_NFS_V4_1
6589struct nfs4_lock_waiter {
6590 struct task_struct *task;
6591 struct inode *inode;
6592 struct nfs_lowner *owner;
6593 bool notified;
6594};
6595
6596static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006597nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006598{
6599 int ret;
6600 struct cb_notify_lock_args *cbnl = key;
6601 struct nfs4_lock_waiter *waiter = wait->private;
6602 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6603 *wowner = waiter->owner;
6604
6605 /* Only wake if the callback was for the same owner */
6606 if (lowner->clientid != wowner->clientid ||
6607 lowner->id != wowner->id ||
6608 lowner->s_dev != wowner->s_dev)
6609 return 0;
6610
6611 /* Make sure it's for the right inode */
6612 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6613 return 0;
6614
6615 waiter->notified = true;
6616
6617 /* override "private" so we can use default_wake_function */
6618 wait->private = waiter->task;
6619 ret = autoremove_wake_function(wait, mode, flags, key);
6620 wait->private = waiter;
6621 return ret;
6622}
6623
6624static int
6625nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6626{
6627 int status = -ERESTARTSYS;
6628 unsigned long flags;
6629 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6630 struct nfs_server *server = NFS_SERVER(state->inode);
6631 struct nfs_client *clp = server->nfs_client;
6632 wait_queue_head_t *q = &clp->cl_lock_waitq;
6633 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6634 .id = lsp->ls_seqid.owner_id,
6635 .s_dev = server->s_dev };
6636 struct nfs4_lock_waiter waiter = { .task = current,
6637 .inode = state->inode,
6638 .owner = &owner,
6639 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006640 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006641
6642 /* Don't bother with waitqueue if we don't expect a callback */
6643 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6644 return nfs4_retry_setlk_simple(state, cmd, request);
6645
6646 init_wait(&wait);
6647 wait.private = &waiter;
6648 wait.func = nfs4_wake_lock_waiter;
6649 add_wait_queue(q, &wait);
6650
6651 while(!signalled()) {
6652 status = nfs4_proc_setlk(state, cmd, request);
6653 if ((status != -EAGAIN) || IS_SETLK(cmd))
6654 break;
6655
6656 status = -ERESTARTSYS;
6657 spin_lock_irqsave(&q->lock, flags);
6658 if (waiter.notified) {
6659 spin_unlock_irqrestore(&q->lock, flags);
6660 continue;
6661 }
6662 set_current_state(TASK_INTERRUPTIBLE);
6663 spin_unlock_irqrestore(&q->lock, flags);
6664
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006665 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006666 }
6667
6668 finish_wait(q, &wait);
6669 return status;
6670}
6671#else /* !CONFIG_NFS_V4_1 */
6672static inline int
6673nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6674{
6675 return nfs4_retry_setlk_simple(state, cmd, request);
6676}
6677#endif
6678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679static int
6680nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6681{
6682 struct nfs_open_context *ctx;
6683 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 int status;
6685
6686 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006687 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 state = ctx->state;
6689
Trond Myklebustd9531262009-07-21 19:22:38 -04006690 if (IS_GETLK(cmd)) {
6691 if (state != NULL)
6692 return nfs4_proc_getlk(state, F_GETLK, request);
6693 return 0;
6694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695
6696 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6697 return -EINVAL;
6698
Trond Myklebustd9531262009-07-21 19:22:38 -04006699 if (request->fl_type == F_UNLCK) {
6700 if (state != NULL)
6701 return nfs4_proc_unlck(state, cmd, request);
6702 return 0;
6703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Trond Myklebustd9531262009-07-21 19:22:38 -04006705 if (state == NULL)
6706 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006707
6708 if ((request->fl_flags & FL_POSIX) &&
6709 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6710 return -ENOLCK;
6711
Jeff Layton1ea67db2016-09-17 18:17:37 -04006712 status = nfs4_set_lock_state(state, request);
6713 if (status != 0)
6714 return status;
6715
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006716 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717}
6718
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006719int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006720{
6721 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006722 int err;
6723
6724 err = nfs4_set_lock_state(state, fl);
6725 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006726 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006727 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006728 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006729}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006730
Trond Myklebustcf470c32012-03-07 13:49:12 -05006731struct nfs_release_lockowner_data {
6732 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006733 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006734 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006735 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006736 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006737};
6738
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006739static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6740{
6741 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006742 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006743 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6744 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006745 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006746 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006747}
6748
6749static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6750{
6751 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006752 struct nfs_server *server = data->server;
6753
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006754 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006755
6756 switch (task->tk_status) {
6757 case 0:
6758 renew_lease(server, data->timestamp);
6759 break;
6760 case -NFS4ERR_STALE_CLIENTID:
6761 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006762 nfs4_schedule_lease_recovery(server->nfs_client);
6763 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006764 case -NFS4ERR_LEASE_MOVED:
6765 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006766 if (nfs4_async_handle_error(task, server,
6767 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006768 rpc_restart_call_prepare(task);
6769 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006770}
6771
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006772static void nfs4_release_lockowner_release(void *calldata)
6773{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006774 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006775 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006776 kfree(calldata);
6777}
6778
Trond Myklebust17280172012-03-11 13:11:00 -04006779static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006780 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6781 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006782 .rpc_release = nfs4_release_lockowner_release,
6783};
6784
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006785static void
6786nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006787{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006788 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006789 struct rpc_message msg = {
6790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6791 };
6792
6793 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006794 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006795
Trond Myklebustcf470c32012-03-07 13:49:12 -05006796 data = kmalloc(sizeof(*data), GFP_NOFS);
6797 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006798 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006799 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006800 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006801 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6802 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6803 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006804
Trond Myklebustcf470c32012-03-07 13:49:12 -05006805 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006806 msg.rpc_resp = &data->res;
6807 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006808 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006809}
6810
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006811#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6812
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006813static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006814 struct dentry *unused, struct inode *inode,
6815 const char *key, const void *buf,
6816 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006817{
Al Viro59301222016-05-27 10:19:30 -04006818 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006819}
6820
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006821static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006822 struct dentry *unused, struct inode *inode,
6823 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006824{
Al Virob2968212016-04-10 20:48:24 -04006825 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006826}
6827
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006828static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006829{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006830 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006831}
6832
David Quigleyc9bccef2013-05-22 12:50:45 -04006833#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006834
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006835static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006836 struct dentry *unused, struct inode *inode,
6837 const char *key, const void *buf,
6838 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006839{
6840 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006841 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006842
6843 return -EOPNOTSUPP;
6844}
6845
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006846static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006847 struct dentry *unused, struct inode *inode,
6848 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006849{
6850 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006851 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006852 return -EOPNOTSUPP;
6853}
6854
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006855static ssize_t
6856nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006857{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006858 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006859
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006860 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6861 len = security_inode_listsecurity(inode, list, list_len);
6862 if (list_len && len > list_len)
6863 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006864 }
6865 return len;
6866}
6867
6868static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6869 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006870 .get = nfs4_xattr_get_nfs4_label,
6871 .set = nfs4_xattr_set_nfs4_label,
6872};
David Quigleyc9bccef2013-05-22 12:50:45 -04006873
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006874#else
6875
6876static ssize_t
6877nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6878{
6879 return 0;
6880}
6881
6882#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006883
Andy Adamson533eb462011-06-13 18:25:56 -04006884/*
6885 * nfs_fhget will use either the mounted_on_fileid or the fileid
6886 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006887static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6888{
Andy Adamson533eb462011-06-13 18:25:56 -04006889 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6890 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6891 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006892 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006893 return;
6894
6895 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006896 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006897 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6898 fattr->nlink = 2;
6899}
6900
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006901static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6902 const struct qstr *name,
6903 struct nfs4_fs_locations *fs_locations,
6904 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006905{
6906 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006907 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006908 struct nfs4_fs_locations_arg args = {
6909 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006910 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006911 .page = page,
6912 .bitmask = bitmask,
6913 };
Benny Halevy22958462009-04-01 09:22:02 -04006914 struct nfs4_fs_locations_res res = {
6915 .fs_locations = fs_locations,
6916 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006917 struct rpc_message msg = {
6918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6919 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006920 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006921 };
6922 int status;
6923
Harvey Harrison3110ff82008-05-02 13:42:44 -07006924 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006925
Chuck Leverc05cefc2017-11-05 15:45:22 -05006926 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6927 bitmask[1] = nfs4_fattr_bitmap[1];
6928
Andy Adamson533eb462011-06-13 18:25:56 -04006929 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6930 * is not supported */
6931 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05006932 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006933 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05006934 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006935
Trond Myklebustc228fd32007-01-13 02:28:11 -05006936 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006937 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006938 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006939 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006940 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006941 return status;
6942}
6943
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006944int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6945 const struct qstr *name,
6946 struct nfs4_fs_locations *fs_locations,
6947 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006948{
6949 struct nfs4_exception exception = { };
6950 int err;
6951 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006952 err = _nfs4_proc_fs_locations(client, dir, name,
6953 fs_locations, page);
6954 trace_nfs4_get_fs_locations(dir, name, err);
6955 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006956 &exception);
6957 } while (exception.retry);
6958 return err;
6959}
6960
Chuck Leverb03d7352013-10-17 14:12:50 -04006961/*
6962 * This operation also signals the server that this client is
6963 * performing migration recovery. The server can stop returning
6964 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6965 * appended to this compound to identify the client ID which is
6966 * performing recovery.
6967 */
6968static int _nfs40_proc_get_locations(struct inode *inode,
6969 struct nfs4_fs_locations *locations,
6970 struct page *page, struct rpc_cred *cred)
6971{
6972 struct nfs_server *server = NFS_SERVER(inode);
6973 struct rpc_clnt *clnt = server->client;
6974 u32 bitmask[2] = {
6975 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6976 };
6977 struct nfs4_fs_locations_arg args = {
6978 .clientid = server->nfs_client->cl_clientid,
6979 .fh = NFS_FH(inode),
6980 .page = page,
6981 .bitmask = bitmask,
6982 .migration = 1, /* skip LOOKUP */
6983 .renew = 1, /* append RENEW */
6984 };
6985 struct nfs4_fs_locations_res res = {
6986 .fs_locations = locations,
6987 .migration = 1,
6988 .renew = 1,
6989 };
6990 struct rpc_message msg = {
6991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6992 .rpc_argp = &args,
6993 .rpc_resp = &res,
6994 .rpc_cred = cred,
6995 };
6996 unsigned long now = jiffies;
6997 int status;
6998
6999 nfs_fattr_init(&locations->fattr);
7000 locations->server = server;
7001 locations->nlocations = 0;
7002
7003 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7004 nfs4_set_sequence_privileged(&args.seq_args);
7005 status = nfs4_call_sync_sequence(clnt, server, &msg,
7006 &args.seq_args, &res.seq_res);
7007 if (status)
7008 return status;
7009
7010 renew_lease(server, now);
7011 return 0;
7012}
7013
7014#ifdef CONFIG_NFS_V4_1
7015
7016/*
7017 * This operation also signals the server that this client is
7018 * performing migration recovery. The server can stop asserting
7019 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7020 * performing this operation is identified in the SEQUENCE
7021 * operation in this compound.
7022 *
7023 * When the client supports GETATTR(fs_locations_info), it can
7024 * be plumbed in here.
7025 */
7026static int _nfs41_proc_get_locations(struct inode *inode,
7027 struct nfs4_fs_locations *locations,
7028 struct page *page, struct rpc_cred *cred)
7029{
7030 struct nfs_server *server = NFS_SERVER(inode);
7031 struct rpc_clnt *clnt = server->client;
7032 u32 bitmask[2] = {
7033 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7034 };
7035 struct nfs4_fs_locations_arg args = {
7036 .fh = NFS_FH(inode),
7037 .page = page,
7038 .bitmask = bitmask,
7039 .migration = 1, /* skip LOOKUP */
7040 };
7041 struct nfs4_fs_locations_res res = {
7042 .fs_locations = locations,
7043 .migration = 1,
7044 };
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7047 .rpc_argp = &args,
7048 .rpc_resp = &res,
7049 .rpc_cred = cred,
7050 };
7051 int status;
7052
7053 nfs_fattr_init(&locations->fattr);
7054 locations->server = server;
7055 locations->nlocations = 0;
7056
7057 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7058 nfs4_set_sequence_privileged(&args.seq_args);
7059 status = nfs4_call_sync_sequence(clnt, server, &msg,
7060 &args.seq_args, &res.seq_res);
7061 if (status == NFS4_OK &&
7062 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7063 status = -NFS4ERR_LEASE_MOVED;
7064 return status;
7065}
7066
7067#endif /* CONFIG_NFS_V4_1 */
7068
7069/**
7070 * nfs4_proc_get_locations - discover locations for a migrated FSID
7071 * @inode: inode on FSID that is migrating
7072 * @locations: result of query
7073 * @page: buffer
7074 * @cred: credential to use for this operation
7075 *
7076 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7077 * operation failed, or a negative errno if a local error occurred.
7078 *
7079 * On success, "locations" is filled in, but if the server has
7080 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7081 * asserted.
7082 *
7083 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7084 * from this client that require migration recovery.
7085 */
7086int nfs4_proc_get_locations(struct inode *inode,
7087 struct nfs4_fs_locations *locations,
7088 struct page *page, struct rpc_cred *cred)
7089{
7090 struct nfs_server *server = NFS_SERVER(inode);
7091 struct nfs_client *clp = server->nfs_client;
7092 const struct nfs4_mig_recovery_ops *ops =
7093 clp->cl_mvops->mig_recovery_ops;
7094 struct nfs4_exception exception = { };
7095 int status;
7096
7097 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7098 (unsigned long long)server->fsid.major,
7099 (unsigned long long)server->fsid.minor,
7100 clp->cl_hostname);
7101 nfs_display_fhandle(NFS_FH(inode), __func__);
7102
7103 do {
7104 status = ops->get_locations(inode, locations, page, cred);
7105 if (status != -NFS4ERR_DELAY)
7106 break;
7107 nfs4_handle_exception(server, status, &exception);
7108 } while (exception.retry);
7109 return status;
7110}
7111
Chuck Lever44c99932013-10-17 14:13:30 -04007112/*
7113 * This operation also signals the server that this client is
7114 * performing "lease moved" recovery. The server can stop
7115 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7116 * is appended to this compound to identify the client ID which is
7117 * performing recovery.
7118 */
7119static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7120{
7121 struct nfs_server *server = NFS_SERVER(inode);
7122 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7123 struct rpc_clnt *clnt = server->client;
7124 struct nfs4_fsid_present_arg args = {
7125 .fh = NFS_FH(inode),
7126 .clientid = clp->cl_clientid,
7127 .renew = 1, /* append RENEW */
7128 };
7129 struct nfs4_fsid_present_res res = {
7130 .renew = 1,
7131 };
7132 struct rpc_message msg = {
7133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7134 .rpc_argp = &args,
7135 .rpc_resp = &res,
7136 .rpc_cred = cred,
7137 };
7138 unsigned long now = jiffies;
7139 int status;
7140
7141 res.fh = nfs_alloc_fhandle();
7142 if (res.fh == NULL)
7143 return -ENOMEM;
7144
7145 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7146 nfs4_set_sequence_privileged(&args.seq_args);
7147 status = nfs4_call_sync_sequence(clnt, server, &msg,
7148 &args.seq_args, &res.seq_res);
7149 nfs_free_fhandle(res.fh);
7150 if (status)
7151 return status;
7152
7153 do_renew_lease(clp, now);
7154 return 0;
7155}
7156
7157#ifdef CONFIG_NFS_V4_1
7158
7159/*
7160 * This operation also signals the server that this client is
7161 * performing "lease moved" recovery. The server can stop asserting
7162 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7163 * this operation is identified in the SEQUENCE operation in this
7164 * compound.
7165 */
7166static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7167{
7168 struct nfs_server *server = NFS_SERVER(inode);
7169 struct rpc_clnt *clnt = server->client;
7170 struct nfs4_fsid_present_arg args = {
7171 .fh = NFS_FH(inode),
7172 };
7173 struct nfs4_fsid_present_res res = {
7174 };
7175 struct rpc_message msg = {
7176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7177 .rpc_argp = &args,
7178 .rpc_resp = &res,
7179 .rpc_cred = cred,
7180 };
7181 int status;
7182
7183 res.fh = nfs_alloc_fhandle();
7184 if (res.fh == NULL)
7185 return -ENOMEM;
7186
7187 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7188 nfs4_set_sequence_privileged(&args.seq_args);
7189 status = nfs4_call_sync_sequence(clnt, server, &msg,
7190 &args.seq_args, &res.seq_res);
7191 nfs_free_fhandle(res.fh);
7192 if (status == NFS4_OK &&
7193 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7194 status = -NFS4ERR_LEASE_MOVED;
7195 return status;
7196}
7197
7198#endif /* CONFIG_NFS_V4_1 */
7199
7200/**
7201 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7202 * @inode: inode on FSID to check
7203 * @cred: credential to use for this operation
7204 *
7205 * Server indicates whether the FSID is present, moved, or not
7206 * recognized. This operation is necessary to clear a LEASE_MOVED
7207 * condition for this client ID.
7208 *
7209 * Returns NFS4_OK if the FSID is present on this server,
7210 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7211 * NFS4ERR code if some error occurred on the server, or a
7212 * negative errno if a local failure occurred.
7213 */
7214int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7215{
7216 struct nfs_server *server = NFS_SERVER(inode);
7217 struct nfs_client *clp = server->nfs_client;
7218 const struct nfs4_mig_recovery_ops *ops =
7219 clp->cl_mvops->mig_recovery_ops;
7220 struct nfs4_exception exception = { };
7221 int status;
7222
7223 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7224 (unsigned long long)server->fsid.major,
7225 (unsigned long long)server->fsid.minor,
7226 clp->cl_hostname);
7227 nfs_display_fhandle(NFS_FH(inode), __func__);
7228
7229 do {
7230 status = ops->fsid_present(inode, cred);
7231 if (status != -NFS4ERR_DELAY)
7232 break;
7233 nfs4_handle_exception(server, status, &exception);
7234 } while (exception.retry);
7235 return status;
7236}
7237
Andy Adamson5ec16a82013-08-08 10:57:55 -04007238/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007239 * If 'use_integrity' is true and the state managment nfs_client
7240 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7241 * and the machine credential as per RFC3530bis and RFC5661 Security
7242 * Considerations sections. Otherwise, just use the user cred with the
7243 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007244 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007245static 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 +00007246{
7247 int status;
7248 struct nfs4_secinfo_arg args = {
7249 .dir_fh = NFS_FH(dir),
7250 .name = name,
7251 };
7252 struct nfs4_secinfo_res res = {
7253 .flavors = flavors,
7254 };
7255 struct rpc_message msg = {
7256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7257 .rpc_argp = &args,
7258 .rpc_resp = &res,
7259 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007260 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007261 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007262
7263 if (use_integrity) {
7264 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007265 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7266 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007267 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007268
7269 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007270
7271 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7272 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7273
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007274 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7275 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007276 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007277
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007278 if (cred)
7279 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007280
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007281 return status;
7282}
7283
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007284int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7285 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007286{
7287 struct nfs4_exception exception = { };
7288 int err;
7289 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007290 err = -NFS4ERR_WRONGSEC;
7291
7292 /* try to use integrity protection with machine cred */
7293 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7294 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7295
7296 /*
7297 * if unable to use integrity protection, or SECINFO with
7298 * integrity protection returns NFS4ERR_WRONGSEC (which is
7299 * disallowed by spec, but exists in deployed servers) use
7300 * the current filesystem's rpc_client and the user cred.
7301 */
7302 if (err == -NFS4ERR_WRONGSEC)
7303 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7304
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007305 trace_nfs4_secinfo(dir, name, err);
7306 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007307 &exception);
7308 } while (exception.retry);
7309 return err;
7310}
7311
Andy Adamson557134a2009-04-01 09:21:53 -04007312#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007313/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007314 * Check the exchange flags returned by the server for invalid flags, having
7315 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7316 * DS flags set.
7317 */
7318static int nfs4_check_cl_exchange_flags(u32 flags)
7319{
7320 if (flags & ~EXCHGID4_FLAG_MASK_R)
7321 goto out_inval;
7322 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7323 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7324 goto out_inval;
7325 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7326 goto out_inval;
7327 return NFS_OK;
7328out_inval:
7329 return -NFS4ERR_INVAL;
7330}
7331
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007332static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007333nfs41_same_server_scope(struct nfs41_server_scope *a,
7334 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007335{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007336 if (a->server_scope_sz != b->server_scope_sz)
7337 return false;
7338 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007339}
7340
Andy Adamson02a95de2016-02-05 16:08:37 -05007341static void
7342nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7343{
7344}
7345
7346static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7347 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7348};
7349
Andy Adamson357f54d2010-12-14 10:11:57 -05007350/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007351 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007352 *
7353 * The 4.1 client currently uses the same TCP connection for the
7354 * fore and backchannel.
7355 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007356static
7357int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7358 struct rpc_xprt *xprt,
7359 struct nfs_client *clp,
7360 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007361{
7362 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007363 struct nfs41_bind_conn_to_session_args args = {
7364 .client = clp,
7365 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7366 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007367 struct nfs41_bind_conn_to_session_res res;
7368 struct rpc_message msg = {
7369 .rpc_proc =
7370 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007371 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007372 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007373 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007374 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007375 struct rpc_task_setup task_setup_data = {
7376 .rpc_client = clnt,
7377 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007378 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007379 .rpc_message = &msg,
7380 .flags = RPC_TASK_TIMEOUT,
7381 };
7382 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007383
Trond Myklebust71a097c2015-02-18 09:27:18 -08007384 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7385 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7386 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007387
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007388 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7389 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7390 args.dir = NFS4_CDFC4_FORE;
7391
7392 task = rpc_run_task(&task_setup_data);
7393 if (!IS_ERR(task)) {
7394 status = task->tk_status;
7395 rpc_put_task(task);
7396 } else
7397 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007398 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007399 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007400 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007401 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7402 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007403 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007404 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007405 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007406 dprintk("NFS: %s: Unexpected direction from server\n",
7407 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007408 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007409 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007410 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007411 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7412 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007413 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007414 }
7415 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007416
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007417 return status;
7418}
7419
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007420struct rpc_bind_conn_calldata {
7421 struct nfs_client *clp;
7422 struct rpc_cred *cred;
7423};
7424
7425static int
7426nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7427 struct rpc_xprt *xprt,
7428 void *calldata)
7429{
7430 struct rpc_bind_conn_calldata *p = calldata;
7431
7432 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7433}
7434
7435int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7436{
7437 struct rpc_bind_conn_calldata data = {
7438 .clp = clp,
7439 .cred = cred,
7440 };
7441 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7442 nfs4_proc_bind_conn_to_session_callback, &data);
7443}
7444
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007445/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007446 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7447 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007448 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007449static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7450 .how = SP4_MACH_CRED,
7451 .enforce.u.words = {
7452 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7453 1 << (OP_EXCHANGE_ID - 32) |
7454 1 << (OP_CREATE_SESSION - 32) |
7455 1 << (OP_DESTROY_SESSION - 32) |
7456 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007457 },
7458 .allow.u.words = {
7459 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007460 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007461 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007462 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007463 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007464 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007465 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007466 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007467 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007468 1 << (OP_FREE_STATEID - 32) |
7469 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007470 }
7471};
7472
7473/*
7474 * Select the state protection mode for client `clp' given the server results
7475 * from exchange_id in `sp'.
7476 *
7477 * Returns 0 on success, negative errno otherwise.
7478 */
7479static int nfs4_sp4_select_mode(struct nfs_client *clp,
7480 struct nfs41_state_protection *sp)
7481{
7482 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7483 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7484 1 << (OP_EXCHANGE_ID - 32) |
7485 1 << (OP_CREATE_SESSION - 32) |
7486 1 << (OP_DESTROY_SESSION - 32) |
7487 1 << (OP_DESTROY_CLIENTID - 32)
7488 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007489 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007490 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007491 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007492
7493 if (sp->how == SP4_MACH_CRED) {
7494 /* Print state protect result */
7495 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7496 for (i = 0; i <= LAST_NFS4_OP; i++) {
7497 if (test_bit(i, sp->enforce.u.longs))
7498 dfprintk(MOUNT, " enforce op %d\n", i);
7499 if (test_bit(i, sp->allow.u.longs))
7500 dfprintk(MOUNT, " allow op %d\n", i);
7501 }
7502
7503 /* make sure nothing is on enforce list that isn't supported */
7504 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7505 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7506 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007507 ret = -EINVAL;
7508 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007509 }
7510 }
7511
7512 /*
7513 * Minimal mode - state operations are allowed to use machine
7514 * credential. Note this already happens by default, so the
7515 * client doesn't have to do anything more than the negotiation.
7516 *
7517 * NOTE: we don't care if EXCHANGE_ID is in the list -
7518 * we're already using the machine cred for exchange_id
7519 * and will never use a different cred.
7520 */
7521 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7522 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7523 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7524 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7525 dfprintk(MOUNT, "sp4_mach_cred:\n");
7526 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007527 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007528 } else {
7529 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007530 ret = -EINVAL;
7531 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007532 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007533
7534 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007535 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7536 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007537 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7538 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007539 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007540 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007541
Andrew Elble99ade3c2015-12-02 09:39:51 -05007542 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7543 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007544 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007545 }
7546
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007547 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7548 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7549 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007550 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007551 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007552
7553 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7554 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7555 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007556 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007557 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007558
7559 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7560 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007561 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007562 }
7563
7564 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7565 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007566 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007567 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007568 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007569out:
7570 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007571 return 0;
7572}
7573
Andy Adamson8d89bd72016-09-09 09:22:18 -04007574struct nfs41_exchange_id_data {
7575 struct nfs41_exchange_id_res res;
7576 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007577};
7578
Andy Adamson8d89bd72016-09-09 09:22:18 -04007579static void nfs4_exchange_id_release(void *data)
7580{
7581 struct nfs41_exchange_id_data *cdata =
7582 (struct nfs41_exchange_id_data *)data;
7583
Olga Kornievskaia63513232017-03-13 10:36:19 -04007584 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007585 kfree(cdata->res.impl_id);
7586 kfree(cdata->res.server_scope);
7587 kfree(cdata->res.server_owner);
7588 kfree(cdata);
7589}
7590
7591static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007592 .rpc_release = nfs4_exchange_id_release,
7593};
7594
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007595/*
7596 * _nfs4_proc_exchange_id()
7597 *
7598 * Wrapper for EXCHANGE_ID operation.
7599 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007600static struct rpc_task *
7601nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007602 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007603{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007604 struct rpc_message msg = {
7605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007606 .rpc_cred = cred,
7607 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007608 struct rpc_task_setup task_setup_data = {
7609 .rpc_client = clp->cl_rpcclient,
7610 .callback_ops = &nfs4_exchange_id_call_ops,
7611 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007612 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007613 };
7614 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007615 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007616
Elena Reshetova212bf412017-10-20 12:53:38 +03007617 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007618 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007619
Trond Myklebust9c760d12017-07-31 18:38:50 -04007620 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007622 if (!calldata)
7623 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007624
Trond Myklebustfd405592017-08-01 16:02:47 -04007625 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007626
7627 status = nfs4_init_uniform_client_string(clp);
7628 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007629 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007630
Andy Adamson8d89bd72016-09-09 09:22:18 -04007631 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7632 GFP_NOFS);
7633 status = -ENOMEM;
7634 if (unlikely(calldata->res.server_owner == NULL))
7635 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007636
Andy Adamson8d89bd72016-09-09 09:22:18 -04007637 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007638 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007639 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007640 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007641
Andy Adamson8d89bd72016-09-09 09:22:18 -04007642 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7643 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007644 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007645
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007646 switch (sp4_how) {
7647 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007648 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007649 break;
7650
7651 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007652 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007653 break;
7654
7655 default:
7656 /* unsupported! */
7657 WARN_ON_ONCE(1);
7658 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007659 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007660 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007661 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007662 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007663 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007664 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7665 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007666 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007667 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007668 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7669 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007670#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007671 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007672#endif
7673 msg.rpc_argp = &calldata->args;
7674 msg.rpc_resp = &calldata->res;
7675 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007676
Trond Myklebust9c760d12017-07-31 18:38:50 -04007677 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007678
7679out_impl_id:
7680 kfree(calldata->res.impl_id);
7681out_server_scope:
7682 kfree(calldata->res.server_scope);
7683out_server_owner:
7684 kfree(calldata->res.server_owner);
7685out_calldata:
7686 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007687out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007688 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007689 return ERR_PTR(status);
7690}
7691
7692/*
7693 * _nfs4_proc_exchange_id()
7694 *
7695 * Wrapper for EXCHANGE_ID operation.
7696 */
7697static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7698 u32 sp4_how)
7699{
7700 struct rpc_task *task;
7701 struct nfs41_exchange_id_args *argp;
7702 struct nfs41_exchange_id_res *resp;
7703 int status;
7704
7705 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7706 if (IS_ERR(task))
7707 return PTR_ERR(task);
7708
7709 argp = task->tk_msg.rpc_argp;
7710 resp = task->tk_msg.rpc_resp;
7711 status = task->tk_status;
7712 if (status != 0)
7713 goto out;
7714
7715 status = nfs4_check_cl_exchange_flags(resp->flags);
7716 if (status != 0)
7717 goto out;
7718
7719 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7720 if (status != 0)
7721 goto out;
7722
7723 clp->cl_clientid = resp->clientid;
7724 clp->cl_exchange_flags = resp->flags;
7725 clp->cl_seqid = resp->seqid;
7726 /* Client ID is not confirmed */
7727 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7728 clear_bit(NFS4_SESSION_ESTABLISHED,
7729 &clp->cl_session->session_state);
7730
7731 if (clp->cl_serverscope != NULL &&
7732 !nfs41_same_server_scope(clp->cl_serverscope,
7733 resp->server_scope)) {
7734 dprintk("%s: server_scope mismatch detected\n",
7735 __func__);
7736 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7737 }
7738
7739 swap(clp->cl_serverowner, resp->server_owner);
7740 swap(clp->cl_serverscope, resp->server_scope);
7741 swap(clp->cl_implid, resp->impl_id);
7742
7743 /* Save the EXCHANGE_ID verifier session trunk tests */
7744 memcpy(clp->cl_confirm.data, argp->verifier.data,
7745 sizeof(clp->cl_confirm.data));
7746out:
7747 trace_nfs4_exchange_id(clp, status);
7748 rpc_put_task(task);
7749 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007750}
7751
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007752/*
7753 * nfs4_proc_exchange_id()
7754 *
7755 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7756 *
7757 * Since the clientid has expired, all compounds using sessions
7758 * associated with the stale clientid will be returning
7759 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7760 * be in some phase of session reset.
7761 *
7762 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7763 */
7764int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7765{
7766 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7767 int status;
7768
7769 /* try SP4_MACH_CRED if krb5i/p */
7770 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7771 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007772 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007773 if (!status)
7774 return 0;
7775 }
7776
7777 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007778 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007779}
7780
Andy Adamson04fa2c62016-09-09 09:22:29 -04007781/**
7782 * nfs4_test_session_trunk
7783 *
7784 * This is an add_xprt_test() test function called from
7785 * rpc_clnt_setup_test_and_add_xprt.
7786 *
7787 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7788 * and is dereferrenced in nfs4_exchange_id_release
7789 *
7790 * Upon success, add the new transport to the rpc_clnt
7791 *
7792 * @clnt: struct rpc_clnt to get new transport
7793 * @xprt: the rpc_xprt to test
7794 * @data: call data for _nfs4_proc_exchange_id.
7795 */
7796int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7797 void *data)
7798{
7799 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007800 struct rpc_task *task;
7801 int status;
7802
Andy Adamson04fa2c62016-09-09 09:22:29 -04007803 u32 sp4_how;
7804
7805 dprintk("--> %s try %s\n", __func__,
7806 xprt->address_strings[RPC_DISPLAY_ADDR]);
7807
7808 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7809
7810 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007811 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7812 if (IS_ERR(task))
7813 return PTR_ERR(task);
7814
7815 status = task->tk_status;
7816 if (status == 0)
7817 status = nfs4_detect_session_trunking(adata->clp,
7818 task->tk_msg.rpc_resp, xprt);
7819
7820 rpc_put_task(task);
7821 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007822}
7823EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7824
Trond Myklebust66245532012-05-25 17:18:09 -04007825static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7826 struct rpc_cred *cred)
7827{
7828 struct rpc_message msg = {
7829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7830 .rpc_argp = clp,
7831 .rpc_cred = cred,
7832 };
7833 int status;
7834
7835 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007836 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007837 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007838 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007839 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7840 return status;
7841}
7842
7843static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7844 struct rpc_cred *cred)
7845{
7846 unsigned int loop;
7847 int ret;
7848
7849 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7850 ret = _nfs4_proc_destroy_clientid(clp, cred);
7851 switch (ret) {
7852 case -NFS4ERR_DELAY:
7853 case -NFS4ERR_CLIENTID_BUSY:
7854 ssleep(1);
7855 break;
7856 default:
7857 return ret;
7858 }
7859 }
7860 return 0;
7861}
7862
7863int nfs4_destroy_clientid(struct nfs_client *clp)
7864{
7865 struct rpc_cred *cred;
7866 int ret = 0;
7867
7868 if (clp->cl_mvops->minor_version < 1)
7869 goto out;
7870 if (clp->cl_exchange_flags == 0)
7871 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007872 if (clp->cl_preserve_clid)
7873 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007874 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007875 ret = nfs4_proc_destroy_clientid(clp, cred);
7876 if (cred)
7877 put_rpccred(cred);
7878 switch (ret) {
7879 case 0:
7880 case -NFS4ERR_STALE_CLIENTID:
7881 clp->cl_exchange_flags = 0;
7882 }
7883out:
7884 return ret;
7885}
7886
Andy Adamson2050f0c2009-04-01 09:22:30 -04007887struct nfs4_get_lease_time_data {
7888 struct nfs4_get_lease_time_args *args;
7889 struct nfs4_get_lease_time_res *res;
7890 struct nfs_client *clp;
7891};
7892
7893static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7894 void *calldata)
7895{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007896 struct nfs4_get_lease_time_data *data =
7897 (struct nfs4_get_lease_time_data *)calldata;
7898
7899 dprintk("--> %s\n", __func__);
7900 /* just setup sequence, do not trigger session recovery
7901 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007902 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007903 &data->args->la_seq_args,
7904 &data->res->lr_seq_res,
7905 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007906 dprintk("<-- %s\n", __func__);
7907}
7908
7909/*
7910 * Called from nfs4_state_manager thread for session setup, so don't recover
7911 * from sequence operation or clientid errors.
7912 */
7913static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7914{
7915 struct nfs4_get_lease_time_data *data =
7916 (struct nfs4_get_lease_time_data *)calldata;
7917
7918 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007919 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7920 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007921 switch (task->tk_status) {
7922 case -NFS4ERR_DELAY:
7923 case -NFS4ERR_GRACE:
7924 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7925 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7926 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007927 /* fall through */
7928 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007929 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007930 return;
7931 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007932 dprintk("<-- %s\n", __func__);
7933}
7934
Trond Myklebust17280172012-03-11 13:11:00 -04007935static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007936 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7937 .rpc_call_done = nfs4_get_lease_time_done,
7938};
7939
7940int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7941{
7942 struct rpc_task *task;
7943 struct nfs4_get_lease_time_args args;
7944 struct nfs4_get_lease_time_res res = {
7945 .lr_fsinfo = fsinfo,
7946 };
7947 struct nfs4_get_lease_time_data data = {
7948 .args = &args,
7949 .res = &res,
7950 .clp = clp,
7951 };
7952 struct rpc_message msg = {
7953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7954 .rpc_argp = &args,
7955 .rpc_resp = &res,
7956 };
7957 struct rpc_task_setup task_setup = {
7958 .rpc_client = clp->cl_rpcclient,
7959 .rpc_message = &msg,
7960 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007961 .callback_data = &data,
7962 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007963 };
7964 int status;
7965
Chuck Levera9c92d62013-08-09 12:48:18 -04007966 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007967 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007968 task = rpc_run_task(&task_setup);
7969
7970 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007971 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007972
Anna Schumakerf6148712017-04-07 14:15:23 -04007973 status = task->tk_status;
7974 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007975 return status;
7976}
7977
Andy Adamsonfc931582009-04-01 09:22:31 -04007978/*
7979 * Initialize the values to be used by the client in CREATE_SESSION
7980 * If nfs4_init_session set the fore channel request and response sizes,
7981 * use them.
7982 *
7983 * Set the back channel max_resp_sz_cached to zero to force the client to
7984 * always set csa_cachethis to FALSE because the current implementation
7985 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7986 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007987static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7988 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007989{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007990 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007991 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007992
Andy Adamson18aad3d2013-06-26 12:21:49 -04007993 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7994 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7995
Andy Adamsonfc931582009-04-01 09:22:31 -04007996 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007997 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7998 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007999 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008000 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008001
8002 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008003 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008004 __func__,
8005 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008006 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008007
8008 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008009 args->bc_attrs.max_rqst_sz = max_bc_payload;
8010 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008011 args->bc_attrs.max_resp_sz_cached = 0;
8012 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008013 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008014
8015 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8016 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8017 __func__,
8018 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8019 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8020 args->bc_attrs.max_reqs);
8021}
8022
Trond Myklebust79969dd2015-02-18 11:30:18 -08008023static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8024 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008025{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008026 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008027 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008028
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008029 if (rcvd->max_resp_sz > sent->max_resp_sz)
8030 return -EINVAL;
8031 /*
8032 * Our requested max_ops is the minimum we need; we're not
8033 * prepared to break up compounds into smaller pieces than that.
8034 * So, no point even trying to continue if the server won't
8035 * cooperate:
8036 */
8037 if (rcvd->max_ops < sent->max_ops)
8038 return -EINVAL;
8039 if (rcvd->max_reqs == 0)
8040 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008041 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8042 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008043 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008044}
8045
Trond Myklebust79969dd2015-02-18 11:30:18 -08008046static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8047 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008048{
8049 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008050 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008051
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008052 if (!(res->flags & SESSION4_BACK_CHAN))
8053 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008054 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8055 return -EINVAL;
8056 if (rcvd->max_resp_sz < sent->max_resp_sz)
8057 return -EINVAL;
8058 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8059 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008060 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008061 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008062 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008063 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008064out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008065 return 0;
8066}
Andy Adamson8d353012009-04-01 09:22:32 -04008067
Andy Adamson8d353012009-04-01 09:22:32 -04008068static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008069 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008070{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008071 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008072
Trond Myklebust79969dd2015-02-18 11:30:18 -08008073 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008074 if (ret)
8075 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008076 return nfs4_verify_back_channel_attrs(args, res);
8077}
8078
8079static void nfs4_update_session(struct nfs4_session *session,
8080 struct nfs41_create_session_res *res)
8081{
8082 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008083 /* Mark client id and session as being confirmed */
8084 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8085 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008086 session->flags = res->flags;
8087 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008088 if (res->flags & SESSION4_BACK_CHAN)
8089 memcpy(&session->bc_attrs, &res->bc_attrs,
8090 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008091}
8092
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008093static int _nfs4_proc_create_session(struct nfs_client *clp,
8094 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008095{
8096 struct nfs4_session *session = clp->cl_session;
8097 struct nfs41_create_session_args args = {
8098 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008099 .clientid = clp->cl_clientid,
8100 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008101 .cb_program = NFS4_CALLBACK,
8102 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008103 struct nfs41_create_session_res res;
8104
Andy Adamsonfc931582009-04-01 09:22:31 -04008105 struct rpc_message msg = {
8106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8107 .rpc_argp = &args,
8108 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008109 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008110 };
8111 int status;
8112
Chuck Lever6b26cc82016-05-02 14:40:40 -04008113 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008114 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008115
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008116 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008117 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008118
Trond Myklebustb519d402016-09-11 14:50:01 -04008119 switch (status) {
8120 case -NFS4ERR_STALE_CLIENTID:
8121 case -NFS4ERR_DELAY:
8122 case -ETIMEDOUT:
8123 case -EACCES:
8124 case -EAGAIN:
8125 goto out;
8126 };
8127
8128 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008129 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008130 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008131 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008132 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008133 if (status)
8134 goto out;
8135 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008136 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008137out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008138 return status;
8139}
8140
8141/*
8142 * Issues a CREATE_SESSION operation to the server.
8143 * It is the responsibility of the caller to verify the session is
8144 * expired before calling this routine.
8145 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008146int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008147{
8148 int status;
8149 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008150 struct nfs4_session *session = clp->cl_session;
8151
8152 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8153
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008154 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008155 if (status)
8156 goto out;
8157
Andy Adamsonaacd5532011-11-09 13:58:21 -05008158 /* Init or reset the session slot tables */
8159 status = nfs4_setup_session_slot_tables(session);
8160 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008161 if (status)
8162 goto out;
8163
8164 ptr = (unsigned *)&session->sess_id.data[0];
8165 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8166 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008167out:
8168 dprintk("<-- %s\n", __func__);
8169 return status;
8170}
8171
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008172/*
8173 * Issue the over-the-wire RPC DESTROY_SESSION.
8174 * The caller must serialize access to this routine.
8175 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008176int nfs4_proc_destroy_session(struct nfs4_session *session,
8177 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008178{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008179 struct rpc_message msg = {
8180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8181 .rpc_argp = session,
8182 .rpc_cred = cred,
8183 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008184 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008185
8186 dprintk("--> nfs4_proc_destroy_session\n");
8187
8188 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008189 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8190 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008191
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008192 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008193 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008194
8195 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008196 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008197 "Session has been destroyed regardless...\n", status);
8198
8199 dprintk("<-- nfs4_proc_destroy_session\n");
8200 return status;
8201}
8202
Trond Myklebust7b38c362012-05-23 13:23:31 -04008203/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008204 * Renew the cl_session lease.
8205 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008206struct nfs4_sequence_data {
8207 struct nfs_client *clp;
8208 struct nfs4_sequence_args args;
8209 struct nfs4_sequence_res res;
8210};
8211
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008212static void nfs41_sequence_release(void *data)
8213{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008214 struct nfs4_sequence_data *calldata = data;
8215 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008216
Elena Reshetova212bf412017-10-20 12:53:38 +03008217 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008218 nfs4_schedule_state_renewal(clp);
8219 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008220 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008221}
8222
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008223static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8224{
8225 switch(task->tk_status) {
8226 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008227 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8228 return -EAGAIN;
8229 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008230 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008231 }
8232 return 0;
8233}
8234
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008235static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008236{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008237 struct nfs4_sequence_data *calldata = data;
8238 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008239
Trond Myklebust14516c32010-07-31 14:29:06 -04008240 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8241 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008242
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008243 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008244 if (task->tk_status < 0) {
8245 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008246 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008247 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008248
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008249 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8250 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008251 return;
8252 }
8253 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008254 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008255out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008256 dprintk("<-- %s\n", __func__);
8257}
8258
8259static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8260{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008261 struct nfs4_sequence_data *calldata = data;
8262 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008263 struct nfs4_sequence_args *args;
8264 struct nfs4_sequence_res *res;
8265
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008266 args = task->tk_msg.rpc_argp;
8267 res = task->tk_msg.rpc_resp;
8268
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008269 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008270}
8271
8272static const struct rpc_call_ops nfs41_sequence_ops = {
8273 .rpc_call_done = nfs41_sequence_call_done,
8274 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008275 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008276};
8277
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008278static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8279 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008280 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008281 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008282{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008283 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008284 struct rpc_message msg = {
8285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8286 .rpc_cred = cred,
8287 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008288 struct rpc_task_setup task_setup_data = {
8289 .rpc_client = clp->cl_rpcclient,
8290 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008291 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008292 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008293 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008294 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008295
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008296 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008297 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008298 goto out_err;
8299
8300 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008301 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008302 if (calldata == NULL)
8303 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008304 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008305 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008306 if (is_privileged)
8307 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008308 msg.rpc_argp = &calldata->args;
8309 msg.rpc_resp = &calldata->res;
8310 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008311 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008312
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008313 ret = rpc_run_task(&task_setup_data);
8314 if (IS_ERR(ret))
8315 goto out_err;
8316 return ret;
8317out_put_clp:
8318 nfs_put_client(clp);
8319out_err:
8320 nfs41_release_slot(slot);
8321 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008322}
8323
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008324static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008325{
8326 struct rpc_task *task;
8327 int ret = 0;
8328
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008329 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008330 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008331 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008332 if (IS_ERR(task))
8333 ret = PTR_ERR(task);
8334 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008335 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008336 dprintk("<-- %s status=%d\n", __func__, ret);
8337 return ret;
8338}
8339
8340static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8341{
8342 struct rpc_task *task;
8343 int ret;
8344
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008345 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008346 if (IS_ERR(task)) {
8347 ret = PTR_ERR(task);
8348 goto out;
8349 }
8350 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008351 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008352 ret = task->tk_status;
8353 rpc_put_task(task);
8354out:
8355 dprintk("<-- %s status=%d\n", __func__, ret);
8356 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008357}
8358
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008359struct nfs4_reclaim_complete_data {
8360 struct nfs_client *clp;
8361 struct nfs41_reclaim_complete_args arg;
8362 struct nfs41_reclaim_complete_res res;
8363};
8364
8365static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8366{
8367 struct nfs4_reclaim_complete_data *calldata = data;
8368
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008369 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008370 &calldata->arg.seq_args,
8371 &calldata->res.seq_res,
8372 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008373}
8374
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008375static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8376{
8377 switch(task->tk_status) {
8378 case 0:
8379 case -NFS4ERR_COMPLETE_ALREADY:
8380 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8381 break;
8382 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008383 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008384 /* fall through */
8385 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008386 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008387 case -NFS4ERR_BADSESSION:
8388 case -NFS4ERR_DEADSESSION:
8389 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8390 nfs4_schedule_session_recovery(clp->cl_session,
8391 task->tk_status);
8392 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008393 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008394 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008395 }
8396 return 0;
8397}
8398
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008399static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8400{
8401 struct nfs4_reclaim_complete_data *calldata = data;
8402 struct nfs_client *clp = calldata->clp;
8403 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8404
8405 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008406 if (!nfs41_sequence_done(task, res))
8407 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008408
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008409 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008410 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8411 rpc_restart_call_prepare(task);
8412 return;
8413 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008414 dprintk("<-- %s\n", __func__);
8415}
8416
8417static void nfs4_free_reclaim_complete_data(void *data)
8418{
8419 struct nfs4_reclaim_complete_data *calldata = data;
8420
8421 kfree(calldata);
8422}
8423
8424static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8425 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8426 .rpc_call_done = nfs4_reclaim_complete_done,
8427 .rpc_release = nfs4_free_reclaim_complete_data,
8428};
8429
8430/*
8431 * Issue a global reclaim complete.
8432 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008433static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8434 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008435{
8436 struct nfs4_reclaim_complete_data *calldata;
8437 struct rpc_task *task;
8438 struct rpc_message msg = {
8439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008440 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008441 };
8442 struct rpc_task_setup task_setup_data = {
8443 .rpc_client = clp->cl_rpcclient,
8444 .rpc_message = &msg,
8445 .callback_ops = &nfs4_reclaim_complete_call_ops,
8446 .flags = RPC_TASK_ASYNC,
8447 };
8448 int status = -ENOMEM;
8449
8450 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008451 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008452 if (calldata == NULL)
8453 goto out;
8454 calldata->clp = clp;
8455 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008456
Chuck Levera9c92d62013-08-09 12:48:18 -04008457 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008458 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008459 msg.rpc_argp = &calldata->arg;
8460 msg.rpc_resp = &calldata->res;
8461 task_setup_data.callback_data = calldata;
8462 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008463 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008464 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008465 goto out;
8466 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008467 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008468 if (status == 0)
8469 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008470 rpc_put_task(task);
8471out:
8472 dprintk("<-- %s status=%d\n", __func__, status);
8473 return status;
8474}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008475
8476static void
8477nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8478{
8479 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008480 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008481
8482 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008483 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008484 &lgp->res.seq_res, task);
8485 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486}
8487
8488static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8489{
8490 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008491
8492 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008493 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008494 dprintk("<-- %s\n", __func__);
8495}
8496
8497static int
8498nfs4_layoutget_handle_exception(struct rpc_task *task,
8499 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8500{
Trond Myklebustee314c22012-10-01 17:25:48 -07008501 struct inode *inode = lgp->args.inode;
8502 struct nfs_server *server = NFS_SERVER(inode);
8503 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008504 int nfs4err = task->tk_status;
8505 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008506 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507
Boaz Harroshed7e5422014-01-22 20:34:54 +02008508 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008509
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008510 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008512 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008513
8514 /*
8515 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8516 * on the file. set tk_status to -ENODATA to tell upper layer to
8517 * retry go inband.
8518 */
8519 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008520 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008521 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008522 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008523 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8524 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8525 */
8526 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008527 status = -EOVERFLOW;
8528 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008529 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008530 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008531 * (or clients) writing to the same RAID stripe except when
8532 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008533 *
8534 * Treat it like we would RECALLCONFLICT -- we retry for a little
8535 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008536 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008537 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008538 if (lgp->args.minlength == 0) {
8539 status = -EOVERFLOW;
8540 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008541 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008542 status = -EBUSY;
8543 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008544 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008545 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008546 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008547 case -NFS4ERR_DELEG_REVOKED:
8548 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008549 case -NFS4ERR_EXPIRED:
8550 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008551 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008552 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008553 lo = NFS_I(inode)->layout;
8554 /* If the open stateid was bad, then recover it. */
8555 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008556 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008557 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008558 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008559 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008560 break;
8561 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008562
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008563 /*
8564 * Mark the bad layout state as invalid, then retry
8565 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008566 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008567 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008568 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008569 pnfs_free_lseg_list(&head);
8570 status = -EAGAIN;
8571 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008572 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008573
Trond Myklebust8ac09252017-01-23 22:44:12 -05008574 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008575 err = nfs4_handle_exception(server, nfs4err, exception);
8576 if (!status) {
8577 if (exception->retry)
8578 status = -EAGAIN;
8579 else
8580 status = err;
8581 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008582out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008583 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008584 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008585}
8586
Idan Kedar85541162012-08-02 11:47:10 +03008587static size_t max_response_pages(struct nfs_server *server)
8588{
8589 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8590 return nfs_page_array_len(0, max_resp_sz);
8591}
8592
8593static void nfs4_free_pages(struct page **pages, size_t size)
8594{
8595 int i;
8596
8597 if (!pages)
8598 return;
8599
8600 for (i = 0; i < size; i++) {
8601 if (!pages[i])
8602 break;
8603 __free_page(pages[i]);
8604 }
8605 kfree(pages);
8606}
8607
8608static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8609{
8610 struct page **pages;
8611 int i;
8612
8613 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8614 if (!pages) {
8615 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8616 return NULL;
8617 }
8618
8619 for (i = 0; i < size; i++) {
8620 pages[i] = alloc_page(gfp_flags);
8621 if (!pages[i]) {
8622 dprintk("%s: failed to allocate page\n", __func__);
8623 nfs4_free_pages(pages, size);
8624 return NULL;
8625 }
8626 }
8627
8628 return pages;
8629}
8630
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008631static void nfs4_layoutget_release(void *calldata)
8632{
8633 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008634 struct inode *inode = lgp->args.inode;
8635 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008636 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008637
8638 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008639 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008640 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008641 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008642 put_nfs_open_context(lgp->args.ctx);
8643 kfree(calldata);
8644 dprintk("<-- %s\n", __func__);
8645}
8646
8647static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8648 .rpc_call_prepare = nfs4_layoutget_prepare,
8649 .rpc_call_done = nfs4_layoutget_done,
8650 .rpc_release = nfs4_layoutget_release,
8651};
8652
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008653struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008654nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008656 struct inode *inode = lgp->args.inode;
8657 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008658 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008659 struct rpc_task *task;
8660 struct rpc_message msg = {
8661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8662 .rpc_argp = &lgp->args,
8663 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008664 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008665 };
8666 struct rpc_task_setup task_setup_data = {
8667 .rpc_client = server->client,
8668 .rpc_message = &msg,
8669 .callback_ops = &nfs4_layoutget_call_ops,
8670 .callback_data = lgp,
8671 .flags = RPC_TASK_ASYNC,
8672 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008673 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008674 struct nfs4_exception exception = {
8675 .inode = inode,
8676 .timeout = *timeout,
8677 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008678 int status = 0;
8679
8680 dprintk("--> %s\n", __func__);
8681
Peng Tao4bd5a982014-11-17 11:05:17 +08008682 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8683 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8684
Idan Kedar85541162012-08-02 11:47:10 +03008685 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8686 if (!lgp->args.layout.pages) {
8687 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008688 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008689 }
8690 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8691
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008692 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008693 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008694 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008695
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008696 task = rpc_run_task(&task_setup_data);
8697 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008698 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008699 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008700 if (status == 0) {
8701 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8702 *timeout = exception.timeout;
8703 }
8704
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008705 trace_nfs4_layoutget(lgp->args.ctx,
8706 &lgp->args.range,
8707 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008708 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008709 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008710
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008711 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8712 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008713 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008714 rpc_put_task(task);
8715 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008716 if (status)
8717 return ERR_PTR(status);
8718 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008719}
8720
Benny Halevycbe82602011-05-22 19:52:37 +03008721static void
8722nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8723{
8724 struct nfs4_layoutreturn *lrp = calldata;
8725
8726 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008727 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008728 &lrp->args.seq_args,
8729 &lrp->res.seq_res,
8730 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008731}
8732
8733static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8734{
8735 struct nfs4_layoutreturn *lrp = calldata;
8736 struct nfs_server *server;
8737
8738 dprintk("--> %s\n", __func__);
8739
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008740 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008741 return;
8742
8743 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008744 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008745 case -NFS4ERR_OLD_STATEID:
8746 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8747 lrp->args.inode))
8748 goto out_restart;
8749 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008750 default:
8751 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008752 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008753 case 0:
8754 break;
8755 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008756 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008757 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008758 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008759 }
Benny Halevycbe82602011-05-22 19:52:37 +03008760 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008761 return;
8762out_restart:
8763 task->tk_status = 0;
8764 nfs4_sequence_free_slot(&lrp->res.seq_res);
8765 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008766}
8767
8768static void nfs4_layoutreturn_release(void *calldata)
8769{
8770 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008771 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008772
8773 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008774 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008775 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008776 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008777 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8778 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008779 pnfs_put_layout_hdr(lrp->args.layout);
8780 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008781 kfree(calldata);
8782 dprintk("<-- %s\n", __func__);
8783}
8784
8785static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8786 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8787 .rpc_call_done = nfs4_layoutreturn_done,
8788 .rpc_release = nfs4_layoutreturn_release,
8789};
8790
Peng Tao6c166052014-11-17 09:30:40 +08008791int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008792{
8793 struct rpc_task *task;
8794 struct rpc_message msg = {
8795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8796 .rpc_argp = &lrp->args,
8797 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008798 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008799 };
8800 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008801 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008802 .rpc_message = &msg,
8803 .callback_ops = &nfs4_layoutreturn_call_ops,
8804 .callback_data = lrp,
8805 };
Peng Tao6c166052014-11-17 09:30:40 +08008806 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008807
Andrew Elble99ade3c2015-12-02 09:39:51 -05008808 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8809 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8810 &task_setup_data.rpc_client, &msg);
8811
Benny Halevycbe82602011-05-22 19:52:37 +03008812 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008813 if (!sync) {
8814 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8815 if (!lrp->inode) {
8816 nfs4_layoutreturn_release(lrp);
8817 return -EAGAIN;
8818 }
8819 task_setup_data.flags |= RPC_TASK_ASYNC;
8820 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008821 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008822 task = rpc_run_task(&task_setup_data);
8823 if (IS_ERR(task))
8824 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008825 if (sync)
8826 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008827 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008828 dprintk("<-- %s status=%d\n", __func__, status);
8829 rpc_put_task(task);
8830 return status;
8831}
8832
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008833static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008834_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8835 struct pnfs_device *pdev,
8836 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008837{
8838 struct nfs4_getdeviceinfo_args args = {
8839 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008840 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8841 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008842 };
8843 struct nfs4_getdeviceinfo_res res = {
8844 .pdev = pdev,
8845 };
8846 struct rpc_message msg = {
8847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8848 .rpc_argp = &args,
8849 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008850 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008851 };
8852 int status;
8853
8854 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008855 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008856 if (res.notification & ~args.notify_types)
8857 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008858 if (res.notification != args.notify_types)
8859 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008860
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008861 dprintk("<-- %s status=%d\n", __func__, status);
8862
8863 return status;
8864}
8865
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008866int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8867 struct pnfs_device *pdev,
8868 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008869{
8870 struct nfs4_exception exception = { };
8871 int err;
8872
8873 do {
8874 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008875 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008876 &exception);
8877 } while (exception.retry);
8878 return err;
8879}
8880EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8881
Andy Adamson863a3c62011-03-23 13:27:54 +00008882static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8883{
8884 struct nfs4_layoutcommit_data *data = calldata;
8885 struct nfs_server *server = NFS_SERVER(data->args.inode);
8886
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008887 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008888 &data->args.seq_args,
8889 &data->res.seq_res,
8890 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008891}
8892
8893static void
8894nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8895{
8896 struct nfs4_layoutcommit_data *data = calldata;
8897 struct nfs_server *server = NFS_SERVER(data->args.inode);
8898
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008899 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008900 return;
8901
8902 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008903 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8904 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8905 case -NFS4ERR_BADLAYOUT: /* no layout */
8906 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008907 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008908 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008909 break;
8910 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008911 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008912 rpc_restart_call_prepare(task);
8913 return;
8914 }
8915 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008916}
8917
8918static void nfs4_layoutcommit_release(void *calldata)
8919{
8920 struct nfs4_layoutcommit_data *data = calldata;
8921
Andy Adamsondb29c082011-07-30 20:52:38 -04008922 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008923 nfs_post_op_update_inode_force_wcc(data->args.inode,
8924 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008925 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008926 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008927 kfree(data);
8928}
8929
8930static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8931 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8932 .rpc_call_done = nfs4_layoutcommit_done,
8933 .rpc_release = nfs4_layoutcommit_release,
8934};
8935
8936int
Andy Adamsonef311532011-03-12 02:58:10 -05008937nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008938{
8939 struct rpc_message msg = {
8940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8941 .rpc_argp = &data->args,
8942 .rpc_resp = &data->res,
8943 .rpc_cred = data->cred,
8944 };
8945 struct rpc_task_setup task_setup_data = {
8946 .task = &data->task,
8947 .rpc_client = NFS_CLIENT(data->args.inode),
8948 .rpc_message = &msg,
8949 .callback_ops = &nfs4_layoutcommit_ops,
8950 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008951 };
8952 struct rpc_task *task;
8953 int status = 0;
8954
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008955 dprintk("NFS: initiating layoutcommit call. sync %d "
8956 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008957 data->args.lastbytewritten,
8958 data->args.inode->i_ino);
8959
Trond Myklebust472e2592015-02-05 16:50:30 -05008960 if (!sync) {
8961 data->inode = nfs_igrab_and_active(data->args.inode);
8962 if (data->inode == NULL) {
8963 nfs4_layoutcommit_release(data);
8964 return -EAGAIN;
8965 }
8966 task_setup_data.flags = RPC_TASK_ASYNC;
8967 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008968 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008969 task = rpc_run_task(&task_setup_data);
8970 if (IS_ERR(task))
8971 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008972 if (sync)
8973 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008974 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008975 dprintk("%s: status %d\n", __func__, status);
8976 rpc_put_task(task);
8977 return status;
8978}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008979
Andy Adamson97431202013-08-08 10:57:56 -04008980/**
8981 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8982 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8983 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008984static int
8985_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008986 struct nfs_fsinfo *info,
8987 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008988{
8989 struct nfs41_secinfo_no_name_args args = {
8990 .style = SECINFO_STYLE_CURRENT_FH,
8991 };
8992 struct nfs4_secinfo_res res = {
8993 .flavors = flavors,
8994 };
8995 struct rpc_message msg = {
8996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8997 .rpc_argp = &args,
8998 .rpc_resp = &res,
8999 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009000 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009001 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009002 int status;
9003
9004 if (use_integrity) {
9005 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009006 cred = nfs4_get_clid_cred(server->nfs_client);
9007 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009008 }
9009
9010 dprintk("--> %s\n", __func__);
9011 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9012 &res.seq_res, 0);
9013 dprintk("<-- %s status=%d\n", __func__, status);
9014
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009015 if (cred)
9016 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009017
9018 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009019}
9020
9021static int
9022nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9023 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9024{
9025 struct nfs4_exception exception = { };
9026 int err;
9027 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009028 /* first try using integrity protection */
9029 err = -NFS4ERR_WRONGSEC;
9030
9031 /* try to use integrity protection with machine cred */
9032 if (_nfs4_is_integrity_protected(server->nfs_client))
9033 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9034 flavors, true);
9035
9036 /*
9037 * if unable to use integrity protection, or SECINFO with
9038 * integrity protection returns NFS4ERR_WRONGSEC (which is
9039 * disallowed by spec, but exists in deployed servers) use
9040 * the current filesystem's rpc_client and the user cred.
9041 */
9042 if (err == -NFS4ERR_WRONGSEC)
9043 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9044 flavors, false);
9045
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009046 switch (err) {
9047 case 0:
9048 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009049 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009050 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009051 default:
9052 err = nfs4_handle_exception(server, err, &exception);
9053 }
9054 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009055out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009056 return err;
9057}
9058
9059static int
9060nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9061 struct nfs_fsinfo *info)
9062{
9063 int err;
9064 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009065 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009066 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009067 struct nfs4_secinfo4 *secinfo;
9068 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009069
9070 page = alloc_page(GFP_KERNEL);
9071 if (!page) {
9072 err = -ENOMEM;
9073 goto out;
9074 }
9075
9076 flavors = page_address(page);
9077 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9078
9079 /*
9080 * Fall back on "guess and check" method if
9081 * the server doesn't support SECINFO_NO_NAME
9082 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009083 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009084 err = nfs4_find_root_sec(server, fhandle, info);
9085 goto out_freepage;
9086 }
9087 if (err)
9088 goto out_freepage;
9089
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009090 for (i = 0; i < flavors->num_flavors; i++) {
9091 secinfo = &flavors->flavors[i];
9092
9093 switch (secinfo->flavor) {
9094 case RPC_AUTH_NULL:
9095 case RPC_AUTH_UNIX:
9096 case RPC_AUTH_GSS:
9097 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9098 &secinfo->flavor_info);
9099 break;
9100 default:
9101 flavor = RPC_AUTH_MAXFLAVOR;
9102 break;
9103 }
9104
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009105 if (!nfs_auth_info_match(&server->auth_info, flavor))
9106 flavor = RPC_AUTH_MAXFLAVOR;
9107
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009108 if (flavor != RPC_AUTH_MAXFLAVOR) {
9109 err = nfs4_lookup_root_sec(server, fhandle,
9110 info, flavor);
9111 if (!err)
9112 break;
9113 }
9114 }
9115
9116 if (flavor == RPC_AUTH_MAXFLAVOR)
9117 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009118
9119out_freepage:
9120 put_page(page);
9121 if (err == -EACCES)
9122 return -EPERM;
9123out:
9124 return err;
9125}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009126
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009127static int _nfs41_test_stateid(struct nfs_server *server,
9128 nfs4_stateid *stateid,
9129 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009130{
9131 int status;
9132 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009133 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009134 };
9135 struct nfs41_test_stateid_res res;
9136 struct rpc_message msg = {
9137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9138 .rpc_argp = &args,
9139 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009140 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009141 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009142 struct rpc_clnt *rpc_client = server->client;
9143
9144 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9145 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009146
Chuck Lever38527b12012-07-11 16:30:23 -04009147 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009148 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009149 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009150 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009151 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009152 if (status != NFS_OK) {
9153 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009154 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009155 }
9156 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009157 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009158}
9159
Trond Myklebust43912bb2016-09-22 13:38:56 -04009160static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9161 int err, struct nfs4_exception *exception)
9162{
9163 exception->retry = 0;
9164 switch(err) {
9165 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009166 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009167 nfs4_handle_exception(server, err, exception);
9168 break;
9169 case -NFS4ERR_BADSESSION:
9170 case -NFS4ERR_BADSLOT:
9171 case -NFS4ERR_BAD_HIGH_SLOT:
9172 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9173 case -NFS4ERR_DEADSESSION:
9174 nfs4_do_handle_exception(server, err, exception);
9175 }
9176}
9177
Chuck Lever38527b12012-07-11 16:30:23 -04009178/**
9179 * nfs41_test_stateid - perform a TEST_STATEID operation
9180 *
9181 * @server: server / transport on which to perform the operation
9182 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009183 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009184 *
9185 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9186 * Otherwise a negative NFS4ERR value is returned if the operation
9187 * failed or the state ID is not currently valid.
9188 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009189static int nfs41_test_stateid(struct nfs_server *server,
9190 nfs4_stateid *stateid,
9191 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009192{
9193 struct nfs4_exception exception = { };
9194 int err;
9195 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009196 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009197 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009198 } while (exception.retry);
9199 return err;
9200}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009201
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009202struct nfs_free_stateid_data {
9203 struct nfs_server *server;
9204 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009205 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009206};
9207
9208static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9209{
9210 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009211 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009212 &data->args.seq_args,
9213 &data->res.seq_res,
9214 task);
9215}
9216
9217static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9218{
9219 struct nfs_free_stateid_data *data = calldata;
9220
9221 nfs41_sequence_done(task, &data->res.seq_res);
9222
9223 switch (task->tk_status) {
9224 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009225 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009226 rpc_restart_call_prepare(task);
9227 }
9228}
9229
9230static void nfs41_free_stateid_release(void *calldata)
9231{
9232 kfree(calldata);
9233}
9234
Trond Myklebust17f26b12013-08-21 15:48:42 -04009235static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009236 .rpc_call_prepare = nfs41_free_stateid_prepare,
9237 .rpc_call_done = nfs41_free_stateid_done,
9238 .rpc_release = nfs41_free_stateid_release,
9239};
9240
9241static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009242 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009243 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009244 bool privileged)
9245{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009246 struct rpc_message msg = {
9247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009248 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009249 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009250 struct rpc_task_setup task_setup = {
9251 .rpc_client = server->client,
9252 .rpc_message = &msg,
9253 .callback_ops = &nfs41_free_stateid_ops,
9254 .flags = RPC_TASK_ASYNC,
9255 };
9256 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009257
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009258 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9259 &task_setup.rpc_client, &msg);
9260
Chuck Lever38527b12012-07-11 16:30:23 -04009261 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009262 data = kmalloc(sizeof(*data), GFP_NOFS);
9263 if (!data)
9264 return ERR_PTR(-ENOMEM);
9265 data->server = server;
9266 nfs4_stateid_copy(&data->args.stateid, stateid);
9267
9268 task_setup.callback_data = data;
9269
9270 msg.rpc_argp = &data->args;
9271 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009272 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009273 if (privileged)
9274 nfs4_set_sequence_privileged(&data->args.seq_args);
9275
9276 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009277}
9278
Chuck Lever38527b12012-07-11 16:30:23 -04009279/**
9280 * nfs41_free_stateid - perform a FREE_STATEID operation
9281 *
9282 * @server: server / transport on which to perform the operation
9283 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009284 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009285 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009286 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009287 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009288 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009289static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009290 const nfs4_stateid *stateid,
9291 struct rpc_cred *cred,
9292 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009293{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009294 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009295
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009296 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009297 if (IS_ERR(task))
9298 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009299 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009300 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009301}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009302
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009303static void
9304nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009305{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009306 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009307
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009308 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009309 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009310}
9311
Trond Myklebust36281ca2012-03-04 18:13:56 -05009312static bool nfs41_match_stateid(const nfs4_stateid *s1,
9313 const nfs4_stateid *s2)
9314{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009315 if (s1->type != s2->type)
9316 return false;
9317
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009318 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009319 return false;
9320
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009321 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009322 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009323
Anna Schumaker045c5512017-01-11 16:59:48 -05009324 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009325}
9326
Andy Adamson557134a2009-04-01 09:21:53 -04009327#endif /* CONFIG_NFS_V4_1 */
9328
Trond Myklebust36281ca2012-03-04 18:13:56 -05009329static bool nfs4_match_stateid(const nfs4_stateid *s1,
9330 const nfs4_stateid *s2)
9331{
Trond Myklebustf597c532012-03-04 18:13:56 -05009332 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009333}
9334
9335
Trond Myklebust17280172012-03-11 13:11:00 -04009336static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009337 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009338 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 .recover_open = nfs4_open_reclaim,
9340 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009341 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009342 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343};
9344
Andy Adamson591d71c2009-04-01 09:22:47 -04009345#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009346static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009347 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9348 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9349 .recover_open = nfs4_open_reclaim,
9350 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009351 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009352 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009353 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009354};
9355#endif /* CONFIG_NFS_V4_1 */
9356
Trond Myklebust17280172012-03-11 13:11:00 -04009357static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009358 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009359 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009360 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009362 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363};
9364
Andy Adamson591d71c2009-04-01 09:22:47 -04009365#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009366static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009367 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9368 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009369 .recover_open = nfs41_open_expired,
9370 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009371 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009372};
9373#endif /* CONFIG_NFS_V4_1 */
9374
Trond Myklebust17280172012-03-11 13:11:00 -04009375static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009376 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009377 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009378 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009379};
9380
9381#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009382static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009383 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009384 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009385 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009386};
9387#endif
9388
Chuck Leverec011fe2013-10-17 14:12:39 -04009389static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009390 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009391 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009392};
9393
9394#if defined(CONFIG_NFS_V4_1)
9395static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009396 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009397 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009398};
9399#endif /* CONFIG_NFS_V4_1 */
9400
Trond Myklebust97dc1352010-06-16 09:52:26 -04009401static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9402 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009403 .init_caps = NFS_CAP_READDIRPLUS
9404 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009405 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009406 .init_client = nfs40_init_client,
9407 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009408 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009409 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009410 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009411 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009412 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009413 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009414 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9415 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9416 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009417 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009418};
9419
9420#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009421static struct nfs_seqid *
9422nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9423{
9424 return NULL;
9425}
9426
Trond Myklebust97dc1352010-06-16 09:52:26 -04009427static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9428 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009429 .init_caps = NFS_CAP_READDIRPLUS
9430 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009431 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009432 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009433 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009434 .init_client = nfs41_init_client,
9435 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009436 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009437 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009438 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009439 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009440 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009441 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009442 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009443 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9444 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9445 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009446 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009447};
9448#endif
9449
Steve Dickson42c2c422013-05-22 12:50:38 -04009450#if defined(CONFIG_NFS_V4_2)
9451static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9452 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009453 .init_caps = NFS_CAP_READDIRPLUS
9454 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009455 | NFS_CAP_POSIX_LOCK
9456 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009457 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009458 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009459 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009460 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009461 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009462 | NFS_CAP_LAYOUTSTATS
9463 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009464 .init_client = nfs41_init_client,
9465 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009466 .match_stateid = nfs41_match_stateid,
9467 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009468 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009469 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009470 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009471 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009472 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009473 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9474 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9475 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009476 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009477};
9478#endif
9479
Trond Myklebust97dc1352010-06-16 09:52:26 -04009480const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9481 [0] = &nfs_v4_0_minor_ops,
9482#if defined(CONFIG_NFS_V4_1)
9483 [1] = &nfs_v4_1_minor_ops,
9484#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009485#if defined(CONFIG_NFS_V4_2)
9486 [2] = &nfs_v4_2_minor_ops,
9487#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009488};
9489
Trond Myklebust13997822016-07-24 17:10:52 -04009490static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009491{
9492 ssize_t error, error2;
9493
9494 error = generic_listxattr(dentry, list, size);
9495 if (error < 0)
9496 return error;
9497 if (list) {
9498 list += error;
9499 size -= error;
9500 }
9501
9502 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9503 if (error2 < 0)
9504 return error2;
9505 return error + error2;
9506}
9507
Trond Myklebust17f26b12013-08-21 15:48:42 -04009508static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009509 .create = nfs_create,
9510 .lookup = nfs_lookup,
9511 .atomic_open = nfs_atomic_open,
9512 .link = nfs_link,
9513 .unlink = nfs_unlink,
9514 .symlink = nfs_symlink,
9515 .mkdir = nfs_mkdir,
9516 .rmdir = nfs_rmdir,
9517 .mknod = nfs_mknod,
9518 .rename = nfs_rename,
9519 .permission = nfs_permission,
9520 .getattr = nfs_getattr,
9521 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009522 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009523};
9524
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009525static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009526 .permission = nfs_permission,
9527 .getattr = nfs_getattr,
9528 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009529 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009530};
9531
David Howells509de812006-08-22 20:06:11 -04009532const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009533 .version = 4, /* protocol version */
9534 .dentry_ops = &nfs4_dentry_operations,
9535 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009536 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009537 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009539 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009540 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009541 .getattr = nfs4_proc_getattr,
9542 .setattr = nfs4_proc_setattr,
9543 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009544 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 .access = nfs4_proc_access,
9546 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547 .create = nfs4_proc_create,
9548 .remove = nfs4_proc_remove,
9549 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009550 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009551 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009552 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009553 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009554 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 .link = nfs4_proc_link,
9556 .symlink = nfs4_proc_symlink,
9557 .mkdir = nfs4_proc_mkdir,
9558 .rmdir = nfs4_proc_remove,
9559 .readdir = nfs4_proc_readdir,
9560 .mknod = nfs4_proc_mknod,
9561 .statfs = nfs4_proc_statfs,
9562 .fsinfo = nfs4_proc_fsinfo,
9563 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009564 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009566 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009568 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009570 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009572 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009573 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009574 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009575 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009576 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009577 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009578 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009579 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009580 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009581 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009582 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009583 .create_server = nfs4_create_server,
9584 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585};
9586
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009587static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009588 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009589 .list = nfs4_xattr_list_nfs4_acl,
9590 .get = nfs4_xattr_get_nfs4_acl,
9591 .set = nfs4_xattr_set_nfs4_acl,
9592};
9593
9594const struct xattr_handler *nfs4_xattr_handlers[] = {
9595 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009596#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9597 &nfs4_xattr_nfs4_label_handler,
9598#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009599 NULL
9600};
9601
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602/*
9603 * Local variables:
9604 * c-basic-offset: 8
9605 * End:
9606 */